ESHttpMarshaller { @NotNull public final String marshal(@NotNull final SerializerRegistry registry, @NotNull final List<CommonEvent> commonEvents) { Contract.requireArgNotNull("registry", registry); Contract.requireArgNotNull("commonEvents", commonEvents); final Serializer serializer = registry.getSerializer(EscEvents.SER_TYPE); final List<EscEvent> eventList = new ArrayList<>(); for (final CommonEvent commonEvent : commonEvents) { eventList.add(createEscEvent(registry, serializer.getMimeType(), commonEvent)); } final EscEvents events = new EscEvents(eventList); final byte[] data = serializer.marshal(events, EscEvents.SER_TYPE); return new String(data, serializer.getMimeType().getEncoding()); }  @NotNull final String marshal(@NotNull final SerializerRegistry registry,
            @NotNull final List<CommonEvent> commonEvents); @NotNull final String marshal(@NotNull final SerializerRegistry registry, 
            @NotNull final CommonEvent commonEvent);  }
@Test public void testMarshalEventXML() throws IOException { final String expectedXml = IOUtils.toString( this.getClass().getResourceAsStream("/" + this.getClass().getSimpleName() + "_Event.xml"), Charset.forName("utf-8")); final UUID uuid = UUID.fromString("a07d6a1a-715d-4d8e-98fa-b158e3339303"); final CommonEvent commonEvent = new SimpleCommonEvent(new EventId(uuid), MyEvent.TYPE, new MyEvent(uuid, "Whatever...")); final ESHttpMarshaller testee = new ESHttpMarshaller(); final String currentXml = testee.marshal(createXmlRegistry(), commonEvent); final Diff documentDiff = DiffBuilder.compare(expectedXml).withTest(currentXml).ignoreWhitespace() .build(); assertThat(documentDiff.hasDifferences()).describedAs(documentDiff.toString()).isFalse(); }
@Test public void testMarshalEventJsonP() throws IOException { final String expectedJson = IOUtils.toString( this.getClass().getResourceAsStream("/" + this.getClass().getSimpleName() + "_Event.json"), Charset.forName("utf-8")); final UUID uuid = UUID.fromString("a07d6a1a-715d-4d8e-98fa-b158e3339303"); final JsonObject myEvent = Json.createObjectBuilder().add("id", uuid.toString()) .add("description", "Whatever...").build(); final CommonEvent commonEvent = new SimpleCommonEvent(new EventId(uuid), MyEvent.TYPE, myEvent); final ESHttpMarshaller testee = new ESHttpMarshaller(); final String currentJson = testee.marshal(createJsonRegistry(), commonEvent); assertThatJson(currentJson).isEqualTo(expectedJson); }
@Test public void testMarshalEventsXML() throws IOException { final String expectedXml = IOUtils.toString( this.getClass().getResourceAsStream("/" + this.getClass().getSimpleName() + "_Events.xml"), Charset.forName("utf-8")); final UUID uuid1 = UUID.fromString("a07d6a1a-715d-4d8e-98fa-b158e3339303"); final CommonEvent commonEvent1 = new SimpleCommonEvent(new EventId(uuid1), MyEvent.TYPE, new MyEvent(uuid1, "Whatever...")); final UUID uuid2 = UUID.fromString("ee9e13fc-e799-4835-9830-ecb9d620c1c9"); final CommonEvent commonEvent2 = new SimpleCommonEvent(new EventId(uuid2), MyEvent.TYPE, new MyEvent(uuid2, "Hello XML")); final List<CommonEvent> commonEvents = new ArrayList<>(); commonEvents.add(commonEvent1); commonEvents.add(commonEvent2); final ESHttpMarshaller testee = new ESHttpMarshaller(); final String currentXml = testee.marshal(createXmlRegistry(), commonEvents); final Diff documentDiff = DiffBuilder.compare(expectedXml).withTest(currentXml).ignoreWhitespace() .build(); assertThat(documentDiff.hasDifferences()).describedAs(documentDiff.toString()).isFalse(); }
@Test public void testMarshalEventsJSON() throws IOException { final String expectedJson = IOUtils.toString( this.getClass().getResourceAsStream("/" + this.getClass().getSimpleName() + "_Events.json"), Charset.forName("utf-8")); final UUID uuid1 = UUID.fromString("a07d6a1a-715d-4d8e-98fa-b158e3339303"); final JsonObject myEvent1 = Json.createObjectBuilder().add("id", uuid1.toString()) .add("description", "Whatever...").build(); final CommonEvent commonEvent1 = new SimpleCommonEvent(new EventId(uuid1), MyEvent.TYPE, myEvent1); final UUID uuid2 = UUID.fromString("ee9e13fc-e799-4835-9830-ecb9d620c1c9"); final JsonObject myEvent2 = Json.createObjectBuilder().add("id", uuid2.toString()) .add("description", "Hello JSON").build(); final CommonEvent commonEvent2 = new SimpleCommonEvent(new EventId(uuid2), MyEvent.TYPE, myEvent2); final List<CommonEvent> commonEvents = new ArrayList<>(); commonEvents.add(commonEvent1); commonEvents.add(commonEvent2); final ESHttpMarshaller testee = new ESHttpMarshaller(); final String currentJson = testee.marshal(createJsonRegistry(), commonEvents); assertThatJson(currentJson).isEqualTo(expectedJson); }
@Test public void testMarshalEventsJsonB() throws IOException { final String expectedJson = IOUtils.toString( this.getClass().getResourceAsStream("/" + this.getClass().getSimpleName() + "_Events.json"), Charset.forName("utf-8")); final UUID uuid1 = UUID.fromString("a07d6a1a-715d-4d8e-98fa-b158e3339303"); final JsonObject myEvent1 = Json.createObjectBuilder().add("id", uuid1.toString()) .add("description", "Whatever...").build(); final CommonEvent commonEvent1 = new SimpleCommonEvent(new EventId(uuid1), MyEvent.TYPE, myEvent1); final UUID uuid2 = UUID.fromString("ee9e13fc-e799-4835-9830-ecb9d620c1c9"); final JsonObject myEvent2 = Json.createObjectBuilder().add("id", uuid2.toString()) .add("description", "Hello JSON").build(); final CommonEvent commonEvent2 = new SimpleCommonEvent(new EventId(uuid2), MyEvent.TYPE, myEvent2); final List<CommonEvent> commonEvents = new ArrayList<>(); commonEvents.add(commonEvent1); commonEvents.add(commonEvent2); final ESHttpMarshaller testee = new ESHttpMarshaller(); final String currentJson = testee.marshal(createJsonbRegistry(), commonEvents); assertThatJson(currentJson).isEqualTo(expectedJson); }