CommonEvent2EventDataConverter implements Converter<CommonEvent, EventData> { @Override public final EventData convert(final CommonEvent commonEvent) { final String dataType = commonEvent.getDataType().asBaseType(); final SerializedDataType serUserDataType = new SerializedDataType(dataType); final Serializer userDataSerializer = serRegistry.getSerializer(serUserDataType); final byte[] serUserData = userDataSerializer.marshal(commonEvent.getData(), serUserDataType); final byte[] serData; if (userDataSerializer.getMimeType().matchEncoding(targetContentType)) { serData = serUserData; } else { final Base64Data base64data = new Base64Data(serUserData); final Serializer base64Serializer = serRegistry.getSerializer(Base64Data.SER_TYPE); serData = base64Serializer.marshal(base64data, Base64Data.SER_TYPE); } final EscMeta escMeta = EscSpiUtils.createEscMeta(serRegistry, targetContentType, commonEvent); final SerializedDataType escMetaType = new SerializedDataType(EscMeta.TYPE.asBaseType()); final Serializer escMetaSerializer = getSerializer(escMetaType); final byte[] escSerMeta = escMetaSerializer.marshal(escMeta, escMetaType); final EventData.Builder builder = EventData.newBuilder().eventId(commonEvent.getId().asBaseType()) .type(dataType); if (targetContentType.isJson()) { builder.jsonData(serData); builder.jsonMetadata(escSerMeta); } else { builder.data(serData); builder.metadata(escSerMeta); } return builder.build(); } CommonEvent2EventDataConverter(@NotNull final SerializerRegistry serRegistry,
            final EnhancedMimeType targetContentType); @Override final EventData convert(final CommonEvent commonEvent); @Override final Class<CommonEvent> getSourceType(); @Override final Class<EventData> getTargetType();  }
@Test public final void testConvert() { final EnhancedMimeType targetContentType = EnhancedMimeType.create("application", "json", Charset.forName("utf-8")); final SimpleSerializerDeserializerRegistry serRegistry = new SimpleSerializerDeserializerRegistry(); final JsonDeSerializer deserializer = new JsonDeSerializer(); serRegistry.addSerializer(new SerializedDataType("MyData"), deserializer); serRegistry.addSerializer(new SerializedDataType("MyMeta"), deserializer); serRegistry.addSerializer(new SerializedDataType(EscMeta.TYPE.asBaseType()), deserializer); final EventId id = new EventId(); final TypeName dataType = new TypeName("MyData"); final Object data = Json.createObjectBuilder().add("id", 1).add("name", "Peter").build(); final TypeName metaType = new TypeName("MyMeta"); final Object meta = Json.createObjectBuilder().add("ip", "127.0.0.1").build(); final CommonEvent commonEvent = new SimpleCommonEvent(id, dataType, data, metaType, meta); final CommonEvent2EventDataConverter testee = new CommonEvent2EventDataConverter(serRegistry, targetContentType); final EventData eventData = testee.convert(commonEvent); assertThat(eventData.eventId).isEqualTo(commonEvent.getId().asBaseType()); assertThat(eventData.type).isEqualTo(commonEvent.getDataType().asBaseType()); assertThat(eventData.isJsonData).isTrue(); assertThat(new String(eventData.data, deserializer.getMimeType().getEncoding())) .isEqualTo("{\"id\":1,\"name\":\"Peter\"}"); assertThat(eventData.isJsonMetadata).isTrue(); assertThat(new String(eventData.metadata, deserializer.getMimeType().getEncoding())).isEqualTo( "{\"data-type\":\"MyData\"," + "\"data-content-type\":\"application/json; encoding=UTF-8\"," + "\"meta-type\":\"MyMeta\",\"meta-content-type\":\"application/json; encoding=UTF-8\"," + "\"MyMeta\":{\"ip\":\"127.0.0.1\"}}"); }