Search in sources :

Example 11 with FlatMapFunction

use of org.apache.samza.operators.functions.FlatMapFunction in project samza by apache.

the class TestMessageStreamImpl method testFilter.

@Test
public void testFilter() {
    StreamApplicationDescriptorImpl mockGraph = mock(StreamApplicationDescriptorImpl.class);
    OperatorSpec mockOpSpec = mock(OperatorSpec.class);
    MessageStreamImpl<TestMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph, mockOpSpec);
    FilterFunction<Object> mockFilterFn = mock(FilterFunction.class);
    inputStream.filter(mockFilterFn);
    ArgumentCaptor<OperatorSpec> registeredOpCaptor = ArgumentCaptor.forClass(OperatorSpec.class);
    verify(mockOpSpec).registerNextOperatorSpec(registeredOpCaptor.capture());
    OperatorSpec<?, TestMessageEnvelope> registeredOpSpec = registeredOpCaptor.getValue();
    assertTrue(registeredOpSpec instanceof StreamOperatorSpec);
    FlatMapFunction transformFn = ((StreamOperatorSpec) registeredOpSpec).getTransformFn();
    assertNotNull(transformFn);
    assertEquals(OpCode.FILTER, registeredOpSpec.getOpCode());
    Object mockInput = new Object();
    when(mockFilterFn.apply(anyObject())).thenReturn(true);
    assertTrue(transformFn.apply(mockInput).contains(mockInput));
    when(mockFilterFn.apply(anyObject())).thenReturn(false);
    assertTrue(transformFn.apply(mockInput).isEmpty());
}
Also used : StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) PartitionByOperatorSpec(org.apache.samza.operators.spec.PartitionByOperatorSpec) JoinOperatorSpec(org.apache.samza.operators.spec.JoinOperatorSpec) SendToTableOperatorSpec(org.apache.samza.operators.spec.SendToTableOperatorSpec) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) OutputOperatorSpec(org.apache.samza.operators.spec.OutputOperatorSpec) WindowOperatorSpec(org.apache.samza.operators.spec.WindowOperatorSpec) StreamTableJoinOperatorSpec(org.apache.samza.operators.spec.StreamTableJoinOperatorSpec) IntermediateMessageStreamImpl(org.apache.samza.operators.stream.IntermediateMessageStreamImpl) StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) FlatMapFunction(org.apache.samza.operators.functions.FlatMapFunction) Matchers.anyObject(org.mockito.Matchers.anyObject) Test(org.junit.Test)

Aggregations

TestMessageEnvelope (org.apache.samza.operators.data.TestMessageEnvelope)11 FlatMapFunction (org.apache.samza.operators.functions.FlatMapFunction)11 Test (org.junit.Test)11 TestOutputMessageEnvelope (org.apache.samza.operators.data.TestOutputMessageEnvelope)8 ArrayList (java.util.ArrayList)5 MapFunction (org.apache.samza.operators.functions.MapFunction)5 SinkFunction (org.apache.samza.operators.functions.SinkFunction)5 OperatorSpec (org.apache.samza.operators.spec.OperatorSpec)5 SinkOperatorSpec (org.apache.samza.operators.spec.SinkOperatorSpec)5 StreamOperatorSpec (org.apache.samza.operators.spec.StreamOperatorSpec)5 WindowOperatorSpec (org.apache.samza.operators.spec.WindowOperatorSpec)5 Collection (java.util.Collection)4 List (java.util.List)4 StreamApplicationDescriptorImpl (org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl)4 KV (org.apache.samza.operators.KV)4 JoinOperatorSpec (org.apache.samza.operators.spec.JoinOperatorSpec)4 OutputOperatorSpec (org.apache.samza.operators.spec.OutputOperatorSpec)4 PartitionByOperatorSpec (org.apache.samza.operators.spec.PartitionByOperatorSpec)4 SendToTableOperatorSpec (org.apache.samza.operators.spec.SendToTableOperatorSpec)4 StreamTableJoinOperatorSpec (org.apache.samza.operators.spec.StreamTableJoinOperatorSpec)4