Search in sources :

Example 11 with ProtobufField

use of com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField in project incubator-servicecomb-java-chassis by apache.

the class TestParamSerializer method testSerialize.

@Test
public void testSerialize() {
    boolean status = true;
    Assert.assertNotNull(paramSerializer);
    String[] stringArray = new String[1];
    stringArray[0] = "abc";
    ProtobufGenerator obj = null;
    try {
        obj = new ProtobufGenerator(Mockito.mock(IOContext.class), 2, Mockito.mock(ObjectCodec.class), Mockito.mock(OutputStream.class));
    } catch (IOException exce) {
    }
    Assert.assertNotNull(obj);
    new MockUp<ProtobufGenerator>() {

        @Mock
        public void writeStartObject() throws IOException {
        }

        ProtobufSchema protobufSchema = new ProtobufSchema(null, null);

        @Mock
        public ProtobufSchema getSchema() {
            return protobufSchema;
        }
    };
    ProtobufMessage protobufMessage = new ProtobufMessage(null, null);
    new MockUp<ProtobufSchema>() {

        @Mock
        public ProtobufMessage getRootType() {
            return protobufMessage;
        }
    };
    List<ProtobufField> listProtobufField = new ArrayList<>();
    listProtobufField.add(Mockito.mock(ProtobufField.class));
    new MockUp<ProtobufMessage>() {

        @Mock
        public Iterable<ProtobufField> fields() {
            return listProtobufField;
        }
    };
    new MockUp<JsonGenerator>() {

        @Mock
        public void writeObjectField(String fieldName, Object pojo) throws IOException {
        }
    };
    new MockUp<ProtobufGenerator>() {

        @Mock
        public void writeEndObject() throws IOException {
        }
    };
    try {
        paramSerializer.serialize(stringArray, obj, Mockito.mock(SerializerProvider.class));
    } catch (IOException e) {
        status = false;
    }
    Assert.assertTrue(status);
}
Also used : ArrayList(java.util.ArrayList) ProtobufSchema(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema) MockUp(mockit.MockUp) IOException(java.io.IOException) ProtobufMessage(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufMessage) ProtobufGenerator(com.fasterxml.jackson.dataformat.protobuf.ProtobufGenerator) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) ProtobufField(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField) Test(org.junit.Test)

Example 12 with ProtobufField

use of com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField in project incubator-servicecomb-java-chassis by apache.

the class AbstractFieldCodec method initFieldMap.

private void initFieldMap(ProtobufSchema schema, Type[] types) {
    Iterator<ProtobufField> fieldIter = schema.getRootType().fields().iterator();
    for (int idx = 0; idx < schema.getRootType().getFieldCount(); idx++) {
        JavaType type = TypeFactory.defaultInstance().constructType(types[idx]);
        ProtobufField field = fieldIter.next();
        ReaderHelpData helpData = new ReaderHelpData();
        helpData.index = idx;
        helpData.deser = ((CseObjectReader) reader).findDeserializer(type);
        readerHelpDataMap.put(field.name, helpData);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ProtobufField(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField)

Example 13 with ProtobufField

use of com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField in project incubator-servicecomb-java-chassis by apache.

the class TestAbstractFieldCodec method testInit.

@Test
public void testInit() {
    ProtobufField[] protobufFieldArray = new ProtobufField[5];
    FieldElement rawType = null;
    FieldType type = FieldType.STRING;
    ProtobufField p = new ProtobufField(rawType, type);
    protobufFieldArray[0] = p;
    Type[] types = new Type[1];
    types[0] = Integer.TYPE;
    Mockito.when(schema.getRootType()).thenReturn(Mockito.mock(ProtobufMessage.class));
    Mockito.when(schema.getRootType().getFieldCount()).thenReturn(1);
    Mockito.when(schema.getRootType().fields()).thenReturn(Arrays.asList(protobufFieldArray));
    abstractFieldCodec.init(schema, types);
    Assert.assertNotNull(abstractFieldCodec.readerHelpDataMap.get("UNKNOWN"));
}
Also used : ProtobufMessage(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufMessage) FieldType(com.fasterxml.jackson.dataformat.protobuf.schema.FieldType) Type(java.lang.reflect.Type) FieldElement(com.fasterxml.jackson.dataformat.protobuf.protoparser.protoparser.FieldElement) ProtobufField(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField) FieldType(com.fasterxml.jackson.dataformat.protobuf.schema.FieldType) Test(org.junit.Test)

Example 14 with ProtobufField

use of com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField in project java-chassis by ServiceComb.

the class ParamSerializer method serialize.

@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
    gen.writeStartObject();
    ProtobufGenerator protobufGenerator = (ProtobufGenerator) gen;
    Iterator<ProtobufField> iter = protobufGenerator.getSchema().getRootType().fields().iterator();
    Object[] values = (Object[]) value;
    for (int idx = 0; idx < values.length; idx++) {
        gen.writeObjectField(iter.next().name, values[idx]);
    }
    gen.writeEndObject();
}
Also used : ProtobufGenerator(com.fasterxml.jackson.dataformat.protobuf.ProtobufGenerator) ProtobufField(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField)

Example 15 with ProtobufField

use of com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField in project incubator-servicecomb-java-chassis by apache.

the class TestStandardParamCodec method testInit.

@Test
public void testInit() {
    Assert.assertNotNull(standardParamCodec);
    ProtobufField[] protobufFieldArray = new ProtobufField[5];
    FieldElement rawType = null;
    FieldType type = FieldType.STRING;
    ProtobufField p = new ProtobufField(rawType, type);
    protobufFieldArray[0] = p;
    Type[] types = new Type[1];
    types[0] = Integer.TYPE;
    Mockito.when(schema.getSchemaType()).thenReturn(FORMAT_NAME_PROTOBUF);
    Mockito.when(schema.getRootType()).thenReturn(Mockito.mock(ProtobufMessage.class));
    Mockito.when(schema.getRootType().getFieldCount()).thenReturn(1);
    Mockito.when(schema.getRootType().fields()).thenReturn(Arrays.asList(protobufFieldArray));
    Assert.assertNull(standardParamCodec.writer);
    Assert.assertNull(standardParamCodec.reader);
    standardParamCodec.init(schema, types);
    Assert.assertNotNull(standardParamCodec.writer);
    Assert.assertNotNull(standardParamCodec.reader);
}
Also used : ProtobufMessage(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufMessage) FieldType(com.fasterxml.jackson.dataformat.protobuf.schema.FieldType) Type(java.lang.reflect.Type) FieldElement(com.fasterxml.jackson.dataformat.protobuf.protoparser.protoparser.FieldElement) ProtobufField(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField) FieldType(com.fasterxml.jackson.dataformat.protobuf.schema.FieldType) Test(org.junit.Test)

Aggregations

ProtobufField (com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField)18 ProtobufMessage (com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufMessage)12 Test (org.junit.Test)12 FieldElement (com.fasterxml.jackson.dataformat.protobuf.protoparser.protoparser.FieldElement)10 FieldType (com.fasterxml.jackson.dataformat.protobuf.schema.FieldType)10 Type (java.lang.reflect.Type)10 ProtobufGenerator (com.fasterxml.jackson.dataformat.protobuf.ProtobufGenerator)6 JavaType (com.fasterxml.jackson.databind.JavaType)2 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)2 ProtobufSchema (com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 MockUp (mockit.MockUp)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1