Skip to content

Commit ae2752f

Browse files
committed
add CookieEntity
1 parent 039f53c commit ae2752f

File tree

7 files changed

+315
-1
lines changed

7 files changed

+315
-1
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.wego.httpcache.dao.entities;
2+
3+
public class CookieEntity {
4+
private String name;
5+
private String value;
6+
private String rawValue;
7+
private String domain;
8+
private String path;
9+
private long expires;
10+
private int maxAge;
11+
private boolean secure;
12+
private boolean httpOnly;
13+
14+
public CookieEntity() {}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public String getValue() {
21+
return value;
22+
}
23+
24+
public String getRawValue() {
25+
return rawValue;
26+
}
27+
28+
public String getDomain() {
29+
return domain;
30+
}
31+
32+
public String getPath() {
33+
return path;
34+
}
35+
36+
public int getMaxAge() {
37+
return maxAge;
38+
}
39+
40+
public boolean isSecure() {
41+
return secure;
42+
}
43+
44+
public boolean isHttpOnly() {
45+
return httpOnly;
46+
}
47+
48+
public long getExpires() {
49+
return expires;
50+
}
51+
52+
public static final class Builder {
53+
54+
private String name;
55+
private String value;
56+
private String rawValue;
57+
private String domain;
58+
private String path;
59+
private long expires;
60+
private int maxAge;
61+
private boolean secure;
62+
private boolean httpOnly;
63+
64+
public Builder() {}
65+
66+
public Builder setName(String name) {
67+
this.name = name;
68+
return this;
69+
}
70+
71+
public Builder setValue(String value) {
72+
this.value = value;
73+
return this;
74+
}
75+
76+
public Builder setRawValue(String rawValue) {
77+
this.rawValue = rawValue;
78+
return this;
79+
}
80+
81+
public Builder setDomain(String domain) {
82+
this.domain = domain;
83+
return this;
84+
}
85+
86+
public Builder setPath(String path) {
87+
this.path = path;
88+
return this;
89+
}
90+
91+
public Builder setExpires(long expires) {
92+
this.expires = expires;
93+
return this;
94+
}
95+
96+
public Builder setMaxAge(int maxAge) {
97+
this.maxAge = maxAge;
98+
return this;
99+
}
100+
101+
public Builder setSecure(boolean secure) {
102+
this.secure = secure;
103+
return this;
104+
}
105+
106+
public Builder setHttpOnly(boolean httpOnly) {
107+
this.httpOnly = httpOnly;
108+
return this;
109+
}
110+
111+
public CookieEntity build() {
112+
CookieEntity cookieEntity = new CookieEntity();
113+
cookieEntity.domain = this.domain;
114+
cookieEntity.maxAge = this.maxAge;
115+
cookieEntity.secure = this.secure;
116+
cookieEntity.httpOnly = this.httpOnly;
117+
cookieEntity.rawValue = this.rawValue;
118+
cookieEntity.name = this.name;
119+
cookieEntity.path = this.path;
120+
cookieEntity.value = this.value;
121+
cookieEntity.expires = this.expires;
122+
return cookieEntity;
123+
}
124+
}
125+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.wego.httpcache.dao.mappers;
2+
3+
import com.google.inject.Singleton;
4+
import com.ning.http.client.cookie.Cookie;
5+
import com.wego.httpcache.dao.entities.CookieEntity;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
@Singleton
10+
public class CookieEntityToCookie {
11+
12+
public Cookie transform(CookieEntity cookieEntity) {
13+
Cookie cookie = null;
14+
15+
if (cookieEntity != null) {
16+
cookie =
17+
Cookie.newValidCookie(
18+
cookieEntity.getName(),
19+
cookieEntity.getValue(),
20+
cookieEntity.getDomain(),
21+
cookieEntity.getRawValue(),
22+
cookieEntity.getPath(),
23+
cookieEntity.getExpires(),
24+
cookieEntity.getMaxAge(),
25+
cookieEntity.isSecure(),
26+
cookieEntity.isHttpOnly());
27+
}
28+
29+
return cookie;
30+
}
31+
32+
public List<Cookie> transform(List<CookieEntity> cookieEntities) {
33+
return cookieEntities.stream().map(this::transform).collect(Collectors.toList());
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.wego.httpcache.dao.mappers;
2+
3+
import com.google.inject.Singleton;
4+
import com.ning.http.client.cookie.Cookie;
5+
import com.wego.httpcache.dao.entities.CookieEntity;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
@Singleton
10+
public class CookieToCookieEntity {
11+
12+
public CookieEntity transform(Cookie cookie) {
13+
CookieEntity cookieEntity = null;
14+
15+
if (cookie != null) {
16+
cookieEntity =
17+
new CookieEntity.Builder()
18+
.setName(cookie.getName())
19+
.setValue(cookie.getValue())
20+
.setRawValue(cookie.getRawValue())
21+
.setDomain(cookie.getDomain())
22+
.setPath(cookie.getPath())
23+
.setExpires(cookie.getExpires())
24+
.setMaxAge(cookie.getMaxAge())
25+
.setSecure(cookie.isSecure())
26+
.setHttpOnly(cookie.isHttpOnly())
27+
.build();
28+
}
29+
30+
return cookieEntity;
31+
}
32+
33+
public List<CookieEntity> transform(List<Cookie> cookies) {
34+
return cookies.stream().map(this::transform).collect(Collectors.toList());
35+
}
36+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.wego.httpcache.dao.mappers;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import com.ning.http.client.cookie.Cookie;
6+
import com.wego.httpcache.dao.entities.CookieEntity;
7+
import com.wego.httpcache.fixtures.CookieEntityFixture;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import org.junit.Test;
11+
12+
public class TestCookieEntityToCookie {
13+
private CookieEntityToCookie cookieEntityToCookie = new CookieEntityToCookie();
14+
15+
@Test
16+
public void transform_whenInputIsNull_returnsNull() throws Exception {
17+
CookieEntity cookieEntity = null;
18+
assertThat(cookieEntityToCookie.transform(cookieEntity)).isEqualTo(null);
19+
}
20+
21+
@Test
22+
public void transform_whenValidInput_returnsCookieEntity() throws Exception {
23+
CookieEntity cookieEntity = CookieEntityFixture.create();
24+
25+
Cookie cookie = cookieEntityToCookie.transform(cookieEntity);
26+
27+
assertThat(cookie.getName()).isEqualTo(cookieEntity.getName());
28+
assertThat(cookie.getValue()).isEqualTo(cookieEntity.getValue());
29+
assertThat(cookie.getRawValue()).isEqualTo(cookieEntity.getRawValue());
30+
assertThat(cookie.getDomain()).isEqualTo(cookieEntity.getDomain());
31+
assertThat(cookie.getPath()).isEqualTo(cookieEntity.getPath());
32+
assertThat(cookie.getExpires()).isEqualTo(cookieEntity.getExpires());
33+
assertThat(cookie.getMaxAge()).isEqualTo(cookieEntity.getMaxAge());
34+
assertThat(cookie.isSecure()).isEqualTo(cookieEntity.isSecure());
35+
assertThat(cookie.isHttpOnly()).isEqualTo(cookieEntity.isHttpOnly());
36+
}
37+
38+
@Test
39+
public void transform_returnsListOfCookieEntity() throws Exception {
40+
CookieEntity cookieEntity = CookieEntityFixture.create();
41+
42+
List<Cookie> cookies = cookieEntityToCookie.transform(Arrays.asList(cookieEntity));
43+
44+
assertThat(cookies.size()).isEqualTo(1);
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.wego.httpcache.dao.mappers;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import com.ning.http.client.cookie.Cookie;
6+
import com.wego.httpcache.dao.entities.CookieEntity;
7+
import com.wego.httpcache.fixtures.CookieFixture;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import org.junit.Test;
11+
12+
public class TestCookieToCookieEntity {
13+
private CookieToCookieEntity cookieToCookieEntity = new CookieToCookieEntity();
14+
15+
@Test
16+
public void transform_whenInputIsNull_returnsNull() throws Exception {
17+
Cookie cookie = null;
18+
assertThat(cookieToCookieEntity.transform(cookie)).isEqualTo(null);
19+
}
20+
21+
@Test
22+
public void transform_whenValidInput_returnsCookieEntity() throws Exception {
23+
Cookie cookie = CookieFixture.create();
24+
25+
CookieEntity cookieEntity = cookieToCookieEntity.transform(cookie);
26+
27+
assertThat(cookieEntity.getName()).isEqualTo(cookie.getName());
28+
assertThat(cookieEntity.getValue()).isEqualTo(cookie.getValue());
29+
assertThat(cookieEntity.getRawValue()).isEqualTo(cookie.getRawValue());
30+
assertThat(cookieEntity.getDomain()).isEqualTo(cookie.getDomain());
31+
assertThat(cookieEntity.getPath()).isEqualTo(cookie.getPath());
32+
assertThat(cookieEntity.getExpires()).isEqualTo(cookie.getExpires());
33+
assertThat(cookieEntity.getMaxAge()).isEqualTo(cookie.getMaxAge());
34+
assertThat(cookieEntity.isSecure()).isEqualTo(cookie.isSecure());
35+
assertThat(cookieEntity.isHttpOnly()).isEqualTo(cookie.isHttpOnly());
36+
}
37+
38+
@Test
39+
public void transform_returnsListOfCookieEntity() throws Exception {
40+
Cookie cookie = CookieFixture.create();
41+
42+
List<CookieEntity> cookieEntities = cookieToCookieEntity.transform(Arrays.asList(cookie));
43+
44+
assertThat(cookieEntities.size()).isEqualTo(1);
45+
}
46+
47+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.wego.httpcache.fixtures;
2+
3+
import com.wego.httpcache.dao.entities.CookieEntity;
4+
import org.apache.commons.lang3.StringUtils;
5+
6+
public class CookieEntityFixture {
7+
public static CookieEntity create() {
8+
return createWithKeyAndValue("key", "value");
9+
}
10+
11+
public static CookieEntity createWithKeyAndValue(String key, String value) {
12+
return new CookieEntity.Builder()
13+
.setName(key)
14+
.setValue(value)
15+
.setRawValue(StringUtils.join(key, "=", value))
16+
.setDomain("wego.com")
17+
.setPath("./")
18+
.setExpires(1)
19+
.setMaxAge(2)
20+
.setSecure(true)
21+
.setHttpOnly(true)
22+
.build();
23+
}
24+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public static Cookie create() {
99
}
1010

1111
public static Cookie createWithKeyAndValue(String key, String value) {
12-
return new Cookie(key, value, StringUtils.join(key, "=", value), "", "", 1, 2, true, true);
12+
return new Cookie(
13+
key, value, StringUtils.join(key, "=", value), "wego.com", "./", 1, 2, true, true);
1314
}
1415
}

0 commit comments

Comments
 (0)