diff --git a/build.gradle b/build.gradle index d1830f7..68ede34 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,8 @@ group 'com.wego' version '1.0.1' apply plugin: 'java' +apply plugin: 'maven' +apply plugin: 'checkstyle' sourceCompatibility = '1.8' targetCompatibility = '1.8' @@ -10,10 +12,6 @@ task wrapper(type: Wrapper) { gradleVersion = '2.8' } -apply plugin: 'maven' - -apply plugin: 'checkstyle' - repositories { mavenCentral() } diff --git a/src/main/java/com/wego/httpcache/samples/SampleApp.java b/src/main/java/com/wego/httpcache/samples/SampleApp.java index 6922e28..b99029e 100644 --- a/src/main/java/com/wego/httpcache/samples/SampleApp.java +++ b/src/main/java/com/wego/httpcache/samples/SampleApp.java @@ -22,7 +22,7 @@ public static AsyncHttpCacheService getAsyncHttpCacheService() { injector.getInstance(AsyncHttpCacheServiceFactory.class); AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); - return asyncHttpCacheServiceFactory.create(asyncHttpClient, 10000); + return asyncHttpCacheServiceFactory.create("SampleApp", asyncHttpClient, 10000); } public static void main(String[] args) throws Exception { diff --git a/src/main/java/com/wego/httpcache/services/factories/AsyncHttpCacheServiceFactory.java b/src/main/java/com/wego/httpcache/services/factories/AsyncHttpCacheServiceFactory.java index ab5fc57..c29103a 100644 --- a/src/main/java/com/wego/httpcache/services/factories/AsyncHttpCacheServiceFactory.java +++ b/src/main/java/com/wego/httpcache/services/factories/AsyncHttpCacheServiceFactory.java @@ -4,5 +4,5 @@ import com.wego.httpcache.services.AsyncHttpCacheService; public interface AsyncHttpCacheServiceFactory { - AsyncHttpCacheService create(AsyncHttpClient asyncHttpClient, long ttl); + AsyncHttpCacheService create(String serviceName, AsyncHttpClient asyncHttpClient, long ttl); } diff --git a/src/main/java/com/wego/httpcache/services/impl/AsyncHttpCacheServiceImpl.java b/src/main/java/com/wego/httpcache/services/impl/AsyncHttpCacheServiceImpl.java index 54b9f58..ca640e0 100644 --- a/src/main/java/com/wego/httpcache/services/impl/AsyncHttpCacheServiceImpl.java +++ b/src/main/java/com/wego/httpcache/services/impl/AsyncHttpCacheServiceImpl.java @@ -17,14 +17,16 @@ public class AsyncHttpCacheServiceImpl implements AsyncHttpCacheService { + private static final String DELIMITER = ":"; @Inject private CachedResponseService cachedResponseService; - - @Inject private AsyncHttpClient asyncHttpClient; - + private String serviceName; + private AsyncHttpClient asyncHttpClient; private long ttl; @Inject - public AsyncHttpCacheServiceImpl(@Assisted AsyncHttpClient asyncHttpClient, @Assisted long ttl) { + public AsyncHttpCacheServiceImpl( + @Assisted String serviceName, @Assisted AsyncHttpClient asyncHttpClient, @Assisted long ttl) { + this.serviceName = serviceName; this.asyncHttpClient = asyncHttpClient; this.ttl = ttl; } @@ -58,10 +60,9 @@ public Optional> executeRequest( private String buildResponseId(Request request) { String requestStringId = StringUtils.join( - request, - request.getBodyEncoding(), - Lists.newArrayList(request.getCookies()).toString()); - return String.valueOf(MurmurHash.hash64A(requestStringId.getBytes(), 0)); + request, request.getStringData(), Lists.newArrayList(request.getCookies()).toString()); + return StringUtils.joinWith( + DELIMITER, serviceName, String.valueOf(MurmurHash.hash64A(requestStringId.getBytes(), 0))); } private AsyncCompletionHandlerBase buildCachingHandler( diff --git a/src/test/java/com/wego/httpcache/dao/impl/guava/TestGuavaCachedResponseImpl.java b/src/test/java/com/wego/httpcache/dao/impl/guava/TestGuavaCachedResponseImpl.java new file mode 100644 index 0000000..69432e2 --- /dev/null +++ b/src/test/java/com/wego/httpcache/dao/impl/guava/TestGuavaCachedResponseImpl.java @@ -0,0 +1,16 @@ +package com.wego.httpcache.dao.impl.guava; + +import org.junit.Before; +import org.junit.Test; + +public class TestGuavaCachedResponseImpl { + + @Before + public void setUp() throws Exception {} + + @Test + public void save() throws Exception {} + + @Test + public void findById() throws Exception {} +} diff --git a/src/test/java/com/wego/httpcache/fixtures/RequestFixture.java b/src/test/java/com/wego/httpcache/fixtures/RequestFixture.java index bab66e0..2038dc9 100644 --- a/src/test/java/com/wego/httpcache/fixtures/RequestFixture.java +++ b/src/test/java/com/wego/httpcache/fixtures/RequestFixture.java @@ -6,6 +6,7 @@ import org.apache.commons.lang3.StringUtils; public class RequestFixture { + public static Request create(String method, String url) { return new RequestBuilder().setMethod(method).setUrl(url).build(); } @@ -27,6 +28,10 @@ public static Request createWithParams(String method, String url, String key, St return new RequestBuilder().setMethod(method).setUrl(url).addParameter(key, value).build(); } + public static Request createWithBody(String method, String url, String body) { + return new RequestBuilder().setMethod(method).setUrl(url).setBody(body).build(); + } + public static Request createWithFullParams() { return new RequestBuilder() .setMethod("GET") @@ -35,6 +40,7 @@ public static Request createWithFullParams() { .addQueryParameter("queryParam", "test") .addParameter("param", "test") .addCookie(new Cookie("cookie", "test", "", "", "", 1, 2, true, true)) + .setBody("test") .build(); } } diff --git a/src/test/java/com/wego/httpcache/services/impl/TestAsyncHttpCachedServiceImpl.java b/src/test/java/com/wego/httpcache/services/impl/TestAsyncHttpCacheServiceImpl.java similarity index 86% rename from src/test/java/com/wego/httpcache/services/impl/TestAsyncHttpCachedServiceImpl.java rename to src/test/java/com/wego/httpcache/services/impl/TestAsyncHttpCacheServiceImpl.java index f2cbbda..761b819 100644 --- a/src/test/java/com/wego/httpcache/services/impl/TestAsyncHttpCachedServiceImpl.java +++ b/src/test/java/com/wego/httpcache/services/impl/TestAsyncHttpCacheServiceImpl.java @@ -42,18 +42,25 @@ import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) -public class TestAsyncHttpCachedServiceImpl { +public class TestAsyncHttpCacheServiceImpl { + + private static final String SERVICE_NAME = "Service Name"; + private static long CACHING_TTL = 60; - @Rule public WireMockRule wireMockRule = new WireMockRule(8089); + @Rule + public WireMockRule wireMockRule = new WireMockRule(8089); - @Spy private AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); + @Spy + private AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); @InjectMocks private AsyncHttpCacheService asyncHttpCacheService = - new AsyncHttpCacheServiceImpl(asyncHttpClient, CACHING_TTL); + new AsyncHttpCacheServiceImpl(SERVICE_NAME, asyncHttpClient, CACHING_TTL); - @Mock private CachedResponseService cachedResponseService; - @Mock private Request request; + @Mock + private CachedResponseService cachedResponseService; + @Mock + private Request request; @Test public void executeRequest_whenWasCached_getResponseFromCacheAndCallOnComplete() @@ -155,11 +162,11 @@ public void buildResponseId_returnsDifferentIdsForDifferentRequest() throws Exce Request requestWithDifferentUrl = RequestFixture.create("GET", "http://localhost:8089/resources/2"); - Request requestWithParams = + Request requestWithQueryParams = RequestFixture.createWithQueryParam( "GET", "http://localhost:8089/resources/2", "queryParam", "test1"); - Request requestWithDifferentParams = + Request requestWithDifferentQueryParams = RequestFixture.createWithQueryParam( "GET", "http://localhost:8089/resources/2", "queryParam", "test2"); @@ -182,31 +189,41 @@ public void buildResponseId_returnsDifferentIdsForDifferentRequest() throws Exce Request requestWithDifferentMethod = RequestFixture.create("POST", "http://localhost:8089/resources/"); - Request requestWithBody = + Request requestWithParams = RequestFixture.createWithParams( "POST", "http://localhost:8089/resources/", "param", "test1"); - Request requestWithDifferentBody = + Request requestWithDifferentParams = RequestFixture.createWithParams( "POST", "http://localhost:8089/resources/", "param", "test2"); + Request requestWithBody = + RequestFixture.createWithBody( + "POST", "http://localhost:8089/resources/", "test1"); + + Request requestWithDifferentBody = + RequestFixture.createWithBody( + "POST", "http://localhost:8089/resources/", "test2"); + Method method = AsyncHttpCacheServiceImpl.class.getDeclaredMethod("buildResponseId", Request.class); method.setAccessible(true); final List responseIds = Stream.of( - request, - requestWithDifferentUrl, - requestWithDifferentMethod, - requestWithBody, - requestWithDifferentBody, - requestWithCookie, - requestWithDifferentCookie, - requestWithHeader, - requestWithDifferentHeader, - requestWithParams, - requestWithDifferentParams) + request, + requestWithDifferentUrl, + requestWithDifferentMethod, + requestWithParams, + requestWithDifferentParams, + requestWithCookie, + requestWithDifferentCookie, + requestWithHeader, + requestWithDifferentHeader, + requestWithQueryParams, + requestWithDifferentQueryParams, + requestWithBody, + requestWithDifferentBody) .map( rq -> { try { @@ -219,7 +236,8 @@ public void buildResponseId_returnsDifferentIdsForDifferentRequest() throws Exce .filter(Objects::nonNull) .collect(Collectors.toList()); - assertThat(Sets.newHashSet(responseIds).size()).isEqualTo(11); + assertThat(Sets.newHashSet(responseIds).size()).isEqualTo(13); + assertThat(responseIds.get(0)).startsWith(SERVICE_NAME); } @Test