Create SVG in UI
[clamp.git] / src / test / java / org / onap / clamp / loop / LoopControllerTestItCase.java
index 67ae985..f170bc6 100644 (file)
@@ -30,21 +30,21 @@ import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
-
+import java.io.IOException;
 import java.util.Set;
 import javax.transaction.Transactional;
-
-import org.junit.After;
-import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-
 import org.onap.clamp.clds.Application;
 import org.onap.clamp.clds.util.JsonUtils;
-
+import org.onap.clamp.loop.service.Service;
+import org.onap.clamp.loop.template.LoopTemplate;
+import org.onap.clamp.loop.template.PolicyModel;
+import org.onap.clamp.loop.template.PolicyModelsService;
 import org.onap.clamp.policy.microservice.MicroServicePolicy;
-import org.onap.clamp.policy.microservice.MicroservicePolicyService;
+import org.onap.clamp.policy.microservice.MicroServicePolicyService;
 import org.onap.clamp.policy.operational.OperationalPolicy;
+import org.onap.clamp.policy.operational.OperationalPolicyService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
@@ -63,7 +63,13 @@ public class LoopControllerTestItCase {
     LoopsRepository loopsRepository;
 
     @Autowired
-    MicroservicePolicyService microServicePolicyService;
+    MicroServicePolicyService microServicePolicyService;
+
+    @Autowired
+    OperationalPolicyService operationalPolicyService;
+
+    @Autowired
+    PolicyModelsService policyModelsService;
 
     @Autowired
     LoopController loopController;
@@ -71,27 +77,24 @@ public class LoopControllerTestItCase {
     private void saveTestLoopToDb() {
         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
+        LoopTemplate template = new LoopTemplate();
+        template.setName("testTemplate");
+        testLoop.setLoopTemplate(template);
+        Service modelService = new Service("{\"name\":\"serviceName\",\"UUID\":\"uuid\"}", "{}");
+        testLoop.setModelService(modelService);
         loopService.saveOrUpdateLoop(testLoop);
     }
 
     private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
-        return new Loop(loopName, loopBlueprint, loopSvg);
-    }
-
-    @Before
-    public void setUp() {
-        saveTestLoopToDb();
-    }
-
-    @After
-    public void tearDown() {
-        loopsRepository.deleteAll();
+        return new Loop(loopName);
     }
 
     @Test
+    @Transactional
     public void testUpdateOperationalPolicies() {
+        saveTestLoopToDb();
         String policy = "[{\"name\":\"OPERATIONAL_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules\","
-                + "\"configurationsJson\":{\"guard_policies\":{},"
+                + "\"configurationsJson\":{"
                 + "\"operational_policy\":{\"controlLoop\":{\"trigger_policy\":\"unique-policy-id-1-modifyConfig\","
                 + "\"timeout\":\"3600\",\"abatement\":\"false\","
                 + "\"controlLoopName\":\"LOOP_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules\"},"
@@ -113,6 +116,7 @@ public class LoopControllerTestItCase {
     @Test
     @Transactional
     public void testUpdateGlobalProperties() {
+        saveTestLoopToDb();
         String policy = "{\"dcaeDeployParameters\":{\"aaiEnrichmentHost\":\"aai.onap.svc.cluster.local\","
                 + "\"aaiEnrichmentPort\":\"8443\",\"enableAAIEnrichment\":\"false\",\"dmaap_host\":\"message-router"
                 + ".onap\",\"dmaap_port\":\"3904\",\"enableRedisCaching\":\"false\",\"redisHosts\":\"dcae-redis.onap"
@@ -134,17 +138,37 @@ public class LoopControllerTestItCase {
     @Test
     @Transactional
     public void testUpdateMicroservicePolicy() {
-        MicroServicePolicy policy = new MicroServicePolicy("policyName", "",
-                                                      "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
-                                                      JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
+        saveTestLoopToDb();
+        PolicyModel policyModel = new PolicyModel("testPolicyModel",
+                "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0");
+        policyModelsService.saveOrUpdatePolicyModel(policyModel);
+        MicroServicePolicy policy = new MicroServicePolicy("policyName", policyModel, false,
+                JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
         loopController.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, policy);
         assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
     }
 
     @Test
     @Transactional
-    public void testGetSvgRepresentation() {
-        String svgRepresentation =  loopController.getSvgRepresentation(EXAMPLE_LOOP_NAME);
-        assertThat(svgRepresentation).isEqualTo("representation");
+    public void testAddAndRemoveOperationalPolicies() throws IOException {
+        saveTestLoopToDb();
+        PolicyModel policyModel = new PolicyModel("testPolicyModel",
+                null, "1.0.0");
+        policyModelsService.saveOrUpdatePolicyModel(policyModel);
+
+        loopController.addOperationalPolicy(EXAMPLE_LOOP_NAME, "testPolicyModel", "1.0.0");
+
+        Loop newLoop = loopController.getLoop(EXAMPLE_LOOP_NAME);
+        Set<OperationalPolicy> opPolicyList = newLoop.getOperationalPolicies();
+        assertThat(opPolicyList.size()).isEqualTo(1);
+        for (OperationalPolicy policy : opPolicyList) {
+            assertThat(policy.getName().contains("OPERATIONAL_serviceName")).isTrue();
+            assertThat(policy.getPolicyModel().getPolicyModelType()).isEqualTo("testPolicyModel");
+            assertThat(policy.getPolicyModel().getVersion()).isEqualTo("1.0.0");
+        }
+
+        loopController.removeOperationalPolicy(EXAMPLE_LOOP_NAME, "testPolicyModel", "1.0.0");
+        Loop newLoop2 = loopController.getLoop(EXAMPLE_LOOP_NAME);
+        assertThat(newLoop2.getOperationalPolicies().size()).isEqualTo(0);
     }
 }
\ No newline at end of file