Search in sources :

Example 6 with StreamOperatorSpec

use of org.apache.samza.operators.spec.StreamOperatorSpec 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);
}
Also used : ArrayList(java.util.ArrayList) StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) PartialJoinOperatorSpec(org.apache.samza.operators.spec.PartialJoinOperatorSpec) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) TestOutputMessageEnvelope(org.apache.samza.operators.data.TestOutputMessageEnvelope) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.junit.Test)

Aggregations

TestMessageEnvelope (org.apache.samza.operators.data.TestMessageEnvelope)6 OperatorSpec (org.apache.samza.operators.spec.OperatorSpec)6 PartialJoinOperatorSpec (org.apache.samza.operators.spec.PartialJoinOperatorSpec)6 SinkOperatorSpec (org.apache.samza.operators.spec.SinkOperatorSpec)6 StreamOperatorSpec (org.apache.samza.operators.spec.StreamOperatorSpec)6 Test (org.junit.Test)5 TestOutputMessageEnvelope (org.apache.samza.operators.data.TestOutputMessageEnvelope)4 ArrayList (java.util.ArrayList)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 Config (org.apache.samza.config.Config)2 MessageType (org.apache.samza.operators.data.MessageType)2 FlatMapFunction (org.apache.samza.operators.functions.FlatMapFunction)2 TaskContext (org.apache.samza.task.TaskContext)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Duration (java.time.Duration)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1