Fix the stage builds
[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", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
75                 "UUID-blueprint");
76         LoopTemplate template = new LoopTemplate();
77         template.setName("templateName");
78         template.setBlueprint("yamlcontent");
79         loopTest.setLoopTemplate(template);
80         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
81                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
82                 "{\"param1\":\"value1\"}", true);
83         loopTest.addMicroServicePolicy(microServicePolicy);
84         loopService.saveOrUpdateLoop(loopTest);
85         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
86                 .build();
87
88         camelContext.createProducerTemplate().send("direct:deploy-loop", myCamelExchange);
89
90         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
91         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNotNull();
92         assertThat(loopAfterTest.getDcaeDeploymentId()).isNotNull();
93     }
94
95     /**
96      * This method tests the deployment of multiple separated  blueprints.
97      *
98      * @throws JsonSyntaxException In case of issues
99      * @throws IOException In case of issues
100      */
101     @Test
102     @Transactional
103     public void deployWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
104         Loop loopTest2 = createLoop("ControlLoopTest2", "<xml></xml>", "yamlcontent", "{\"dcaeDeployParameters\": {"
105                 + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName1_tca\"},"
106                 + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName2_tca\"}"
107                 + "}}", "UUID-blueprint");
108         LoopTemplate template = new LoopTemplate();
109         template.setName("templateName");
110         loopTest2.setLoopTemplate(template);
111         MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "", "{\"configtype\":\"json\"}",
112                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true);
113         MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "", "{\"configtype\":\"json\"}",
114                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true);
115         loopTest2.addMicroServicePolicy(microServicePolicy1);
116         loopTest2.addMicroServicePolicy(microServicePolicy2);
117         loopsRepository.saveAndFlush(loopTest2);
118         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest2)
119                 .build();
120
121         camelContext.createProducerTemplate().send("direct:deploy-loop", myCamelExchange);
122
123         Loop loopAfterTest = loopService.getLoop("ControlLoopTest2");
124         Set<MicroServicePolicy> policyList = loopAfterTest.getMicroServicePolicies();
125         for (MicroServicePolicy policy : policyList) {
126             assertThat(policy.getDcaeDeploymentStatusUrl()).isNotNull();
127             assertThat(policy.getDcaeDeploymentId()).isNotNull();
128         }
129         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNull();
130         assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
131     }
132
133     /**
134      * This method tests the undeployment of a single blueprint.
135      *
136      * @throws JsonSyntaxException In case of issues
137      * @throws IOException In case of issues
138      */
139     @Test
140     @Transactional
141     public void undeployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
142         Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
143                 "UUID-blueprint");
144         LoopTemplate template = new LoopTemplate();
145         template.setName("templateName");
146         template.setBlueprint("yamlcontent");
147         loopTest.setLoopTemplate(template);
148         loopTest.setDcaeDeploymentId("testDeploymentId");
149         loopTest.setDcaeDeploymentStatusUrl("testUrl");
150         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
151                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
152                 "{\"param1\":\"value1\"}", true);
153         loopTest.addMicroServicePolicy(microServicePolicy);
154         loopService.saveOrUpdateLoop(loopTest);
155         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
156                 .build();
157
158         camelContext.createProducerTemplate().send("direct:undeploy-loop", myCamelExchange);
159
160         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
161         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
162     }
163
164     /**
165      * This method tests the undeployment of multiple separated blueprints.
166      *
167      * @throws JsonSyntaxException In case of issues
168      * @throws IOException In case of issues
169      */
170     @Test
171     @Transactional
172     public void undeployWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
173         Loop loopTest2 = createLoop("ControlLoopTest2", "<xml></xml>", "yamlcontent", "{\"dcaeDeployParameters\": {"
174                 + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName1_tca\"},"
175                 + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName2_tca\"}"
176                 + "}}", "UUID-blueprint");
177         LoopTemplate template = new LoopTemplate();
178         template.setName("templateName");
179         loopTest2.setLoopTemplate(template);
180         MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "", "{\"configtype\":\"json\"}",
181                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true,
182                 "testDeploymentId1", "testDeploymentStatusUrl1");
183         MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "", "{\"configtype\":\"json\"}",
184                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true,
185                 "testDeploymentId2", "testDeploymentStatusUrl2");
186         loopTest2.addMicroServicePolicy(microServicePolicy1);
187         loopTest2.addMicroServicePolicy(microServicePolicy2);
188         loopsRepository.saveAndFlush(loopTest2);
189         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest2)
190                 .build();
191
192         camelContext.createProducerTemplate().send("direct:undeploy-loop", myCamelExchange);
193
194         Loop loopAfterTest = loopService.getLoop("ControlLoopTest2");
195         Set<MicroServicePolicy> policyList = loopAfterTest.getMicroServicePolicies();
196         for (MicroServicePolicy policy : policyList) {
197             assertThat(policy.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
198         }
199         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNull();
200         assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
201     }
202
203     /**
204      * This method tests the DCAE get status for a single blueprint.
205      *
206      * @throws JsonSyntaxException In case of issues
207      * @throws IOException In case of issues
208      */
209     @Test
210     @Transactional
211     public void getStatusWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
212         Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
213                 "UUID-blueprint");
214         LoopTemplate template = new LoopTemplate();
215         template.setName("templateName");
216         template.setBlueprint("yamlcontent");
217         loopTest.setLoopTemplate(template);
218         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
219                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
220                 "{\"param1\":\"value1\"}", true);
221         loopTest.addMicroServicePolicy(microServicePolicy);
222         loopService.saveOrUpdateLoop(loopTest);
223         assertThat(loopTest.getComponents().size()).isEqualTo(2);
224         assertThat(loopTest.getComponent("DCAE")).isNotNull();
225         assertThat(loopTest.getComponent("POLICY")).isNotNull();
226         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
227                 .build();
228
229         camelContext.createProducerTemplate().send("direct:update-dcae-status-for-loop", myCamelExchange);
230
231         assertThat(loopTest.getComponent("DCAE").getState().getStateName()).isEqualTo("BLUEPRINT_DEPLOYED");
232
233         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
234         assertThat(loopAfterTest.getComponents().size()).isEqualTo(2);
235         assertThat(loopAfterTest.getComponent("DCAE")).isNotNull();
236         assertThat(loopAfterTest.getComponent("POLICY")).isNotNull();
237     }
238
239     /**
240      * This method tests the dcae get status for multiple blueprints.
241      *
242      * @throws JsonSyntaxException In case of issues
243      * @throws IOException In case of issues
244      */
245     @Test
246     @Transactional
247     public void getStatusWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
248         Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
249                 "UUID-blueprint");
250         LoopTemplate template = new LoopTemplate();
251         template.setName("templateName");
252         loopTest.setLoopTemplate(template);
253         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
254                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
255                 "{\"param1\":\"value1\"}", true);
256         MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("configPolicyTest2", "",
257                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
258                 "{\"param1\":\"value1\"}", true);
259         loopTest.addMicroServicePolicy(microServicePolicy);
260         loopTest.addMicroServicePolicy(microServicePolicy2);
261         loopService.saveOrUpdateLoop(loopTest);
262         assertThat(loopTest.getComponents().size()).isEqualTo(3);
263         assertThat(loopTest.getComponent("DCAE")).isNull();
264         assertThat(loopTest.getComponent("DCAE_configPolicyTest")).isNotNull();
265         assertThat(loopTest.getComponent("DCAE_configPolicyTest2")).isNotNull();
266         assertThat(loopTest.getComponent("POLICY")).isNotNull();
267         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
268                 .build();
269
270         camelContext.createProducerTemplate().send("direct:update-dcae-status-for-loop", myCamelExchange);
271
272         assertThat(loopTest.getComponent("DCAE_configPolicyTest").getState().getStateName())
273             .isEqualTo("BLUEPRINT_DEPLOYED");
274         assertThat(loopTest.getComponent("DCAE_configPolicyTest2").getState().getStateName())
275             .isEqualTo("BLUEPRINT_DEPLOYED");
276
277         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
278         assertThat(loopAfterTest.getComponents().size()).isEqualTo(3);
279         assertThat(loopAfterTest.getComponent("DCAE")).isNull();
280         assertThat(loopAfterTest.getComponent("POLICY")).isNotNull();
281         assertThat(loopTest.getComponent("DCAE_configPolicyTest")).isNotNull();
282         assertThat(loopTest.getComponent("DCAE_configPolicyTest2")).isNotNull();
283     }
284
285     private Loop createLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
286             String dcaeBlueprintId) throws JsonSyntaxException, IOException {
287         Loop loop = new Loop(name, svgRepresentation);
288         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
289         loop.setLastComputedState(LoopState.DESIGN);
290         return loop;
291     }
292
293     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
294             String policyTosca, String jsonProperties, boolean shared) {
295
296         PolicyModel policyModel = new PolicyModel(modelType, policyTosca,"1.0.0");
297         policyModelsService.saveOrUpdatePolicyModel(policyModel);
298         MicroServicePolicy microService = new MicroServicePolicy(name, policyModel,
299                 shared,
300                 gson.fromJson(jsonRepresentation, JsonObject.class), null, null, null);
301
302         microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
303         return microService;
304     }
305
306     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
307             String policyTosca, String jsonProperties, boolean shared, String deploymengId,
308             String deploymentStatusUrl) {
309         MicroServicePolicy microService = getMicroServicePolicy(name, modelType, jsonRepresentation, policyTosca,
310                 jsonProperties, shared);
311
312         microService.setDcaeDeploymentId(deploymengId);
313         microService.setDcaeDeploymentStatusUrl(deploymentStatusUrl);
314         return microService;
315     }
316 }