Update deploy-loop route
[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     private Loop createLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
123             String dcaeBlueprintId) throws JsonSyntaxException, IOException {
124         Loop loop = new Loop(name, blueprint, svgRepresentation);
125         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
126         loop.setLastComputedState(LoopState.DESIGN);
127         loop.setDcaeBlueprintId(dcaeBlueprintId);
128         return loop;
129     }
130
131     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
132             String policyTosca, String jsonProperties, boolean shared) {
133         MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared,
134                 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
135         microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
136         return microService;
137     }
138 }