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;
32 import java.io.IOException;
33 import java.util.HashSet;
36 import javax.transaction.Transactional;
38 import org.apache.camel.CamelContext;
39 import org.apache.camel.Exchange;
40 import org.apache.camel.builder.ExchangeBuilder;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.onap.clamp.clds.Application;
44 import org.onap.clamp.loop.template.LoopTemplate;
45 import org.onap.clamp.policy.microservice.MicroServicePolicy;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.test.context.junit4.SpringRunner;
50 @RunWith(SpringRunner.class)
51 @SpringBootTest(classes = Application.class)
52 public class DeployFlowTestItCase {
53 private Gson gson = new Gson();
56 CamelContext camelContext;
59 LoopService loopService;
63 public void deployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
64 Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
66 LoopTemplate template = new LoopTemplate();
67 template.setName("templateName");
68 template.setBlueprint("yamlcontent");
69 loopTest.setLoopTemplate(template);
70 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
71 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
72 "{\"param1\":\"value1\"}", true);
73 loopTest.addMicroServicePolicy(microServicePolicy);
74 loopService.saveOrUpdateLoop(loopTest);
75 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
78 camelContext.createProducerTemplate().send("direct:deploy-loop", myCamelExchange);
80 Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
81 assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNotNull();
82 assertThat(loopAfterTest.getDcaeDeploymentId()).isNotNull();
87 public void deployWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
88 Loop loopTest2 = createLoop("ControlLoopTest2", "<xml></xml>", "yamlcontent", "{\"dcaeDeployParameters\": {"
89 + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName1_tca\"},"
90 + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName2_tca\"}"
91 + "}}", "UUID-blueprint");
92 LoopTemplate template = new LoopTemplate();
93 template.setName("templateName");
94 loopTest2.setLoopTemplate(template);
95 MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "", "{\"configtype\":\"json\"}",
96 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true);
97 MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "", "{\"configtype\":\"json\"}",
98 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true);
99 loopTest2.addMicroServicePolicy(microServicePolicy1);
100 loopTest2.addMicroServicePolicy(microServicePolicy2);
101 loopService.saveOrUpdateLoop(loopTest2);
102 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest2)
105 camelContext.createProducerTemplate().send("direct:deploy-loop", myCamelExchange);
107 Loop loopAfterTest = loopService.getLoop("ControlLoopTest2");
108 Set<MicroServicePolicy> policyList = loopAfterTest.getMicroServicePolicies();
109 for (MicroServicePolicy policy : policyList) {
110 assertThat(policy.getDcaeDeploymentStatusUrl()).isNotNull();
111 assertThat(policy.getDcaeDeploymentId()).isNotNull();
113 assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNull();
114 assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
119 public void undeployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
120 Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
122 LoopTemplate template = new LoopTemplate();
123 template.setName("templateName");
124 template.setBlueprint("yamlcontent");
125 loopTest.setLoopTemplate(template);
126 loopTest.setDcaeDeploymentId("testDeploymentId");
127 loopTest.setDcaeDeploymentStatusUrl("testUrl");
128 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
129 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
130 "{\"param1\":\"value1\"}", true);
131 loopTest.addMicroServicePolicy(microServicePolicy);
132 loopService.saveOrUpdateLoop(loopTest);
133 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
136 camelContext.createProducerTemplate().send("direct:undeploy-loop", myCamelExchange);
138 Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
139 assertThat(loopAfterTest.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
144 public void undeployWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
145 Loop loopTest2 = createLoop("ControlLoopTest2", "<xml></xml>", "yamlcontent", "{\"dcaeDeployParameters\": {"
146 + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName1_tca\"},"
147 + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName2_tca\"}"
148 + "}}", "UUID-blueprint");
149 LoopTemplate template = new LoopTemplate();
150 template.setName("templateName");
151 loopTest2.setLoopTemplate(template);
152 MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "", "{\"configtype\":\"json\"}",
153 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true,
154 "testDeploymentId1", "testDeploymentStatusUrl1");
155 MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "", "{\"configtype\":\"json\"}",
156 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true,
157 "testDeploymentId2", "testDeploymentStatusUrl2");
158 loopTest2.addMicroServicePolicy(microServicePolicy1);
159 loopTest2.addMicroServicePolicy(microServicePolicy2);
160 loopService.saveOrUpdateLoop(loopTest2);
161 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest2)
164 camelContext.createProducerTemplate().send("direct:undeploy-loop", myCamelExchange);
166 Loop loopAfterTest = loopService.getLoop("ControlLoopTest2");
167 Set<MicroServicePolicy> policyList = loopAfterTest.getMicroServicePolicies();
168 for (MicroServicePolicy policy : policyList) {
169 assertThat(policy.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
171 assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNull();
172 assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
175 private Loop createLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
176 String dcaeBlueprintId) throws JsonSyntaxException, IOException {
177 Loop loop = new Loop(name, svgRepresentation);
178 loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
179 loop.setLastComputedState(LoopState.DESIGN);
180 loop.setDcaeBlueprintId(dcaeBlueprintId);
184 private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
185 String policyTosca, String jsonProperties, boolean shared) {
186 MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared,
187 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
188 microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
192 private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
193 String policyTosca, String jsonProperties, boolean shared, String deploymengId,
194 String deploymentStatusUrl) {
195 MicroServicePolicy microService = getMicroServicePolicy(name, modelType, jsonRepresentation, policyTosca,
196 jsonProperties, shared);
198 microService.setDcaeDeploymentId(deploymengId);
199 microService.setDcaeDeploymentStatusUrl(deploymentStatusUrl);