e1cee341b296ab378d0d8cf0e5168c795bb969e8
[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 @RunWith(SpringRunner.class)
51 @SpringBootTest(classes = Application.class)
52 public class DeployFlowTestItCase {
53     private Gson gson = new Gson();
54
55     @Autowired
56     CamelContext camelContext;
57
58     @Autowired
59     LoopService loopService;
60
61     @Test
62     @Transactional
63     public void deployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
64         Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
65                 "UUID-blueprint");
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)
76                 .build();
77
78         camelContext.createProducerTemplate().send("direct:deploy-loop", myCamelExchange);
79
80         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
81         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNotNull();
82         assertThat(loopAfterTest.getDcaeDeploymentId()).isNotNull();
83     }
84
85     @Test
86     @Transactional
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)
103                 .build();
104
105         camelContext.createProducerTemplate().send("direct:deploy-loop", myCamelExchange);
106
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();
112         }
113         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNull();
114         assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
115     }
116
117     @Test
118     @Transactional
119     public void undeployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
120         Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
121                 "UUID-blueprint");
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)
134                 .build();
135
136         camelContext.createProducerTemplate().send("direct:undeploy-loop", myCamelExchange);
137
138         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
139         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
140     }
141
142     @Test
143     @Transactional
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)
162                 .build();
163
164         camelContext.createProducerTemplate().send("direct:undeploy-loop", myCamelExchange);
165
166         Loop loopAfterTest = loopService.getLoop("ControlLoopTest2");
167         Set<MicroServicePolicy> policyList = loopAfterTest.getMicroServicePolicies();
168         for (MicroServicePolicy policy : policyList) {
169             assertThat(policy.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
170         }
171         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNull();
172         assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
173     }
174
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         return loop;
181     }
182
183     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
184             String policyTosca, String jsonProperties, boolean shared) {
185         MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared,
186                 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
187         microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
188         return microService;
189     }
190
191     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
192             String policyTosca, String jsonProperties, boolean shared, String deploymengId,
193             String deploymentStatusUrl) {
194         MicroServicePolicy microService = getMicroServicePolicy(name, modelType, jsonRepresentation, policyTosca,
195                 jsonProperties, shared);
196
197         microService.setDcaeDeploymentId(deploymengId);
198         microService.setDcaeDeploymentStatusUrl(deploymentStatusUrl);
199         return microService;
200     }
201 }