MultiPayloadMessageFactory extends AdaptrisMessageFactory { @Override public AdaptrisMessage newMessage(byte[] payload) { return newMessage(defaultPayloadId(), payload, null); } MultiPayloadMessageFactory(); @Override AdaptrisMessage newMessage(byte[] payload); AdaptrisMessage newMessage(@NotNull String payloadId, byte[] payload); @Override AdaptrisMessage newMessage(byte[] payload, Set metadata); AdaptrisMessage newMessage(@NotNull String payloadId, byte[] payload, Set metadata); @Override AdaptrisMessage newMessage(String payload); @Override AdaptrisMessage newMessage(String payload, String charEncoding); @Override AdaptrisMessage newMessage(String payload, Set metadata); @Override AdaptrisMessage newMessage(String payload, String charEncoding, Set metadata); AdaptrisMessage newMessage(@NotNull String payloadId, String content, String charEncoding); AdaptrisMessage newMessage(@NotNull String payloadId, String content, String charEncoding, Set metadata); @Override AdaptrisMessage newMessage(AdaptrisMessage source, Collection<String> metadataKeysToPreserve); AdaptrisMessage newMessage(@NotNull String payloadId, AdaptrisMessage source,
      Collection<String> metadataKeysToPreserve); @Override AdaptrisMessage newMessage(); @Override String getDefaultCharEncoding(); @Override void setDefaultCharEncoding(String s); String getDefaultPayloadId(); void setDefaultPayloadId(String defaultPayloadId);  }
@Test public void testMessageFactoryPayloadID() { MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) messageFactory.newMessage(ID, PAYLOAD); assertTrue(message.hasPayloadId(ID)); assertEquals(ID, message.getCurrentPayloadId()); assertEquals(1, message.getPayloadCount()); assertArrayEquals(PAYLOAD, message.getPayload()); assertEquals(0, message.getMetadata().size()); }
@Test public void testMessageFactoryPayloadMetadata() { MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) messageFactory.newMessage(PAYLOAD, METADATA); assertEquals(MultiPayloadAdaptrisMessage.DEFAULT_PAYLOAD_ID, message.getCurrentPayloadId()); assertEquals(1, message.getPayloadCount()); assertArrayEquals(PAYLOAD, message.getPayload()); assertEquals(1, message.getMetadata().size()); }
@Test public void testMessageFactoryPayloadMetadataID() { MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) messageFactory.newMessage(ID, PAYLOAD, METADATA); assertEquals(ID, message.getCurrentPayloadId()); assertEquals(1, message.getPayloadCount()); assertArrayEquals(PAYLOAD, message.getPayload()); assertEquals(1, message.getMetadata().size()); }
@Test public void testMessageFactoryContent() { MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) messageFactory.newMessage(CONTENT); assertEquals(MultiPayloadAdaptrisMessage.DEFAULT_PAYLOAD_ID, message.getCurrentPayloadId()); assertEquals(1, message.getPayloadCount()); assertEquals(CONTENT, message.getContent()); assertEquals(0, message.getMetadata().size()); }
@Test public void testMessageFactoryContentEncoding() { MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) messageFactory.newMessage(CONTENT, ENCODING); assertEquals(MultiPayloadAdaptrisMessage.DEFAULT_PAYLOAD_ID, message.getCurrentPayloadId()); assertEquals(1, message.getPayloadCount()); assertEquals(CONTENT, message.getContent()); assertEquals(ENCODING, message.getContentEncoding()); assertEquals(0, message.getMetadata().size()); }
@Test public void testMessageFactoryContentEncodingMetadata() { MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) messageFactory.newMessage(CONTENT, ENCODING, METADATA); assertEquals(MultiPayloadAdaptrisMessage.DEFAULT_PAYLOAD_ID, message.getCurrentPayloadId()); assertEquals(1, message.getPayloadCount()); assertEquals(CONTENT, message.getContent()); assertEquals(ENCODING, message.getContentEncoding()); assertEquals(1, message.getMetadata().size()); }
@Test public void testMessageFactoryContentEncodingMetadataID() { MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) messageFactory.newMessage(ID, CONTENT, ENCODING, METADATA); assertEquals(ID, message.getCurrentPayloadId()); assertEquals(1, message.getPayloadCount()); assertEquals(CONTENT, message.getContent()); assertEquals(ENCODING, message.getContentEncoding()); assertEquals(1, message.getMetadata().size()); }
@Test public void testMessageFactoryCloneMessage() throws Exception { AdaptrisMessage singleMessage = DefaultMessageFactory.getDefaultInstance().newMessage(PAYLOAD, METADATA); MultiPayloadAdaptrisMessage multiMessage = (MultiPayloadAdaptrisMessage) messageFactory.newMessage(singleMessage, null); assertEquals(MultiPayloadAdaptrisMessage.DEFAULT_PAYLOAD_ID, multiMessage.getCurrentPayloadId()); assertEquals(1, multiMessage.getPayloadCount()); assertEquals(0, multiMessage.getPayload().length); assertEquals(1, multiMessage.getMetadata().size()); }
@Test public void testMessageStreams() throws Exception { MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) messageFactory.newMessage((byte[])null); InputStream is = message.getInputStream(); assertTrue(is.read() == -1); OutputStream os = message.getOutputStream(); os.write(PAYLOAD); os.close(); is = message.getInputStream(); byte[] bytes = new byte[PAYLOAD.length]; is.read(bytes); assertArrayEquals(PAYLOAD, bytes); message.setContentEncoding(null); try (Writer writer = message.getWriter()) { writer.write(CONTENT); } assertEquals(CONTENT, message.getContent()); }
@Test public void testOutputForLogging() throws Exception { MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) messageFactory.newMessage("cupcake", PAYLOAD); message.addContent("bacon", CONTENT); String logged = message.getPayloadForLogging(); assertTrue(message.getPayloadCount() == 2); assertTrue(logged.contains("bacon:" + CONTENT)); assertTrue(logged.contains("cupcake")); assertFalse(logged.contains("cupcake:" + new String(PAYLOAD))); }
@Test public void testResolve() { MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage) messageFactory.newMessage("bacon", CONTENT, ENCODING, METADATA); message.addPayload("cake", PAYLOAD); message.switchPayload("bacon"); assertEquals(CONTENT.length() + "," + CONTENT, message.resolve("%message{%size},%payload_id{bacon}", true)); assertEquals(CONTENT.length() + "," + CONTENT, message.resolve("%message{%size},%payload_id{bacon}", false)); assertEquals(CONTENT.length() + "," + CONTENT, message.resolve("%message{%size},%payload{id:bacon}", true)); assertEquals(CONTENT.length() + "," + CONTENT, message.resolve("%message{%size},%payload{id:bacon}", false)); assertEquals(new String(PAYLOAD), message.resolve("%payload_id{cake}")); assertNull(message.resolve(null)); assertEquals("VALUE", message.resolve("%message{KEY}")); try { message.resolve("%payload_id{fail}"); fail(); } catch (Exception e) { } }