44import static org .junit .Assert .assertFalse ;
55import static org .junit .Assert .assertNotNull ;
66import static org .junit .Assert .assertTrue ;
7+ import static org .mockito .Mockito .mock ;
8+ import static org .mockito .Mockito .when ;
79
810import java .io .IOException ;
911import java .io .InputStream ;
1416import java .util .Map ;
1517import java .util .concurrent .atomic .AtomicInteger ;
1618
17- import org .junit .Ignore ;
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 ;
1832import org .junit .Test ;
33+ import org .mockito .ArgumentCaptor ;
1934
2035
2136public class JSONUtilsTest {
2237
23- @ SuppressWarnings ("unchecked" )
38+ @ SuppressWarnings ("unchecked" )
2439 @ Test
2540 public void fromStringTest () {
2641 String testString = "{\" seq\" :3,\" id\" :\" e48dfa735d9fad88db6b7cd696002df7\" ,\" changes\" :[{\" rev\" :\" 2-6aebf275bc3f29b67695c727d448df8e\" }]}" ;
@@ -96,7 +111,7 @@ public void fromURLredirectHTTPSToHTTP() throws Exception {
96111 // Should not fail because of http://stackoverflow.com/questions/1884230/java-doesnt-follow-redirect-in-urlconnection
97112 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4620571
98113 assertTrue (context instanceof Map );
99- assertFalse (((Map ) context ).isEmpty ());
114+ assertFalse (((Map <?,?>) context ).isEmpty ());
100115 }
101116
102117
@@ -106,7 +121,29 @@ public void fromURLredirect() throws Exception {
106121 URL url = new URL ("http://purl.org/wf4ever/ro-bundle/context.json" );
107122 Object context = JSONUtils .fromURL (url );
108123 assertTrue (context instanceof Map );
109- assertFalse (((Map ) context ).isEmpty ());
124+ assertFalse (((Map <?,?>)context ).isEmpty ());
125+ }
126+
127+
128+ @ Test
129+ public void fromURLCache () throws Exception {
130+ URL url = new URL ("http://json-ld.org/contexts/person.jsonld" );
131+ JSONUtils .fromURL (url );
132+
133+ // Now try to get it again and ensure it is
134+ // cached
135+ HttpClient client = new CachingHttpClient (JSONUtils .getHttpClient ());
136+ HttpUriRequest get = new HttpGet (url .toURI ());
137+ get .setHeader ("Accept" , JSONUtils .ACCEPT_HEADER );
138+ HttpContext localContext = new BasicHttpContext ();
139+ HttpResponse respo = client .execute (get , localContext );
140+ EntityUtils .consume (respo .getEntity ());
141+
142+ // Check cache status
143+ // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html
144+ CacheResponseStatus responseStatus = (CacheResponseStatus ) localContext .getAttribute (
145+ CachingHttpClient .CACHE_RESPONSE_STATUS );
146+ assertFalse (CacheResponseStatus .CACHE_MISS .equals (responseStatus ));
110147 }
111148
112149
@@ -133,12 +170,73 @@ public InputStream getInputStream() throws IOException {
133170 assertEquals (0 , requests .get ());
134171 Object context = JSONUtils .fromURL (url );
135172 assertEquals (1 , requests .get ());
136- assertTrue (context instanceof Map );
137- assertFalse (((Map ) context ).isEmpty ());
173+ assertTrue (context instanceof Map );
174+ assertFalse (((Map <?,?>)context ).isEmpty ());
175+ }
176+
177+ protected HttpClient fakeHttpClient (ArgumentCaptor <HttpUriRequest > httpRequest ) throws IllegalStateException , IOException {
178+ HttpClient httpClient = mock (HttpClient .class );
179+ HttpResponse fakeResponse = mock (HttpResponse .class );
180+ StatusLine statusCode = mock (StatusLine .class );
181+ when (statusCode .getStatusCode ()).thenReturn (200 );
182+ when (fakeResponse .getStatusLine ()).thenReturn (statusCode );
183+ HttpEntity entity = mock (HttpEntity .class );
184+ when (entity .getContent ())
185+ .thenReturn (
186+ JSONUtilsTest .class
187+ .getResourceAsStream ("/custom/contexttest-0001.jsonld" ));
188+ when (fakeResponse .getEntity ()).thenReturn (entity );
189+ when (httpClient .execute (httpRequest .capture ())).thenReturn (
190+ fakeResponse );
191+ return httpClient ;
192+ }
193+
194+ @ Test
195+ public void fromURLAcceptHeaders () throws Exception {
196+
197+ URL url = new URL ("http://example.com/fake-jsonld-test" );
198+ ArgumentCaptor <HttpUriRequest > httpRequest = ArgumentCaptor .forClass (HttpUriRequest .class );
199+ JSONUtils .setHttpClient (fakeHttpClient (httpRequest ));
200+ try {
201+ Object context = JSONUtils .fromURL (url );
202+ assertTrue (context instanceof Map );
203+ } finally {
204+ JSONUtils .setHttpClient (null );
205+ }
206+ assertEquals (1 , httpRequest .getAllValues ().size ());
207+ HttpUriRequest req = httpRequest .getValue ();
208+ assertEquals (url .toURI (), req .getURI ());
138209
139- // assertEquals(1, requestProperties.get("Accept").size());
140- // String expected = "application/ld+json, application/json;q=0.9, application/javascript;q=0.5, text/javascript;q=0.5, text/plain;q=0.2, */*;q=0.1";
141- // assertEquals(expected, requestProperties.get("Accept").get(0));
210+ Header [] accept = req .getHeaders ("Accept" );
211+ assertEquals (1 , accept .length );
212+ assertEquals (JSONUtils .ACCEPT_HEADER , accept [0 ].getValue ());
213+ // Test that this header parses correctly
214+ HeaderElement [] elems = accept [0 ].getElements ();
215+ assertEquals ("application/ld+json" , elems [0 ].getName ());
216+ assertEquals (0 , elems [0 ].getParameterCount ());
217+
218+ assertEquals ("application/json" , elems [1 ].getName ());
219+ assertEquals (1 , elems [1 ].getParameterCount ());
220+ assertEquals ("0.9" , elems [1 ].getParameterByName ("q" ).getValue ());
221+
222+ assertEquals ("application/javascript" , elems [2 ].getName ());
223+ assertEquals (1 , elems [2 ].getParameterCount ());
224+ assertEquals ("0.5" , elems [2 ].getParameterByName ("q" ).getValue ());
142225
226+ assertEquals ("text/javascript" , elems [3 ].getName ());
227+ assertEquals (1 , elems [3 ].getParameterCount ());
228+ assertEquals ("0.5" , elems [3 ].getParameterByName ("q" ).getValue ());
229+
230+ assertEquals ("text/plain" , elems [4 ].getName ());
231+ assertEquals (1 , elems [4 ].getParameterCount ());
232+ assertEquals ("0.2" , elems [4 ].getParameterByName ("q" ).getValue ());
233+
234+ assertEquals ("*/*" , elems [5 ].getName ());
235+ assertEquals (1 , elems [5 ].getParameterCount ());
236+ assertEquals ("0.1" , elems [5 ].getParameterByName ("q" ).getValue ());
237+
238+ assertEquals (6 , elems .length );
143239 }
240+
241+
144242}
0 commit comments