Sonar Critical Fix
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / test / java / org / onap / dcae / apod / analytics / cdap / tca / worker / TCADMaaPSubscriberWorkerTest.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.tca.worker;
22
23 import co.cask.cdap.api.app.ApplicationSpecification;
24 import co.cask.cdap.api.worker.WorkerConfigurer;
25 import co.cask.cdap.api.worker.WorkerContext;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.onap.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
30 import org.onap.dcae.apod.analytics.cdap.common.exception.CDAPSettingsException;
31 import org.onap.dcae.apod.analytics.cdap.tca.BaseAnalyticsCDAPTCAUnitTest;
32
33 import static org.mockito.ArgumentMatchers.anyString;
34 import static org.mockito.ArgumentMatchers.eq;
35 import static org.mockito.Mockito.doNothing;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.times;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.when;
40
41 /**
42  * @author Rajiv Singla . Creation Date: 12/20/2016.
43  */
44 public class TCADMaaPSubscriberWorkerTest extends BaseAnalyticsCDAPTCAUnitTest {
45
46     private static final String TEST_SUBSCRIBER_OUTPUT_STREAM_NAME = "testSubscriberOutputStream";
47
48     private WorkerConfigurer workerConfigurer;
49     private WorkerContext workerContext;
50     private TCADMaaPSubscriberWorker subscriberWorker;
51     private ApplicationSpecification mockApplicationSpecification;
52
53     @Before
54     public void before() throws Exception {
55         workerConfigurer = mock(WorkerConfigurer.class);
56         workerContext = mock(WorkerContext.class);
57         mockApplicationSpecification = Mockito.mock(ApplicationSpecification.class);
58         when(workerContext.getApplicationSpecification()).thenReturn(mockApplicationSpecification);
59         doNothing().when(workerConfigurer).setName(anyString());
60         doNothing().when(workerConfigurer).setDescription(anyString());
61         subscriberWorker =
62                 new TCADMaaPSubscriberWorker(TEST_SUBSCRIBER_OUTPUT_STREAM_NAME);
63
64     }
65
66     @Test
67     public void testConfigure() throws Exception {
68         subscriberWorker.configure(workerConfigurer);
69         verify(workerConfigurer, times(1))
70                 .setName(eq(CDAPComponentsConstants.TCA_FIXED_DMAAP_SUBSCRIBER_WORKER));
71         verify(workerConfigurer, times(1))
72                 .setDescription(eq(CDAPComponentsConstants.TCA_FIXED_DMAAP_SUBSCRIBER_DESCRIPTION_WORKER));
73     }
74
75     @Test(expected = CDAPSettingsException.class)
76     public void testInitializeWhenSettingsHaveErrors() throws Exception {
77         when(mockApplicationSpecification.getConfiguration()).thenReturn("{}");
78         subscriberWorker.initialize(workerContext);
79     }
80
81     @Test
82     public void testInitializeWhenSettingsAreValid() throws Exception {
83         when(workerContext.getRuntimeArguments()).thenReturn(getPreferenceMap());
84         when(mockApplicationSpecification.getConfiguration()).thenReturn(fromStream(TCA_APP_CONFIG_FILE_LOCATION));
85         subscriberWorker.initialize(workerContext);
86     }
87
88 }