use of org.apache.samza.operators.data.MessageType in project samza by apache.
the class TestMessageStreamImpl method testSink.
@Test
public void testSink() {
MessageStreamImpl<TestMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph);
SystemStream testStream = new SystemStream("test-sys", "test-stream");
SinkFunction<TestMessageEnvelope> xSink = (TestMessageEnvelope m, MessageCollector mc, TaskCoordinator tc) -> {
mc.send(new OutgoingMessageEnvelope(testStream, m.getMessage()));
tc.commit(TaskCoordinator.RequestScope.CURRENT_TASK);
};
inputStream.sink(xSink);
Collection<OperatorSpec> subs = inputStream.getRegisteredOperatorSpecs();
assertEquals(subs.size(), 1);
OperatorSpec<TestMessageEnvelope> sinkOp = subs.iterator().next();
assertTrue(sinkOp instanceof SinkOperatorSpec);
assertEquals(((SinkOperatorSpec) sinkOp).getSinkFn(), xSink);
TestMessageEnvelope mockTest1 = mock(TestMessageEnvelope.class);
MessageType mockMsgBody = mock(MessageType.class);
when(mockTest1.getMessage()).thenReturn(mockMsgBody);
final List<OutgoingMessageEnvelope> outMsgs = new ArrayList<>();
MessageCollector mockCollector = mock(MessageCollector.class);
doAnswer(invocation -> {
outMsgs.add((OutgoingMessageEnvelope) invocation.getArguments()[0]);
return null;
}).when(mockCollector).send(any());
TaskCoordinator mockCoordinator = mock(TaskCoordinator.class);
((SinkOperatorSpec) sinkOp).getSinkFn().apply(mockTest1, mockCollector, mockCoordinator);
assertEquals(1, outMsgs.size());
assertEquals(testStream, outMsgs.get(0).getSystemStream());
assertEquals(mockMsgBody, outMsgs.get(0).getMessage());
}
use of org.apache.samza.operators.data.MessageType in project samza by apache.
the class TestMessageStreamImpl method testMap.
@Test
public void testMap() {
MessageStreamImpl<TestMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph);
MapFunction<TestMessageEnvelope, TestOutputMessageEnvelope> xMap = (TestMessageEnvelope m) -> new TestOutputMessageEnvelope(m.getKey(), m.getMessage().getValue().length() + 1);
MessageStream<TestOutputMessageEnvelope> outputStream = inputStream.map(xMap);
Collection<OperatorSpec> subs = inputStream.getRegisteredOperatorSpecs();
assertEquals(subs.size(), 1);
OperatorSpec<TestOutputMessageEnvelope> mapOp = subs.iterator().next();
assertTrue(mapOp instanceof StreamOperatorSpec);
assertEquals(mapOp.getNextStream(), outputStream);
// assert that the transformation function is what we defined above
TestMessageEnvelope xTestMsg = mock(TestMessageEnvelope.class);
MessageType mockInnerTestMessage = mock(MessageType.class);
when(xTestMsg.getKey()).thenReturn("test-msg-key");
when(xTestMsg.getMessage()).thenReturn(mockInnerTestMessage);
when(mockInnerTestMessage.getValue()).thenReturn("123456789");
Collection<TestOutputMessageEnvelope> cOutputMsg = ((StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope>) mapOp).getTransformFn().apply(xTestMsg);
assertEquals(cOutputMsg.size(), 1);
TestOutputMessageEnvelope outputMessage = cOutputMsg.iterator().next();
assertEquals(outputMessage.getKey(), xTestMsg.getKey());
assertEquals(outputMessage.getMessage(), Integer.valueOf(xTestMsg.getMessage().getValue().length() + 1));
}
use of org.apache.samza.operators.data.MessageType in project samza by apache.
the class TestStreamGraphImpl method testMultipleGetInputStream.
@Test(expected = IllegalStateException.class)
public void testMultipleGetInputStream() {
ApplicationRunner mockRunner = mock(ApplicationRunner.class);
Config mockConfig = mock(Config.class);
StreamSpec testStreamSpec1 = new StreamSpec("test-stream-1", "physical-stream-1", "test-system");
StreamSpec testStreamSpec2 = new StreamSpec("test-stream-2", "physical-stream-2", "test-system");
StreamSpec nonExistentStreamSpec = new StreamSpec("non-existent-stream", "physical-stream-1", "test-system");
when(mockRunner.getStreamSpec("test-stream-1")).thenReturn(testStreamSpec1);
when(mockRunner.getStreamSpec("test-stream-2")).thenReturn(testStreamSpec2);
StreamGraphImpl graph = new StreamGraphImpl(mockRunner, mockConfig);
BiFunction<String, MessageType, TestInputMessageEnvelope> xMsgBuilder = (k, v) -> new TestInputMessageEnvelope(k, v.getValue(), v.getEventTime(), "input-id-1");
//create 2 streams for the corresponding streamIds
MessageStream<TestInputMessageEnvelope> inputStream1 = graph.getInputStream("test-stream-1", xMsgBuilder);
MessageStream<TestInputMessageEnvelope> inputStream2 = graph.getInputStream("test-stream-2", xMsgBuilder);
//assert that the streamGraph contains only the above 2 streams
assertEquals(graph.getInputStreams().get(testStreamSpec1), inputStream1);
assertEquals(graph.getInputStreams().get(testStreamSpec2), inputStream2);
assertEquals(graph.getInputStreams().get(nonExistentStreamSpec), null);
assertEquals(graph.getInputStreams().size(), 2);
//should throw IllegalStateException
graph.getInputStream("test-stream-1", xMsgBuilder);
}
Aggregations