095df9d846bb37bbba091ccd2c0a1ab1a4c3653a
[clamp.git] / src / test / java / org / onap / clamp / clds / client / req / tca / TcaRequestFormatterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.client.req.tca;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30
31 import com.google.gson.JsonObject;
32 import java.io.IOException;
33 import org.junit.Test;
34 import org.onap.clamp.clds.config.ClampProperties;
35 import org.onap.clamp.clds.model.properties.ModelProperties;
36 import org.onap.clamp.clds.model.properties.Tca;
37 import org.onap.clamp.clds.model.properties.TcaItem;
38 import org.onap.clamp.clds.util.JsonUtils;
39
40 public class TcaRequestFormatterTest {
41
42     private static final String TCA_POLICY_PROPERTIES_TEMPLATE = "{"
43         + " \"domain\": \"measurementsForVfScaling\","
44         + " \"metricsPerEventName\": ["
45         + "  {"
46         + "   \"eventName\": \"???\","
47         + "   \"controlLoopSchemaType\": \"VNF\","
48         + "   \"policyScope\": \"DCAE\","
49         + "   \"policyName\": \"???\","
50         + "   \"policyVersion\": \"v0.0.1\","
51         + "   \"thresholds\": ["
52         + "   ]"
53         + "  }"
54         + " ]"
55         + "}";
56
57     @Test
58     public void shouldReturnFormattedTcaPolicyRequest() throws IOException {
59         //given
60         String service = "TestService";
61         String policy = "TestService_scope.PolicyName";
62         ClampProperties clampProperties = mock(ClampProperties.class);
63         String expectedRequestText = 
64             "{ "
65             + " \"domain\": \"measurementsForVfScaling\", "
66             + " \"metricsPerEventName\": [ "
67             + "  { "
68             + "   \"eventName\": \"vLoadBalancer\", "
69             + "   \"controlLoopSchemaType\": \"VNF\", "
70             + "   \"policyScope\": \"DCAE\", "
71             + "   \"policyName\": \"TestService_scope.PolicyName\", "
72             + "   \"policyVersion\": \"v0.0.1\", "
73             + "   \"thresholds\": [] "
74             + "  } "
75             + " ] "
76             + "}";
77     
78         JsonObject tcaPolicyPropertiesTemplate = JsonUtils.GSON
79             .fromJson(TCA_POLICY_PROPERTIES_TEMPLATE, JsonObject.class);
80
81         JsonObject expectedRequest = JsonUtils.GSON.fromJson(expectedRequestText, JsonObject.class);
82
83         ModelProperties modelProperties = mock(ModelProperties.class);
84         Tca tca = mock(Tca.class);
85         TcaItem tcaItem = mock(TcaItem.class);
86         when(clampProperties.getJsonTemplate(any(), any())).thenReturn(tcaPolicyPropertiesTemplate);
87         when(tca.getTcaItem()).thenReturn(tcaItem);
88         when(tcaItem.getEventName()).thenReturn("vLoadBalancer");
89         when(tcaItem.getControlLoopSchemaType()).thenReturn("VNF");
90
91         //when
92         JsonObject policyContent = TcaRequestFormatter
93             .createPolicyContent(clampProperties, modelProperties, service, policy, tca);
94
95         //then
96         assertThat(expectedRequest).isEqualTo(policyContent);
97     }
98 }