use of org.apache.samza.operators.data.TestMessageEnvelope in project samza by apache.
the class TestMessageStreamImpl method testFlatMap.
@Test
public void testFlatMap() {
MessageStreamImpl<TestMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph);
List<TestOutputMessageEnvelope> flatOuts = new ArrayList<TestOutputMessageEnvelope>() {
{
this.add(mock(TestOutputMessageEnvelope.class));
this.add(mock(TestOutputMessageEnvelope.class));
this.add(mock(TestOutputMessageEnvelope.class));
}
};
final List<TestMessageEnvelope> inputMsgs = new ArrayList<>();
FlatMapFunction<TestMessageEnvelope, TestOutputMessageEnvelope> xFlatMap = (TestMessageEnvelope message) -> {
inputMsgs.add(message);
return flatOuts;
};
MessageStream<TestOutputMessageEnvelope> outputStream = inputStream.flatMap(xFlatMap);
Collection<OperatorSpec> subs = inputStream.getRegisteredOperatorSpecs();
assertEquals(subs.size(), 1);
OperatorSpec<TestOutputMessageEnvelope> flatMapOp = subs.iterator().next();
assertTrue(flatMapOp instanceof StreamOperatorSpec);
assertEquals(flatMapOp.getNextStream(), outputStream);
assertEquals(((StreamOperatorSpec) flatMapOp).getTransformFn(), xFlatMap);
TestMessageEnvelope mockInput = mock(TestMessageEnvelope.class);
// assert that the transformation function is what we defined above
List<TestOutputMessageEnvelope> result = (List<TestOutputMessageEnvelope>) ((StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope>) flatMapOp).getTransformFn().apply(mockInput);
assertEquals(flatOuts, result);
assertEquals(inputMsgs.size(), 1);
assertEquals(inputMsgs.get(0), mockInput);
}
use of org.apache.samza.operators.data.TestMessageEnvelope in project samza by apache.
the class TestStreamOperatorImpl method testSimpleOperatorClose.
@Test
public void testSimpleOperatorClose() {
StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope> mockOp = mock(StreamOperatorSpec.class);
FlatMapFunction<TestMessageEnvelope, TestOutputMessageEnvelope> txfmFn = mock(FlatMapFunction.class);
when(mockOp.getTransformFn()).thenReturn(txfmFn);
Config mockConfig = mock(Config.class);
TaskContext mockContext = mock(TaskContext.class);
StreamOperatorImpl<TestMessageEnvelope, TestOutputMessageEnvelope> opImpl = spy(new StreamOperatorImpl<>(mockOp, mockConfig, mockContext));
// ensure that close is not called yet
verify(txfmFn, times(0)).close();
opImpl.handleClose();
// ensure that close is called once inside handleClose()
verify(txfmFn, times(1)).close();
}
use of org.apache.samza.operators.data.TestMessageEnvelope in project samza by apache.
the class TestOperatorSpecs method testCreateStreamOperator.
@Test
public void testCreateStreamOperator() {
FlatMapFunction<Object, TestMessageEnvelope> transformFn = m -> new ArrayList<TestMessageEnvelope>() {
{
this.add(new TestMessageEnvelope(m.toString(), m.toString(), 12345L));
}
};
MessageStreamImpl<TestMessageEnvelope> mockOutput = mock(MessageStreamImpl.class);
StreamOperatorSpec<Object, TestMessageEnvelope> streamOp = OperatorSpecs.createStreamOperatorSpec(transformFn, mockOutput, 1);
assertEquals(streamOp.getTransformFn(), transformFn);
Object mockInput = mock(Object.class);
when(mockInput.toString()).thenReturn("test-string-1");
List<TestMessageEnvelope> outputs = (List<TestMessageEnvelope>) streamOp.getTransformFn().apply(mockInput);
assertEquals(outputs.size(), 1);
assertEquals(outputs.get(0).getKey(), "test-string-1");
assertEquals(outputs.get(0).getMessage().getValue(), "test-string-1");
assertEquals(outputs.get(0).getMessage().getEventTime(), 12345L);
assertEquals(streamOp.getNextStream(), mockOutput);
}
use of org.apache.samza.operators.data.TestMessageEnvelope in project samza by apache.
the class TestOperatorSpecs method testCreatePartialJoinOperator.
@Test
public void testCreatePartialJoinOperator() {
PartialJoinFunction<String, TestMessageEnvelope, TestMessageEnvelope, TestOutputMessageEnvelope> thisPartialJoinFn = mock(PartialJoinFunction.class);
PartialJoinFunction<String, TestMessageEnvelope, TestMessageEnvelope, TestOutputMessageEnvelope> otherPartialJoinFn = mock(PartialJoinFunction.class);
StreamGraphImpl mockGraph = mock(StreamGraphImpl.class);
MessageStreamImpl<TestOutputMessageEnvelope> joinOutput = TestMessageStreamImplUtil.getMessageStreamImpl(mockGraph);
PartialJoinOperatorSpec<String, TestMessageEnvelope, TestMessageEnvelope, TestOutputMessageEnvelope> partialJoinSpec = OperatorSpecs.createPartialJoinOperatorSpec(thisPartialJoinFn, otherPartialJoinFn, 1000 * 60, joinOutput, 1);
assertEquals(partialJoinSpec.getNextStream(), joinOutput);
assertEquals(partialJoinSpec.getThisPartialJoinFn(), thisPartialJoinFn);
assertEquals(partialJoinSpec.getOtherPartialJoinFn(), otherPartialJoinFn);
}
Aggregations