Search in sources :

Example 1 with ProtobufGenerator

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

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<ProtobufField>();
    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 (JsonProcessingException e) {
        status = false;
    } 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) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ProtobufField(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField) Test(org.junit.Test)

Example 2 with ProtobufGenerator

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

the class ResultSerializer method serialize.

@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
    gen.writeStartObject();
    ProtobufGenerator protobufGenerator = (ProtobufGenerator) gen;
    ProtobufField field = protobufGenerator.getSchema().getRootType().firstField();
    gen.writeObjectField(field.name, value);
    gen.writeEndObject();
}
Also used : ProtobufGenerator(com.fasterxml.jackson.dataformat.protobuf.ProtobufGenerator) ProtobufField(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField)

Example 3 with ProtobufGenerator

use of com.fasterxml.jackson.dataformat.protobuf.ProtobufGenerator 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 4 with ProtobufGenerator

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

the class TestResultSerializer method testSerialize.

@Test
public void testSerialize() {
    boolean status = true;
    Assert.assertNotNull(resultSerializer);
    String[] stringArray = new String[1];
    stringArray[0] = "abc";
    ProtobufGenerator obj = null;
    try {
        obj = new ProtobufGenerator(Mockito.mock(IOContext.class), 2, Mockito.mock(ObjectCodec.class), outputStream);
    } 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;
        }
    };
    new MockUp<ProtobufMessage>() {

        @Mock
        public ProtobufField firstField() {
            return Mockito.mock(ProtobufField.class);
        }
    };
    new MockUp<JsonGenerator>() {

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

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

Aggregations

ProtobufGenerator (com.fasterxml.jackson.dataformat.protobuf.ProtobufGenerator)4 ProtobufField (com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)2 ProtobufMessage (com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufMessage)2 ProtobufSchema (com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema)2 IOException (java.io.IOException)2 MockUp (mockit.MockUp)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1