use of org.apache.samza.operators.functions.FlatMapFunction in project samza by apache.
the class TestMessageStreamImpl method testMerge.
@Test
public void testMerge() {
StreamApplicationDescriptorImpl mockGraph = mock(StreamApplicationDescriptorImpl.class);
OperatorSpec mockOpSpec1 = mock(OperatorSpec.class);
MessageStream<TestMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph, mockOpSpec1);
// other streams have the same message type T as input stream message type M
OperatorSpec mockOpSpec2 = mock(OperatorSpec.class);
OperatorSpec mockOpSpec3 = mock(OperatorSpec.class);
Collection<MessageStream<TestMessageEnvelope>> otherStreams1 = ImmutableList.of(new MessageStreamImpl<>(mockGraph, mockOpSpec2), new MessageStreamImpl<>(mockGraph, mockOpSpec3));
inputStream.merge(otherStreams1);
ArgumentCaptor<OperatorSpec> registeredOpCaptor1 = ArgumentCaptor.forClass(OperatorSpec.class);
verify(mockOpSpec1).registerNextOperatorSpec(registeredOpCaptor1.capture());
OperatorSpec<?, TestMessageEnvelope> registeredOpSpec1 = registeredOpCaptor1.getValue();
assertTrue(registeredOpSpec1 instanceof StreamOperatorSpec);
FlatMapFunction transformFn = ((StreamOperatorSpec) registeredOpSpec1).getTransformFn();
ArgumentCaptor<OperatorSpec> registeredOpCaptor2 = ArgumentCaptor.forClass(OperatorSpec.class);
verify(mockOpSpec2).registerNextOperatorSpec(registeredOpCaptor2.capture());
OperatorSpec<?, TestMessageEnvelope> registeredOpSpec2 = registeredOpCaptor2.getValue();
ArgumentCaptor<OperatorSpec> registeredOpCaptor3 = ArgumentCaptor.forClass(OperatorSpec.class);
verify(mockOpSpec3).registerNextOperatorSpec(registeredOpCaptor3.capture());
OperatorSpec<?, TestMessageEnvelope> registeredOpSpec3 = registeredOpCaptor3.getValue();
assertEquals(registeredOpSpec1, registeredOpSpec2);
assertEquals(registeredOpSpec2, registeredOpSpec3);
assertEquals(OpCode.MERGE, registeredOpSpec1.getOpCode());
assertNotNull(transformFn);
TestMessageEnvelope mockInput = mock(TestMessageEnvelope.class);
assertTrue(transformFn.apply(mockInput).contains(mockInput));
assertEquals(1, transformFn.apply(mockInput).size());
}
use of org.apache.samza.operators.functions.FlatMapFunction in project samza by apache.
the class TestMessageStreamImpl method testFlatMapWithRelaxedTypes.
@Test
public void testFlatMapWithRelaxedTypes() {
StreamApplicationDescriptorImpl mockGraph = mock(StreamApplicationDescriptorImpl.class);
OperatorSpec mockOpSpec = mock(OperatorSpec.class);
MessageStreamImpl<TestInputMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph, mockOpSpec);
FlatMapFunction flatMapFunction = (FlatMapFunction<TestMessageEnvelope, TestOutputMessageEnvelope>) message -> Collections.emptyList();
// should compile since TestInputMessageEnvelope extends TestMessageEnvelope
inputStream.flatMap(flatMapFunction);
ArgumentCaptor<OperatorSpec> registeredOpCaptor = ArgumentCaptor.forClass(OperatorSpec.class);
verify(mockOpSpec).registerNextOperatorSpec(registeredOpCaptor.capture());
OperatorSpec<?, TestMessageEnvelope> registeredOpSpec = registeredOpCaptor.getValue();
assertTrue(registeredOpSpec instanceof StreamOperatorSpec);
assertNotNull(((StreamOperatorSpec) registeredOpSpec).getTransformFn());
assertEquals(OpCode.FLAT_MAP, registeredOpSpec.getOpCode());
}
use of org.apache.samza.operators.functions.FlatMapFunction in project samza by apache.
the class TestOperatorSpec method testStreamOperatorSpecWithMapWithFunctionReference.
@Test
public void testStreamOperatorSpecWithMapWithFunctionReference() {
MapFunction<KV<String, Object>, Object> mapFn = KV::getValue;
StreamOperatorSpec<KV<String, Object>, Object> streamOperatorSpec = OperatorSpecs.createMapOperatorSpec(mapFn, "op0");
StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope> cloneOperatorSpec = (StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope>) OperatorSpecTestUtils.copyOpSpec(streamOperatorSpec);
assertNotEquals(streamOperatorSpec, cloneOperatorSpec);
assertTrue(streamOperatorSpec.isClone(cloneOperatorSpec));
MapFunction userFn = (MapFunction) Whitebox.getInternalState(streamOperatorSpec, "mapFn");
assertEquals(userFn, mapFn);
assertNotEquals(streamOperatorSpec.getTransformFn(), cloneOperatorSpec.getTransformFn());
MapFunction clonedUserFn = (MapFunction) Whitebox.getInternalState(cloneOperatorSpec, "mapFn");
assertTrue(cloneOperatorSpec.getTransformFn() instanceof FlatMapFunction);
assertTrue(clonedUserFn instanceof MapFunction);
assertNotEquals(userFn, clonedUserFn);
}
use of org.apache.samza.operators.functions.FlatMapFunction in project samza by apache.
the class TestOperatorSpec method testStreamOperatorSpecWithMapWithEnum.
@Test
public void testStreamOperatorSpecWithMapWithEnum() {
MapFunction<TestMessageEnvelope, TestOutputMessageEnvelope> mapFn = new MapWithEnum(OperatorSpecTestUtils.TestEnum.One);
StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope> streamOperatorSpec = OperatorSpecs.createMapOperatorSpec(mapFn, "op0");
assertTrue(streamOperatorSpec instanceof MapOperatorSpec);
StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope> cloneOperatorSpec = (StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope>) OperatorSpecTestUtils.copyOpSpec(streamOperatorSpec);
assertNotEquals(streamOperatorSpec, cloneOperatorSpec);
assertTrue(streamOperatorSpec.isClone(cloneOperatorSpec));
MapFunction userFn = (MapFunction) Whitebox.getInternalState(streamOperatorSpec, "mapFn");
assertEquals(userFn, mapFn);
assertNotEquals(streamOperatorSpec.getTransformFn(), cloneOperatorSpec.getTransformFn());
MapFunction clonedUserFn = (MapFunction) Whitebox.getInternalState(cloneOperatorSpec, "mapFn");
assertTrue(cloneOperatorSpec.getTransformFn() instanceof FlatMapFunction);
assertTrue(clonedUserFn instanceof MapWithEnum);
assertNotEquals(userFn, clonedUserFn);
// originally the types should be the same
assertTrue(((MapWithEnum) userFn).getType() == ((MapWithEnum) clonedUserFn).getType());
// after changing the type of the cloned user function, the types are different now
((MapWithEnum) clonedUserFn).setType(OperatorSpecTestUtils.TestEnum.Two);
assertTrue(((MapWithEnum) userFn).getType() != ((MapWithEnum) clonedUserFn).getType());
}
use of org.apache.samza.operators.functions.FlatMapFunction in project samza by apache.
the class TestOperatorSpec method testStreamOperatorSpecWithMapAndListInClosure.
@Test
public void testStreamOperatorSpecWithMapAndListInClosure() {
List<Integer> integers = new ArrayList<>(1);
integers.add(0, 100);
List<String> keys = new ArrayList<>(1);
keys.add(0, "test-1");
MapFunction<TestMessageEnvelope, TestOutputMessageEnvelope> mapFn = m -> new TestOutputMessageEnvelope(keys.get(m.getKey().hashCode() % 1), integers.get(m.getMessage().hashCode() % 1));
StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope> streamOperatorSpec = OperatorSpecs.createMapOperatorSpec(mapFn, "op0");
StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope> cloneOperatorSpec = (StreamOperatorSpec<TestMessageEnvelope, TestOutputMessageEnvelope>) OperatorSpecTestUtils.copyOpSpec(streamOperatorSpec);
assertNotEquals(streamOperatorSpec, cloneOperatorSpec);
assertTrue(streamOperatorSpec.isClone(cloneOperatorSpec));
MapFunction userFn = (MapFunction) Whitebox.getInternalState(streamOperatorSpec, "mapFn");
assertEquals(userFn, mapFn);
assertNotEquals(streamOperatorSpec.getTransformFn(), cloneOperatorSpec.getTransformFn());
MapFunction clonedUserFn = (MapFunction) Whitebox.getInternalState(cloneOperatorSpec, "mapFn");
assertTrue(cloneOperatorSpec.getTransformFn() instanceof FlatMapFunction);
assertTrue(clonedUserFn instanceof MapFunction);
assertNotEquals(userFn, clonedUserFn);
// verify changing the values in the original keys and integers list will change the result of the original map function
TestMessageEnvelope mockImsg = new TestMessageEnvelope("input-key-x", new String("value-x"));
assertEquals(((MapFunction) userFn).apply(mockImsg), new TestOutputMessageEnvelope("test-1", 100));
integers.set(0, 200);
keys.set(0, "test-2");
assertEquals(((MapFunction) userFn).apply(mockImsg), new TestOutputMessageEnvelope("test-2", 200));
// verify that the cloned map function uses a different copy of lists and still yields the same result
assertEquals(((MapFunction) clonedUserFn).apply(mockImsg), new TestOutputMessageEnvelope("test-1", 100));
}
Aggregations