Skip to content

Commit 9d88d3e

Browse files
committed
test: add Deserialize test for dto
1 parent 8572e07 commit 9d88d3e

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,46 @@
11
package io.github.collagid.core.api.dtos;
22

3-
public class RecordDTO {
4-
private String id;
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.DeserializationContext;
6+
import com.fasterxml.jackson.databind.JsonDeserializer;
7+
import com.fasterxml.jackson.databind.JsonNode;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
10+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
11+
import com.fasterxml.jackson.databind.node.ObjectNode;
12+
13+
import java.io.IOException;
14+
import java.io.Serializable;
15+
16+
@JsonDeserialize(using = RecordDTO.RecordDTODeserializer.class)
17+
public class RecordDTO extends ObjectNode implements Serializable {
18+
public RecordDTO() {
19+
super(JsonNodeFactory.instance);
20+
}
521

622
public void setId(String id) {
7-
this.id = id;
23+
this.put("id", id);
24+
}
25+
26+
public String getId() {
27+
return this.path("id").asText();
28+
}
29+
30+
static class RecordDTODeserializer extends JsonDeserializer<RecordDTO> {
31+
32+
@Override
33+
public RecordDTO deserialize(JsonParser p, DeserializationContext ctxt)
34+
throws IOException, JsonProcessingException {
35+
System.out.println("xxx");
36+
ObjectMapper mapper = (ObjectMapper) p.getCodec();
37+
JsonNode node = mapper.readTree(p);
38+
RecordDTO recordDTO = new RecordDTO();
39+
if (node.isObject()) {
40+
ObjectNode objectNode = (ObjectNode) node;
41+
recordDTO.setAll(objectNode);
42+
}
43+
return recordDTO;
44+
}
845
}
946
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.collagid.core.api.dtos;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.node.ObjectNode;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
public class RecordDTOTest {
11+
12+
@Test
13+
public void testSetId() {
14+
RecordDTO recordDTO = new RecordDTO();
15+
recordDTO.setId("123");
16+
assertEquals("123", recordDTO.getId());
17+
System.out.println(recordDTO.toPrettyString());
18+
try {
19+
RecordDTO record = this.get("{\n" +
20+
" \"id\" : \"123\"\n" +
21+
"}");
22+
assertEquals(record.getId(), "123");
23+
} catch (JsonProcessingException e) {
24+
throw new RuntimeException(e);
25+
}
26+
}
27+
28+
private RecordDTO get(String text) throws JsonProcessingException {
29+
ObjectNode jsonNodes = new ObjectMapper().readValue(text, ObjectNode.class);
30+
RecordDTO recordDTO = new RecordDTO();
31+
recordDTO.setAll(jsonNodes);
32+
return recordDTO;
33+
}
34+
}

0 commit comments

Comments
 (0)