Search in sources :

Example 1 with TestAllTypes

use of com.google.protobuf.util.JsonTestProto.TestAllTypes in project curiostack by curioswitch.

the class MessageMarshallerTest method parserAcceptsStringForNumericField.

@Test
public void parserAcceptsStringForNumericField() throws Exception {
    TestAllTypes.Builder builder = TestAllTypes.newBuilder();
    mergeFromJson("{\n" + "  \"optionalInt32\": \"1234\",\n" + "  \"optionalUint32\": \"5678\",\n" + "  \"optionalSint32\": \"9012\",\n" + "  \"optionalFixed32\": \"3456\",\n" + "  \"optionalSfixed32\": \"7890\",\n" + "  \"optionalFloat\": \"1.5\",\n" + "  \"optionalDouble\": \"1.25\",\n" + "  \"optionalBool\": \"true\"\n" + "}", builder);
    TestAllTypes message = builder.build();
    assertEquals(1234, message.getOptionalInt32());
    assertEquals(5678, message.getOptionalUint32());
    assertEquals(9012, message.getOptionalSint32());
    assertEquals(3456, message.getOptionalFixed32());
    assertEquals(7890, message.getOptionalSfixed32());
    assertEquals(1.5f, message.getOptionalFloat(), 0.000001);
    assertEquals(1.25, message.getOptionalDouble(), 0.000001);
    assertEquals(true, message.getOptionalBool());
}
Also used : TestAllTypes(com.google.protobuf.util.JsonTestProto.TestAllTypes) Test(org.junit.Test)

Example 2 with TestAllTypes

use of com.google.protobuf.util.JsonTestProto.TestAllTypes in project curiostack by curioswitch.

the class MessageMarshallerTest method defaultDoesNotHtmlExcape.

@Test
public void defaultDoesNotHtmlExcape() throws Exception {
    TestAllTypes message = TestAllTypes.newBuilder().setOptionalString("=").build();
    assertMatchesUpstream(message);
}
Also used : TestAllTypes(com.google.protobuf.util.JsonTestProto.TestAllTypes) Test(org.junit.Test)

Example 3 with TestAllTypes

use of com.google.protobuf.util.JsonTestProto.TestAllTypes in project curiostack by curioswitch.

the class MessageMarshallerTest method omittingInsignificantWhitespace.

@Test
public void omittingInsignificantWhitespace() throws Exception {
    TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(12345).build();
    assertMatchesUpstream(message, false, false, true);
    TestAllTypes message1 = TestAllTypes.getDefaultInstance();
    assertMatchesUpstream(message1, false, false, true);
    TestAllTypes message2 = JsonTestUtil.testAllTypesAllFields();
    assertMatchesUpstream(message2, false, false, true);
}
Also used : TestAllTypes(com.google.protobuf.util.JsonTestProto.TestAllTypes) Test(org.junit.Test)

Example 4 with TestAllTypes

use of com.google.protobuf.util.JsonTestProto.TestAllTypes in project curiostack by curioswitch.

the class MessageMarshallerTest method parserAcceptsNull.

@Test
public void parserAcceptsNull() throws Exception {
    TestAllTypes.Builder builder = TestAllTypes.newBuilder();
    mergeFromJson("{\n" + "  \"optionalInt32\": null,\n" + "  \"optionalInt64\": null,\n" + "  \"optionalUint32\": null,\n" + "  \"optionalUint64\": null,\n" + "  \"optionalSint32\": null,\n" + "  \"optionalSint64\": null,\n" + "  \"optionalFixed32\": null,\n" + "  \"optionalFixed64\": null,\n" + "  \"optionalSfixed32\": null,\n" + "  \"optionalSfixed64\": null,\n" + "  \"optionalFloat\": null,\n" + "  \"optionalDouble\": null,\n" + "  \"optionalBool\": null,\n" + "  \"optionalString\": null,\n" + "  \"optionalBytes\": null,\n" + "  \"optionalNestedMessage\": null,\n" + "  \"optionalNestedEnum\": null,\n" + "  \"repeatedInt32\": null,\n" + "  \"repeatedInt64\": null,\n" + "  \"repeatedUint32\": null,\n" + "  \"repeatedUint64\": null,\n" + "  \"repeatedSint32\": null,\n" + "  \"repeatedSint64\": null,\n" + "  \"repeatedFixed32\": null,\n" + "  \"repeatedFixed64\": null,\n" + "  \"repeatedSfixed32\": null,\n" + "  \"repeatedSfixed64\": null,\n" + "  \"repeatedFloat\": null,\n" + "  \"repeatedDouble\": null,\n" + "  \"repeatedBool\": null,\n" + "  \"repeatedString\": null,\n" + "  \"repeatedBytes\": null,\n" + "  \"repeatedNestedMessage\": null,\n" + "  \"repeatedNestedEnum\": null\n" + "}", builder);
    TestAllTypes message = builder.build();
    assertEquals(TestAllTypes.getDefaultInstance(), message);
    // Repeated field elements cannot be null.
    TestAllTypes.Builder builder2 = TestAllTypes.newBuilder();
    assertThatThrownBy(() -> mergeFromJson("{\n" + "  \"repeatedInt32\": [null, null],\n" + "}", builder2)).isInstanceOf(IOException.class);
    TestAllTypes.Builder builder3 = TestAllTypes.newBuilder();
    assertThatThrownBy(() -> mergeFromJson("{\n" + "  \"repeatedNestedMessage\": [null, null],\n" + "}", builder3)).isInstanceOf(IOException.class);
}
Also used : TestAllTypes(com.google.protobuf.util.JsonTestProto.TestAllTypes) Test(org.junit.Test)

Example 5 with TestAllTypes

use of com.google.protobuf.util.JsonTestProto.TestAllTypes in project curiostack by curioswitch.

the class MessageMarshallerTest method unknownEnumValue.

@Test
public void unknownEnumValue() throws Exception {
    TestAllTypes message = TestAllTypes.newBuilder().setOptionalNestedEnumValue(12345).addRepeatedNestedEnumValue(12345).addRepeatedNestedEnumValue(0).build();
    assertMatchesUpstream(message);
// FIXME: https://github.com/google/protobuf/issues/2936
// We use the singular put methods in generated code to avoid unnecessary creation of
// intermediate maps. However, the singular put methods strangely do not allow unknown enum
// values. As unknown enum values are quite rare in practice, and only maps are affected, we'll
// skip support for now and revisit if this is an issue for people and upstream does not change
// their API.
/*
    TestMap.Builder mapBuilder = TestMap.newBuilder();
    mapBuilder.putInt32ToEnumMapValue(1, 0);
    Map<Integer, Integer> mapWithInvalidValues = new HashMap<Integer, Integer>();
    mapWithInvalidValues.put(2, 12345);
    mapBuilder.putAllInt32ToEnumMapValue(mapWithInvalidValues);
    TestMap mapMessage = mapBuilder.buildAndAdd();
    assertMatchesUpstream(mapMessage);
    */
}
Also used : TestAllTypes(com.google.protobuf.util.JsonTestProto.TestAllTypes) Test(org.junit.Test)

Aggregations

TestAllTypes (com.google.protobuf.util.JsonTestProto.TestAllTypes)9 Test (org.junit.Test)9 Any (com.google.protobuf.Any)1 BoolValue (com.google.protobuf.BoolValue)1 BytesValue (com.google.protobuf.BytesValue)1 DoubleValue (com.google.protobuf.DoubleValue)1 FloatValue (com.google.protobuf.FloatValue)1 Int32Value (com.google.protobuf.Int32Value)1 Int64Value (com.google.protobuf.Int64Value)1 ListValue (com.google.protobuf.ListValue)1 NullValue (com.google.protobuf.NullValue)1 StringValue (com.google.protobuf.StringValue)1 Struct (com.google.protobuf.Struct)1 UInt32Value (com.google.protobuf.UInt32Value)1 UInt64Value (com.google.protobuf.UInt64Value)1 Value (com.google.protobuf.Value)1 TestAny (com.google.protobuf.util.JsonTestProto.TestAny)1 TestCustomJsonName (com.google.protobuf.util.JsonTestProto.TestCustomJsonName)1 TestMap (com.google.protobuf.util.JsonTestProto.TestMap)1 TestOneof (com.google.protobuf.util.JsonTestProto.TestOneof)1