Search in sources :

Example 1 with TestInputMessageEnvelope

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

the class TestStreamGraphImpl method testGetOutputStream.

@Test
public void testGetOutputStream() {
    ApplicationRunner mockRunner = mock(ApplicationRunner.class);
    Config mockConfig = mock(Config.class);
    StreamSpec testStreamSpec = new StreamSpec("test-stream-1", "physical-stream-1", "test-system");
    when(mockRunner.getStreamSpec("test-stream-1")).thenReturn(testStreamSpec);
    class MyMessageType extends MessageType {

        public final String outputId;

        public MyMessageType(String value, long eventTime, String outputId) {
            super(value, eventTime);
            this.outputId = outputId;
        }
    }
    StreamGraphImpl graph = new StreamGraphImpl(mockRunner, mockConfig);
    Function<TestMessageEnvelope, String> xKeyExtractor = x -> x.getKey();
    Function<TestMessageEnvelope, MyMessageType> xMsgExtractor = x -> new MyMessageType(x.getMessage().getValue(), x.getMessage().getEventTime(), "test-output-id-1");
    OutputStream<String, MyMessageType, TestInputMessageEnvelope> mOutputStream = graph.getOutputStream("test-stream-1", xKeyExtractor, xMsgExtractor);
    assertEquals(graph.getOutputStreams().get(testStreamSpec), mOutputStream);
    assertTrue(mOutputStream instanceof OutputStreamInternalImpl);
    assertEquals(((OutputStreamInternalImpl) mOutputStream).getKeyExtractor(), xKeyExtractor);
    assertEquals(((OutputStreamInternalImpl) mOutputStream).getMsgExtractor(), xMsgExtractor);
    TestInputMessageEnvelope xInputMsg = new TestInputMessageEnvelope("test-key-1", "test-msg-1", 33333L, "input-id-1");
    assertEquals(((OutputStreamInternalImpl<String, MyMessageType, TestInputMessageEnvelope>) mOutputStream).getKeyExtractor().apply(xInputMsg), "test-key-1");
    assertEquals(((OutputStreamInternalImpl<String, MyMessageType, TestInputMessageEnvelope>) mOutputStream).getMsgExtractor().apply(xInputMsg).getValue(), "test-msg-1");
    assertEquals(((OutputStreamInternalImpl<String, MyMessageType, TestInputMessageEnvelope>) mOutputStream).getMsgExtractor().apply(xInputMsg).getEventTime(), 33333L);
    assertEquals(((OutputStreamInternalImpl<String, MyMessageType, TestInputMessageEnvelope>) mOutputStream).getMsgExtractor().apply(xInputMsg).outputId, "test-output-id-1");
}
Also used : TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MessageType(org.apache.samza.operators.data.MessageType) BiFunction(java.util.function.BiFunction) JobConfig(org.apache.samza.config.JobConfig) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) Assert.assertTrue(org.junit.Assert.assertTrue) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Function(java.util.function.Function) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) Config(org.apache.samza.config.Config) IntermediateStreamInternalImpl(org.apache.samza.operators.stream.IntermediateStreamInternalImpl) InputStreamInternalImpl(org.apache.samza.operators.stream.InputStreamInternalImpl) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) StreamSpec(org.apache.samza.system.StreamSpec) JobConfig(org.apache.samza.config.JobConfig) Config(org.apache.samza.config.Config) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MessageType(org.apache.samza.operators.data.MessageType) Test(org.junit.Test)

Example 2 with TestInputMessageEnvelope

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

the class TestStreamGraphImpl method testGetIntermediateStream.

@Test
public void testGetIntermediateStream() {
    ApplicationRunner mockRunner = mock(ApplicationRunner.class);
    Config mockConfig = mock(Config.class);
    StreamSpec testStreamSpec = new StreamSpec("myJob-i001-test-stream-1", "physical-stream-1", "test-system");
    when(mockRunner.getStreamSpec("myJob-i001-test-stream-1")).thenReturn(testStreamSpec);
    when(mockConfig.get(JobConfig.JOB_NAME())).thenReturn("myJob");
    when(mockConfig.get(JobConfig.JOB_ID(), "1")).thenReturn("i001");
    class MyMessageType extends MessageType {

        public final String outputId;

        public MyMessageType(String value, long eventTime, String outputId) {
            super(value, eventTime);
            this.outputId = outputId;
        }
    }
    StreamGraphImpl graph = new StreamGraphImpl(mockRunner, mockConfig);
    Function<TestMessageEnvelope, String> xKeyExtractor = x -> x.getKey();
    Function<TestMessageEnvelope, MyMessageType> xMsgExtractor = x -> new MyMessageType(x.getMessage().getValue(), x.getMessage().getEventTime(), "test-output-id-1");
    BiFunction<String, MessageType, TestInputMessageEnvelope> xMsgBuilder = (k, v) -> new TestInputMessageEnvelope(k, v.getValue(), v.getEventTime(), "input-id-1");
    MessageStream<TestMessageEnvelope> mIntermediateStream = graph.getIntermediateStream("test-stream-1", xKeyExtractor, xMsgExtractor, xMsgBuilder);
    assertEquals(graph.getOutputStreams().get(testStreamSpec), mIntermediateStream);
    assertTrue(mIntermediateStream instanceof IntermediateStreamInternalImpl);
    assertEquals(((IntermediateStreamInternalImpl) mIntermediateStream).getKeyExtractor(), xKeyExtractor);
    assertEquals(((IntermediateStreamInternalImpl) mIntermediateStream).getMsgExtractor(), xMsgExtractor);
    assertEquals(((IntermediateStreamInternalImpl) mIntermediateStream).getMsgBuilder(), xMsgBuilder);
    TestMessageEnvelope xInputMsg = new TestMessageEnvelope("test-key-1", "test-msg-1", 33333L);
    assertEquals(((IntermediateStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mIntermediateStream).getKeyExtractor().apply(xInputMsg), "test-key-1");
    assertEquals(((IntermediateStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mIntermediateStream).getMsgExtractor().apply(xInputMsg).getValue(), "test-msg-1");
    assertEquals(((IntermediateStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mIntermediateStream).getMsgExtractor().apply(xInputMsg).getEventTime(), 33333L);
    assertEquals(((IntermediateStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mIntermediateStream).getMsgBuilder().apply("test-key-1", new MyMessageType("test-msg-1", 33333L, "test-output-id-1")).getKey(), "test-key-1");
    assertEquals(((IntermediateStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mIntermediateStream).getMsgBuilder().apply("test-key-1", new MyMessageType("test-msg-1", 33333L, "test-output-id-1")).getMessage().getValue(), "test-msg-1");
    assertEquals(((IntermediateStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mIntermediateStream).getMsgBuilder().apply("test-key-1", new MyMessageType("test-msg-1", 33333L, "test-output-id-1")).getMessage().getEventTime(), 33333L);
}
Also used : TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MessageType(org.apache.samza.operators.data.MessageType) BiFunction(java.util.function.BiFunction) JobConfig(org.apache.samza.config.JobConfig) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) Assert.assertTrue(org.junit.Assert.assertTrue) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Function(java.util.function.Function) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) Config(org.apache.samza.config.Config) IntermediateStreamInternalImpl(org.apache.samza.operators.stream.IntermediateStreamInternalImpl) InputStreamInternalImpl(org.apache.samza.operators.stream.InputStreamInternalImpl) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) StreamSpec(org.apache.samza.system.StreamSpec) IntermediateStreamInternalImpl(org.apache.samza.operators.stream.IntermediateStreamInternalImpl) JobConfig(org.apache.samza.config.JobConfig) Config(org.apache.samza.config.Config) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MessageType(org.apache.samza.operators.data.MessageType) Test(org.junit.Test)

Example 3 with TestInputMessageEnvelope

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

the class TestOperatorSpecs method testCreateWindowOperatorWithRelaxedTypes.

@Test
public void testCreateWindowOperatorWithRelaxedTypes() throws Exception {
    Function<TestMessageEnvelope, String> keyExtractor = m -> m.getKey();
    FoldLeftFunction<TestMessageEnvelope, Integer> aggregator = (m, c) -> c + 1;
    Supplier<Integer> initialValue = () -> 0;
    //instantiate a window using reflection
    WindowInternal<TestInputMessageEnvelope, String, Integer> window = new WindowInternal(null, initialValue, aggregator, keyExtractor, null, WindowType.TUMBLING);
    MessageStreamImpl<WindowPane<String, Integer>> mockWndOut = mock(MessageStreamImpl.class);
    WindowOperatorSpec spec = OperatorSpecs.createWindowOperatorSpec(window, mockWndOut, 1);
    assertEquals(spec.getWindow(), window);
    assertEquals(spec.getWindow().getKeyExtractor(), keyExtractor);
    assertEquals(spec.getWindow().getFoldLeftFunction(), aggregator);
    // make sure that the functions with relaxed types work as expected
    TestInputMessageEnvelope inputMsg = new TestInputMessageEnvelope("test-input-key1", "test-value-1", 23456L, "input-id-1");
    assertEquals("test-input-key1", spec.getWindow().getKeyExtractor().apply(inputMsg));
    assertEquals(1, spec.getWindow().getFoldLeftFunction().apply(inputMsg, 0));
}
Also used : PartialJoinFunction(org.apache.samza.operators.functions.PartialJoinFunction) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) Function(java.util.function.Function) Supplier(java.util.function.Supplier) WindowPane(org.apache.samza.operators.windows.WindowPane) ArrayList(java.util.ArrayList) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) MessageCollector(org.apache.samza.task.MessageCollector) MessageStreamImpl(org.apache.samza.operators.MessageStreamImpl) SystemStream(org.apache.samza.system.SystemStream) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TestOutputMessageEnvelope(org.apache.samza.operators.data.TestOutputMessageEnvelope) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) WindowType(org.apache.samza.operators.windows.internal.WindowType) MessageType(org.apache.samza.operators.data.MessageType) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) FlatMapFunction(org.apache.samza.operators.functions.FlatMapFunction) TaskCoordinator(org.apache.samza.task.TaskCoordinator) StreamGraphImpl(org.apache.samza.operators.StreamGraphImpl) Matchers.any(org.mockito.Matchers.any) List(java.util.List) SinkFunction(org.apache.samza.operators.functions.SinkFunction) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) TestMessageStreamImplUtil(org.apache.samza.operators.TestMessageStreamImplUtil) WindowInternal(org.apache.samza.operators.windows.internal.WindowInternal) FoldLeftFunction(org.apache.samza.operators.functions.FoldLeftFunction) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) WindowInternal(org.apache.samza.operators.windows.internal.WindowInternal) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) WindowPane(org.apache.samza.operators.windows.WindowPane) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) Test(org.junit.Test)

Example 4 with TestInputMessageEnvelope

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

the class TestStreamGraphImpl method testGetInputStream.

@Test
public void testGetInputStream() {
    ApplicationRunner mockRunner = mock(ApplicationRunner.class);
    Config mockConfig = mock(Config.class);
    StreamSpec testStreamSpec = new StreamSpec("test-stream-1", "physical-stream-1", "test-system");
    when(mockRunner.getStreamSpec("test-stream-1")).thenReturn(testStreamSpec);
    StreamGraphImpl graph = new StreamGraphImpl(mockRunner, mockConfig);
    BiFunction<String, MessageType, TestInputMessageEnvelope> xMsgBuilder = (k, v) -> new TestInputMessageEnvelope(k, v.getValue(), v.getEventTime(), "input-id-1");
    MessageStream<TestMessageEnvelope> mInputStream = graph.getInputStream("test-stream-1", xMsgBuilder);
    assertEquals(graph.getInputStreams().get(testStreamSpec), mInputStream);
    assertTrue(mInputStream instanceof InputStreamInternalImpl);
    assertEquals(((InputStreamInternalImpl) mInputStream).getMsgBuilder(), xMsgBuilder);
    String key = "test-input-key";
    MessageType msgBody = new MessageType("test-msg-value", 333333L);
    TestMessageEnvelope xInputMsg = ((InputStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mInputStream).getMsgBuilder().apply(key, msgBody);
    assertEquals(xInputMsg.getKey(), key);
    assertEquals(xInputMsg.getMessage().getValue(), msgBody.getValue());
    assertEquals(xInputMsg.getMessage().getEventTime(), msgBody.getEventTime());
    assertEquals(((TestInputMessageEnvelope) xInputMsg).getInputId(), "input-id-1");
}
Also used : TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MessageType(org.apache.samza.operators.data.MessageType) BiFunction(java.util.function.BiFunction) JobConfig(org.apache.samza.config.JobConfig) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) Assert.assertTrue(org.junit.Assert.assertTrue) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Function(java.util.function.Function) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) Config(org.apache.samza.config.Config) IntermediateStreamInternalImpl(org.apache.samza.operators.stream.IntermediateStreamInternalImpl) InputStreamInternalImpl(org.apache.samza.operators.stream.InputStreamInternalImpl) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) StreamSpec(org.apache.samza.system.StreamSpec) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) JobConfig(org.apache.samza.config.JobConfig) Config(org.apache.samza.config.Config) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) InputStreamInternalImpl(org.apache.samza.operators.stream.InputStreamInternalImpl) MessageType(org.apache.samza.operators.data.MessageType) Test(org.junit.Test)

Example 5 with TestInputMessageEnvelope

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

the class TestStreamGraphImpl method testMultipleGetInputStream.

@Test(expected = IllegalStateException.class)
public void testMultipleGetInputStream() {
    ApplicationRunner mockRunner = mock(ApplicationRunner.class);
    Config mockConfig = mock(Config.class);
    StreamSpec testStreamSpec1 = new StreamSpec("test-stream-1", "physical-stream-1", "test-system");
    StreamSpec testStreamSpec2 = new StreamSpec("test-stream-2", "physical-stream-2", "test-system");
    StreamSpec nonExistentStreamSpec = new StreamSpec("non-existent-stream", "physical-stream-1", "test-system");
    when(mockRunner.getStreamSpec("test-stream-1")).thenReturn(testStreamSpec1);
    when(mockRunner.getStreamSpec("test-stream-2")).thenReturn(testStreamSpec2);
    StreamGraphImpl graph = new StreamGraphImpl(mockRunner, mockConfig);
    BiFunction<String, MessageType, TestInputMessageEnvelope> xMsgBuilder = (k, v) -> new TestInputMessageEnvelope(k, v.getValue(), v.getEventTime(), "input-id-1");
    //create 2 streams for the corresponding streamIds
    MessageStream<TestInputMessageEnvelope> inputStream1 = graph.getInputStream("test-stream-1", xMsgBuilder);
    MessageStream<TestInputMessageEnvelope> inputStream2 = graph.getInputStream("test-stream-2", xMsgBuilder);
    //assert that the streamGraph contains only the above 2 streams
    assertEquals(graph.getInputStreams().get(testStreamSpec1), inputStream1);
    assertEquals(graph.getInputStreams().get(testStreamSpec2), inputStream2);
    assertEquals(graph.getInputStreams().get(nonExistentStreamSpec), null);
    assertEquals(graph.getInputStreams().size(), 2);
    //should throw IllegalStateException
    graph.getInputStream("test-stream-1", xMsgBuilder);
}
Also used : TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MessageType(org.apache.samza.operators.data.MessageType) BiFunction(java.util.function.BiFunction) JobConfig(org.apache.samza.config.JobConfig) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) Assert.assertTrue(org.junit.Assert.assertTrue) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Function(java.util.function.Function) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) Config(org.apache.samza.config.Config) IntermediateStreamInternalImpl(org.apache.samza.operators.stream.IntermediateStreamInternalImpl) InputStreamInternalImpl(org.apache.samza.operators.stream.InputStreamInternalImpl) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) StreamSpec(org.apache.samza.system.StreamSpec) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) JobConfig(org.apache.samza.config.JobConfig) Config(org.apache.samza.config.Config) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) MessageType(org.apache.samza.operators.data.MessageType) Test(org.junit.Test)

Aggregations

Function (java.util.function.Function)5 MessageType (org.apache.samza.operators.data.MessageType)5 TestInputMessageEnvelope (org.apache.samza.operators.data.TestInputMessageEnvelope)5 TestMessageEnvelope (org.apache.samza.operators.data.TestMessageEnvelope)5 OutputStreamInternalImpl (org.apache.samza.operators.stream.OutputStreamInternalImpl)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Test (org.junit.Test)5 Mockito.mock (org.mockito.Mockito.mock)5 Mockito.when (org.mockito.Mockito.when)5 BiFunction (java.util.function.BiFunction)4 Config (org.apache.samza.config.Config)4 JobConfig (org.apache.samza.config.JobConfig)4 InputStreamInternalImpl (org.apache.samza.operators.stream.InputStreamInternalImpl)4 IntermediateStreamInternalImpl (org.apache.samza.operators.stream.IntermediateStreamInternalImpl)4 ApplicationRunner (org.apache.samza.runtime.ApplicationRunner)4 StreamSpec (org.apache.samza.system.StreamSpec)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1