Create SVG in UI
[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 import java.io.IOException;
34 import java.util.Set;
35 import javax.transaction.Transactional;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.onap.clamp.clds.Application;
39 import org.onap.clamp.clds.util.JsonUtils;
40 import org.onap.clamp.loop.service.Service;
41 import org.onap.clamp.loop.template.LoopTemplate;
42 import org.onap.clamp.loop.template.PolicyModel;
43 import org.onap.clamp.loop.template.PolicyModelsService;
44 import org.onap.clamp.policy.microservice.MicroServicePolicy;
45 import org.onap.clamp.policy.microservice.MicroServicePolicyService;
46 import org.onap.clamp.policy.operational.OperationalPolicy;
47 import org.onap.clamp.policy.operational.OperationalPolicyService;
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     OperationalPolicyService operationalPolicyService;
70
71     @Autowired
72     PolicyModelsService policyModelsService;
73
74     @Autowired
75     LoopController loopController;
76
77     private void saveTestLoopToDb() {
78         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
79         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
80         LoopTemplate template = new LoopTemplate();
81         template.setName("testTemplate");
82         testLoop.setLoopTemplate(template);
83         Service modelService = new Service("{\"name\":\"serviceName\",\"UUID\":\"uuid\"}", "{}");
84         testLoop.setModelService(modelService);
85         loopService.saveOrUpdateLoop(testLoop);
86     }
87
88     private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
89         return new Loop(loopName);
90     }
91
92     @Test
93     @Transactional
94     public void testUpdateOperationalPolicies() {
95         saveTestLoopToDb();
96         String policy = "[{\"name\":\"OPERATIONAL_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules\","
97                 + "\"configurationsJson\":{"
98                 + "\"operational_policy\":{\"controlLoop\":{\"trigger_policy\":\"unique-policy-id-1-modifyConfig\","
99                 + "\"timeout\":\"3600\",\"abatement\":\"false\","
100                 + "\"controlLoopName\":\"LOOP_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules\"},"
101                 + "\"policies\":[{\"id\":\"unique-policy-id-1-modifyConfig\",\"recipe\":\"ModifyConfig\","
102                 + "\"retry\":\"2\",\"timeout\":\"1200\",\"actor\":\"APPC\",\"payload\":\"{\\\"active-streams\\\":5}\","
103                 + "\"success\":\"\",\"failure\":\"\",\"failure_timeout\":\"\",\"failure_retries\":\"\","
104                 + "\"failure_exception\":\"\",\"failure_guard\":\"\",\"target\":{\"type\":\"VNF\","
105                 + "\"resourceID\":\"vFW_PG_T1\"}}]}}}]";
106         JsonParser parser = new JsonParser();
107         JsonElement ele = parser.parse(policy);
108         JsonArray arr = ele.getAsJsonArray();
109         Loop loop = loopController.updateOperationalPolicies(EXAMPLE_LOOP_NAME, arr);
110         assertThat(loop.getOperationalPolicies()).hasSize(1);
111         Set<OperationalPolicy> opSet = loop.getOperationalPolicies();
112         OperationalPolicy op = opSet.iterator().next();
113         assertThat(op.getName()).isEqualTo("OPERATIONAL_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules");
114     }
115
116     @Test
117     @Transactional
118     public void testUpdateGlobalProperties() {
119         saveTestLoopToDb();
120         String policy = "{\"dcaeDeployParameters\":{\"aaiEnrichmentHost\":\"aai.onap.svc.cluster.local\","
121                 + "\"aaiEnrichmentPort\":\"8443\",\"enableAAIEnrichment\":\"false\",\"dmaap_host\":\"message-router"
122                 + ".onap\",\"dmaap_port\":\"3904\",\"enableRedisCaching\":\"false\",\"redisHosts\":\"dcae-redis.onap"
123                 + ".svc.cluster.local:6379\",\"tag_version\":\"nexus3.onap.org:10001/onap/org.onap.dcaegen2.deployments"
124                 + ".tca-cdap-container:1.1.1\",\"consul_host\":\"consul-server.onap\",\"consul_port\":\"8500\","
125                 + "\"cbs_host\":\"config-binding-service\",\"cbs_port\":\"10000\",\"external_port\":\"32012\","
126                 + "\"policy_model_id\":\"onap.policies.monitoring.cdap.tca.hi.lo.app\","
127                 + "\"policy_id\":\"tca_k8s_CLTCA_v1_0_vFW_PG_T10_k8s-tca-clamp-policy-05162019\"}}";
128         JsonParser parser = new JsonParser();
129         JsonElement ele = parser.parse(policy);
130         JsonObject obj = ele.getAsJsonObject();
131         loopController.updateGlobalPropertiesJson(EXAMPLE_LOOP_NAME, obj);
132         Loop loop = loopController.getLoop(EXAMPLE_LOOP_NAME);
133         JsonObject globalPropertiesJson = loop.getGlobalPropertiesJson();
134         JsonObject prop = globalPropertiesJson.getAsJsonObject("dcaeDeployParameters");
135         assertThat(prop.get("aaiEnrichmentHost").getAsString()).isEqualTo("aai.onap.svc.cluster.local");
136     }
137
138     @Test
139     @Transactional
140     public void testUpdateMicroservicePolicy() {
141         saveTestLoopToDb();
142         PolicyModel policyModel = new PolicyModel("testPolicyModel",
143                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0");
144         policyModelsService.saveOrUpdatePolicyModel(policyModel);
145         MicroServicePolicy policy = new MicroServicePolicy("policyName", policyModel, false,
146                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
147         loopController.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, policy);
148         assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
149     }
150
151     @Test
152     @Transactional
153     public void testAddAndRemoveOperationalPolicies() throws IOException {
154         saveTestLoopToDb();
155         PolicyModel policyModel = new PolicyModel("testPolicyModel",
156                 null, "1.0.0");
157         policyModelsService.saveOrUpdatePolicyModel(policyModel);
158
159         loopController.addOperationalPolicy(EXAMPLE_LOOP_NAME, "testPolicyModel", "1.0.0");
160
161         Loop newLoop = loopController.getLoop(EXAMPLE_LOOP_NAME);
162         Set<OperationalPolicy> opPolicyList = newLoop.getOperationalPolicies();
163         assertThat(opPolicyList.size()).isEqualTo(1);
164         for (OperationalPolicy policy : opPolicyList) {
165             assertThat(policy.getName().contains("OPERATIONAL_serviceName")).isTrue();
166             assertThat(policy.getPolicyModel().getPolicyModelType()).isEqualTo("testPolicyModel");
167             assertThat(policy.getPolicyModel().getVersion()).isEqualTo("1.0.0");
168         }
169
170         loopController.removeOperationalPolicy(EXAMPLE_LOOP_NAME, "testPolicyModel", "1.0.0");
171         Loop newLoop2 = loopController.getLoop(EXAMPLE_LOOP_NAME);
172         assertThat(newLoop2.getOperationalPolicies().size()).isEqualTo(0);
173     }
174 }