|
| 1 | +package com.github.jsonldjava.core; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.net.URL; |
| 13 | +import java.net.URLConnection; |
| 14 | +import java.net.URLStreamHandler; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.concurrent.atomic.AtomicInteger; |
| 18 | + |
| 19 | +import org.apache.http.Header; |
| 20 | +import org.apache.http.HeaderElement; |
| 21 | +import org.apache.http.HttpEntity; |
| 22 | +import org.apache.http.HttpResponse; |
| 23 | +import org.apache.http.StatusLine; |
| 24 | +import org.apache.http.client.HttpClient; |
| 25 | +import org.apache.http.client.cache.CacheResponseStatus; |
| 26 | +import org.apache.http.client.methods.HttpGet; |
| 27 | +import org.apache.http.client.methods.HttpUriRequest; |
| 28 | +import org.apache.http.impl.client.cache.CachingHttpClient; |
| 29 | +import org.apache.http.protocol.BasicHttpContext; |
| 30 | +import org.apache.http.protocol.HttpContext; |
| 31 | +import org.apache.http.util.EntityUtils; |
| 32 | +import org.junit.Test; |
| 33 | +import org.mockito.ArgumentCaptor; |
| 34 | + |
| 35 | +public class DocumentLoaderTest { |
| 36 | + |
| 37 | + DocumentLoader documentLoader = new DocumentLoader(); |
| 38 | + |
| 39 | + @SuppressWarnings("unchecked") |
| 40 | + @Test |
| 41 | + public void fromURLTest0001() throws Exception { |
| 42 | + final URL contexttest = getClass().getResource("/custom/contexttest-0001.jsonld"); |
| 43 | + assertNotNull(contexttest); |
| 44 | + final Object context = documentLoader.fromURL(contexttest); |
| 45 | + assertTrue(context instanceof Map); |
| 46 | + final Map<String, Object> contextMap = (Map<String, Object>) context; |
| 47 | + assertEquals(1, contextMap.size()); |
| 48 | + final Map<String, Object> cont = (Map<String, Object>) contextMap.get("@context"); |
| 49 | + assertEquals(3, cont.size()); |
| 50 | + assertEquals("http://example.org/", cont.get("ex")); |
| 51 | + final Map<String, Object> term1 = (Map<String, Object>) cont.get("term1"); |
| 52 | + assertEquals("ex:term1", term1.get("@id")); |
| 53 | + } |
| 54 | + |
| 55 | + @SuppressWarnings("unchecked") |
| 56 | + @Test |
| 57 | + public void fromURLTest0002() throws Exception { |
| 58 | + final URL contexttest = getClass().getResource("/custom/contexttest-0002.jsonld"); |
| 59 | + assertNotNull(contexttest); |
| 60 | + final Object context = documentLoader.fromURL(contexttest); |
| 61 | + assertTrue(context instanceof List); |
| 62 | + final List<Map<String, Object>> contextList = (List<Map<String, Object>>) context; |
| 63 | + |
| 64 | + final Map<String, Object> contextMap1 = contextList.get(0); |
| 65 | + assertEquals(1, contextMap1.size()); |
| 66 | + final Map<String, Object> cont1 = (Map<String, Object>) contextMap1.get("@context"); |
| 67 | + assertEquals(2, cont1.size()); |
| 68 | + assertEquals("http://example.org/", cont1.get("ex")); |
| 69 | + final Map<String, Object> term1 = (Map<String, Object>) cont1.get("term1"); |
| 70 | + assertEquals("ex:term1", term1.get("@id")); |
| 71 | + |
| 72 | + final Map<String, Object> contextMap2 = contextList.get(1); |
| 73 | + assertEquals(1, contextMap2.size()); |
| 74 | + final Map<String, Object> cont2 = (Map<String, Object>) contextMap2.get("@context"); |
| 75 | + assertEquals(1, cont2.size()); |
| 76 | + final Map<String, Object> term2 = (Map<String, Object>) cont2.get("term2"); |
| 77 | + assertEquals("ex:term2", term2.get("@id")); |
| 78 | + } |
| 79 | + |
| 80 | + // @Ignore("Integration test") |
| 81 | + @Test |
| 82 | + public void fromURLredirectHTTPSToHTTP() throws Exception { |
| 83 | + final URL url = new URL("https://w3id.org/bundle/context"); |
| 84 | + final Object context = documentLoader.fromURL(url); |
| 85 | + // Should not fail because of |
| 86 | + // http://stackoverflow.com/questions/1884230/java-doesnt-follow-redirect-in-urlconnection |
| 87 | + // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4620571 |
| 88 | + assertTrue(context instanceof Map); |
| 89 | + assertFalse(((Map<?, ?>) context).isEmpty()); |
| 90 | + } |
| 91 | + |
| 92 | + // @Ignore("Integration test") |
| 93 | + @Test |
| 94 | + public void fromURLredirect() throws Exception { |
| 95 | + final URL url = new URL("http://purl.org/wf4ever/ro-bundle/context.json"); |
| 96 | + final Object context = documentLoader.fromURL(url); |
| 97 | + assertTrue(context instanceof Map); |
| 98 | + assertFalse(((Map<?, ?>) context).isEmpty()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void fromURLCache() throws Exception { |
| 103 | + final URL url = new URL("http://json-ld.org/contexts/person.jsonld"); |
| 104 | + documentLoader.fromURL(url); |
| 105 | + |
| 106 | + // Now try to get it again and ensure it is |
| 107 | + // cached |
| 108 | + final HttpClient client = new CachingHttpClient(documentLoader.getHttpClient()); |
| 109 | + final HttpUriRequest get = new HttpGet(url.toURI()); |
| 110 | + get.setHeader("Accept", DocumentLoader.ACCEPT_HEADER); |
| 111 | + final HttpContext localContext = new BasicHttpContext(); |
| 112 | + final HttpResponse respo = client.execute(get, localContext); |
| 113 | + EntityUtils.consume(respo.getEntity()); |
| 114 | + |
| 115 | + // Check cache status |
| 116 | + // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html |
| 117 | + final CacheResponseStatus responseStatus = (CacheResponseStatus) localContext |
| 118 | + .getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS); |
| 119 | + assertFalse(CacheResponseStatus.CACHE_MISS.equals(responseStatus)); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void fromURLCustomHandler() throws Exception { |
| 124 | + final AtomicInteger requests = new AtomicInteger(); |
| 125 | + final URLStreamHandler handler = new URLStreamHandler() { |
| 126 | + @Override |
| 127 | + protected URLConnection openConnection(URL u) throws IOException { |
| 128 | + return new URLConnection(u) { |
| 129 | + @Override |
| 130 | + public void connect() throws IOException { |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public InputStream getInputStream() throws IOException { |
| 136 | + requests.incrementAndGet(); |
| 137 | + return getClass().getResourceAsStream("/custom/contexttest-0001.jsonld"); |
| 138 | + } |
| 139 | + }; |
| 140 | + } |
| 141 | + }; |
| 142 | + final URL url = new URL(null, "jsonldtest:context", handler); |
| 143 | + assertEquals(0, requests.get()); |
| 144 | + final Object context = documentLoader.fromURL(url); |
| 145 | + assertEquals(1, requests.get()); |
| 146 | + assertTrue(context instanceof Map); |
| 147 | + assertFalse(((Map<?, ?>) context).isEmpty()); |
| 148 | + } |
| 149 | + |
| 150 | + protected HttpClient fakeHttpClient(ArgumentCaptor<HttpUriRequest> httpRequest) |
| 151 | + throws IllegalStateException, IOException { |
| 152 | + final HttpClient httpClient = mock(HttpClient.class); |
| 153 | + final HttpResponse fakeResponse = mock(HttpResponse.class); |
| 154 | + final StatusLine statusCode = mock(StatusLine.class); |
| 155 | + when(statusCode.getStatusCode()).thenReturn(200); |
| 156 | + when(fakeResponse.getStatusLine()).thenReturn(statusCode); |
| 157 | + final HttpEntity entity = mock(HttpEntity.class); |
| 158 | + when(entity.getContent()).thenReturn( |
| 159 | + DocumentLoaderTest.class.getResourceAsStream("/custom/contexttest-0001.jsonld")); |
| 160 | + when(fakeResponse.getEntity()).thenReturn(entity); |
| 161 | + when(httpClient.execute(httpRequest.capture())).thenReturn(fakeResponse); |
| 162 | + return httpClient; |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void fromURLAcceptHeaders() throws Exception { |
| 167 | + |
| 168 | + final URL url = new URL("http://example.com/fake-jsonld-test"); |
| 169 | + final ArgumentCaptor<HttpUriRequest> httpRequest = ArgumentCaptor |
| 170 | + .forClass(HttpUriRequest.class); |
| 171 | + documentLoader.setHttpClient(fakeHttpClient(httpRequest)); |
| 172 | + try { |
| 173 | + final Object context = documentLoader.fromURL(url); |
| 174 | + assertTrue(context instanceof Map); |
| 175 | + } finally { |
| 176 | + documentLoader.setHttpClient(null); |
| 177 | + } |
| 178 | + assertEquals(1, httpRequest.getAllValues().size()); |
| 179 | + final HttpUriRequest req = httpRequest.getValue(); |
| 180 | + assertEquals(url.toURI(), req.getURI()); |
| 181 | + |
| 182 | + final Header[] accept = req.getHeaders("Accept"); |
| 183 | + assertEquals(1, accept.length); |
| 184 | + assertEquals(DocumentLoader.ACCEPT_HEADER, accept[0].getValue()); |
| 185 | + // Test that this header parses correctly |
| 186 | + final HeaderElement[] elems = accept[0].getElements(); |
| 187 | + assertEquals("application/ld+json", elems[0].getName()); |
| 188 | + assertEquals(0, elems[0].getParameterCount()); |
| 189 | + |
| 190 | + assertEquals("application/json", elems[1].getName()); |
| 191 | + assertEquals(1, elems[1].getParameterCount()); |
| 192 | + assertEquals("0.9", elems[1].getParameterByName("q").getValue()); |
| 193 | + |
| 194 | + assertEquals("application/javascript", elems[2].getName()); |
| 195 | + assertEquals(1, elems[2].getParameterCount()); |
| 196 | + assertEquals("0.5", elems[2].getParameterByName("q").getValue()); |
| 197 | + |
| 198 | + assertEquals("text/javascript", elems[3].getName()); |
| 199 | + assertEquals(1, elems[3].getParameterCount()); |
| 200 | + assertEquals("0.5", elems[3].getParameterByName("q").getValue()); |
| 201 | + |
| 202 | + assertEquals("text/plain", elems[4].getName()); |
| 203 | + assertEquals(1, elems[4].getParameterCount()); |
| 204 | + assertEquals("0.2", elems[4].getParameterByName("q").getValue()); |
| 205 | + |
| 206 | + assertEquals("*/*", elems[5].getName()); |
| 207 | + assertEquals(1, elems[5].getParameterCount()); |
| 208 | + assertEquals("0.1", elems[5].getParameterByName("q").getValue()); |
| 209 | + |
| 210 | + assertEquals(6, elems.length); |
| 211 | + } |
| 212 | + |
| 213 | +} |
0 commit comments