Skip to content

Commit c4b0cfc

Browse files
author
eugenp
committed
further jackson work - ignroing new fields, and some httpclient cookie work
1 parent e4df951 commit c4b0cfc

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed

httpclient/src/test/java/org/baeldung/httpclient/HttpClientCookieLiveTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public final void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_then
6969
final BasicCookieStore cookieStore = new BasicCookieStore();
7070
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
7171
cookie.setDomain(".github.com");
72+
cookie.setPath("/");
7273
cookieStore.addCookie(cookie);
7374
final DefaultHttpClient client = new DefaultHttpClient();
7475
client.setCookieStore(cookieStore);
@@ -85,6 +86,7 @@ public final void whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly() th
8586
final BasicCookieStore cookieStore = new BasicCookieStore();
8687
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
8788
cookie.setDomain(".github.com");
89+
cookie.setPath("/");
8890
cookieStore.addCookie(cookie);
8991
instance = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
9092

@@ -100,13 +102,15 @@ public final void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly() throw
100102
final BasicCookieStore cookieStore = new BasicCookieStore();
101103
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
102104
cookie.setDomain(".github.com");
105+
cookie.setPath("/");
103106
cookieStore.addCookie(cookie);
104107
instance = HttpClientBuilder.create().build();
105108

106109
final HttpGet request = new HttpGet("http://www.github.com");
107110

108111
final HttpContext localContext = new BasicHttpContext();
109112
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
113+
// localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // before 4.3
110114
response = instance.execute(request, localContext);
111115

112116
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));

httpclient/src/test/java/org/baeldung/httpclient/HttpClientLiveTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,30 @@ public final void whenRequestHasCustomUserAgent_thenCorrect() throws ClientProto
157157
response = instance.execute(new HttpGet(SAMPLE_URL));
158158
}
159159

160+
// tests - cancel request
161+
162+
@Test
163+
public final void whenRequestIsCanceled_thenCorrect() throws ClientProtocolException, IOException {
164+
instance = HttpClients.custom().build();
165+
final HttpGet request = new HttpGet(SAMPLE_URL);
166+
response = instance.execute(request);
167+
168+
try {
169+
final HttpEntity entity = response.getEntity();
170+
171+
System.out.println("----------------------------------------");
172+
System.out.println(response.getStatusLine());
173+
if (entity != null) {
174+
System.out.println("Response content length: " + entity.getContentLength());
175+
}
176+
System.out.println("----------------------------------------");
177+
178+
// Do not feel like reading the response body
179+
// Call abort on the request object
180+
request.abort();
181+
} finally {
182+
response.close();
183+
}
184+
}
185+
160186
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.baeldung.jackson.ignore;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
5+
6+
@JsonInclude(Include.NON_NULL)
7+
public class MyDtoIgnoreNull {
8+
9+
private String stringValue;
10+
private int intValue;
11+
private boolean booleanValue;
12+
13+
public MyDtoIgnoreNull() {
14+
super();
15+
}
16+
17+
public MyDtoIgnoreNull(final String stringValue, final int intValue, final boolean booleanValue) {
18+
super();
19+
20+
this.stringValue = stringValue;
21+
this.intValue = intValue;
22+
this.booleanValue = booleanValue;
23+
}
24+
25+
// API
26+
27+
public String getStringValue() {
28+
return stringValue;
29+
}
30+
31+
public void setStringValue(final String stringValue) {
32+
this.stringValue = stringValue;
33+
}
34+
35+
public int getIntValue() {
36+
return intValue;
37+
}
38+
39+
public void setIntValue(final int intValue) {
40+
this.intValue = intValue;
41+
}
42+
43+
public boolean isBooleanValue() {
44+
return booleanValue;
45+
}
46+
47+
public void setBooleanValue(final boolean booleanValue) {
48+
this.booleanValue = booleanValue;
49+
}
50+
51+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.baeldung.jackson.test;
2+
3+
import static org.hamcrest.Matchers.instanceOf;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.io.IOException;
7+
import java.util.LinkedHashMap;
8+
import java.util.List;
9+
10+
import org.baeldung.jackson.ignore.MyDto;
11+
import org.junit.Test;
12+
13+
import com.fasterxml.jackson.core.JsonParseException;
14+
import com.fasterxml.jackson.core.type.TypeReference;
15+
import com.fasterxml.jackson.databind.JsonMappingException;
16+
import com.fasterxml.jackson.databind.ObjectMapper;
17+
import com.fasterxml.jackson.databind.type.CollectionType;
18+
import com.google.common.collect.Lists;
19+
20+
public class JacksonCollectionDeserializationUnitTest {
21+
22+
// tests - json to multiple entity
23+
24+
@Test
25+
public final void givenJsonArray_whenDeserializingAsArray_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
26+
final ObjectMapper mapper = new ObjectMapper();
27+
28+
final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false));
29+
final String jsonArray = mapper.writeValueAsString(listOfDtos);
30+
// [{"stringValue":"a","intValue":1,"booleanValue":true},{"stringValue":"bc","intValue":3,"booleanValue":false}]
31+
32+
final MyDto[] asArray = mapper.readValue(jsonArray, MyDto[].class);
33+
assertThat(asArray[0], instanceOf(MyDto.class));
34+
}
35+
36+
@Test
37+
public final void givenJsonArray_whenDeserializingAsListWithNoTypeInfo_thenNotCorrect() throws JsonParseException, JsonMappingException, IOException {
38+
final ObjectMapper mapper = new ObjectMapper();
39+
40+
final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false));
41+
final String jsonArray = mapper.writeValueAsString(listOfDtos);
42+
// [{"stringValue":"a","intValue":1,"booleanValue":true},{"stringValue":"bc","intValue":3,"booleanValue":false}]
43+
44+
final List<MyDto> asList = mapper.readValue(jsonArray, List.class);
45+
assertThat((Object) asList.get(0), instanceOf(LinkedHashMap.class));
46+
}
47+
48+
@Test
49+
public final void givenJsonArray_whenDeserializingAsListWithTypeReferenceHelp_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
50+
final ObjectMapper mapper = new ObjectMapper();
51+
52+
final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false));
53+
final String jsonArray = mapper.writeValueAsString(listOfDtos);
54+
// [{"stringValue":"a","intValue":1,"booleanValue":true},{"stringValue":"bc","intValue":3,"booleanValue":false}]
55+
56+
final List<MyDto> asList = mapper.readValue(jsonArray, new TypeReference<List<MyDto>>() {
57+
});
58+
assertThat(asList.get(0), instanceOf(MyDto.class));
59+
}
60+
61+
@Test
62+
public final void givenJsonArray_whenDeserializingAsListWithJavaTypeHelp_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
63+
final ObjectMapper mapper = new ObjectMapper();
64+
65+
final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false));
66+
final String jsonArray = mapper.writeValueAsString(listOfDtos);
67+
// [{"stringValue":"a","intValue":1,"booleanValue":true},{"stringValue":"bc","intValue":3,"booleanValue":false}]
68+
69+
final CollectionType javaType = mapper.getTypeFactory().constructCollectionType(List.class, MyDto.class);
70+
final List<MyDto> asList = mapper.readValue(jsonArray, javaType);
71+
assertThat(asList.get(0), instanceOf(MyDto.class));
72+
}
73+
74+
}
75+
// a (private) no-args constructor is required (simulate without)

jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
import org.baeldung.jackson.ignore.MyDtoFieldNameChanged;
1212
import org.baeldung.jackson.ignore.MyDtoIgnoreField;
1313
import org.baeldung.jackson.ignore.MyDtoIgnoreFieldByName;
14+
import org.baeldung.jackson.ignore.MyDtoIgnoreNull;
1415
import org.baeldung.jackson.ignore.MyDtoIncludeNonDefault;
1516
import org.baeldung.jackson.ignore.MyDtoWithFilter;
1617
import org.baeldung.jackson.ignore.MyMixInForString;
1718
import org.junit.Test;
1819

20+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
1921
import com.fasterxml.jackson.core.JsonGenerator;
2022
import com.fasterxml.jackson.core.JsonParseException;
23+
import com.fasterxml.jackson.core.JsonProcessingException;
2124
import com.fasterxml.jackson.databind.ObjectMapper;
2225
import com.fasterxml.jackson.databind.SerializerProvider;
2326
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
@@ -169,6 +172,33 @@ protected final boolean include(final PropertyWriter writer) {
169172
System.out.println(dtoAsString);
170173
}
171174

175+
@Test
176+
public final void givenIgnoringNullFieldsOnClass_whenSerializingObjectWithNullField_thenFieldIsIgnroed() throws JsonProcessingException {
177+
final ObjectMapper mapper = new ObjectMapper();
178+
final MyDtoIgnoreNull dtoObject = new MyDtoIgnoreNull();
179+
180+
final String dtoAsString = mapper.writeValueAsString(dtoObject);
181+
182+
assertThat(dtoAsString, containsString("intValue"));
183+
assertThat(dtoAsString, containsString("booleanValue"));
184+
assertThat(dtoAsString, not(containsString("stringValue")));
185+
System.out.println(dtoAsString);
186+
}
187+
188+
@Test
189+
public final void givenIgnoringNullFieldsGlobally_whenSerializingObjectWithNullField_thenFieldIsIgnroed() throws JsonProcessingException {
190+
final ObjectMapper mapper = new ObjectMapper();
191+
mapper.setSerializationInclusion(Include.NON_NULL);
192+
final MyDto dtoObject = new MyDto();
193+
194+
final String dtoAsString = mapper.writeValueAsString(dtoObject);
195+
196+
assertThat(dtoAsString, containsString("intValue"));
197+
assertThat(dtoAsString, containsString("booleanValue"));
198+
assertThat(dtoAsString, not(containsString("stringValue")));
199+
System.out.println(dtoAsString);
200+
}
201+
172202
// tests - multiple entities to json
173203

174204
@Test

0 commit comments

Comments
 (0)