package com.github.jsonldjava.utils;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.net.URL;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.cache.CacheResponseStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.cache.CachingHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class JsonUtilsIT {

    @Test
    public void fromURLredirectHTTPSToHTTP() throws Exception {
        final URL url = new URL("https://w3id.org/bundle/context");
        final Object context = JSONUtils.fromURL(url);
        // Should not fail because of
        // http://stackoverflow.com/questions/1884230/java-doesnt-follow-redirect-in-urlconnection
        // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4620571
        assertTrue(context instanceof Map);
        assertFalse(((Map<?, ?>) context).isEmpty());
    }

    @Test
    public void fromURLredirect() throws Exception {
        final URL url = new URL("http://purl.org/wf4ever/ro-bundle/context.json");
        final Object context = JSONUtils.fromURL(url);
        assertTrue(context instanceof Map);
        assertFalse(((Map<?, ?>) context).isEmpty());
    }

    @Test
    public void fromURLCache() throws Exception {
        final URL url = new URL("http://json-ld.org/contexts/person.jsonld");
        JSONUtils.fromURL(url);

        // Now try to get it again and ensure it is
        // cached
        final HttpClient client = new CachingHttpClient(JSONUtils.getHttpClient());
        final HttpUriRequest get = new HttpGet(url.toURI());
        get.setHeader("Accept", JSONUtils.ACCEPT_HEADER);
        final HttpContext localContext = new BasicHttpContext();
        final HttpResponse respo = client.execute(get, localContext);
        EntityUtils.consume(respo.getEntity());

        // Check cache status
        // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html
        final CacheResponseStatus responseStatus = (CacheResponseStatus) localContext
                .getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS);
        assertFalse(CacheResponseStatus.CACHE_MISS.equals(responseStatus));
    }

}

@@ -596,16 +596,41 @@
 yUtils;%0A
+import org.junit.Ignore;%0A
 import o
@@ -644,16 +644,16 @@
 t.Test;%0A
-
 %0Apublic 
@@ -1658,17 +1658,25 @@
 L(url);%0A
+        
 %0A
-
         
@@ -2396,16 +2396,16 @@
 TATUS);%0A
-
         
@@ -2477,12 +2477,689 @@
 );%0A    %7D
+%0A    %0A    @Ignore%0A    @Test%0A    public void fromURLCacheWithRedirect() throws Exception %7B%0A        final URL url = new URL(%22https://w3id.org/web-keys/v1%22);%0A        JSONUtils.fromURL(url);%0A        %0A        System.out.println(%22To test caching, turn off wifi/disconnect network cable now!%22);%0A        Thread.sleep(5000);%0A        System.out.println(%22Checking loading from cache:%22);%0A        %0A        // Now try to get it again and ensure it is cached%0A        Object cached = JSONUtils.fromURL(url);%0A        System.out.println(cached);%0A        assertTrue(cached instanceof Map);%0A        Map%3CString,?%3E cachedMap = (Map%3CString, ?%3E) cached;%0A        assertFalse(cachedMap.isEmpty());%0A    %7D
 %0A%0A%7D%0A

Test cache is used even across redirects