7b4f72b69816c159aab9430c25373e05c92d4346
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / test / java / org / openecomp / dcae / apod / analytics / cdap / tca / utils / CDAPTCAUtilsTest.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.cdap.tca.utils;\r
22 \r
23 import co.cask.cdap.api.RuntimeContext;\r
24 import co.cask.cdap.api.app.ApplicationSpecification;\r
25 import org.junit.Test;\r
26 import org.mockito.Mockito;\r
27 import org.openecomp.dcae.apod.analytics.cdap.tca.BaseAnalyticsCDAPTCAUnitTest;\r
28 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;\r
29 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;\r
30 \r
31 import java.util.Map;\r
32 \r
33 import static org.hamcrest.CoreMatchers.is;\r
34 import static org.junit.Assert.assertEquals;\r
35 import static org.junit.Assert.assertThat;\r
36 import static org.mockito.Mockito.mock;\r
37 import static org.mockito.Mockito.when;\r
38 \r
39 /**\r
40  * @author Rajiv Singla . Creation Date: 11/9/2016.\r
41  */\r
42 public class CDAPTCAUtilsTest extends BaseAnalyticsCDAPTCAUnitTest {\r
43 \r
44     @Test\r
45     public void testGetValidatedTCAAppPreferences() throws Exception {\r
46         RuntimeContext runtimeContext = mock(RuntimeContext.class);\r
47         final Map<String, String> preferenceMap = getPreferenceMap();\r
48         preferenceMap.remove("subscriberHostName");\r
49         preferenceMap.remove("publisherHostName");\r
50         when(runtimeContext.getRuntimeArguments()).thenReturn(preferenceMap);\r
51         ApplicationSpecification mockApplicationSpecification = Mockito.mock(ApplicationSpecification.class);\r
52         when(mockApplicationSpecification.getConfiguration()).thenReturn(fromStream(TCA_APP_CONFIG_FILE_LOCATION));\r
53         when(runtimeContext.getApplicationSpecification()).thenReturn(mockApplicationSpecification);\r
54         TCAAppPreferences validatedTCAAppPreferences = CDAPTCAUtils.getValidatedTCAAppPreferences(runtimeContext);\r
55         assertEquals(validatedTCAAppPreferences.getSubscriberHostName(), "HOSTNAME");\r
56     }\r
57 \r
58     @Test\r
59     public void testConvertRuntimeContextToTCAPolicy() throws Exception {\r
60 \r
61         final TCAPolicy tcaPolicy =\r
62                 CDAPTCAUtils.getValidatedTCAPolicyPreferences(getTestFlowletContextWithValidPolicy());\r
63         assertThat("Policy Domain must be measurementsForVfScaling",\r
64                 tcaPolicy.getDomain(), is("measurementsForVfScaling"));\r
65         assertThat("Policy must have 2 metrics per functional roles",\r
66                 tcaPolicy.getMetricsPerEventName().size(), is(2));\r
67     }\r
68 \r
69     @Test\r
70     public void testConvertRuntimeContextToTCAPolicyFromJSON() throws Exception {\r
71 \r
72         final TCAPolicy tcaPolicy =\r
73                 CDAPTCAUtils.getValidatedTCAPolicyPreferences(getTestFlowletContextWithValidPolicyFromJSON());\r
74         assertThat("Policy Domain must be measurementsForVfScaling",\r
75                 tcaPolicy.getDomain(), is("measurementsForVfScaling"));\r
76         assertThat("Policy must have 2 metrics per functional roles",\r
77                 tcaPolicy.getMetricsPerEventName().size(), is(2));\r
78     }\r
79 \r
80 }\r