|
| 1 | +/** |
| 2 | + * Copyright 2025 Martynas Jusevičius <martynas@atomgraph.com> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | +package com.atomgraph.linkeddatahub.server.filter.request; |
| 18 | + |
| 19 | +import com.atomgraph.client.MediaTypes; |
| 20 | +import com.atomgraph.client.vocabulary.AC; |
| 21 | +import jakarta.ws.rs.NotAcceptableException; |
| 22 | +import jakarta.ws.rs.container.ContainerRequestContext; |
| 23 | +import jakarta.ws.rs.core.MediaType; |
| 24 | +import jakarta.ws.rs.core.Request; |
| 25 | +import jakarta.ws.rs.core.Variant; |
| 26 | +import java.io.IOException; |
| 27 | +import java.net.URI; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.Locale; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | +import org.mockito.Mock; |
| 34 | +import org.mockito.junit.MockitoJUnitRunner; |
| 35 | +import static org.mockito.ArgumentMatchers.anyList; |
| 36 | +import static org.mockito.Mockito.*; |
| 37 | + |
| 38 | +/** |
| 39 | + * Unit tests for {@link ProxyRequestFilter}. |
| 40 | + * |
| 41 | + * @author Martynas Jusevičius {@literal <martynas@atomgraph.com>} |
| 42 | + */ |
| 43 | +@RunWith(MockitoJUnitRunner.class) |
| 44 | +public class ProxyRequestFilterTest |
| 45 | +{ |
| 46 | + |
| 47 | + @Mock private ContainerRequestContext requestContext; |
| 48 | + @Mock private Request request; |
| 49 | + @Mock private com.atomgraph.linkeddatahub.Application system; |
| 50 | + |
| 51 | + private ProxyRequestFilter filter; |
| 52 | + |
| 53 | + @Before |
| 54 | + public void setUp() |
| 55 | + { |
| 56 | + filter = new ProxyRequestFilter(); |
| 57 | + filter.mediaTypes = new MediaTypes(); |
| 58 | + filter.request = request; |
| 59 | + filter.system = system; |
| 60 | + when(system.getSupportedLanguages()).thenReturn(Collections.emptyList()); |
| 61 | + } |
| 62 | + |
| 63 | + /** No proxy properties set — filter must be a no-op. */ |
| 64 | + @Test |
| 65 | + public void testNonProxyRequestSkipsFilter() throws IOException |
| 66 | + { |
| 67 | + // getProperty returns null by default; resolveTargetURI returns empty → filter exits immediately |
| 68 | + filter.filter(requestContext); |
| 69 | + verify(request, never()).selectVariant(anyList()); |
| 70 | + verify(requestContext, never()).abortWith(any()); |
| 71 | + } |
| 72 | + |
| 73 | + /** No acceptable RDF/SPARQL variant — filter must throw 406. */ |
| 74 | + @Test(expected = NotAcceptableException.class) |
| 75 | + public void testNullVariantThrowsNotAcceptable() throws IOException |
| 76 | + { |
| 77 | + when(requestContext.getProperty(AC.uri.getURI())) |
| 78 | + .thenReturn(URI.create("http://example.org/resource")); |
| 79 | + when(request.selectVariant(anyList())).thenReturn(null); |
| 80 | + filter.filter(requestContext); |
| 81 | + } |
| 82 | + |
| 83 | + /** text/html selected as best variant — filter must throw 406. */ |
| 84 | + @Test(expected = NotAcceptableException.class) |
| 85 | + public void testHtmlVariantThrowsNotAcceptable() throws IOException |
| 86 | + { |
| 87 | + when(requestContext.getProperty(AC.uri.getURI())) |
| 88 | + .thenReturn(URI.create("http://example.org/resource")); |
| 89 | + when(request.selectVariant(anyList())) |
| 90 | + .thenReturn(new Variant(MediaType.TEXT_HTML_TYPE, (Locale) null, null)); |
| 91 | + filter.filter(requestContext); |
| 92 | + } |
| 93 | + |
| 94 | + /** application/xhtml+xml selected as best variant — filter must throw 406. */ |
| 95 | + @Test(expected = NotAcceptableException.class) |
| 96 | + public void testXhtmlVariantThrowsNotAcceptable() throws IOException |
| 97 | + { |
| 98 | + when(requestContext.getProperty(AC.uri.getURI())) |
| 99 | + .thenReturn(URI.create("http://example.org/resource")); |
| 100 | + when(request.selectVariant(anyList())) |
| 101 | + .thenReturn(new Variant(MediaType.APPLICATION_XHTML_XML_TYPE, (Locale) null, null)); |
| 102 | + filter.filter(requestContext); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments