RecordedEvent2CommonEventConverter implements Converter<RecordedEvent, CommonEvent> { @Override public final CommonEvent convert(final RecordedEvent eventData) { final EnhancedMimeType escMetaMimeType = metaMimeType(eventData.isJson); final SerializedDataType escSerMetaType = new SerializedDataType(EscMeta.TYPE.asBaseType()); final Deserializer escMetaDeserializer = deserRegistry.getDeserializer(escSerMetaType, escMetaMimeType); final EscMeta escMeta = escMetaDeserializer.unmarshal(eventData.metadata, escSerMetaType, escMetaMimeType); final EnhancedMimeType metaMimeType = escMeta.getMetaContentType(); final String metaTransferEncoding; if (escMeta.getMetaType() == null) { metaTransferEncoding = null; } else { metaTransferEncoding = escMeta.getMetaContentType().getParameter("transfer-encoding"); } final EnhancedMimeType dataMimeType = escMeta.getDataContentType(); final SerializedDataType serDataType = new SerializedDataType(escMeta.getDataType()); final Deserializer dataDeserializer = deserRegistry.getDeserializer(serDataType, dataMimeType); final String dataTransferEncoding = escMeta.getDataContentType().getParameter("transfer-encoding"); final Object data = unmarshal(dataTransferEncoding, serDataType, dataDeserializer, dataMimeType, eventData.data, metaMimeType, escMetaMimeType); final EventId eventId = new EventId(eventData.eventId); final TypeName dataType = new TypeName(eventData.eventType); if (escMeta.getMetaType() == null) { return new SimpleCommonEvent(eventId, dataType, data); } final TypeName metaType = new TypeName(escMeta.getMetaType()); final SerializedDataType serMetaType = new SerializedDataType(escMeta.getMetaType()); final Deserializer metaDeserializer = deserRegistry.getDeserializer(serMetaType, metaMimeType); final Object meta = unmarshal(metaTransferEncoding, serMetaType, metaDeserializer, metaMimeType, escMeta.getMeta(), metaMimeType, escMetaMimeType); return new SimpleCommonEvent(eventId, dataType, data, metaType, meta); } RecordedEvent2CommonEventConverter(@NotNull final DeserializerRegistry deserRegistry); @Override final CommonEvent convert(final RecordedEvent eventData); @Override final Class<RecordedEvent> getSourceType(); @Override final Class<CommonEvent> getTargetType();  }
@Test public final void testConvertJsonJsonJson() throws IOException { final EnhancedMimeType envelope = EnhancedMimeType.create("application", "json", Charset.forName("utf-8")); final int json = 1; final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); registry.add(EscMeta.SER_TYPE, envelope.getBaseType(), new EscMetaJsonDeSerializer()); registry.add(MyEvent.SER_TYPE, "application/json", new MyEvent.MyEventJsonDeSerializer()); registry.add(MyMeta.SER_TYPE, "application/json", new MyMeta.MyMetaJsonDeSerializer()); final MyEvent myEvent = new MyEvent(UUID.randomUUID(), "Hello, JSON!"); final MyMeta myMeta = new MyMeta("michael"); final CommonEvent commonEvent = new SimpleCommonEvent(new EventId(myEvent.getId()), MyEvent.TYPE, myEvent, MyMeta.TYPE, myMeta); final CommonEvent2EventDataConverter converter = new CommonEvent2EventDataConverter(registry, envelope); final EventData eventData = converter.convert(commonEvent); final EventRecord eventRecord = EventRecord.newBuilder() .setEventId(ByteString.copyFrom(UUIDConverter.toBytes(eventData.eventId))) .setEventStreamId("mystream").setEventNumber(1).setEventType(eventData.type) .setDataContentType(json).setData(ByteString.copyFrom(eventData.data)) .setMetadataContentType(1).setMetadata(ByteString.copyFrom(eventData.metadata)) .setCreated(System.currentTimeMillis()).build(); final RecordedEvent recordedEvent = new RecordedEvent(eventRecord); final RecordedEvent2CommonEventConverter testee = new RecordedEvent2CommonEventConverter(registry); final CommonEvent result = testee.convert(recordedEvent); assertThat(result.getId()).isEqualTo(new EventId(myEvent.getId())); assertThat(result.getDataType()).isEqualTo(MyEvent.TYPE); assertThat(result.getMetaType()).isEqualTo(MyMeta.TYPE); assertThat(result.getData()).isInstanceOf(MyEvent.class); final MyEvent copyMyEvent = (MyEvent) result.getData(); assertThat(copyMyEvent.getId()).isEqualTo(myEvent.getId()); assertThat(copyMyEvent.getDescription()).isEqualTo(myEvent.getDescription()); assertThat(result.getMeta()).isInstanceOf(MyMeta.class); final MyMeta copyMyMeta = (MyMeta) result.getMeta(); assertThat(copyMyMeta.getUser()).isEqualTo(myMeta.getUser()); }
@Test public final void testConvertJsonJsonOther() throws IOException { final EnhancedMimeType envelope = EnhancedMimeType.create("application", "json", Charset.forName("utf-8")); final int json = 1; final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); registry.add(EscMeta.SER_TYPE, envelope.getBaseType(), new EscMetaJsonDeSerializer()); registry.add(Base64Data.SER_TYPE, envelope.getBaseType(), new Base64DataJsonDeSerializer()); registry.add(MyMeta.SER_TYPE, "application/json", new MyMeta.MyMetaJsonDeSerializer()); registry.add(MyEvent.SER_TYPE, "application/xml", new XmlDeSerializer(JAXB_CLASSES)); final MyEvent myEvent = new MyEvent(UUID.randomUUID(), "Hello, XML!"); final MyMeta myMeta = new MyMeta("michael"); final CommonEvent commonEvent = new SimpleCommonEvent(new EventId(myEvent.getId()), MyEvent.TYPE, myEvent, MyMeta.TYPE, myMeta); final CommonEvent2EventDataConverter converter = new CommonEvent2EventDataConverter(registry, envelope); final EventData eventData = converter.convert(commonEvent); final EventRecord eventRecord = EventRecord.newBuilder() .setEventId(ByteString.copyFrom(UUIDConverter.toBytes(eventData.eventId))) .setEventStreamId("mystream").setEventNumber(1).setEventType(eventData.type) .setDataContentType(json).setData(ByteString.copyFrom(eventData.data)) .setMetadataContentType(1).setMetadata(ByteString.copyFrom(eventData.metadata)) .setCreated(System.currentTimeMillis()).build(); final RecordedEvent recordedEvent = new RecordedEvent(eventRecord); final RecordedEvent2CommonEventConverter testee = new RecordedEvent2CommonEventConverter(registry); final CommonEvent result = testee.convert(recordedEvent); assertThat(result.getId()).isEqualTo(new EventId(myEvent.getId())); assertThat(result.getDataType()).isEqualTo(MyEvent.TYPE); assertThat(result.getMetaType()).isEqualTo(MyMeta.TYPE); assertThat(result.getData()).isInstanceOf(MyEvent.class); final MyEvent copyMyEvent = (MyEvent) result.getData(); assertThat(copyMyEvent.getId()).isEqualTo(myEvent.getId()); assertThat(copyMyEvent.getDescription()).isEqualTo(myEvent.getDescription()); assertThat(result.getMeta()).isInstanceOf(MyMeta.class); final MyMeta copyMyMeta = (MyMeta) result.getMeta(); assertThat(copyMyMeta.getUser()).isEqualTo(myMeta.getUser()); }
@Test public final void testConvertJsonOtherOther() throws IOException { final EnhancedMimeType envelope = EnhancedMimeType.create("application", "json", Charset.forName("utf-8")); final int json = 1; final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); registry.add(EscMeta.SER_TYPE, envelope.getBaseType(), new EscMetaJsonDeSerializer()); registry.add(Base64Data.SER_TYPE, envelope.getBaseType(), new Base64DataJsonDeSerializer()); registry.add(MyMeta.SER_TYPE, "application/xml", new XmlDeSerializer(JAXB_CLASSES)); registry.add(MyEvent.SER_TYPE, "application/xml", new XmlDeSerializer(JAXB_CLASSES)); final MyEvent myEvent = new MyEvent(UUID.randomUUID(), "Hello, XML!"); final MyMeta myMeta = new MyMeta("michael"); final CommonEvent commonEvent = new SimpleCommonEvent(new EventId(myEvent.getId()), MyEvent.TYPE, myEvent, MyMeta.TYPE, myMeta); final CommonEvent2EventDataConverter converter = new CommonEvent2EventDataConverter(registry, envelope); final EventData eventData = converter.convert(commonEvent); final EventRecord eventRecord = EventRecord.newBuilder() .setEventId(ByteString.copyFrom(UUIDConverter.toBytes(eventData.eventId))) .setEventStreamId("mystream").setEventNumber(1).setEventType(eventData.type) .setDataContentType(json).setData(ByteString.copyFrom(eventData.data)) .setMetadataContentType(1).setMetadata(ByteString.copyFrom(eventData.metadata)) .setCreated(System.currentTimeMillis()).build(); final RecordedEvent recordedEvent = new RecordedEvent(eventRecord); final RecordedEvent2CommonEventConverter testee = new RecordedEvent2CommonEventConverter(registry); final CommonEvent result = testee.convert(recordedEvent); assertThat(result.getId()).isEqualTo(new EventId(myEvent.getId())); assertThat(result.getDataType()).isEqualTo(MyEvent.TYPE); assertThat(result.getMetaType()).isEqualTo(MyMeta.TYPE); assertThat(result.getData()).isInstanceOf(MyEvent.class); final MyEvent copyMyEvent = (MyEvent) result.getData(); assertThat(copyMyEvent.getId()).isEqualTo(myEvent.getId()); assertThat(copyMyEvent.getDescription()).isEqualTo(myEvent.getDescription()); assertThat(result.getMeta()).isInstanceOf(MyMeta.class); final MyMeta copyMyMeta = (MyMeta) result.getMeta(); assertThat(copyMyMeta.getUser()).isEqualTo(myMeta.getUser()); }
@Test public final void testConvertXmlXmlXml() throws IOException { final EnhancedMimeType envelope = EnhancedMimeType.create("application", "xml", Charset.forName("utf-8")); final int json = 0; final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); registry.add(EscMeta.SER_TYPE, envelope.getBaseType(), new XmlDeSerializer(JAXB_CLASSES)); registry.add(MyEvent.SER_TYPE, "application/xml", new XmlDeSerializer(JAXB_CLASSES)); registry.add(MyMeta.SER_TYPE, "application/xml", new XmlDeSerializer(JAXB_CLASSES)); final MyEvent myEvent = new MyEvent(UUID.randomUUID(), "Hello, JSON!"); final MyMeta myMeta = new MyMeta("michael"); final CommonEvent commonEvent = new SimpleCommonEvent(new EventId(myEvent.getId()), MyEvent.TYPE, myEvent, MyMeta.TYPE, myMeta); final CommonEvent2EventDataConverter converter = new CommonEvent2EventDataConverter(registry, envelope); final EventData eventData = converter.convert(commonEvent); final EventRecord eventRecord = EventRecord.newBuilder() .setEventId(ByteString.copyFrom(UUIDConverter.toBytes(eventData.eventId))) .setEventStreamId("mystream").setEventNumber(1).setEventType(eventData.type) .setDataContentType(json).setData(ByteString.copyFrom(eventData.data)) .setMetadataContentType(1).setMetadata(ByteString.copyFrom(eventData.metadata)) .setCreated(System.currentTimeMillis()).build(); final RecordedEvent recordedEvent = new RecordedEvent(eventRecord); final RecordedEvent2CommonEventConverter testee = new RecordedEvent2CommonEventConverter(registry); final CommonEvent result = testee.convert(recordedEvent); assertThat(result.getId()).isEqualTo(new EventId(myEvent.getId())); assertThat(result.getDataType()).isEqualTo(MyEvent.TYPE); assertThat(result.getMetaType()).isEqualTo(MyMeta.TYPE); assertThat(result.getData()).isInstanceOf(MyEvent.class); final MyEvent copyMyEvent = (MyEvent) result.getData(); assertThat(copyMyEvent.getId()).isEqualTo(myEvent.getId()); assertThat(copyMyEvent.getDescription()).isEqualTo(myEvent.getDescription()); assertThat(result.getMeta()).isInstanceOf(MyMeta.class); final MyMeta copyMyMeta = (MyMeta) result.getMeta(); assertThat(copyMyMeta.getUser()).isEqualTo(myMeta.getUser()); }
@Test public final void testConvertXmlXmlOther() throws IOException { final EnhancedMimeType envelope = EnhancedMimeType.create("application", "xml", Charset.forName("utf-8")); final int json = 0; final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); registry.add(EscMeta.SER_TYPE, envelope.getBaseType(), new XmlDeSerializer(JAXB_CLASSES)); registry.add(Base64Data.SER_TYPE, envelope.getBaseType(), new XmlDeSerializer(JAXB_CLASSES)); registry.add(MyMeta.SER_TYPE, "application/xml", new XmlDeSerializer(JAXB_CLASSES)); registry.add(MyEvent.SER_TYPE, "application/json", new MyEvent.MyEventJsonDeSerializer()); final MyEvent myEvent = new MyEvent(UUID.randomUUID(), "Hello, XML!"); final MyMeta myMeta = new MyMeta("michael"); final CommonEvent commonEvent = new SimpleCommonEvent(new EventId(myEvent.getId()), MyEvent.TYPE, myEvent, MyMeta.TYPE, myMeta); final CommonEvent2EventDataConverter converter = new CommonEvent2EventDataConverter(registry, envelope); final EventData eventData = converter.convert(commonEvent); final EventRecord eventRecord = EventRecord.newBuilder() .setEventId(ByteString.copyFrom(UUIDConverter.toBytes(eventData.eventId))) .setEventStreamId("mystream").setEventNumber(1).setEventType(eventData.type) .setDataContentType(json).setData(ByteString.copyFrom(eventData.data)) .setMetadataContentType(1).setMetadata(ByteString.copyFrom(eventData.metadata)) .setCreated(System.currentTimeMillis()).build(); final RecordedEvent recordedEvent = new RecordedEvent(eventRecord); final RecordedEvent2CommonEventConverter testee = new RecordedEvent2CommonEventConverter(registry); final CommonEvent result = testee.convert(recordedEvent); assertThat(result.getId()).isEqualTo(new EventId(myEvent.getId())); assertThat(result.getDataType()).isEqualTo(MyEvent.TYPE); assertThat(result.getMetaType()).isEqualTo(MyMeta.TYPE); assertThat(result.getData()).isInstanceOf(MyEvent.class); final MyEvent copyMyEvent = (MyEvent) result.getData(); assertThat(copyMyEvent.getId()).isEqualTo(myEvent.getId()); assertThat(copyMyEvent.getDescription()).isEqualTo(myEvent.getDescription()); assertThat(result.getMeta()).isInstanceOf(MyMeta.class); final MyMeta copyMyMeta = (MyMeta) result.getMeta(); assertThat(copyMyMeta.getUser()).isEqualTo(myMeta.getUser()); }
@Test public final void testConvertXmlOtherOther() throws IOException { final EnhancedMimeType envelope = EnhancedMimeType.create("application", "xml", Charset.forName("utf-8")); final int json = 0; final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); registry.add(EscMeta.SER_TYPE, envelope.getBaseType(), new XmlDeSerializer(JAXB_CLASSES)); registry.add(Base64Data.SER_TYPE, envelope.getBaseType(), new XmlDeSerializer(JAXB_CLASSES)); registry.add(MyMeta.SER_TYPE, "application/json", new MyMeta.MyMetaJsonDeSerializer()); registry.add(MyEvent.SER_TYPE, "application/json", new MyEvent.MyEventJsonDeSerializer()); final MyEvent myEvent = new MyEvent(UUID.randomUUID(), "Hello, JSON!"); final MyMeta myMeta = new MyMeta("michael"); final CommonEvent commonEvent = new SimpleCommonEvent(new EventId(myEvent.getId()), MyEvent.TYPE, myEvent, MyMeta.TYPE, myMeta); final CommonEvent2EventDataConverter converter = new CommonEvent2EventDataConverter(registry, envelope); final EventData eventData = converter.convert(commonEvent); final EventRecord eventRecord = EventRecord.newBuilder() .setEventId(ByteString.copyFrom(UUIDConverter.toBytes(eventData.eventId))) .setEventStreamId("mystream").setEventNumber(1).setEventType(eventData.type) .setDataContentType(json).setData(ByteString.copyFrom(eventData.data)) .setMetadataContentType(1).setMetadata(ByteString.copyFrom(eventData.metadata)) .setCreated(System.currentTimeMillis()).build(); final RecordedEvent recordedEvent = new RecordedEvent(eventRecord); final RecordedEvent2CommonEventConverter testee = new RecordedEvent2CommonEventConverter(registry); final CommonEvent result = testee.convert(recordedEvent); assertThat(result.getId()).isEqualTo(new EventId(myEvent.getId())); assertThat(result.getDataType()).isEqualTo(MyEvent.TYPE); assertThat(result.getMetaType()).isEqualTo(MyMeta.TYPE); assertThat(result.getData()).isInstanceOf(MyEvent.class); final MyEvent copyMyEvent = (MyEvent) result.getData(); assertThat(copyMyEvent.getId()).isEqualTo(myEvent.getId()); assertThat(copyMyEvent.getDescription()).isEqualTo(myEvent.getDescription()); assertThat(result.getMeta()).isInstanceOf(MyMeta.class); final MyMeta copyMyMeta = (MyMeta) result.getMeta(); assertThat(copyMyMeta.getUser()).isEqualTo(myMeta.getUser()); }