use of org.apache.samza.operators.data.TestMessageEnvelope in project samza by apache.
the class TestMessageStreamImpl method validateMergeOperator.
private void validateMergeOperator(MessageStream<TestMessageEnvelope> mergeSource, MessageStream<TestMessageEnvelope> mergeOutput) {
Collection<OperatorSpec> subs = ((MessageStreamImpl<TestMessageEnvelope>) mergeSource).getRegisteredOperatorSpecs();
assertEquals(subs.size(), 1);
OperatorSpec<TestMessageEnvelope> mergeOp = subs.iterator().next();
assertTrue(mergeOp instanceof StreamOperatorSpec);
assertEquals(((StreamOperatorSpec) mergeOp).getNextStream(), mergeOutput);
TestMessageEnvelope mockMsg = mock(TestMessageEnvelope.class);
Collection<TestMessageEnvelope> outputs = ((StreamOperatorSpec<TestMessageEnvelope, TestMessageEnvelope>) mergeOp).getTransformFn().apply(mockMsg);
assertEquals(outputs.size(), 1);
assertEquals(outputs.iterator().next(), mockMsg);
}
use of org.apache.samza.operators.data.TestMessageEnvelope in project samza by apache.
the class TestStreamGraphImpl method testGetInputStream.
@Test
public void testGetInputStream() {
ApplicationRunner mockRunner = mock(ApplicationRunner.class);
Config mockConfig = mock(Config.class);
StreamSpec testStreamSpec = new StreamSpec("test-stream-1", "physical-stream-1", "test-system");
when(mockRunner.getStreamSpec("test-stream-1")).thenReturn(testStreamSpec);
StreamGraphImpl graph = new StreamGraphImpl(mockRunner, mockConfig);
BiFunction<String, MessageType, TestInputMessageEnvelope> xMsgBuilder = (k, v) -> new TestInputMessageEnvelope(k, v.getValue(), v.getEventTime(), "input-id-1");
MessageStream<TestMessageEnvelope> mInputStream = graph.getInputStream("test-stream-1", xMsgBuilder);
assertEquals(graph.getInputStreams().get(testStreamSpec), mInputStream);
assertTrue(mInputStream instanceof InputStreamInternalImpl);
assertEquals(((InputStreamInternalImpl) mInputStream).getMsgBuilder(), xMsgBuilder);
String key = "test-input-key";
MessageType msgBody = new MessageType("test-msg-value", 333333L);
TestMessageEnvelope xInputMsg = ((InputStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mInputStream).getMsgBuilder().apply(key, msgBody);
assertEquals(xInputMsg.getKey(), key);
assertEquals(xInputMsg.getMessage().getValue(), msgBody.getValue());
assertEquals(xInputMsg.getMessage().getEventTime(), msgBody.getEventTime());
assertEquals(((TestInputMessageEnvelope) xInputMsg).getInputId(), "input-id-1");
}
use of org.apache.samza.operators.data.TestMessageEnvelope in project samza by apache.
the class TestMessageStreamImpl method testFilter.
@Test
public void testFilter() {
MessageStreamImpl<TestMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph);
FilterFunction<TestMessageEnvelope> xFilter = (TestMessageEnvelope m) -> m.getMessage().getEventTime() > 123456L;
MessageStream<TestMessageEnvelope> outputStream = inputStream.filter(xFilter);
Collection<OperatorSpec> subs = inputStream.getRegisteredOperatorSpecs();
assertEquals(subs.size(), 1);
OperatorSpec<TestMessageEnvelope> filterOp = subs.iterator().next();
assertTrue(filterOp instanceof StreamOperatorSpec);
assertEquals(filterOp.getNextStream(), outputStream);
// assert that the transformation function is what we defined above
FlatMapFunction<TestMessageEnvelope, TestMessageEnvelope> txfmFn = ((StreamOperatorSpec<TestMessageEnvelope, TestMessageEnvelope>) filterOp).getTransformFn();
TestMessageEnvelope mockMsg = mock(TestMessageEnvelope.class);
MessageType mockInnerTestMessage = mock(MessageType.class);
when(mockMsg.getMessage()).thenReturn(mockInnerTestMessage);
when(mockInnerTestMessage.getEventTime()).thenReturn(11111L);
Collection<TestMessageEnvelope> output = txfmFn.apply(mockMsg);
assertTrue(output.isEmpty());
when(mockMsg.getMessage()).thenReturn(mockInnerTestMessage);
when(mockInnerTestMessage.getEventTime()).thenReturn(999999L);
output = txfmFn.apply(mockMsg);
assertEquals(output.size(), 1);
assertEquals(output.iterator().next(), mockMsg);
}
use of org.apache.samza.operators.data.TestMessageEnvelope 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.TestMessageEnvelope 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));
}
Aggregations