2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T 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.JsonObject;
30 import com.google.gson.JsonSyntaxException;
31 import java.io.IOException;
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;
48 @RunWith(SpringRunner.class)
49 @SpringBootTest(classes = Application.class)
50 public class DeployFlowTestItCase {
51 private Gson gson = new Gson();
54 CamelContext camelContext;
57 PolicyModelsService policyModelsService;
60 LoopService loopService;
63 LoopsRepository loopsRepository;
66 * This method tests a deployment a single blueprint.
68 * @throws JsonSyntaxException In case of issues
69 * @throws IOException In case of issues
73 public void deployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
74 Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent",
75 "{\"dcaeDeployParameters\":{\"loop template blueprint\": {\"policy_id\": \"name\"}}}",
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)
89 camelContext.createProducerTemplate().send("direct:deploy-loop", myCamelExchange);
91 Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
92 assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNotNull();
93 assertThat(loopAfterTest.getDcaeDeploymentId()).isNotNull();
97 * This method tests the deployment of multiple separated blueprints.
99 * @throws JsonSyntaxException In case of issues
100 * @throws IOException In case of issues
104 public void deployWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
105 Loop loopTest2 = createLoop("ControlLoopTest2", "<xml></xml>", "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)
122 camelContext.createProducerTemplate().send("direct:deploy-loop", myCamelExchange);
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();
130 assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNull();
131 assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
135 * This method tests the undeployment of a single blueprint.
137 * @throws JsonSyntaxException In case of issues
138 * @throws IOException In case of issues
142 public void undeployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
143 Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
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)
159 camelContext.createProducerTemplate().send("direct:undeploy-loop", myCamelExchange);
161 Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
162 assertThat(loopAfterTest.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
166 * This method tests the undeployment of multiple separated blueprints.
168 * @throws JsonSyntaxException In case of issues
169 * @throws IOException In case of issues
173 public void undeployWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
174 Loop loopTest2 = createLoop("ControlLoopTest2", "<xml></xml>", "yamlcontent", "{\"dcaeDeployParameters\": {"
175 + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName1_tca\"},"
176 + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName2_tca\"}"
177 + "}}", "UUID-blueprint");
178 LoopTemplate template = new LoopTemplate();
179 template.setName("templateName");
180 loopTest2.setLoopTemplate(template);
181 MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "", "{\"configtype\":\"json\"}",
182 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true,
183 "testDeploymentId1", "testDeploymentStatusUrl1");
184 MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "", "{\"configtype\":\"json\"}",
185 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true,
186 "testDeploymentId2", "testDeploymentStatusUrl2");
187 loopTest2.addMicroServicePolicy(microServicePolicy1);
188 loopTest2.addMicroServicePolicy(microServicePolicy2);
189 loopsRepository.saveAndFlush(loopTest2);
190 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest2)
193 camelContext.createProducerTemplate().send("direct:undeploy-loop", myCamelExchange);
195 Loop loopAfterTest = loopService.getLoop("ControlLoopTest2");
196 Set<MicroServicePolicy> policyList = loopAfterTest.getMicroServicePolicies();
197 for (MicroServicePolicy policy : policyList) {
198 assertThat(policy.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
200 assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNull();
201 assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
205 * This method tests the DCAE get status for a single blueprint.
207 * @throws JsonSyntaxException In case of issues
208 * @throws IOException In case of issues
212 public void getStatusWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
213 Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
215 LoopTemplate template = new LoopTemplate();
216 template.setName("templateName");
217 template.setBlueprint("yamlcontent");
218 loopTest.setLoopTemplate(template);
219 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
220 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
221 "{\"param1\":\"value1\"}", true);
222 loopTest.addMicroServicePolicy(microServicePolicy);
223 loopService.saveOrUpdateLoop(loopTest);
224 assertThat(loopTest.getComponents().size()).isEqualTo(2);
225 assertThat(loopTest.getComponent("DCAE")).isNotNull();
226 assertThat(loopTest.getComponent("POLICY")).isNotNull();
227 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
230 camelContext.createProducerTemplate().send("direct:update-dcae-status-for-loop", myCamelExchange);
232 assertThat(loopTest.getComponent("DCAE").getState().getStateName()).isEqualTo("BLUEPRINT_DEPLOYED");
234 Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
235 assertThat(loopAfterTest.getComponents().size()).isEqualTo(2);
236 assertThat(loopAfterTest.getComponent("DCAE")).isNotNull();
237 assertThat(loopAfterTest.getComponent("POLICY")).isNotNull();
241 * This method tests the dcae get status for multiple blueprints.
243 * @throws JsonSyntaxException In case of issues
244 * @throws IOException In case of issues
248 public void getStatusWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
249 Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
251 LoopTemplate template = new LoopTemplate();
252 template.setName("templateName");
253 loopTest.setLoopTemplate(template);
254 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
255 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
256 "{\"param1\":\"value1\"}", true);
257 MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("configPolicyTest2", "",
258 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
259 "{\"param1\":\"value1\"}", true);
260 loopTest.addMicroServicePolicy(microServicePolicy);
261 loopTest.addMicroServicePolicy(microServicePolicy2);
262 loopService.saveOrUpdateLoop(loopTest);
263 assertThat(loopTest.getComponents().size()).isEqualTo(3);
264 assertThat(loopTest.getComponent("DCAE")).isNull();
265 assertThat(loopTest.getComponent("DCAE_configPolicyTest")).isNotNull();
266 assertThat(loopTest.getComponent("DCAE_configPolicyTest2")).isNotNull();
267 assertThat(loopTest.getComponent("POLICY")).isNotNull();
268 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
271 camelContext.createProducerTemplate().send("direct:update-dcae-status-for-loop", myCamelExchange);
273 assertThat(loopTest.getComponent("DCAE_configPolicyTest").getState().getStateName())
274 .isEqualTo("BLUEPRINT_DEPLOYED");
275 assertThat(loopTest.getComponent("DCAE_configPolicyTest2").getState().getStateName())
276 .isEqualTo("BLUEPRINT_DEPLOYED");
278 Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
279 assertThat(loopAfterTest.getComponents().size()).isEqualTo(3);
280 assertThat(loopAfterTest.getComponent("DCAE")).isNull();
281 assertThat(loopAfterTest.getComponent("POLICY")).isNotNull();
282 assertThat(loopTest.getComponent("DCAE_configPolicyTest")).isNotNull();
283 assertThat(loopTest.getComponent("DCAE_configPolicyTest2")).isNotNull();
286 private Loop createLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
287 String dcaeBlueprintId) throws JsonSyntaxException, IOException {
288 Loop loop = new Loop(name, svgRepresentation);
289 loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
290 loop.setLastComputedState(LoopState.DESIGN);
294 private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
295 String policyTosca, String jsonProperties, boolean shared) {
297 PolicyModel policyModel = new PolicyModel(modelType, policyTosca,"1.0.0");
298 policyModelsService.saveOrUpdatePolicyModel(policyModel);
299 MicroServicePolicy microService = new MicroServicePolicy(name, policyModel,
301 gson.fromJson(jsonRepresentation, JsonObject.class), null, null, null);
303 microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
307 private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
308 String policyTosca, String jsonProperties, boolean shared, String deploymengId,
309 String deploymentStatusUrl) {
310 MicroServicePolicy microService = getMicroServicePolicy(name, modelType, jsonRepresentation, policyTosca,
311 jsonProperties, shared);
313 microService.setDcaeDeploymentId(deploymengId);
314 microService.setDcaeDeploymentStatusUrl(deploymentStatusUrl);