Update get Dcae Status flow
[clamp.git] / src / test / java / org / onap / clamp / loop / DeployFlowTestItCase.java
index d7c2eda..c85d5a5 100644 (file)
@@ -47,7 +47,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
-
 @RunWith(SpringRunner.class)
 @SpringBootTest(classes = Application.class)
 public class DeployFlowTestItCase {
@@ -59,11 +58,14 @@ public class DeployFlowTestItCase {
     @Autowired
     LoopService loopService;
 
+    @Autowired
+    LoopsRepository loopsRepository;
+
     @Test
     @Transactional
     public void deployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
         Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
-                 "UUID-blueprint");
+                "UUID-blueprint");
         LoopTemplate template = new LoopTemplate();
         template.setName("templateName");
         template.setBlueprint("yamlcontent");
@@ -73,11 +75,10 @@ public class DeployFlowTestItCase {
                 "{\"param1\":\"value1\"}", true);
         loopTest.addMicroServicePolicy(microServicePolicy);
         loopService.saveOrUpdateLoop(loopTest);
-        Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
-            .withProperty("loopObject", loopTest).build();
+        Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
+                .build();
 
-        camelContext.createProducerTemplate()
-            .send("direct:deploy-loop", myCamelExchange);
+        camelContext.createProducerTemplate().send("direct:deploy-loop", myCamelExchange);
 
         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl()).isNotNull();
@@ -88,26 +89,23 @@ public class DeployFlowTestItCase {
     @Transactional
     public void deployWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
         Loop loopTest2 = createLoop("ControlLoopTest2", "<xml></xml>", "yamlcontent", "{\"dcaeDeployParameters\": {"
-            + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName1_tca\"},"
-            + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName2_tca\"}"
-            + "}}", "UUID-blueprint");
+                + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName1_tca\"},"
+                + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName2_tca\"}"
+                + "}}", "UUID-blueprint");
         LoopTemplate template = new LoopTemplate();
         template.setName("templateName");
         loopTest2.setLoopTemplate(template);
-        MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "",
-                "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
-                "{\"param1\":\"value1\"}", true);
-        MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "",
-                "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
-                "{\"param1\":\"value1\"}", true);
+        MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "", "{\"configtype\":\"json\"}",
+                "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true);
+        MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "", "{\"configtype\":\"json\"}",
+                "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true);
         loopTest2.addMicroServicePolicy(microServicePolicy1);
         loopTest2.addMicroServicePolicy(microServicePolicy2);
-        loopService.saveOrUpdateLoop(loopTest2);
-        Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
-            .withProperty("loopObject", loopTest2).build();
+        loopsRepository.saveAndFlush(loopTest2);
+        Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest2)
+                .build();
 
-        camelContext.createProducerTemplate()
-            .send("direct:deploy-loop", myCamelExchange);
+        camelContext.createProducerTemplate().send("direct:deploy-loop", myCamelExchange);
 
         Loop loopAfterTest = loopService.getLoop("ControlLoopTest2");
         Set<MicroServicePolicy> policyList = loopAfterTest.getMicroServicePolicies();
@@ -123,7 +121,7 @@ public class DeployFlowTestItCase {
     @Transactional
     public void undeployWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
         Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
-                 "UUID-blueprint");
+                "UUID-blueprint");
         LoopTemplate template = new LoopTemplate();
         template.setName("templateName");
         template.setBlueprint("yamlcontent");
@@ -135,11 +133,10 @@ public class DeployFlowTestItCase {
                 "{\"param1\":\"value1\"}", true);
         loopTest.addMicroServicePolicy(microServicePolicy);
         loopService.saveOrUpdateLoop(loopTest);
-        Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
-            .withProperty("loopObject", loopTest).build();
+        Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
+                .build();
 
-        camelContext.createProducerTemplate()
-            .send("direct:undeploy-loop", myCamelExchange);
+        camelContext.createProducerTemplate().send("direct:undeploy-loop", myCamelExchange);
 
         Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
         assertThat(loopAfterTest.getDcaeDeploymentStatusUrl().contains("/uninstall")).isTrue();
@@ -149,26 +146,25 @@ public class DeployFlowTestItCase {
     @Transactional
     public void undeployWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
         Loop loopTest2 = createLoop("ControlLoopTest2", "<xml></xml>", "yamlcontent", "{\"dcaeDeployParameters\": {"
-            + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName1_tca\"},"
-            + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName2_tca\"}"
-            + "}}", "UUID-blueprint");
+                + "\"microService1\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName1_tca\"},"
+                + "\"microService2\": {\"location_id\": \"\", \"policy_id\": \"TCA_ResourceInstanceName2_tca\"}"
+                + "}}", "UUID-blueprint");
         LoopTemplate template = new LoopTemplate();
         template.setName("templateName");
         loopTest2.setLoopTemplate(template);
-        MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "",
-                "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
-                "{\"param1\":\"value1\"}", true, "testDeploymentId1", "testDeploymentStatusUrl1");
-        MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "",
-                "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
-                "{\"param1\":\"value1\"}", true, "testDeploymentId2", "testDeploymentStatusUrl2");
+        MicroServicePolicy microServicePolicy1 = getMicroServicePolicy("microService1", "", "{\"configtype\":\"json\"}",
+                "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true,
+                "testDeploymentId1", "testDeploymentStatusUrl1");
+        MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("microService2", "", "{\"configtype\":\"json\"}",
+                "tosca_definitions_version: tosca_simple_yaml_1_0_0", "{\"param1\":\"value1\"}", true,
+                "testDeploymentId2", "testDeploymentStatusUrl2");
         loopTest2.addMicroServicePolicy(microServicePolicy1);
         loopTest2.addMicroServicePolicy(microServicePolicy2);
-        loopService.saveOrUpdateLoop(loopTest2);
-        Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
-            .withProperty("loopObject", loopTest2).build();
+        loopsRepository.saveAndFlush(loopTest2);
+        Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest2)
+                .build();
 
-        camelContext.createProducerTemplate()
-            .send("direct:undeploy-loop", myCamelExchange);
+        camelContext.createProducerTemplate().send("direct:undeploy-loop", myCamelExchange);
 
         Loop loopAfterTest = loopService.getLoop("ControlLoopTest2");
         Set<MicroServicePolicy> policyList = loopAfterTest.getMicroServicePolicies();
@@ -179,12 +175,82 @@ public class DeployFlowTestItCase {
         assertThat(loopAfterTest.getDcaeDeploymentId()).isNull();
     }
 
+
+    @Test
+    @Transactional
+    public void getStatusWithSingleBlueprintTest() throws JsonSyntaxException, IOException {
+        Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
+                "UUID-blueprint");
+        LoopTemplate template = new LoopTemplate();
+        template.setName("templateName");
+        template.setBlueprint("yamlcontent");
+        loopTest.setLoopTemplate(template);
+        MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
+                "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
+                "{\"param1\":\"value1\"}", true);
+        loopTest.addMicroServicePolicy(microServicePolicy);
+        loopService.saveOrUpdateLoop(loopTest);
+        assertThat(loopTest.getComponents().size()).isEqualTo(2);
+        assertThat(loopTest.getComponent("DCAE")).isNotNull();
+        assertThat(loopTest.getComponent("POLICY")).isNotNull();
+        Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
+                .build();
+
+        camelContext.createProducerTemplate().send("direct:update-dcae-status-for-loop", myCamelExchange);
+
+        assertThat(loopTest.getComponent("DCAE").getState().getStateName()).isEqualTo("BLUEPRINT_DEPLOYED");
+
+        Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
+        assertThat(loopAfterTest.getComponents().size()).isEqualTo(2);
+        assertThat(loopAfterTest.getComponent("DCAE")).isNotNull();
+        assertThat(loopAfterTest.getComponent("POLICY")).isNotNull();
+    }
+
+    @Test
+    @Transactional
+    public void getStatusWithMultipleBlueprintTest() throws JsonSyntaxException, IOException {
+        Loop loopTest = createLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
+                "UUID-blueprint");
+        LoopTemplate template = new LoopTemplate();
+        template.setName("templateName");
+        loopTest.setLoopTemplate(template);
+        MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
+                "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
+                "{\"param1\":\"value1\"}", true);
+        MicroServicePolicy microServicePolicy2 = getMicroServicePolicy("configPolicyTest2", "",
+                "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
+                "{\"param1\":\"value1\"}", true);
+        loopTest.addMicroServicePolicy(microServicePolicy);
+        loopTest.addMicroServicePolicy(microServicePolicy2);
+        loopService.saveOrUpdateLoop(loopTest);
+        assertThat(loopTest.getComponents().size()).isEqualTo(3);
+        assertThat(loopTest.getComponent("DCAE")).isNull();
+        assertThat(loopTest.getComponent("DCAE_configPolicyTest")).isNotNull();
+        assertThat(loopTest.getComponent("DCAE_configPolicyTest2")).isNotNull();
+        assertThat(loopTest.getComponent("POLICY")).isNotNull();
+        Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext).withProperty("loopObject", loopTest)
+                .build();
+
+        camelContext.createProducerTemplate().send("direct:update-dcae-status-for-loop", myCamelExchange);
+
+        assertThat(loopTest.getComponent("DCAE_configPolicyTest").getState().getStateName())
+            .isEqualTo("BLUEPRINT_DEPLOYED");
+        assertThat(loopTest.getComponent("DCAE_configPolicyTest2").getState().getStateName())
+            .isEqualTo("BLUEPRINT_DEPLOYED");
+
+        Loop loopAfterTest = loopService.getLoop("ControlLoopTest");
+        assertThat(loopAfterTest.getComponents().size()).isEqualTo(3);
+        assertThat(loopAfterTest.getComponent("DCAE")).isNull();
+        assertThat(loopAfterTest.getComponent("POLICY")).isNotNull();
+        assertThat(loopTest.getComponent("DCAE_configPolicyTest")).isNotNull();
+        assertThat(loopTest.getComponent("DCAE_configPolicyTest2")).isNotNull();
+    }
+
     private Loop createLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
             String dcaeBlueprintId) throws JsonSyntaxException, IOException {
-        Loop loop = new Loop(name, blueprint, svgRepresentation);
+        Loop loop = new Loop(name, svgRepresentation);
         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
         loop.setLastComputedState(LoopState.DESIGN);
-        loop.setDcaeBlueprintId(dcaeBlueprintId);
         return loop;
     }
 
@@ -197,9 +263,10 @@ public class DeployFlowTestItCase {
     }
 
     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
-            String policyTosca, String jsonProperties, boolean shared, String deploymengId, String deploymentStatusUrl) {
-        MicroServicePolicy microService = getMicroServicePolicy(name, modelType, jsonRepresentation,
-                policyTosca, jsonProperties, shared);
+            String policyTosca, String jsonProperties, boolean shared, String deploymengId,
+            String deploymentStatusUrl) {
+        MicroServicePolicy microService = getMicroServicePolicy(name, modelType, jsonRepresentation, policyTosca,
+                jsonProperties, shared);
 
         microService.setDcaeDeploymentId(deploymengId);
         microService.setDcaeDeploymentStatusUrl(deploymentStatusUrl);