Data implements ValueObject, Serializable { @SuppressWarnings("unchecked") public final <T> T unmarshalContent(final JAXBContext ctx) { if (!(isJson() || isXml() || isText())) { throw new IllegalStateException( "Can only unmarshal JSON, XML or TEXT content, not: " + mimeType); } if (isJson()) { try { try (final JsonReader reader = Json.createReader(new StringReader(content))) { return (T) reader.readObject(); } } catch (final RuntimeException ex) { throw new RuntimeException( "Error parsing json content: '" + content + "'", ex); } } if (isXml()) { try { return unmarshal(ctx, content, null); } catch (final RuntimeException ex) { throw new RuntimeException( "Error parsing xml content: '" + content + "'", ex); } } return (T) content; } protected  Data();  Data(@NotNull final String type,
            @NotNull final EnhancedMimeType mimeType,
            @NotNull final String content); @NotNull final String getType(); @NotNull final EnhancedMimeType getMimeType(); @NotNull final String getContent(); final boolean isXml(); final boolean isJson(); final boolean isText(); @SuppressWarnings("unchecked") final T unmarshalContent(final JAXBContext ctx); @Override final String toString(); static Data valueOf(final String type, final Object obj);  }
@Test public void testUnmarshalXmlContent() throws JAXBException { final BookAddedEvent event = testee.unmarshalContent( JAXBContext.newInstance(BookAddedEvent.class)); assertThat(event).isNotNull(); assertThat(event.getName()).isEqualTo("Shining"); assertThat(event.getAuthor()).isEqualTo("Stephen King"); }
@Test public void testUnmarshalJsonContent() throws MimeTypeParseException { final Data data = new Data("BookAddedEvent", new EnhancedMimeType("application/json; encoding=utf-8"), "{\"name\":\"Shining\",\"author\":\"Stephen King\"}"); final JsonObject event = data.unmarshalContent(null); assertThat(event.getString("name")).isEqualTo("Shining"); assertThat(event.getString("author")).isEqualTo("Stephen King"); }