Search in sources :

Example 1 with IntermediateMessageSerde

use of org.apache.samza.serializers.IntermediateMessageSerde in project samza by apache.

the class TestIntermediateMessageSerde method testWatermarkMessageSerde.

@Test
public void testWatermarkMessageSerde() {
    IntermediateMessageSerde imserde = new IntermediateMessageSerde(new ObjectSerde());
    String taskName = "task-1";
    WatermarkMessage watermark = new WatermarkMessage(System.currentTimeMillis(), taskName);
    byte[] bytes = imserde.toBytes(watermark);
    WatermarkMessage de = (WatermarkMessage) imserde.fromBytes(bytes);
    assertEquals(MessageType.of(de), MessageType.WATERMARK);
    assertEquals(de.getTaskName(), taskName);
    assertTrue(de.getTimestamp() > 0);
}
Also used : WatermarkMessage(org.apache.samza.system.WatermarkMessage) IntermediateMessageSerde(org.apache.samza.serializers.IntermediateMessageSerde) Test(org.junit.Test)

Example 2 with IntermediateMessageSerde

use of org.apache.samza.serializers.IntermediateMessageSerde in project samza by apache.

the class TestIntermediateMessageSerde method testUserMessageSerde.

@Test
public void testUserMessageSerde() {
    IntermediateMessageSerde imserde = new IntermediateMessageSerde(new ObjectSerde());
    String msg = "this is a test message";
    TestUserMessage userMessage = new TestUserMessage(msg, 0, System.currentTimeMillis());
    byte[] bytes = imserde.toBytes(userMessage);
    TestUserMessage de = (TestUserMessage) imserde.fromBytes(bytes);
    assertEquals(MessageType.of(de), MessageType.USER_MESSAGE);
    assertEquals(de.getMessage(), msg);
    assertEquals(de.getOffset(), 0);
    assertTrue(de.getTimestamp() > 0);
}
Also used : IntermediateMessageSerde(org.apache.samza.serializers.IntermediateMessageSerde) Test(org.junit.Test)

Example 3 with IntermediateMessageSerde

use of org.apache.samza.serializers.IntermediateMessageSerde in project samza by apache.

the class TestIntermediateMessageSerde method testUserMessageSerdeException.

@Test(expected = IllegalArgumentException.class)
public void testUserMessageSerdeException() {
    Serde<?> mockUserMessageSerde = mock(Serde.class);
    when(mockUserMessageSerde.fromBytes(anyObject())).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            byte[] bytes = invocation.getArgumentAt(0, byte[].class);
            if (Arrays.equals(bytes, new byte[] { 1, 2 })) {
                throw new IllegalArgumentException("User message serde failed to deserialize this message.");
            } else {
                // Intermediate message serde shouldn't try to deserialize user message with wrong bytes
                Assert.fail();
                return null;
            }
        }
    });
    IntermediateMessageSerde imserde = new IntermediateMessageSerde(mockUserMessageSerde);
    byte[] bytes = new byte[] { 0, 1, 2 };
    imserde.fromBytes(bytes);
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) IntermediateMessageSerde(org.apache.samza.serializers.IntermediateMessageSerde) Test(org.junit.Test)

Example 4 with IntermediateMessageSerde

use of org.apache.samza.serializers.IntermediateMessageSerde in project samza by apache.

the class TestIntermediateMessageSerde method testEndOfStreamMessageSerde.

@Test
public void testEndOfStreamMessageSerde() {
    IntermediateMessageSerde imserde = new IntermediateMessageSerde(new ObjectSerde());
    String streamId = "test-stream";
    String taskName = "task-1";
    EndOfStreamMessage eos = new EndOfStreamMessage(taskName);
    byte[] bytes = imserde.toBytes(eos);
    EndOfStreamMessage de = (EndOfStreamMessage) imserde.fromBytes(bytes);
    assertEquals(MessageType.of(de), MessageType.END_OF_STREAM);
    assertEquals(de.getTaskName(), taskName);
    assertEquals(de.getVersion(), 1);
}
Also used : IntermediateMessageSerde(org.apache.samza.serializers.IntermediateMessageSerde) EndOfStreamMessage(org.apache.samza.system.EndOfStreamMessage) Test(org.junit.Test)

Aggregations

IntermediateMessageSerde (org.apache.samza.serializers.IntermediateMessageSerde)4 Test (org.junit.Test)4 EndOfStreamMessage (org.apache.samza.system.EndOfStreamMessage)1 WatermarkMessage (org.apache.samza.system.WatermarkMessage)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1