TCA: Replace any openecomp reference by onap
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-plugins / src / test / java / org / onap / dcae / apod / analytics / cdap / plugins / streaming / dmaap / DMaaPMRSourceTest.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
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===========================================
19  */
20
21 package org.onap.dcae.apod.analytics.cdap.plugins.streaming.dmaap;
22
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;
38
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;
43
44 /**
45  * @author Rajiv Singla . Creation Date: 1/24/2017.
46  */
47 public class DMaaPMRSourceTest extends BaseAnalyticsCDAPPluginsUnitTest {
48
49     private PipelineConfigurer pipelineConfigurer;
50
51     @Before
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));
57     }
58
59     @Test
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);
65     }
66
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);
74     }
75
76
77     @Test
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);
85
86         final TestDMaaPMRSourcePluginConfig testDMaaPMRSourcePluginConfig = getTestDMaaPMRSourcePluginConfig();
87         final DMaaPMRSource dMaaPMRSource = new DMaaPMRSource(testDMaaPMRSourcePluginConfig);
88         final JavaDStream<StructuredRecord> stream = dMaaPMRSource.getStream(streamingContext);
89         assertNotNull(stream);
90     }
91 }