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