EventMessageEncoder { public byte[] encode(EventMessage eventMessage) { byteArrayOutputStream.reset(); try { writeNullTerminatedString(dataOutputStream, eventMessage.schemeIdUri); String nonNullValue = eventMessage.value != null ? eventMessage.value : ""; writeNullTerminatedString(dataOutputStream, nonNullValue); writeUnsignedInt(dataOutputStream, eventMessage.durationMs); writeUnsignedInt(dataOutputStream, eventMessage.id); dataOutputStream.write(eventMessage.messageData); dataOutputStream.flush(); return byteArrayOutputStream.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } } EventMessageEncoder(); byte[] encode(EventMessage eventMessage);  }
@Test public void testEncodeEventStream() throws IOException { byte[] foo = new byte[] {1, 2, 3}; byte[] encodedByteArray = new EventMessageEncoder().encode(DECODED_MESSAGE); assertThat(encodedByteArray).isEqualTo(ENCODED_MESSAGE); }
@Test public void testEncodeDecodeEventStream() throws IOException { byte[] encodedByteArray = new EventMessageEncoder().encode(DECODED_MESSAGE); MetadataInputBuffer buffer = new MetadataInputBuffer(); buffer.data = ByteBuffer.allocate(encodedByteArray.length).put(encodedByteArray); EventMessageDecoder decoder = new EventMessageDecoder(); Metadata metadata = decoder.decode(buffer); assertThat(metadata.length()).isEqualTo(1); assertThat(metadata.get(0)).isEqualTo(DECODED_MESSAGE); }
@Test public void testEncodeEventStreamMultipleTimesWorkingCorrectly() throws IOException { EventMessage eventMessage1 = new EventMessage("urn:test", "123", 3000, 1000402, new byte[] {4, 3, 2, 1, 0}); byte[] expectedEmsgBody1 = joinByteArrays( createByteArray(117, 114, 110, 58, 116, 101, 115, 116, 0), createByteArray(49, 50, 51, 0), createByteArray(0, 0, 11, 184), createByteArray(0, 15, 67, 210), createByteArray(4, 3, 2, 1, 0)); EventMessageEncoder eventMessageEncoder = new EventMessageEncoder(); byte[] encodedByteArray = eventMessageEncoder.encode(DECODED_MESSAGE); assertThat(encodedByteArray).isEqualTo(ENCODED_MESSAGE); byte[] encodedByteArray1 = eventMessageEncoder.encode(eventMessage1); assertThat(encodedByteArray1).isEqualTo(expectedEmsgBody1); }