d7c2edadce398b0cff0762c22e4fbc026f649d4e
[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
32 import java.io.IOException;
33 import java.util.HashSet;
34 import java.util.Set;
35
36 import javax.transaction.Transactional;
37
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;
49
50
51 @RunWith(SpringRunner.class)
52 @SpringBootTest(classes = Application.class)
53 public class DeployFlowTestItCase {
54     private Gson gson = new Gson();
55
56     @Autowired
57     CamelContext camelContext;
58
59     @Autowired
60     LoopService loopService;
61
62     @Test
63     @Transactional
64     public void deployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
65         Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
66                  "UUID-blueprint");
67         LoopTemplate template = new LoopTemplate();
68         template.setName("templateName");
69         template.setBlueprint("yamlcontent");
70         loopTest.setLoopTemplate(template);
71         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
72                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
73                 "{\"param1\":\"value1\"}", true);
74         loopTest.addMicroServicePolicy(microServicePolicy);
75         loopService.saveOrUpdateLoop(loopTest);
76         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
77             .withProperty("loopObject", loopTest).build();
78
79         camelContext.createProducerTemplate()
80             .send("direct:deploy-loop", myCamelExchange);
81
82         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
83         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNotNull();
84         assertThat(loopAfterTest.getDcaeDeploymentId()).isNotNull();
85     }
86
87     @Test
88     @Transactional
89     public void deployWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
90         Loop loopTest2 = createLoop("ControlLoopTest2", "<xml></xml>", "yamlcontent", "{\"dcaeDeployParameters\": {"
91             + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName1_tca\"},"
92             + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName2_tca\"}"
93             + "}}", "UUID-blueprint");
94         LoopTemplate template = new LoopTemplate();
95         template.setName("templateName");
96         loopTest2.setLoopTemplate(template);
97         MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "",
98                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
99                 "{\"param1\":\"value1\"}", true);
100         MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "",
101                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
102                 "{\"param1\":\"value1\"}", true);
103         loopTest2.addMicroServicePolicy(microServicePolicy1);
104         loopTest2.addMicroServicePolicy(microServicePolicy2);
105         loopService.saveOrUpdateLoop(loopTest2);
106         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
107             .withProperty("loopObject", loopTest2).build();
108
109         camelContext.createProducerTemplate()
110             .send("direct:deploy-loop", myCamelExchange);
111
112         Loop loopAfterTest = loopService.getLoop("ControlLoopTest2");
113         Set<MicroServicePolicy> policyList = loopAfterTest.getMicroServicePolicies();
114         for (MicroServicePolicy policy : policyList) {
115             assertThat(policy.getDcaeDeploymentStatusUrl()).isNotNull();
116             assertThat(policy.getDcaeDeploymentId()).isNotNull();
117         }
118         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNull();
119         assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
120     }
121
122     @Test
123     @Transactional
124     public void undeployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
125         Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
126                  "UUID-blueprint");
127         LoopTemplate template = new LoopTemplate();
128         template.setName("templateName");
129         template.setBlueprint("yamlcontent");
130         loopTest.setLoopTemplate(template);
131         loopTest.setDcaeDeploymentId("testDeploymentId");
132         loopTest.setDcaeDeploymentStatusUrl("testUrl");
133         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
134                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
135                 "{\"param1\":\"value1\"}", true);
136         loopTest.addMicroServicePolicy(microServicePolicy);
137         loopService.saveOrUpdateLoop(loopTest);
138         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
139             .withProperty("loopObject", loopTest).build();
140
141         camelContext.createProducerTemplate()
142             .send("direct:undeploy-loop", myCamelExchange);
143
144         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
145         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
146     }
147
148     @Test
149     @Transactional
150     public void undeployWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
151         Loop loopTest2 = createLoop("ControlLoopTest2", "<xml></xml>", "yamlcontent", "{\"dcaeDeployParameters\": {"
152             + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName1_tca\"},"
153             + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName2_tca\"}"
154             + "}}", "UUID-blueprint");
155         LoopTemplate template = new LoopTemplate();
156         template.setName("templateName");
157         loopTest2.setLoopTemplate(template);
158         MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "",
159                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
160                 "{\"param1\":\"value1\"}", true, "testDeploymentId1", "testDeploymentStatusUrl1");
161         MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "",
162                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
163                 "{\"param1\":\"value1\"}", true, "testDeploymentId2", "testDeploymentStatusUrl2");
164         loopTest2.addMicroServicePolicy(microServicePolicy1);
165         loopTest2.addMicroServicePolicy(microServicePolicy2);
166         loopService.saveOrUpdateLoop(loopTest2);
167         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
168             .withProperty("loopObject", loopTest2).build();
169
170         camelContext.createProducerTemplate()
171             .send("direct:undeploy-loop", myCamelExchange);
172
173         Loop loopAfterTest = loopService.getLoop("ControlLoopTest2");
174         Set<MicroServicePolicy> policyList = loopAfterTest.getMicroServicePolicies();
175         for (MicroServicePolicy policy : policyList) {
176             assertThat(policy.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
177         }
178         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNull();
179         assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
180     }
181
182     private Loop createLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
183             String dcaeBlueprintId) throws JsonSyntaxException, IOException {
184         Loop loop = new Loop(name, blueprint, svgRepresentation);
185         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
186         loop.setLastComputedState(LoopState.DESIGN);
187         loop.setDcaeBlueprintId(dcaeBlueprintId);
188         return loop;
189     }
190
191     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
192             String policyTosca, String jsonProperties, boolean shared) {
193         MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared,
194                 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
195         microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
196         return microService;
197     }
198
199     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
200             String policyTosca, String jsonProperties, boolean shared, String deploymengId, String deploymentStatusUrl) {
201         MicroServicePolicy microService = getMicroServicePolicy(name, modelType, jsonRepresentation,
202                 policyTosca, jsonProperties, shared);
203
204         microService.setDcaeDeploymentId(deploymengId);
205         microService.setDcaeDeploymentStatusUrl(deploymentStatusUrl);
206         return microService;
207     }
208 }