Skip to content

Commit da5cc05

Browse files
committed
use CookieEntity instead of Cookie in CachedResponseEntity
1 parent ae2752f commit da5cc05

7 files changed

+62
-20
lines changed

src/main/java/com/wego/httpcache/dao/entities/CachedResponseEntity.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package com.wego.httpcache.dao.entities;
22

33
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
4-
import com.ning.http.client.cookie.Cookie;
54
import java.util.List;
65

76
public class CachedResponseEntity {
87

98
private String id;
10-
private List<Cookie> cookies;
9+
private List<CookieEntity> cookies;
1110
private String responseBody;
1211
private FluentCaseInsensitiveStringsMap headers;
1312
private Integer statusCode;
1413
private String statusText;
1514

16-
public List<Cookie> getCookies() {
15+
public List<CookieEntity> getCookies() {
1716
return cookies;
1817
}
1918

@@ -40,7 +39,7 @@ public String getId() {
4039
public static final class Builder {
4140

4241
private String id;
43-
private List<Cookie> cookies;
42+
private List<CookieEntity> cookies;
4443
private String responseBody;
4544
private FluentCaseInsensitiveStringsMap headers;
4645
private Integer statusCode;
@@ -53,7 +52,7 @@ public Builder setId(String id) {
5352
return this;
5453
}
5554

56-
public Builder setCookies(List<Cookie> cookies) {
55+
public Builder setCookies(List<CookieEntity> cookies) {
5756
this.cookies = cookies;
5857
return this;
5958
}

src/main/java/com/wego/httpcache/dao/mappers/CachedResponseEntityToCachedResponse.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.wego.httpcache.dao.mappers;
22

3+
import com.google.inject.Inject;
4+
import com.google.inject.Singleton;
35
import com.wego.httpcache.dao.entities.CachedResponseEntity;
46
import com.wego.httpcache.dao.models.CachedResponse;
57

8+
@Singleton
69
public class CachedResponseEntityToCachedResponse {
710

11+
@Inject
12+
private CookieEntityToCookie cookieEntityToCookie;
13+
814
public CachedResponse transform(CachedResponseEntity cachedResponseEntity) {
915
CachedResponse cachedResponse = null;
1016

@@ -15,7 +21,7 @@ public CachedResponse transform(CachedResponseEntity cachedResponseEntity) {
1521
.setStatusCode(cachedResponseEntity.getStatusCode())
1622
.setStatusText(cachedResponseEntity.getStatusText())
1723
.setHeaders(cachedResponseEntity.getHeaders())
18-
.setCookies(cachedResponseEntity.getCookies())
24+
.setCookies(cookieEntityToCookie.transform(cachedResponseEntity.getCookies()))
1925
.setResponseBody(cachedResponseEntity.getResponseBody())
2026
.build();
2127
}

src/main/java/com/wego/httpcache/dao/mappers/CachedResponseToCachedResponseEntity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package com.wego.httpcache.dao.mappers;
22

3+
import com.google.inject.Inject;
4+
import com.google.inject.Singleton;
35
import com.wego.httpcache.dao.entities.CachedResponseEntity;
46
import com.wego.httpcache.dao.models.CachedResponse;
57
import java.io.IOException;
68

9+
@Singleton
710
public class CachedResponseToCachedResponseEntity {
811

12+
@Inject
13+
private CookieToCookieEntity cookieToCookieEntity;
14+
915
public CachedResponseEntity transform(CachedResponse cachedResponse) throws IOException {
1016
CachedResponseEntity cachedResponseEntity = null;
1117

@@ -14,7 +20,7 @@ public CachedResponseEntity transform(CachedResponse cachedResponse) throws IOEx
1420
new CachedResponseEntity.Builder()
1521
.setId(cachedResponse.getId())
1622
.setResponseBody(cachedResponse.getResponseBody())
17-
.setCookies(cachedResponse.getCookies())
23+
.setCookies(cookieToCookieEntity.transform(cachedResponse.getCookies()))
1824
.setHeaders(cachedResponse.getHeaders())
1925
.setStatusText(cachedResponse.getStatusText())
2026
.setStatusCode(cachedResponse.getStatusCode())
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
package com.wego.httpcache.dao.mappers;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.Matchers.anyList;
5+
import static org.mockito.Mockito.when;
46

7+
import com.ning.http.client.cookie.Cookie;
58
import com.wego.httpcache.dao.entities.CachedResponseEntity;
69
import com.wego.httpcache.dao.models.CachedResponse;
710
import com.wego.httpcache.fixtures.CachedResponseEntityFixture;
11+
import com.wego.httpcache.fixtures.CookieFixture;
12+
import java.util.Arrays;
813
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.mockito.InjectMocks;
16+
import org.mockito.Mock;
17+
import org.mockito.runners.MockitoJUnitRunner;
918

19+
@RunWith(MockitoJUnitRunner.class)
1020
public class TestCachedResponseEntityToCachedResponse {
21+
@Mock private CookieEntityToCookie cookieEntityToCookie;
22+
23+
@InjectMocks
1124
private CachedResponseEntityToCachedResponse cachedResponseEntityToCachedResponse =
1225
new CachedResponseEntityToCachedResponse();
1326

@@ -18,17 +31,19 @@ public void transform_whenInputParamIsNull_returnsNull() throws Exception {
1831

1932
@Test
2033
public void transform_whenValidInputParam_returnsCachedResponse() throws Exception {
21-
CachedResponseEntity cachedResponseEntity = CachedResponseEntityFixture.create();
34+
final CachedResponseEntity cachedResponseEntity = CachedResponseEntityFixture.create();
35+
final Cookie cookie = CookieFixture.create();
36+
37+
when(cookieEntityToCookie.transform(anyList())).thenReturn(Arrays.asList(cookie));
2238

23-
CachedResponse cachedResponse =
24-
cachedResponseEntityToCachedResponse.transform(cachedResponseEntity);
39+
CachedResponse subject = cachedResponseEntityToCachedResponse.transform(cachedResponseEntity);
2540

26-
assertThat(cachedResponse.getId()).isEqualTo(cachedResponseEntity.getId());
27-
assertThat(cachedResponse.getCookies()).isEqualTo(cachedResponseEntity.getCookies());
28-
assertThat(cachedResponse.getHeaders().toString())
41+
assertThat(subject.getId()).isEqualTo(cachedResponseEntity.getId());
42+
assertThat(subject.getCookies()).contains(cookie);
43+
assertThat(subject.getHeaders().toString())
2944
.contains(cachedResponseEntity.getHeaders().toString());
30-
assertThat(cachedResponse.getResponseBody()).isEqualTo(cachedResponseEntity.getResponseBody());
31-
assertThat(cachedResponse.getStatusCode()).isEqualTo(cachedResponseEntity.getStatusCode());
32-
assertThat(cachedResponse.getStatusText()).isEqualTo(cachedResponseEntity.getStatusText());
45+
assertThat(subject.getResponseBody()).isEqualTo(cachedResponseEntity.getResponseBody());
46+
assertThat(subject.getStatusCode()).isEqualTo(cachedResponseEntity.getStatusCode());
47+
assertThat(subject.getStatusText()).isEqualTo(cachedResponseEntity.getStatusText());
3348
}
3449
}

src/test/java/com/wego/httpcache/dao/mappers/TestCachedResponseToCachedResponseEntity.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
package com.wego.httpcache.dao.mappers;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.Matchers.anyList;
5+
import static org.mockito.Mockito.when;
46

57
import com.wego.httpcache.dao.entities.CachedResponseEntity;
8+
import com.wego.httpcache.dao.entities.CookieEntity;
69
import com.wego.httpcache.dao.models.CachedResponse;
710
import com.wego.httpcache.fixtures.CachedResponseFixture;
11+
import com.wego.httpcache.fixtures.CookieEntityFixture;
12+
import java.util.Arrays;
813
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.mockito.InjectMocks;
16+
import org.mockito.Mock;
17+
import org.mockito.runners.MockitoJUnitRunner;
918

19+
@RunWith(MockitoJUnitRunner.class)
1020
public class TestCachedResponseToCachedResponseEntity {
21+
@InjectMocks
1122
private CachedResponseToCachedResponseEntity cachedResponseToCachedResponseEntity =
1223
new CachedResponseToCachedResponseEntity();
1324

25+
@Mock private CookieToCookieEntity cookieToCookieEntity;
26+
1427
@Test
1528
public void transform_whenInputParamIsNull_returnsNull() throws Exception {
1629
assertThat(cachedResponseToCachedResponseEntity.transform(null)).isNull();
@@ -19,12 +32,15 @@ public void transform_whenInputParamIsNull_returnsNull() throws Exception {
1932
@Test
2033
public void transform_whenValidInputParam_returnsCachedResponseEntity() throws Exception {
2134
CachedResponse cachedResponse = CachedResponseFixture.create();
35+
CookieEntity cookieEntity = CookieEntityFixture.create();
36+
37+
when(cookieToCookieEntity.transform(anyList())).thenReturn(Arrays.asList(cookieEntity));
2238

2339
CachedResponseEntity cachedResponseEntity =
2440
cachedResponseToCachedResponseEntity.transform(cachedResponse);
2541

2642
assertThat(cachedResponseEntity.getId()).isEqualTo(cachedResponse.getId());
27-
assertThat(cachedResponseEntity.getCookies()).isEqualTo(cachedResponse.getCookies());
43+
assertThat(cachedResponseEntity.getCookies()).contains(cookieEntity);
2844
assertThat(cachedResponseEntity.getHeaders().toString())
2945
.contains(cachedResponse.getHeaders().toString());
3046
assertThat(cachedResponseEntity.getResponseBody()).isEqualTo(cachedResponse.getResponseBody());

src/test/java/com/wego/httpcache/fixtures/CachedResponseEntityFixture.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public static CachedResponseEntity create() {
99
.setId("ID")
1010
.setResponseBody("Body")
1111
.setStatusCode(200)
12-
.setStatusText("")
12+
.setStatusText("Ok")
1313
.setHeaders(HeaderFixture.create())
14-
.setCookies(Arrays.asList(CookieFixture.create()))
14+
.setCookies(Arrays.asList(CookieEntityFixture.create()))
1515
.build();
1616
}
1717
}

src/test/java/com/wego/httpcache/fixtures/CachedResponseFixture.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static CachedResponse create() {
99
.setId("ID")
1010
.setResponseBody("Body")
1111
.setStatusCode(200)
12-
.setStatusText("")
12+
.setStatusText("Ok")
1313
.setHeaders(HeaderFixture.create())
1414
.setCookies(Arrays.asList(CookieFixture.create()))
1515
.build();

0 commit comments

Comments
 (0)