Search in sources :

Example 1 with TestExtOutputMessageEnvelope

use of org.apache.samza.operators.data.TestExtOutputMessageEnvelope in project samza by apache.

the class TestMessageStreamImpl method testFlatMapWithRelaxedTypes.

@Test
public void testFlatMapWithRelaxedTypes() {
    MessageStreamImpl<TestInputMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph);
    List<TestExtOutputMessageEnvelope> flatOuts = new ArrayList<TestExtOutputMessageEnvelope>() {

        {
            this.add(new TestExtOutputMessageEnvelope("output-key-1", 1, "output-id-001"));
            this.add(new TestExtOutputMessageEnvelope("output-key-2", 2, "output-id-002"));
            this.add(new TestExtOutputMessageEnvelope("output-key-3", 3, "output-id-003"));
        }
    };
    class MyFlatMapFunction implements FlatMapFunction<TestMessageEnvelope, TestExtOutputMessageEnvelope> {

        public final List<TestMessageEnvelope> inputMsgs = new ArrayList<>();

        @Override
        public Collection<TestExtOutputMessageEnvelope> apply(TestMessageEnvelope message) {
            inputMsgs.add(message);
            return flatOuts;
        }

        @Override
        public void init(Config config, TaskContext context) {
            inputMsgs.clear();
        }
    }
    MyFlatMapFunction xFlatMap = new MyFlatMapFunction();
    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(xFlatMap.inputMsgs.size(), 1);
    assertEquals(xFlatMap.inputMsgs.get(0), mockInput);
}
Also used : TaskContext(org.apache.samza.task.TaskContext) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) JobConfig(org.apache.samza.config.JobConfig) MapConfig(org.apache.samza.config.MapConfig) Config(org.apache.samza.config.Config) 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) FlatMapFunction(org.apache.samza.operators.functions.FlatMapFunction) TestExtOutputMessageEnvelope(org.apache.samza.operators.data.TestExtOutputMessageEnvelope) 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

ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Config (org.apache.samza.config.Config)1 JobConfig (org.apache.samza.config.JobConfig)1 MapConfig (org.apache.samza.config.MapConfig)1 TestExtOutputMessageEnvelope (org.apache.samza.operators.data.TestExtOutputMessageEnvelope)1 TestInputMessageEnvelope (org.apache.samza.operators.data.TestInputMessageEnvelope)1 TestMessageEnvelope (org.apache.samza.operators.data.TestMessageEnvelope)1 TestOutputMessageEnvelope (org.apache.samza.operators.data.TestOutputMessageEnvelope)1 FlatMapFunction (org.apache.samza.operators.functions.FlatMapFunction)1 OperatorSpec (org.apache.samza.operators.spec.OperatorSpec)1 PartialJoinOperatorSpec (org.apache.samza.operators.spec.PartialJoinOperatorSpec)1 SinkOperatorSpec (org.apache.samza.operators.spec.SinkOperatorSpec)1 StreamOperatorSpec (org.apache.samza.operators.spec.StreamOperatorSpec)1 TaskContext (org.apache.samza.task.TaskContext)1 Test (org.junit.Test)1