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