2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.loop;
26 import static org.assertj.core.api.Assertions.assertThat;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.JsonObject;
32 import java.io.IOException;
33 import java.util.HashSet;
35 import org.apache.camel.Exchange;
36 import org.apache.camel.Message;
37 import org.json.simple.parser.ParseException;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.onap.clamp.clds.Application;
42 import org.onap.clamp.loop.LoopOperation.TempLoopState;
43 import org.onap.clamp.policy.microservice.MicroServicePolicy;
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;
49 @RunWith(SpringRunner.class)
50 @SpringBootTest(classes = Application.class)
51 public class LoopOperationTestItCase {
53 private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
55 LoopService loopService;
57 private Loop createTestLoop() {
58 String yaml = "imports:\n"
59 + " - \"http://www.getcloudify.org/spec/cloudify/3.4/types.yaml\"\n"
61 + " docker_service_host:\n"
62 + " type: dcae.nodes.SelectedDockerHost";
64 Loop loopTest = new Loop("ControlLoopTest", yaml, "<xml></xml>");
65 loopTest.setGlobalPropertiesJson(new Gson().fromJson("{\"dcaeDeployParameters\":"
66 + "{\"policy_id\": \"name\"}}", JsonObject.class));
67 loopTest.setLastComputedState(LoopState.DESIGN);
68 loopTest.setDcaeDeploymentId("123456789");
69 loopTest.setDcaeDeploymentStatusUrl("http4://localhost:8085");
70 loopTest.setDcaeBlueprintId("UUID-blueprint");
72 MicroServicePolicy microServicePolicy = new MicroServicePolicy("configPolicyTest", "",
73 "tosca_definitions_version: tosca_simple_yaml_1_0_0", true,
74 gson.fromJson("{\"configtype\":\"json\"}", JsonObject.class), new HashSet<>());
75 microServicePolicy.setProperties(new Gson().fromJson("{\"param1\":\"value1\"}", JsonObject.class));
77 loopTest.addMicroServicePolicy(microServicePolicy);
83 public void testAnalysePolicyResponse() {
84 LoopOperation loopOp = new LoopOperation(loopService);
85 String status1 = loopOp.analysePolicyResponse(200);
86 String status2 = loopOp.analysePolicyResponse(404);
87 String status3 = loopOp.analysePolicyResponse(500);
88 String status4 = loopOp.analysePolicyResponse(503);
91 assertThat(status1).isEqualTo("SUBMITTED");
92 assertThat(status2).isEqualTo("NOT_SUBMITTED");
93 assertThat(status3).isEqualTo("IN_ERROR");
94 assertThat(status4).isEqualTo("IN_ERROR");
98 public void testGetOperationalPolicyName() {
99 LoopOperation loopOp = new LoopOperation(loopService);
100 Loop loop = this.createTestLoop();
101 String opName1 = loopOp.getOperationalPolicyName(loop);
102 assertThat(opName1).isNull();
104 OperationalPolicy opPolicy1 = new OperationalPolicy("OperationalPolicyTest1", null,
105 gson.fromJson("{\"type\":\"Operational\"}", JsonObject.class));
106 loop.addOperationalPolicy(opPolicy1);
107 String opName2 = loopOp.getOperationalPolicyName(loop);
108 assertThat(opName2).isEqualTo("OperationalPolicyTest1");
112 public void testAnalyseDcaeResponse() throws ParseException {
113 LoopOperation loopOp = new LoopOperation(loopService);
114 String dcaeStatus1 = loopOp.analyseDcaeResponse(null, null);
115 assertThat(dcaeStatus1).isEqualTo("NOT_DEPLOYED");
117 String dcaeStatus2 = loopOp.analyseDcaeResponse(null, 500);
118 assertThat(dcaeStatus2).isEqualTo("IN_ERROR");
120 String dcaeStatus3 = loopOp.analyseDcaeResponse(null, 404);
121 assertThat(dcaeStatus3).isEqualTo("NOT_DEPLOYED");
123 Exchange camelExchange = Mockito.mock(Exchange.class);
124 Message mockMessage = Mockito.mock(Message.class);
125 Mockito.when(camelExchange.getIn()).thenReturn(mockMessage);
126 Mockito.when(mockMessage.getBody(String.class))
127 .thenReturn("{\"operationType\":\"install\",\"status\":\"succeeded\"}");
128 String dcaeStatus4 = loopOp.analyseDcaeResponse(camelExchange, 200);
129 assertThat(dcaeStatus4).isEqualTo("DEPLOYED");
131 Mockito.when(mockMessage.getBody(String.class))
132 .thenReturn("{\"operationType\":\"install\",\"status\":\"processing\"}");
133 String dcaeStatus5 = loopOp.analyseDcaeResponse(camelExchange, 200);
134 assertThat(dcaeStatus5).isEqualTo("PROCESSING");
136 Mockito.when(mockMessage.getBody(String.class))
137 .thenReturn("{\"operationType\":\"install\",\"status\":\"failed\"}");
138 String dcaeStatus6 = loopOp.analyseDcaeResponse(camelExchange, 200);
139 assertThat(dcaeStatus6).isEqualTo("IN_ERROR");
141 Mockito.when(mockMessage.getBody(String.class))
142 .thenReturn("{\"operationType\":\"uninstall\",\"status\":\"succeeded\"}");
143 String dcaeStatus7 = loopOp.analyseDcaeResponse(camelExchange, 200);
144 assertThat(dcaeStatus7).isEqualTo("NOT_DEPLOYED");
146 Mockito.when(mockMessage.getBody(String.class))
147 .thenReturn("{\"operationType\":\"uninstall\",\"status\":\"processing\"}");
148 String dcaeStatus8 = loopOp.analyseDcaeResponse(camelExchange, 200);
149 assertThat(dcaeStatus8).isEqualTo("PROCESSING");
151 Mockito.when(mockMessage.getBody(String.class))
152 .thenReturn("{\"operationType\":\"uninstall\",\"status\":\"failed\"}");
153 String dcaeStatus9 = loopOp.analyseDcaeResponse(camelExchange, 200);
154 assertThat(dcaeStatus9).isEqualTo("IN_ERROR");
158 public void testUpdateLoopStatus() {
159 LoopOperation loopOp = new LoopOperation(loopService);
160 Loop loop = this.createTestLoop();
161 loopService.saveOrUpdateLoop(loop);
162 LoopState newState1 = loopOp.updateLoopStatus(loop, TempLoopState.SUBMITTED, TempLoopState.DEPLOYED);
163 LoopState dbState1 = loopService.getLoop(loop.getName()).getLastComputedState();
164 assertThat(newState1).isEqualTo(LoopState.DEPLOYED);
165 assertThat(dbState1).isEqualTo(LoopState.DEPLOYED);
167 LoopState newState2 = loopOp.updateLoopStatus(loop, TempLoopState.SUBMITTED, TempLoopState.NOT_DEPLOYED);
168 LoopState dbState2 = loopService.getLoop(loop.getName()).getLastComputedState();
169 assertThat(newState2).isEqualTo(LoopState.SUBMITTED);
170 assertThat(dbState2).isEqualTo(LoopState.SUBMITTED);
172 LoopState newState3 = loopOp.updateLoopStatus(loop, TempLoopState.SUBMITTED, TempLoopState.PROCESSING);
173 assertThat(newState3).isEqualTo(LoopState.WAITING);
175 LoopState newState4 = loopOp.updateLoopStatus(loop, TempLoopState.SUBMITTED, TempLoopState.IN_ERROR);
176 assertThat(newState4).isEqualTo(LoopState.IN_ERROR);
178 LoopState newState5 = loopOp.updateLoopStatus(loop, TempLoopState.NOT_SUBMITTED, TempLoopState.DEPLOYED);
179 assertThat(newState5).isEqualTo(LoopState.IN_ERROR);
181 LoopState newState6 = loopOp.updateLoopStatus(loop, TempLoopState.NOT_SUBMITTED, TempLoopState.PROCESSING);
182 assertThat(newState6).isEqualTo(LoopState.IN_ERROR);
184 LoopState newState7 = loopOp.updateLoopStatus(loop, TempLoopState.NOT_SUBMITTED, TempLoopState.NOT_DEPLOYED);
185 assertThat(newState7).isEqualTo(LoopState.DESIGN);
187 LoopState newState8 = loopOp.updateLoopStatus(loop, TempLoopState.IN_ERROR, TempLoopState.DEPLOYED);
188 assertThat(newState8).isEqualTo(LoopState.IN_ERROR);
190 LoopState newState9 = loopOp.updateLoopStatus(loop, TempLoopState.IN_ERROR, TempLoopState.NOT_DEPLOYED);
191 assertThat(newState9).isEqualTo(LoopState.IN_ERROR);
193 LoopState newState10 = loopOp.updateLoopStatus(loop, TempLoopState.IN_ERROR, TempLoopState.PROCESSING);
194 assertThat(newState10).isEqualTo(LoopState.IN_ERROR);
196 LoopState newState11 = loopOp.updateLoopStatus(loop, TempLoopState.IN_ERROR, TempLoopState.IN_ERROR);
197 assertThat(newState11).isEqualTo(LoopState.IN_ERROR);
201 public void testUpdateLoopInfo() throws ParseException {
202 Loop loop = this.createTestLoop();
203 loopService.saveOrUpdateLoop(loop);
205 Exchange camelExchange = Mockito.mock(Exchange.class);
206 Message mockMessage = Mockito.mock(Message.class);
207 Mockito.when(camelExchange.getIn()).thenReturn(mockMessage);
208 Mockito.when(mockMessage.getBody(String.class))
209 .thenReturn("{\"links\":{\"status\":\"http://testhost/dcae-operationstatus\",\"test2\":\"test2\"}}");
211 LoopOperation loopOp = new LoopOperation(loopService);
212 loopOp.updateLoopInfo(camelExchange, loop, "testNewId");
214 Loop newLoop = loopService.getLoop(loop.getName());
215 String newDeployId = newLoop.getDcaeDeploymentId();
216 String newDeploymentStatusUrl = newLoop.getDcaeDeploymentStatusUrl();
218 assertThat(newDeployId).isEqualTo("testNewId");
219 assertThat(newDeploymentStatusUrl).isEqualTo("http4://testhost/dcae-operationstatus");
223 public void testGetDeploymentId() {
224 Loop loop = this.createTestLoop();
225 LoopOperation loopOp = new LoopOperation(loopService);
226 String deploymentId1 = loopOp.getDeploymentId(loop);
227 assertThat(deploymentId1).isEqualTo("123456789");
229 loop.setDcaeDeploymentId(null);
230 String deploymentId2 = loopOp.getDeploymentId(loop);
231 assertThat(deploymentId2).isEqualTo("closedLoop_ControlLoopTest_deploymentId");
233 loop.setDcaeDeploymentId("");
234 String deploymentId3 = loopOp.getDeploymentId(loop);
235 assertThat(deploymentId3).isEqualTo("closedLoop_ControlLoopTest_deploymentId");
239 public void testGetDeployPayload() throws IOException {
240 Loop loop = this.createTestLoop();
241 LoopOperation loopOp = new LoopOperation(loopService);
242 String deploymentPayload = loopOp.getDeployPayload(loop);
244 String expectedPayload = "{\"serviceTypeId\":\"UUID-blueprint\",\"inputs\":{\"policy_id\":\"name\"}}";
245 assertThat(deploymentPayload).isEqualTo(expectedPayload);