@Test
    @Configuration("{" +
            "  \"ttl\" : 2" +
            "}")
    public void testCaching() throws Throwable {
        PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/cached-resource");

        PolicyTestResponse response = send(request);
        EchoResponse echo = response.entity(EchoResponse.class);
        assertNotNull(echo);
        Long counterValue = echo.getCounter();
        assertNotNull(counterValue);
        assertEquals("application/json", response.header("Content-Type"));

        // Now send the request again - we should get the *same* counter value!
        response = send(request);
        echo = response.entity(EchoResponse.class);
        assertNotNull(echo);
        Long counterValue2 = echo.getCounter();
        assertNotNull(counterValue2);
        assertEquals(counterValue, counterValue2);
        assertEquals("application/json", response.header("Content-Type"));

        // One more time, just to be sure
        response = send(request);
        echo = response.entity(EchoResponse.class);
        assertNotNull(echo);
        Long counterValue3 = echo.getCounter();
        assertNotNull(counterValue3);
        assertEquals(counterValue, counterValue3);
        assertEquals("application/json", response.header("Content-Type"));

        // Now wait for 3s and make sure the cache entry expired
        Thread.sleep(3000);
        response = send(request);
        echo = response.entity(EchoResponse.class);
        assertNotNull(echo);
        Long counterValue4 = echo.getCounter();
        assertNotNull(counterValue4);
        assertNotEquals(counterValue, counterValue4);
        assertEquals("application/json", response.header("Content-Type"));

        // And again - should be re-cached
        response = send(request);
        echo = response.entity(EchoResponse.class);
        assertNotNull(echo);
        Long counterValue5 = echo.getCounter();
        assertNotNull(counterValue5);
        assertEquals(counterValue4, counterValue5);
        assertEquals("application/json", response.header("Content-Type"));
    }
Unit test.

@author eric.wittmann@redhat.com
gateway/engine/policies/src/test/java/io/apiman/gateway/engine/policies/CachingPolicyTest.java