2 * ===============================LICENSE_START======================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============================LICENSE_END===========================================
21 package org.onap.dcae.apod.analytics.cdap.plugins.streaming.dmaap;
23 import co.cask.cdap.api.data.format.StructuredRecord;
24 import co.cask.cdap.api.data.schema.Schema;
25 import co.cask.cdap.etl.api.PipelineConfigurer;
26 import co.cask.cdap.etl.api.StageConfigurer;
27 import co.cask.cdap.etl.api.streaming.StreamingContext;
28 import org.apache.spark.streaming.api.java.JavaDStream;
29 import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
30 import org.apache.spark.streaming.api.java.JavaStreamingContext;
31 import org.apache.spark.streaming.receiver.Receiver;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.onap.dcae.apod.analytics.cdap.common.exception.CDAPSettingsException;
36 import org.onap.dcae.apod.analytics.cdap.plugins.BaseAnalyticsCDAPPluginsUnitTest;
37 import org.onap.dcae.apod.analytics.cdap.plugins.domain.config.dmaap.TestDMaaPMRSourcePluginConfig;
39 import static org.junit.Assert.assertNotNull;
40 import static org.mockito.ArgumentMatchers.any;
41 import static org.mockito.Mockito.doNothing;
42 import static org.mockito.Mockito.when;
45 * @author Rajiv Singla . Creation Date: 1/24/2017.
47 public class DMaaPMRSourceTest extends BaseAnalyticsCDAPPluginsUnitTest {
49 private PipelineConfigurer pipelineConfigurer;
52 public void before() {
53 pipelineConfigurer = Mockito.mock(PipelineConfigurer.class);
54 final StageConfigurer stageConfigurer = Mockito.mock(StageConfigurer.class);
55 when(pipelineConfigurer.getStageConfigurer()).thenReturn(stageConfigurer);
56 doNothing().when(stageConfigurer).setOutputSchema(any(Schema.class));
60 public void testDMaaPMRSourceConfigurePipelineWithValidPluginSettings() throws Exception {
61 final TestDMaaPMRSourcePluginConfig testDMaaPMRSourcePluginConfig = getTestDMaaPMRSourcePluginConfig();
62 final DMaaPMRSource dMaaPMRSource = new DMaaPMRSource(testDMaaPMRSourcePluginConfig);
63 dMaaPMRSource.configurePipeline(pipelineConfigurer);
64 assertNotNull(dMaaPMRSource);
67 @Test(expected = CDAPSettingsException.class)
68 public void testDMaaPMRSourceConfigurePipelineWithInvalidPluginSettings() throws Exception {
69 final TestDMaaPMRSourcePluginConfig testDMaaPMRSourcePluginConfig = getTestDMaaPMRSourcePluginConfig();
70 // blank out DMaaP MR Source Host
71 testDMaaPMRSourcePluginConfig.setHostName(null);
72 final DMaaPMRSource dMaaPMRSource = new DMaaPMRSource(testDMaaPMRSourcePluginConfig);
73 dMaaPMRSource.configurePipeline(pipelineConfigurer);
78 @SuppressWarnings("unchecked")
79 public void testGetStream() throws Exception {
80 final StreamingContext streamingContext = Mockito.mock(StreamingContext.class);
81 final JavaStreamingContext javaStreamingContext = Mockito.mock(JavaStreamingContext.class);
82 final JavaReceiverInputDStream dMaaPMRReceiver = Mockito.mock(JavaReceiverInputDStream.class);
83 when(streamingContext.getSparkStreamingContext()).thenReturn(javaStreamingContext);
84 when(javaStreamingContext.receiverStream(any(Receiver.class))).thenReturn(dMaaPMRReceiver);
86 final TestDMaaPMRSourcePluginConfig testDMaaPMRSourcePluginConfig = getTestDMaaPMRSourcePluginConfig();
87 final DMaaPMRSource dMaaPMRSource = new DMaaPMRSource(testDMaaPMRSourcePluginConfig);
88 final JavaDStream<StructuredRecord> stream = dMaaPMRSource.getStream(streamingContext);
89 assertNotNull(stream);