Skip to content

Commit

Permalink
Add unit test for #126
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 7, 2017
1 parent 3d0e5ec commit 1e66baa
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.fasterxml.jackson.dataformat.protobuf;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Arrays;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonParser;

import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;

// [dataformats-binary#108]
public class ReadNestedUnknownFieldsTest extends ProtobufTestBase
{
// [dataformats-binary#108]
public static class LessNestedField {
@JsonProperty(value = "f1", index = 1)
private NestedOneField f1;
Expand Down Expand Up @@ -75,32 +77,107 @@ public void setNested2(int nested2) {
}
}

// [dataformats-binary#126]
@JsonPropertyOrder({"embed", "state"})
public static class OuterV2 {
@JsonProperty("embed")
public EmbedV2 embed;
@JsonProperty("state")
public String state;
}

@JsonPropertyOrder({"embed", "state"})
public static class Outer {
@JsonProperty("embed")
public Embed embed;
@JsonProperty("state")
public String state;
}

@JsonPropertyOrder({"a", "b", "c", "extraField"})
public static class EmbedV2 {
@JsonProperty("a")
public String a;
@JsonProperty("b")
public String b;
@JsonProperty("c")
public List<String> c;
@JsonProperty("extraField")
public String extraField;
}

@JsonPropertyOrder({"a", "b", "c"})
public static class Embed {
@JsonProperty("a")
public String a;
@JsonProperty("b")
public String b;
@JsonProperty("c")
public List<String> c;
}

/*
/**********************************************************
/* Test methods
/**********************************************************
*/

final ProtobufMapper MAPPER = new ProtobufMapper();
private final ProtobufMapper MAPPER = new ProtobufMapper();

// [dataformats-binary#108]
public void testMultipleUnknown() throws Exception
{
MoreNestedField moreNestedField = new MoreNestedField();
NestedTwoField nestedTwoField = new NestedTwoField();
nestedTwoField.setNested1(1);
nestedTwoField.setNested2(2);
moreNestedField.setF1(nestedTwoField);

byte[] in = MAPPER.writerFor(MoreNestedField.class)
.with(MAPPER.generateSchemaFor(MoreNestedField.class))
.writeValueAsBytes(moreNestedField);

LessNestedField lesser = MAPPER.readerFor(LessNestedField.class)
.with(MAPPER.generateSchemaFor(LessNestedField.class))
// important: skip through unknown
.with(JsonParser.Feature.IGNORE_UNDEFINED)
.readValue(in);

assertEquals(moreNestedField.getF1().getNested2(), lesser.getF1().getNested2());
}

// [dataformats-binary#126]
public void testCheckEndAfterSkip() throws Exception
{
ProtobufMapper mapper = new ProtobufMapper();
mapper.enable(JsonParser.Feature.IGNORE_UNDEFINED);
ProtobufSchema schema = MAPPER.generateSchemaFor(Outer.class);
ProtobufSchema schemaV2 = MAPPER.generateSchemaFor(OuterV2.class);

EmbedV2 embedV2 = new EmbedV2();
embedV2.c = Arrays.asList("c");
embedV2.extraField = "extra";

OuterV2 v2Expected = new OuterV2();
v2Expected.embed = embedV2;
v2Expected.state="state";

// serialize type with extra field
byte[] doc = mapper.writer(schemaV2).writeValueAsBytes(v2Expected);

public void testMultipleUnknown() throws Exception
{
MoreNestedField moreNestedField = new MoreNestedField();
NestedTwoField nestedTwoField = new NestedTwoField();
nestedTwoField.setNested1(1);
nestedTwoField.setNested2(2);
moreNestedField.setF1(nestedTwoField);
// showBytes(bout.toByteArray());

byte[] in = MAPPER.writerFor(MoreNestedField.class)
.with(MAPPER.generateSchemaFor(MoreNestedField.class))
.writeValueAsBytes(moreNestedField);
// deserialize type with extra field
OuterV2 v2Actual = mapper.readerFor(OuterV2.class)
.with(schemaV2).readValue(doc);
// this is ok
assertEquals(v2Expected.state, v2Actual.state);

LessNestedField lesser = MAPPER.readerFor(LessNestedField.class)
.with(MAPPER.generateSchemaFor(LessNestedField.class))
// important: skip through unknown
.with(JsonParser.Feature.IGNORE_UNDEFINED)
.readValue(in);
// deserialize type without extra field
Outer v1Actual = mapper.readerFor(Outer.class).with(schema)
.readValue(doc);

assertEquals(moreNestedField.getF1().getNested2(), lesser.getF1().getNested2());
// Outer.state is skipped when skipping Embed.extraField
assertEquals(v2Expected.state, v1Actual.state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.protobuf.ProtobufFactory;

import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper;
import com.fasterxml.jackson.dataformat.protobuf.ProtobufTestBase;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufMessage;
Expand Down Expand Up @@ -73,22 +74,20 @@ public static class Employee {
/**********************************************************
*/

public void testWithNestedClass() throws Exception {
ObjectMapper mapper = new ObjectMapper(new ProtobufFactory());
ProtobufSchemaGenerator gen = new ProtobufSchemaGenerator();
mapper.acceptJsonFormatVisitor(WithNestedClass.class, gen);
ProtobufSchema schemaWrapper = gen.getGeneratedSchema();
private final ProtobufMapper MAPPER = new ProtobufMapper();

public void testWithNestedClass() throws Exception
{
ProtobufSchema schemaWrapper = MAPPER.generateSchemaFor(WithNestedClass.class);

assertNotNull(schemaWrapper);

// System.out.println(schemaWrapper.getSource().toString());
}

public void testWithIndexAnnotation() throws Exception {
ObjectMapper mapper = new ProtobufMapper();
ProtobufSchemaGenerator gen = new ProtobufSchemaGenerator();
mapper.acceptJsonFormatVisitor(WithIndexAnnotation.class, gen);
ProtobufSchema schemaWrapper = gen.getGeneratedSchema();
public void testWithIndexAnnotation() throws Exception
{
ProtobufSchema schemaWrapper = MAPPER.generateSchemaFor(WithIndexAnnotation.class);

assertNotNull(schemaWrapper);

Expand All @@ -102,10 +101,10 @@ public void testWithIndexAnnotation() throws Exception {
}

public void testSelfRefPojoGenProtobufSchema() throws Exception {
ObjectMapper mapper = new ProtobufMapper();
ProtobufMapper mapper = new ProtobufMapper();
ProtobufSchemaGenerator gen = new ProtobufSchemaGenerator();
mapper.acceptJsonFormatVisitor(Employee.class, gen);
ProtobufSchema schemaWrapper = gen.getGeneratedSchema();
ProtobufSchema schemaWrapper = mapper.generateSchemaFor(Employee.class);

assertNotNull(schemaWrapper);

Expand Down

0 comments on commit 1e66baa

Please sign in to comment.