EscSpiUtils { public static EscMeta createEscMeta(@NotNull final SerializerRegistry registry, @NotNull final EnhancedMimeType targetContentType, @Nullable final CommonEvent commonEvent) { Contract.requireArgNotNull("registry", registry); Contract.requireArgNotNull("targetContentType", targetContentType); if (commonEvent == null) { return null; } final String dataType = commonEvent.getDataType().asBaseType(); final Serializer dataSerializer = registry.getSerializer(new SerializedDataType(dataType)); final EnhancedMimeType dataContentType = contentType(dataSerializer.getMimeType(), targetContentType); if (commonEvent.getMeta() == null) { return new EscMeta(dataType, dataContentType); } final String metaType = commonEvent.getMetaType().asBaseType(); final SerializedDataType serDataType = new SerializedDataType(metaType); final Serializer metaSerializer = registry.getSerializer(serDataType); if (metaSerializer.getMimeType().matchEncoding(targetContentType)) { return new EscMeta(dataType, dataContentType, metaType, metaSerializer.getMimeType(), commonEvent.getMeta()); } final byte[] serMeta = metaSerializer.marshal(commonEvent.getMeta(), serDataType); final EnhancedMimeType metaContentType = contentType(metaSerializer.getMimeType(), targetContentType); return new EscMeta(dataType, dataContentType, metaType, metaContentType, new Base64Data(serMeta)); } private  EscSpiUtils(); @Nullable static SerializedData serialize(@NotNull final SerializerRegistry registry, @NotNull final SerializedDataType type,
            @Nullable final Object data); @NotNull static T deserialize(@NotNull final DeserializerRegistry registry, @NotNull final SerializedData data); static EnhancedMimeType mimeType(@NotNull final SerializerRegistry registry, @NotNull final List<CommonEvent> commonEvents); static List<T> asList(final T[] array); static boolean eventsEqual(@Nullable final List<CommonEvent> eventsA, @Nullable final List<CommonEvent> eventsB); static EscMeta createEscMeta(@NotNull final SerializerRegistry registry, @NotNull final EnhancedMimeType targetContentType,
            @Nullable final CommonEvent commonEvent); static String nodeToString(final Node node); static JsonbSerializer<?>[] createEscJsonbSerializers(); static JsonbSerializer<?>[] joinJsonbSerializers(final JsonbSerializer<?>[] serializersA,
            final JsonbSerializer<?>... serializersB); static JsonbSerializer<?>[] joinJsonbSerializerArrays(final JsonbSerializer<?>[]... serializerArrays); static JsonbDeserializer<?>[] createEscJsonbDeserializers(); static JsonbDeserializer<?>[] joinJsonbDeserializers(final JsonbDeserializer<?>[] deserializersA,
            final JsonbDeserializer<?>... deserializersB); static JsonbDeserializer<?>[] joinJsonbDeserializerArrays(final JsonbDeserializer<?>[]... deserializerArrays);  }
@Test public void testCreateEscMetaNull() { final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); final EscMeta result = EscSpiUtils.createEscMeta(registry, EnhancedMimeType.create("application/xml"), null); assertThat(result).isNull(); }
@Test public void testCreateEscMetaTargetXmlDataXmlMetaNull() { final String description = "Whatever"; final UUID uuid = UUID.randomUUID(); final MyEvent myEvent = new MyEvent(uuid, description); final EventId eventId = new EventId(); final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); registry.addSerializer(MyEvent.SER_TYPE, new XmlDeSerializer(MyEvent.class)); final CommonEvent commonEvent = new SimpleCommonEvent(eventId, MyEvent.TYPE, myEvent); final EscMeta result = EscSpiUtils.createEscMeta(registry, EnhancedMimeType.create("application/xml; encoding=UTF-8"), commonEvent); assertThat(result).isNotNull(); assertThat(result.getDataType()).isEqualTo(MyEvent.TYPE.asBaseType()); assertThat(result.getDataContentType()).isEqualTo( EnhancedMimeType.create("application/xml; encoding=UTF-8")); assertThat(result.getMetaType()).isNull(); assertThat(result.getMetaContentType()).isNull(); assertThat(result.getMeta()).isNull(); }
@Test public void testCreateEscMetaTargetJsonDataXmlMetaNull() { final String description = "Whatever"; final UUID uuid = UUID.randomUUID(); final MyEvent myEvent = new MyEvent(uuid, description); final EventId eventId = new EventId(); final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); registry.addSerializer(MyEvent.SER_TYPE, new XmlDeSerializer(MyEvent.class)); final CommonEvent commonEvent = new SimpleCommonEvent(eventId, MyEvent.TYPE, myEvent); final EscMeta result = EscSpiUtils.createEscMeta(registry, EnhancedMimeType.create("application/json"), commonEvent); assertThat(result).isNotNull(); assertThat(result.getDataType()).isEqualTo(MyEvent.TYPE.asBaseType()); assertThat(result.getDataContentType()).isEqualTo( EnhancedMimeType.create("application/xml; transfer-encoding=base64; encoding=UTF-8")); assertThat(result.getMetaType()).isNull(); assertThat(result.getMetaContentType()).isNull(); assertThat(result.getMeta()).isNull(); }
@Test public void testCreateEscMetaTargetXmlDataXmlMetaXml() { final MyEvent myEvent = new MyEvent(UUID.randomUUID(), "Whatever"); final EventId eventId = new EventId(); final MyMeta myMeta = new MyMeta("peter"); final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); registry.addSerializer(MyEvent.SER_TYPE, new XmlDeSerializer(MyEvent.class)); registry.addSerializer(MyMeta.SER_TYPE, new XmlDeSerializer(MyMeta.class)); final CommonEvent commonEvent = new SimpleCommonEvent(eventId, MyEvent.TYPE, myEvent, MyMeta.TYPE, myMeta); final EscMeta result = EscSpiUtils.createEscMeta(registry, EnhancedMimeType.create("application/xml; encoding=UTF-8"), commonEvent); assertThat(result).isNotNull(); assertThat(result.getDataType()).isEqualTo(MyEvent.TYPE.asBaseType()); assertThat(result.getDataContentType()).isEqualTo( EnhancedMimeType.create("application/xml; encoding=UTF-8")); assertThat(result.getMetaType()).isEqualTo(MyMeta.TYPE.asBaseType()); assertThat(result.getMetaContentType()).isEqualTo( EnhancedMimeType.create("application/xml; encoding=UTF-8")); assertThat(result.getMeta()).isEqualTo(myMeta); }
@Test public void testCreateEscMetaTargetJsonDataXmlMetaXml() { final String description = "Whatever"; final UUID uuid = UUID.randomUUID(); final MyEvent myEvent = new MyEvent(uuid, description); final EventId eventId = new EventId(); final MyMeta myMeta = new MyMeta("peter"); final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); registry.addSerializer(MyEvent.SER_TYPE, new XmlDeSerializer(MyEvent.class)); registry.addSerializer(MyMeta.SER_TYPE, new XmlDeSerializer(MyMeta.class)); final CommonEvent commonEvent = new SimpleCommonEvent(eventId, MyEvent.TYPE, myEvent, MyMeta.TYPE, myMeta); final EscMeta result = EscSpiUtils.createEscMeta(registry, EnhancedMimeType.create("application/json"), commonEvent); assertThat(result).isNotNull(); assertThat(result.getDataType()).isEqualTo(MyEvent.TYPE.asBaseType()); assertThat(result.getDataContentType()).isEqualTo( EnhancedMimeType.create("application/xml; transfer-encoding=base64; encoding=UTF-8")); assertThat(result.getMetaType()).isEqualTo(MyMeta.TYPE.asBaseType()); assertThat(result.getMetaContentType()).isEqualTo( EnhancedMimeType.create("application/xml; transfer-encoding=base64; encoding=UTF-8")); assertThat(result.getMeta()).isInstanceOf(Base64Data.class); final Base64Data base64Meta = (Base64Data) result.getMeta(); assertThat(new String(base64Meta.getDecoded(), Charset.forName("utf-8"))).isEqualTo( "<MyMeta><user>peter</user></MyMeta>"); }