Merge "Update sonar settings"
[clamp.git] / src / test / java / org / onap / clamp / loop / LoopControllerTestItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights
6  *                             reserved.
7  * Modifications Copyright (C) 2019 Huawei Technologies Co., Ltd.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END============================================
21  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.loop;
26
27 import static org.assertj.core.api.Assertions.assertThat;
28
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonElement;
31 import com.google.gson.JsonObject;
32 import com.google.gson.JsonParser;
33
34 import java.util.Set;
35 import javax.transaction.Transactional;
36
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41
42 import org.onap.clamp.clds.Application;
43 import org.onap.clamp.clds.util.JsonUtils;
44
45 import org.onap.clamp.policy.microservice.MicroServicePolicy;
46 import org.onap.clamp.policy.microservice.MicroservicePolicyService;
47 import org.onap.clamp.policy.operational.OperationalPolicy;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.test.context.junit4.SpringRunner;
51
52 @RunWith(SpringRunner.class)
53 @SpringBootTest(classes = Application.class)
54 public class LoopControllerTestItCase {
55
56     private static final String EXAMPLE_LOOP_NAME = "ClosedLoopTest";
57     private static final String EXAMPLE_JSON = "{\"testName\":\"testValue\"}";
58
59     @Autowired
60     LoopService loopService;
61
62     @Autowired
63     LoopsRepository loopsRepository;
64
65     @Autowired
66     MicroservicePolicyService microServicePolicyService;
67
68     @Autowired
69     LoopController loopController;
70
71     private void saveTestLoopToDb() {
72         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
73         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
74         loopService.saveOrUpdateLoop(testLoop);
75     }
76
77     private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
78         return new Loop(loopName, loopBlueprint, loopSvg);
79     }
80
81     @Before
82     public void setUp() {
83         saveTestLoopToDb();
84     }
85
86     @After
87     public void tearDown() {
88         loopsRepository.deleteAll();
89     }
90
91     @Test
92     public void testUpdateOperationalPolicies() {
93         String policy = "[{\"name\":\"OPERATIONAL_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules\","
94                 + "\"configurationsJson\":{\"guard_policies\":{},"
95                 + "\"operational_policy\":{\"controlLoop\":{\"trigger_policy\":\"unique-policy-id-1-modifyConfig\","
96                 + "\"timeout\":\"3600\",\"abatement\":\"false\","
97                 + "\"controlLoopName\":\"LOOP_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules\"},"
98                 + "\"policies\":[{\"id\":\"unique-policy-id-1-modifyConfig\",\"recipe\":\"ModifyConfig\","
99                 + "\"retry\":\"2\",\"timeout\":\"1200\",\"actor\":\"APPC\",\"payload\":\"{\\\"active-streams\\\":5}\","
100                 + "\"success\":\"\",\"failure\":\"\",\"failure_timeout\":\"\",\"failure_retries\":\"\","
101                 + "\"failure_exception\":\"\",\"failure_guard\":\"\",\"target\":{\"type\":\"VNF\","
102                 + "\"resourceID\":\"vFW_PG_T1\"}}]}}}]";
103         JsonParser parser = new JsonParser();
104         JsonElement ele = parser.parse(policy);
105         JsonArray arr = ele.getAsJsonArray();
106         Loop loop = loopController.updateOperationalPolicies(EXAMPLE_LOOP_NAME, arr);
107         assertThat(loop.getOperationalPolicies()).hasSize(1);
108         Set<OperationalPolicy> opSet = loop.getOperationalPolicies();
109         OperationalPolicy op = opSet.iterator().next();
110         assertThat(op.getName()).isEqualTo("OPERATIONAL_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules");
111     }
112
113     @Test
114     @Transactional
115     public void testUpdateGlobalProperties() {
116         String policy = "{\"dcaeDeployParameters\":{\"aaiEnrichmentHost\":\"aai.onap.svc.cluster.local\","
117                 + "\"aaiEnrichmentPort\":\"8443\",\"enableAAIEnrichment\":\"false\",\"dmaap_host\":\"message-router"
118                 + ".onap\",\"dmaap_port\":\"3904\",\"enableRedisCaching\":\"false\",\"redisHosts\":\"dcae-redis.onap"
119                 + ".svc.cluster.local:6379\",\"tag_version\":\"nexus3.onap.org:10001/onap/org.onap.dcaegen2.deployments"
120                 + ".tca-cdap-container:1.1.1\",\"consul_host\":\"consul-server.onap\",\"consul_port\":\"8500\","
121                 + "\"cbs_host\":\"config-binding-service\",\"cbs_port\":\"10000\",\"external_port\":\"32012\","
122                 + "\"policy_model_id\":\"onap.policies.monitoring.cdap.tca.hi.lo.app\","
123                 + "\"policy_id\":\"tca_k8s_CLTCA_v1_0_vFW_PG_T10_k8s-tca-clamp-policy-05162019\"}}";
124         JsonParser parser = new JsonParser();
125         JsonElement ele = parser.parse(policy);
126         JsonObject obj = ele.getAsJsonObject();
127         loopController.updateGlobalPropertiesJson(EXAMPLE_LOOP_NAME, obj);
128         Loop loop = loopController.getLoop(EXAMPLE_LOOP_NAME);
129         JsonObject globalPropertiesJson = loop.getGlobalPropertiesJson();
130         JsonObject prop = globalPropertiesJson.getAsJsonObject("dcaeDeployParameters");
131         assertThat(prop.get("aaiEnrichmentHost").getAsString()).isEqualTo("aai.onap.svc.cluster.local");
132     }
133
134     @Test
135     @Transactional
136     public void testUpdateMicroservicePolicy() {
137         MicroServicePolicy policy = new MicroServicePolicy("policyName", "",
138                                                       "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
139                                                       JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
140         loopController.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, policy);
141         assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
142     }
143
144     @Test
145     @Transactional
146     public void testGetSvgRepresentation() {
147         String svgRepresentation =  loopController.getSvgRepresentation(EXAMPLE_LOOP_NAME);
148         assertThat(svgRepresentation).isEqualTo("representation");
149     }
150 }