Sonar Critical Fix
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / test / java / org / onap / dcae / apod / analytics / cdap / tca / utils / CDAPTCAUtilsTest.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.utils;
22
23 import co.cask.cdap.api.RuntimeContext;
24 import co.cask.cdap.api.app.ApplicationSpecification;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.onap.dcae.apod.analytics.cdap.tca.BaseAnalyticsCDAPTCAUnitTest;
28 import org.onap.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;
29 import org.onap.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
30
31 import java.util.Map;
32
33 import static org.hamcrest.CoreMatchers.is;
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertThat;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38
39 /**
40  * @author Rajiv Singla . Creation Date: 11/9/2016.
41  */
42 public class CDAPTCAUtilsTest extends BaseAnalyticsCDAPTCAUnitTest {
43
44     @Test
45     public void testGetValidatedTCAAppPreferences() throws Exception {
46         RuntimeContext runtimeContext = mock(RuntimeContext.class);
47         final Map<String, String> preferenceMap = getPreferenceMap();
48         preferenceMap.remove("subscriberHostName");
49         preferenceMap.remove("publisherHostName");
50         when(runtimeContext.getRuntimeArguments()).thenReturn(preferenceMap);
51         ApplicationSpecification mockApplicationSpecification = Mockito.mock(ApplicationSpecification.class);
52         when(mockApplicationSpecification.getConfiguration()).thenReturn(fromStream(TCA_APP_CONFIG_FILE_LOCATION));
53         when(runtimeContext.getApplicationSpecification()).thenReturn(mockApplicationSpecification);
54         TCAAppPreferences validatedTCAAppPreferences = CDAPTCAUtils.getValidatedTCAAppPreferences(runtimeContext);
55         assertEquals(validatedTCAAppPreferences.getSubscriberHostName(), "HOSTNAME");
56     }
57
58     @Test
59     public void testConvertRuntimeContextToTCAPolicy() throws Exception {
60
61         final TCAPolicy tcaPolicy =
62                 CDAPTCAUtils.getValidatedTCAPolicyPreferences(getTestFlowletContextWithValidPolicy());
63         assertThat("Policy Domain must be measurementsForVfScaling",
64                 tcaPolicy.getDomain(), is("measurementsForVfScaling"));
65         assertThat("Policy must have 2 metrics per functional roles",
66                 tcaPolicy.getMetricsPerEventName().size(), is(2));
67     }
68
69     @Test
70     public void testConvertRuntimeContextToTCAPolicyFromJSON() throws Exception {
71
72         final TCAPolicy tcaPolicy =
73                 CDAPTCAUtils.getValidatedTCAPolicyPreferences(getTestFlowletContextWithValidPolicyFromJSON());
74         assertThat("Policy Domain must be measurementsForVfScaling",
75                 tcaPolicy.getDomain(), is("measurementsForVfScaling"));
76         assertThat("Policy must have 2 metrics per functional roles",
77                 tcaPolicy.getMetricsPerEventName().size(), is(2));
78     }
79
80 }