Search in sources :

Example 6 with TestAllTypes

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

the class MessageMarshallerTest method includingDefaultValueFields.

@Test
public void includingDefaultValueFields() throws Exception {
    TestAllTypes message = TestAllTypes.getDefaultInstance();
    assertMatchesUpstream(message);
    assertMatchesUpstream(message, true, false, false);
    TestMap mapMessage = TestMap.getDefaultInstance();
    assertMatchesUpstream(mapMessage);
    assertMatchesUpstream(mapMessage, true, false, false);
    TestOneof oneofMessage = TestOneof.getDefaultInstance();
    assertMatchesUpstream(oneofMessage);
    assertMatchesUpstream(oneofMessage, true, false, false);
    oneofMessage = TestOneof.newBuilder().setOneofInt32(42).build();
    assertMatchesUpstream(oneofMessage);
    assertMatchesUpstream(oneofMessage, true, false, false);
    oneofMessage = TestOneof.newBuilder().setOneofNullValue(NullValue.NULL_VALUE).build();
    assertMatchesUpstream(oneofMessage);
    assertMatchesUpstream(oneofMessage, true, false, false);
}
Also used : TestMap(com.google.protobuf.util.JsonTestProto.TestMap) TestAllTypes(com.google.protobuf.util.JsonTestProto.TestAllTypes) TestOneof(com.google.protobuf.util.JsonTestProto.TestOneof) Test(org.junit.Test)

Example 7 with TestAllTypes

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

the class MessageMarshallerTest method specialFloatValues.

@Test
public void specialFloatValues() throws Exception {
    TestAllTypes message = TestAllTypes.newBuilder().addRepeatedFloat(Float.NaN).addRepeatedFloat(Float.POSITIVE_INFINITY).addRepeatedFloat(Float.NEGATIVE_INFINITY).addRepeatedDouble(Double.NaN).addRepeatedDouble(Double.POSITIVE_INFINITY).addRepeatedDouble(Double.NEGATIVE_INFINITY).build();
    assertMatchesUpstream(message);
}
Also used : TestAllTypes(com.google.protobuf.util.JsonTestProto.TestAllTypes) Test(org.junit.Test)

Example 8 with TestAllTypes

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

the class MessageMarshallerTest method preservingProtoFieldNames.

@Test
public void preservingProtoFieldNames() throws Exception {
    TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(12345).build();
    assertMatchesUpstream(message);
    assertMatchesUpstream(message, false, true, false);
    // The json_name field option is ignored when configured to use original proto field names.
    TestCustomJsonName messageWithCustomJsonName = TestCustomJsonName.newBuilder().setValue(12345).build();
    assertMatchesUpstream(message, false, true, false);
    // Parsers accept both original proto field names and lowerCamelCase names.
    TestAllTypes.Builder builder = TestAllTypes.newBuilder();
    mergeFromJson("{\"optionalInt32\": 12345}", builder);
    assertEquals(12345, builder.getOptionalInt32());
    builder.clear();
    mergeFromJson("{\"optional_int32\": 54321}", builder);
    assertEquals(54321, builder.getOptionalInt32());
}
Also used : TestCustomJsonName(com.google.protobuf.util.JsonTestProto.TestCustomJsonName) TestAllTypes(com.google.protobuf.util.JsonTestProto.TestAllTypes) Test(org.junit.Test)

Example 9 with TestAllTypes

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

the class MessageMarshallerTest method anyFields.

@Test
public void anyFields() throws Exception {
    TestAllTypes content = TestAllTypes.newBuilder().setOptionalInt32(1234).build();
    TestAny message = TestAny.newBuilder().setAnyValue(Any.pack(content)).build();
    assertMatchesUpstream(message, TestAllTypes.getDefaultInstance());
    TestAny messageWithDefaultAnyValue = TestAny.newBuilder().setAnyValue(Any.getDefaultInstance()).build();
    assertMatchesUpstream(messageWithDefaultAnyValue);
    // Well-known types have a special formatting when embedded in Any.
    // 
    // 1. Any in Any.
    Any anyMessage = Any.pack(Any.pack(content));
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 2. Wrappers in Any.
    anyMessage = Any.pack(Int32Value.newBuilder().setValue(12345).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(UInt32Value.newBuilder().setValue(12345).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(Int64Value.newBuilder().setValue(12345).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(UInt64Value.newBuilder().setValue(12345).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(FloatValue.newBuilder().setValue(12345).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(DoubleValue.newBuilder().setValue(12345).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(BoolValue.newBuilder().setValue(true).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(StringValue.newBuilder().setValue("Hello").build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(BytesValue.newBuilder().setValue(ByteString.copyFrom(new byte[] { 1, 2 })).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 3. Timestamp in Any.
    anyMessage = Any.pack(Timestamps.parse("1969-12-31T23:59:59Z"));
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 4. Duration in Any
    anyMessage = Any.pack(Durations.parse("12345.10s"));
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 5. FieldMask in Any
    anyMessage = Any.pack(FieldMaskUtil.fromString("foo.bar,baz"));
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 6. Struct in Any
    Struct.Builder structBuilder = Struct.newBuilder();
    structBuilder.putFields("number", Value.newBuilder().setNumberValue(1.125).build());
    anyMessage = Any.pack(structBuilder.build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 7. Value (number type) in Any
    Value.Builder valueBuilder = Value.newBuilder();
    valueBuilder.setNumberValue(1);
    anyMessage = Any.pack(valueBuilder.build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 8. Value (null type) in Any
    anyMessage = Any.pack(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
}
Also used : TestAny(com.google.protobuf.util.JsonTestProto.TestAny) Int64Value(com.google.protobuf.Int64Value) BoolValue(com.google.protobuf.BoolValue) UInt64Value(com.google.protobuf.UInt64Value) StringValue(com.google.protobuf.StringValue) NullValue(com.google.protobuf.NullValue) Value(com.google.protobuf.Value) DoubleValue(com.google.protobuf.DoubleValue) Int32Value(com.google.protobuf.Int32Value) FloatValue(com.google.protobuf.FloatValue) UInt32Value(com.google.protobuf.UInt32Value) BytesValue(com.google.protobuf.BytesValue) ListValue(com.google.protobuf.ListValue) TestAllTypes(com.google.protobuf.util.JsonTestProto.TestAllTypes) TestAny(com.google.protobuf.util.JsonTestProto.TestAny) Any(com.google.protobuf.Any) TestStruct(com.google.protobuf.util.JsonTestProto.TestStruct) Struct(com.google.protobuf.Struct) 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