Search in sources :

Example 1 with ModelProtostuff

use of io.protostuff.runtime.model.ModelProtostuff in project incubator-servicecomb-java-chassis by apache.

the class TestProtobufCompatibleUtils method testProtostuff.

protected byte[] testProtostuff(Map<String, String> map, Map<String, User> userMap) throws IOException {
    ProtobufCompatibleUtils.init();
    RuntimeSchema<ModelProtostuff> schema = RuntimeSchema.createFrom(ModelProtostuff.class);
    ModelProtostuff model = new ModelProtostuff();
    model.setContext(map);
    model.setUserMap(userMap);
    model.getList().add("l1");
    model.getList().add("l2");
    LinkedBuffer linkedBuffer = LinkedBuffer.allocate();
    ProtobufOutput output = new ProtobufOutput(linkedBuffer);
    ProtobufFeature protobufFeature = new ProtobufFeature();
    protobufFeature.setUseProtobufMapCodec(true);
    ProtobufFeatureUtils.setProtobufFeature(protobufFeature);
    schema.writeTo(output, model);
    ByteArrayOutputStream s = new ByteArrayOutputStream();
    LinkedBuffer.writeTo(s, linkedBuffer);
    byte[] bytes = s.toByteArray();
    ModelProtostuff newModel = new ModelProtostuff();
    ByteArrayInput bai = new ByteArrayInput(bytes, false);
    schema.mergeFrom(bai, newModel);
    ProtobufFeatureUtils.removeProtobufFeature();
    Assert.assertEquals("v1", newModel.getContext().get("k1"));
    Assert.assertEquals("v2", newModel.getContext().get("k2"));
    Assert.assertEquals("n1", newModel.getUserMap().get("u1").getName());
    Assert.assertEquals("n2", newModel.getUserMap().get("u2").getName());
    Assert.assertEquals("l1", newModel.getList().get(0));
    Assert.assertEquals("l2", newModel.getList().get(1));
    return bytes;
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) ByteArrayInput(io.protostuff.ByteArrayInput) ModelProtostuff(io.protostuff.runtime.model.ModelProtostuff) ProtobufOutput(io.protostuff.ProtobufOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ByteArrayInput (io.protostuff.ByteArrayInput)1 LinkedBuffer (io.protostuff.LinkedBuffer)1 ProtobufOutput (io.protostuff.ProtobufOutput)1 ModelProtostuff (io.protostuff.runtime.model.ModelProtostuff)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1