Update get Dcae Status flow
[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
36 import javax.transaction.Transactional;
37
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.onap.clamp.clds.Application;
41 import org.onap.clamp.clds.util.JsonUtils;
42 import org.onap.clamp.loop.template.LoopTemplate;
43 import org.onap.clamp.policy.microservice.MicroServicePolicy;
44 import org.onap.clamp.policy.microservice.MicroServicePolicyService;
45 import org.onap.clamp.policy.operational.OperationalPolicy;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.test.context.junit4.SpringRunner;
49
50 @RunWith(SpringRunner.class)
51 @SpringBootTest(classes = Application.class)
52 public class LoopControllerTestItCase {
53
54     private static final String EXAMPLE_LOOP_NAME = "ClosedLoopTest";
55     private static final String EXAMPLE_JSON = "{\"testName\":\"testValue\"}";
56
57     @Autowired
58     LoopService loopService;
59
60     @Autowired
61     LoopsRepository loopsRepository;
62
63     @Autowired
64     MicroServicePolicyService microServicePolicyService;
65
66     @Autowired
67     LoopController loopController;
68
69     private void saveTestLoopToDb() {
70         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
71         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
72         LoopTemplate template =  new LoopTemplate();
73         template.setName("testTemplate");
74         testLoop.setLoopTemplate(template);
75         loopService.saveOrUpdateLoop(testLoop);
76     }
77
78     private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
79         return new Loop(loopName, loopSvg);
80     }
81
82     @Test
83     @Transactional
84     public void testUpdateOperationalPolicies() {
85         saveTestLoopToDb();
86         String policy = "[{\"name\":\"OPERATIONAL_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules\","
87                 + "\"configurationsJson\":{\"guard_policies\":{},"
88                 + "\"operational_policy\":{\"controlLoop\":{\"trigger_policy\":\"unique-policy-id-1-modifyConfig\","
89                 + "\"timeout\":\"3600\",\"abatement\":\"false\","
90                 + "\"controlLoopName\":\"LOOP_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules\"},"
91                 + "\"policies\":[{\"id\":\"unique-policy-id-1-modifyConfig\",\"recipe\":\"ModifyConfig\","
92                 + "\"retry\":\"2\",\"timeout\":\"1200\",\"actor\":\"APPC\",\"payload\":\"{\\\"active-streams\\\":5}\","
93                 + "\"success\":\"\",\"failure\":\"\",\"failure_timeout\":\"\",\"failure_retries\":\"\","
94                 + "\"failure_exception\":\"\",\"failure_guard\":\"\",\"target\":{\"type\":\"VNF\","
95                 + "\"resourceID\":\"vFW_PG_T1\"}}]}}}]";
96         JsonParser parser = new JsonParser();
97         JsonElement ele = parser.parse(policy);
98         JsonArray arr = ele.getAsJsonArray();
99         Loop loop = loopController.updateOperationalPolicies(EXAMPLE_LOOP_NAME, arr);
100         assertThat(loop.getOperationalPolicies()).hasSize(1);
101         Set<OperationalPolicy> opSet = loop.getOperationalPolicies();
102         OperationalPolicy op = opSet.iterator().next();
103         assertThat(op.getName()).isEqualTo("OPERATIONAL_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules");
104     }
105
106     @Test
107     @Transactional
108     public void testUpdateGlobalProperties() {
109         saveTestLoopToDb();
110         String policy = "{\"dcaeDeployParameters\":{\"aaiEnrichmentHost\":\"aai.onap.svc.cluster.local\","
111                 + "\"aaiEnrichmentPort\":\"8443\",\"enableAAIEnrichment\":\"false\",\"dmaap_host\":\"message-router"
112                 + ".onap\",\"dmaap_port\":\"3904\",\"enableRedisCaching\":\"false\",\"redisHosts\":\"dcae-redis.onap"
113                 + ".svc.cluster.local:6379\",\"tag_version\":\"nexus3.onap.org:10001/onap/org.onap.dcaegen2.deployments"
114                 + ".tca-cdap-container:1.1.1\",\"consul_host\":\"consul-server.onap\",\"consul_port\":\"8500\","
115                 + "\"cbs_host\":\"config-binding-service\",\"cbs_port\":\"10000\",\"external_port\":\"32012\","
116                 + "\"policy_model_id\":\"onap.policies.monitoring.cdap.tca.hi.lo.app\","
117                 + "\"policy_id\":\"tca_k8s_CLTCA_v1_0_vFW_PG_T10_k8s-tca-clamp-policy-05162019\"}}";
118         JsonParser parser = new JsonParser();
119         JsonElement ele = parser.parse(policy);
120         JsonObject obj = ele.getAsJsonObject();
121         loopController.updateGlobalPropertiesJson(EXAMPLE_LOOP_NAME, obj);
122         Loop loop = loopController.getLoop(EXAMPLE_LOOP_NAME);
123         JsonObject globalPropertiesJson = loop.getGlobalPropertiesJson();
124         JsonObject prop = globalPropertiesJson.getAsJsonObject("dcaeDeployParameters");
125         assertThat(prop.get("aaiEnrichmentHost").getAsString()).isEqualTo("aai.onap.svc.cluster.local");
126     }
127
128     @Test
129     @Transactional
130     public void testUpdateMicroservicePolicy() {
131         saveTestLoopToDb();
132         MicroServicePolicy policy = new MicroServicePolicy("policyName", "",
133                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
134                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
135         loopController.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, policy);
136         assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
137     }
138
139     @Test
140     @Transactional
141     public void testGetSvgRepresentation() {
142         saveTestLoopToDb();
143         String svgRepresentation = loopController.getSvgRepresentation(EXAMPLE_LOOP_NAME);
144         assertThat(svgRepresentation).isEqualTo("representation");
145     }
146 }