-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
ReadUnknownFields202Test.java
132 lines (109 loc) · 3.87 KB
/
ReadUnknownFields202Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.fasterxml.jackson.dataformat.protobuf.failing;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.dataformat.protobuf.*;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchemaLoader;
// [dataformats-binary#202]
public class ReadUnknownFields202Test extends ProtobufTestBase
{
// [dataformats-binary#202]
static class TestMessageV0
{
@JsonProperty(required = true, index = 1)
private String id;
@JsonProperty(required = false, index = 2)
private String plant;
public TestMessageV0() { }
public TestMessageV0(String id, String plant) {
this.id = id;
this.plant = plant;
}
public String getId() {
return id;
}
public String getPlant() {
return plant;
}
}
static class TestMessageV1
{
@JsonProperty(required = true, index = 1)
private String id;
@JsonProperty(required = false, index = 2)
private String plant;
@JsonProperty(required = false, index = 3)
private Double length;
@JsonProperty(required = false, index = 4)
private Double width;
@JsonProperty(required = false, index = 5)
private String descr;
@JsonProperty(required = false, index = 6)
private String source;
public TestMessageV1() { }
public TestMessageV1(String id, String plant, Double length) {
this.id = id;
this.plant = plant;
this.length = length;
}
public String getId() {
return id;
}
public String getPlant() {
return plant;
}
public Double getLength() {
return length;
}
public Double getWidth() {
return width;
}
public String getDescr() {
return descr;
}
public String getSource() {
return source;
}
}
// [dataformats-binary#202]
private static final String MESSAGE_V0_SCHEMA =
"message TestMessageV0 {\n"
+ "required string id = 1;\n"
+ "optional string plant = 2;\n"
+"}";
private static final String MESSAGE_V1_SCHEMA =
"message TestMessageV1 {\n" +
"required string id = 1;\n" +
"optional double length = 3;\n" +
"optional double width = 4;\n" +
"optional string source = 6;\n" +
"optional string descr = 5;\n" +
"optional string plant = 2;\n" +
"}";
/*
/**********************************************************
/* Test methods
/**********************************************************
*/
// [dataformats-binary#202]
public void testV1toV0() throws Exception {
final ProtobufMapper MAPPER = newMapperBuilder()
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
.enable(JsonParser.Feature.IGNORE_UNDEFINED)
.build();
TestMessageV1 messageV1 = new TestMessageV1("1", "test", 9.9);
ProtobufSchema schemaV1 = ProtobufSchemaLoader.std.parse(MESSAGE_V1_SCHEMA);
byte[] protobufData = MAPPER
.writer(schemaV1)
.writeValueAsBytes(messageV1);
ProtobufSchema schemaV0 = ProtobufSchemaLoader.std.parse(MESSAGE_V0_SCHEMA);
TestMessageV0 messageV0 = MAPPER
.readerFor(TestMessageV0.class)
.with(schemaV0)
.with(JsonParser.Feature.IGNORE_UNDEFINED)
.readValue(protobufData);
assertEquals(messageV1.getId(), messageV0.getId());
assertEquals(messageV1.getPlant(), messageV0.getPlant());
}
}