Fix delete flow bug
[clamp.git] / src / test / java / org / onap / clamp / loop / DeployFlowTestItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27
28 import com.google.gson.Gson;
29 import com.google.gson.JsonObject;
30 import com.google.gson.JsonSyntaxException;
31 import java.io.IOException;
32 import java.util.Set;
33 import javax.transaction.Transactional;
34 import org.apache.camel.CamelContext;
35 import org.apache.camel.Exchange;
36 import org.apache.camel.builder.ExchangeBuilder;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.onap.clamp.clds.Application;
40 import org.onap.clamp.loop.template.LoopTemplate;
41 import org.onap.clamp.loop.template.PolicyModel;
42 import org.onap.clamp.loop.template.PolicyModelsService;
43 import org.onap.clamp.policy.microservice.MicroServicePolicy;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.test.context.junit4.SpringRunner;
47
48 @RunWith(SpringRunner.class)
49 @SpringBootTest(classes = Application.class)
50 public class DeployFlowTestItCase {
51     private Gson gson = new Gson();
52
53     @Autowired
54     CamelContext camelContext;
55
56     @Autowired
57     PolicyModelsService policyModelsService;
58
59     @Autowired
60     LoopService loopService;
61
62     @Autowired
63     LoopsRepository loopsRepository;
64
65     /**
66      * This method tests a deployment a single blueprint.
67      *
68      * @throws JsonSyntaxException In case of issues
69      * @throws IOException         In case of issues
70      */
71     @Test
72     @Transactional
73     public void deployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
74         Loop loopTest = createLoop("ControlLoopTest", "yamlcontent",
75                 "{\"dcaeDeployParameters\":{\"uniqueBlueprintParameters\": {\"policy_id\": \"name\"}}}",
76                 "UUID-blueprint");
77         LoopTemplate template = new LoopTemplate();
78         template.setName("templateName");
79         template.setBlueprint("yamlcontent");
80         loopTest.setLoopTemplate(template);
81         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
82                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
83                 "{\"param1\":\"value1\"}", true);
84         loopTest.addMicroServicePolicy(microServicePolicy);
85         loopService.saveOrUpdateLoop(loopTest);
86         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
87                 .build();
88
89         camelContext.createProducerTemplate().send("direct:deploy-loop", myCamelExchange);
90
91         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
92         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNotNull();
93         assertThat(loopAfterTest.getDcaeDeploymentId()).isNotNull();
94     }
95
96     /**
97      * This method tests the deployment of multiple separated  blueprints.
98      *
99      * @throws JsonSyntaxException In case of issues
100      * @throws IOException         In case of issues
101      */
102     @Test
103     @Transactional
104     public void deployWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
105         Loop loopTest2 = createLoop("ControlLoopTest2", "yamlcontent", "{\"dcaeDeployParameters\": {"
106                 + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName1_tca\"},"
107                 + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName2_tca\"}"
108                 + "}}", "UUID-blueprint");
109         LoopTemplate template = new LoopTemplate();
110         template.setName("templateName");
111         loopTest2.setLoopTemplate(template);
112         MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "", "{\"configtype\":\"json\"}",
113                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true);
114         MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "", "{\"configtype\":\"json\"}",
115                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true);
116         loopTest2.addMicroServicePolicy(microServicePolicy1);
117         loopTest2.addMicroServicePolicy(microServicePolicy2);
118         loopsRepository.saveAndFlush(loopTest2);
119         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest2)
120                 .build();
121
122         camelContext.createProducerTemplate().send("direct:deploy-loop", myCamelExchange);
123
124         Loop loopAfterTest = loopService.getLoop("ControlLoopTest2");
125         Set<MicroServicePolicy> policyList = loopAfterTest.getMicroServicePolicies();
126         for (MicroServicePolicy policy : policyList) {
127             assertThat(policy.getDcaeDeploymentStatusUrl()).isNotNull();
128             assertThat(policy.getDcaeDeploymentId()).isNotNull();
129         }
130         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNull();
131         assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
132     }
133
134     /**
135      * This method tests the undeployment of a single blueprint.
136      *
137      * @throws JsonSyntaxException In case of issues
138      * @throws IOException         In case of issues
139      */
140     @Test
141     @Transactional
142     public void undeployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
143         Loop loopTest = createLoop("ControlLoopTest", "yamlcontent", "{\"testname\":\"testvalue\"}",
144                 "UUID-blueprint");
145         LoopTemplate template = new LoopTemplate();
146         template.setName("templateName");
147         template.setBlueprint("yamlcontent");
148         loopTest.setLoopTemplate(template);
149         loopTest.setDcaeDeploymentId("testDeploymentId");
150         loopTest.setDcaeDeploymentStatusUrl("testUrl");
151         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
152                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
153                 "{\"param1\":\"value1\"}", true);
154         loopTest.addMicroServicePolicy(microServicePolicy);
155         loopService.saveOrUpdateLoop(loopTest);
156         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
157                 .build();
158
159         camelContext.createProducerTemplate().send("direct:undeploy-loop", myCamelExchange);
160
161         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
162         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
163         assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
164     }
165
166     /**
167      * This method tests the undeployment of multiple separated blueprints.
168      *
169      * @throws JsonSyntaxException In case of issues
170      * @throws IOException         In case of issues
171      */
172     @Test
173     @Transactional
174     public void undeployWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
175         Loop loopTest2 = createLoop("ControlLoopTest2", "yamlcontent", "{\"dcaeDeployParameters\": {"
176                 + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName1_tca\"},"
177                 + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName2_tca\"}"
178                 + "}}", "UUID-blueprint");
179         LoopTemplate template = new LoopTemplate();
180         template.setName("templateName");
181         loopTest2.setLoopTemplate(template);
182         MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "", "{\"configtype\":\"json\"}",
183                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true,
184                 "testDeploymentId1", "testDeploymentStatusUrl1");
185         MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "", "{\"configtype\":\"json\"}",
186                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true,
187                 "testDeploymentId2", "testDeploymentStatusUrl2");
188         loopTest2.addMicroServicePolicy(microServicePolicy1);
189         loopTest2.addMicroServicePolicy(microServicePolicy2);
190         loopsRepository.saveAndFlush(loopTest2);
191         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest2)
192                 .build();
193
194         camelContext.createProducerTemplate().send("direct:undeploy-loop", myCamelExchange);
195
196         Loop loopAfterTest = loopService.getLoop("ControlLoopTest2");
197         Set<MicroServicePolicy> policyList = loopAfterTest.getMicroServicePolicies();
198         for (MicroServicePolicy policy : policyList) {
199             assertThat(policy.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
200             assertThat(policy.getDcaeDeploymentId()).isNull();
201             
202         }
203         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNull();
204         assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
205     }
206
207     /**
208      * This method tests the DCAE get status for a single blueprint.
209      *
210      * @throws JsonSyntaxException In case of issues
211      * @throws IOException         In case of issues
212      */
213     @Test
214     @Transactional
215     public void getStatusWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
216         Loop loopTest = createLoop("ControlLoopTest", "yamlcontent", "{\"testname\":\"testvalue\"}",
217                 "UUID-blueprint");
218         LoopTemplate template = new LoopTemplate();
219         template.setName("templateName");
220         template.setBlueprint("yamlcontent");
221         loopTest.setLoopTemplate(template);
222         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
223                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
224                 "{\"param1\":\"value1\"}", true);
225         loopTest.addMicroServicePolicy(microServicePolicy);
226         loopService.saveOrUpdateLoop(loopTest);
227         assertThat(loopTest.getComponents().size()).isEqualTo(2);
228         assertThat(loopTest.getComponent("DCAE")).isNotNull();
229         assertThat(loopTest.getComponent("POLICY")).isNotNull();
230         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
231                 .build();
232
233         camelContext.createProducerTemplate().send("direct:update-dcae-status-for-loop", myCamelExchange);
234
235         assertThat(loopTest.getComponent("DCAE").getState().getStateName()).isEqualTo("BLUEPRINT_DEPLOYED");
236
237         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
238         assertThat(loopAfterTest.getComponents().size()).isEqualTo(2);
239         assertThat(loopAfterTest.getComponent("DCAE")).isNotNull();
240         assertThat(loopAfterTest.getComponent("POLICY")).isNotNull();
241     }
242
243     /**
244      * This method tests the dcae get status for multiple blueprints.
245      *
246      * @throws JsonSyntaxException In case of issues
247      * @throws IOException         In case of issues
248      */
249     @Test
250     @Transactional
251     public void getStatusWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
252         Loop loopTest = createLoop("ControlLoopTest", "yamlcontent", "{\"testname\":\"testvalue\"}",
253                 "UUID-blueprint");
254         LoopTemplate template = new LoopTemplate();
255         template.setName("templateName");
256         loopTest.setLoopTemplate(template);
257         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
258                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
259                 "{\"param1\":\"value1\"}", true);
260         MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("configPolicyTest2", "",
261                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
262                 "{\"param1\":\"value1\"}", true);
263         loopTest.addMicroServicePolicy(microServicePolicy);
264         loopTest.addMicroServicePolicy(microServicePolicy2);
265         loopService.saveOrUpdateLoop(loopTest);
266         assertThat(loopTest.getComponents().size()).isEqualTo(3);
267         assertThat(loopTest.getComponent("DCAE")).isNull();
268         assertThat(loopTest.getComponent("DCAE_configPolicyTest")).isNotNull();
269         assertThat(loopTest.getComponent("DCAE_configPolicyTest2")).isNotNull();
270         assertThat(loopTest.getComponent("POLICY")).isNotNull();
271         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
272                 .build();
273
274         camelContext.createProducerTemplate().send("direct:update-dcae-status-for-loop", myCamelExchange);
275
276         assertThat(loopTest.getComponent("DCAE_configPolicyTest").getState().getStateName())
277                 .isEqualTo("BLUEPRINT_DEPLOYED");
278         assertThat(loopTest.getComponent("DCAE_configPolicyTest2").getState().getStateName())
279                 .isEqualTo("BLUEPRINT_DEPLOYED");
280
281         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
282         assertThat(loopAfterTest.getComponents().size()).isEqualTo(3);
283         assertThat(loopAfterTest.getComponent("DCAE")).isNull();
284         assertThat(loopAfterTest.getComponent("POLICY")).isNotNull();
285         assertThat(loopTest.getComponent("DCAE_configPolicyTest")).isNotNull();
286         assertThat(loopTest.getComponent("DCAE_configPolicyTest2")).isNotNull();
287     }
288
289     private Loop createLoop(String name, String blueprint, String globalPropertiesJson,
290                             String dcaeBlueprintId) throws JsonSyntaxException, IOException {
291         Loop loop = new Loop(name);
292         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
293         loop.setLastComputedState(LoopState.DESIGN);
294         return loop;
295     }
296
297     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
298                                                      String policyTosca, String jsonProperties, boolean shared) {
299
300         PolicyModel policyModel = new PolicyModel(modelType, policyTosca, "1.0.0");
301         policyModelsService.saveOrUpdatePolicyModel(policyModel);
302         MicroServicePolicy microService = new MicroServicePolicy(name, policyModel,
303                 shared,
304                 gson.fromJson(jsonRepresentation, JsonObject.class), null, null, null);
305
306         microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
307         return microService;
308     }
309
310     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
311                                                      String policyTosca, String jsonProperties, boolean shared,
312                                                      String deploymengId,
313                                                      String deploymentStatusUrl) {
314         MicroServicePolicy microService = getMicroServicePolicy(name, modelType, jsonRepresentation, policyTosca,
315                 jsonProperties, shared);
316
317         microService.setDcaeDeploymentId(deploymengId);
318         microService.setDcaeDeploymentStatusUrl(deploymentStatusUrl);
319         return microService;
320     }
321 }