48e746f2252184762d41e1146feed4e28edac9e8
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-plugins / src / test / java / org / openecomp / dcae / apod / analytics / cdap / plugins / batch / sink / dmaap / DMaaPMRSinkTest.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.openecomp.dcae.apod.analytics.cdap.plugins.batch.sink.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.Emitter;
26 import co.cask.cdap.etl.api.PipelineConfigurer;
27 import co.cask.cdap.etl.api.StageConfigurer;
28 import co.cask.cdap.etl.api.batch.BatchSinkContext;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.openecomp.dcae.apod.analytics.cdap.common.exception.CDAPSettingsException;
33 import org.openecomp.dcae.apod.analytics.cdap.plugins.BaseAnalyticsCDAPPluginsUnitTest;
34
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.ArgumentMatchers.eq;
37 import static org.mockito.Mockito.doNothing;
38 import static org.mockito.Mockito.times;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.when;
41
42 /**
43  * @author Rajiv Singla . Creation Date: 1/30/2017.
44  */
45 public class DMaaPMRSinkTest extends BaseAnalyticsCDAPPluginsUnitTest {
46
47     private DMaaPMRSink dMaaPMRSink;
48
49     @Before
50     public void before() {
51         dMaaPMRSink = new DMaaPMRSink(getTestDMaaPMRSinkPluginConfig());
52     }
53
54     @Test
55     public void testConfigurePipeline() throws Exception {
56         final PipelineConfigurer pipelineConfigurer = Mockito.mock(PipelineConfigurer.class);
57         final StageConfigurer stageConfigurer = Mockito.mock(StageConfigurer.class);
58         when(pipelineConfigurer.getStageConfigurer()).thenReturn(stageConfigurer);
59         when(stageConfigurer.getInputSchema()).thenReturn(getDMaaPMRSinkTestSchema());
60         dMaaPMRSink.configurePipeline(pipelineConfigurer);
61         verify(stageConfigurer, times(1)).getInputSchema();
62     }
63
64     @Test(expected = CDAPSettingsException.class)
65     public void testConfigurePipelineWithInvalidSchema() throws Exception {
66         final PipelineConfigurer pipelineConfigurer = Mockito.mock(PipelineConfigurer.class);
67         final StageConfigurer stageConfigurer = Mockito.mock(StageConfigurer.class);
68         when(pipelineConfigurer.getStageConfigurer()).thenReturn(stageConfigurer);
69         when(stageConfigurer.getInputSchema()).thenReturn(Schema.recordOf(
70                 "DMaaPMRSinkInvalidSchema",
71                 Schema.Field.of("message1", Schema.of(Schema.Type.STRING)),
72                 Schema.Field.of("field1", Schema.of(Schema.Type.STRING))
73         ));
74         dMaaPMRSink.configurePipeline(pipelineConfigurer);
75     }
76
77     @Test
78     public void testPrepareRun() throws Exception {
79         final BatchSinkContext batchSinkContext = Mockito.mock(BatchSinkContext.class);
80         dMaaPMRSink.prepareRun(batchSinkContext);
81     }
82
83     @Test
84     public void testTransform() throws Exception {
85         final StructuredRecord structuredRecord = Mockito.mock(StructuredRecord.class);
86         final Emitter emitter = Mockito.mock(Emitter.class);
87         final String incomingTestMessage = "test message";
88         when(structuredRecord.get(
89                 eq(getTestDMaaPMRSinkPluginConfig().getMessageColumnName()))).thenReturn(incomingTestMessage);
90         doNothing().when(emitter).emit(any());
91         dMaaPMRSink.transform(structuredRecord, emitter);
92     }
93
94 }