Update UI to define Pdp Group
[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.util.Set;
34 import javax.transaction.Transactional;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.onap.clamp.clds.Application;
38 import org.onap.clamp.clds.util.JsonUtils;
39 import org.onap.clamp.loop.template.LoopTemplate;
40 import org.onap.clamp.loop.template.PolicyModel;
41 import org.onap.clamp.loop.template.PolicyModelsService;
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     PolicyModelsService policyModelsService;
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         LoopTemplate template =  new LoopTemplate();
75         template.setName("testTemplate");
76         testLoop.setLoopTemplate(template);
77         loopService.saveOrUpdateLoop(testLoop);
78     }
79
80     private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
81         return new Loop(loopName, loopSvg);
82     }
83
84     @Test
85     @Transactional
86     public void testUpdateOperationalPolicies() {
87         saveTestLoopToDb();
88         String policy = "[{\"name\":\"OPERATIONAL_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules\","
89                 + "\"configurationsJson\":{\"guard_policies\":{},"
90                 + "\"operational_policy\":{\"controlLoop\":{\"trigger_policy\":\"unique-policy-id-1-modifyConfig\","
91                 + "\"timeout\":\"3600\",\"abatement\":\"false\","
92                 + "\"controlLoopName\":\"LOOP_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules\"},"
93                 + "\"policies\":[{\"id\":\"unique-policy-id-1-modifyConfig\",\"recipe\":\"ModifyConfig\","
94                 + "\"retry\":\"2\",\"timeout\":\"1200\",\"actor\":\"APPC\",\"payload\":\"{\\\"active-streams\\\":5}\","
95                 + "\"success\":\"\",\"failure\":\"\",\"failure_timeout\":\"\",\"failure_retries\":\"\","
96                 + "\"failure_exception\":\"\",\"failure_guard\":\"\",\"target\":{\"type\":\"VNF\","
97                 + "\"resourceID\":\"vFW_PG_T1\"}}]}}}]";
98         JsonParser parser = new JsonParser();
99         JsonElement ele = parser.parse(policy);
100         JsonArray arr = ele.getAsJsonArray();
101         Loop loop = loopController.updateOperationalPolicies(EXAMPLE_LOOP_NAME, arr);
102         assertThat(loop.getOperationalPolicies()).hasSize(1);
103         Set<OperationalPolicy> opSet = loop.getOperationalPolicies();
104         OperationalPolicy op = opSet.iterator().next();
105         assertThat(op.getName()).isEqualTo("OPERATIONAL_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules");
106     }
107
108     @Test
109     @Transactional
110     public void testUpdateGlobalProperties() {
111         saveTestLoopToDb();
112         String policy = "{\"dcaeDeployParameters\":{\"aaiEnrichmentHost\":\"aai.onap.svc.cluster.local\","
113                 + "\"aaiEnrichmentPort\":\"8443\",\"enableAAIEnrichment\":\"false\",\"dmaap_host\":\"message-router"
114                 + ".onap\",\"dmaap_port\":\"3904\",\"enableRedisCaching\":\"false\",\"redisHosts\":\"dcae-redis.onap"
115                 + ".svc.cluster.local:6379\",\"tag_version\":\"nexus3.onap.org:10001/onap/org.onap.dcaegen2.deployments"
116                 + ".tca-cdap-container:1.1.1\",\"consul_host\":\"consul-server.onap\",\"consul_port\":\"8500\","
117                 + "\"cbs_host\":\"config-binding-service\",\"cbs_port\":\"10000\",\"external_port\":\"32012\","
118                 + "\"policy_model_id\":\"onap.policies.monitoring.cdap.tca.hi.lo.app\","
119                 + "\"policy_id\":\"tca_k8s_CLTCA_v1_0_vFW_PG_T10_k8s-tca-clamp-policy-05162019\"}}";
120         JsonParser parser = new JsonParser();
121         JsonElement ele = parser.parse(policy);
122         JsonObject obj = ele.getAsJsonObject();
123         loopController.updateGlobalPropertiesJson(EXAMPLE_LOOP_NAME, obj);
124         Loop loop = loopController.getLoop(EXAMPLE_LOOP_NAME);
125         JsonObject globalPropertiesJson = loop.getGlobalPropertiesJson();
126         JsonObject prop = globalPropertiesJson.getAsJsonObject("dcaeDeployParameters");
127         assertThat(prop.get("aaiEnrichmentHost").getAsString()).isEqualTo("aai.onap.svc.cluster.local");
128     }
129
130     @Test
131     @Transactional
132     public void testUpdateMicroservicePolicy() {
133         saveTestLoopToDb();
134         PolicyModel policyModel = new PolicyModel("",
135                 "tosca_definitions_version: tosca_simple_yaml_1_0_0","1.0.0");
136         policyModelsService.saveOrUpdatePolicyModel(policyModel);
137         MicroServicePolicy policy = new MicroServicePolicy("policyName", policyModel, false,
138                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
139         loopController.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, policy);
140         assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
141     }
142
143     @Test
144     @Transactional
145     public void testGetSvgRepresentation() {
146         saveTestLoopToDb();
147         String svgRepresentation = loopController.getSvgRepresentation(EXAMPLE_LOOP_NAME);
148         assertThat(svgRepresentation).isEqualTo("representation");
149     }
150 }