Create SVG in UI
[clamp.git] / src / test / java / org / onap / clamp / loop / LoopRepositoriesItCase.java
index 44feaeb..9815575 100644 (file)
@@ -30,10 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import com.google.gson.JsonObject;
-
 import java.time.Instant;
-import java.util.HashSet;
-
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.onap.clamp.clds.Application;
@@ -42,10 +39,10 @@ import org.onap.clamp.loop.log.LoopLog;
 import org.onap.clamp.loop.log.LoopLogRepository;
 import org.onap.clamp.loop.service.Service;
 import org.onap.clamp.loop.service.ServicesRepository;
+import org.onap.clamp.loop.template.LoopElementModel;
+import org.onap.clamp.loop.template.LoopElementModelsRepository;
 import org.onap.clamp.loop.template.LoopTemplate;
 import org.onap.clamp.loop.template.LoopTemplatesRepository;
-import org.onap.clamp.loop.template.MicroServiceModel;
-import org.onap.clamp.loop.template.MicroServiceModelsRepository;
 import org.onap.clamp.loop.template.PolicyModel;
 import org.onap.clamp.loop.template.PolicyModelId;
 import org.onap.clamp.loop.template.PolicyModelsRepository;
@@ -80,7 +77,7 @@ public class LoopRepositoriesItCase {
     private LoopTemplatesRepository loopTemplateRepository;
 
     @Autowired
-    private MicroServiceModelsRepository microServiceModelsRepository;
+    private LoopElementModelsRepository microServiceModelsRepository;
 
     @Autowired
     private PolicyModelsRepository policyModelsRepository;
@@ -92,49 +89,51 @@ public class LoopRepositoriesItCase {
         return new Service(serviceDetails, resourceDetails);
     }
 
-    private OperationalPolicy getOperationalPolicy(String configJson, String name) {
-        return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class));
+    private OperationalPolicy getOperationalPolicy(String configJson, String name, PolicyModel policyModel) {
+        return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class), policyModel,
+                null, null, null);
     }
 
-    private MicroServiceModel getMicroServiceModel(String yaml, String name, String policyType, String createdBy,
-            PolicyModel policyModel) {
-        MicroServiceModel model = new MicroServiceModel(name, policyType, yaml, policyModel);
+    private LoopElementModel getLoopElementModel(String yaml, String name, String policyType, String createdBy,
+                                                 PolicyModel policyModel) {
+        LoopElementModel model = new LoopElementModel(name, policyType, yaml);
+        model.addPolicyModel(policyModel);
         return model;
     }
 
-    private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym,
-            String policyVariant, String createdBy) {
-        return new PolicyModel(policyType, policyModelTosca, version, policyAcronym, policyVariant);
+    private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version,
+                                       String policyAcronym) {
+        return new PolicyModel(policyType, policyModelTosca, version, policyAcronym);
     }
 
-    private LoopTemplate getLoopTemplate(String name, String blueprint, String svgRepresentation, String createdBy,
-            Integer maxInstancesAllowed) {
-        LoopTemplate template = new LoopTemplate(name, blueprint, svgRepresentation, maxInstancesAllowed, null);
-        template.addMicroServiceModel(getMicroServiceModel("yaml", "microService1", "org.onap.policy.drools", createdBy,
-                getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools", "type1", createdBy)));
+    private LoopTemplate getLoopTemplates(String name, String blueprint, String createdBy,
+                                          Integer maxInstancesAllowed) {
+        LoopTemplate template = new LoopTemplate(name, blueprint, maxInstancesAllowed, null);
+        template.addLoopElementModel(getLoopElementModel("yaml", "microService1", "org.onap.policy.drools", createdBy,
+                getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools")));
+        template.addLoopElementModel(getLoopElementModel("yaml", "oppolicy1", "org.onap.policy.drools.legacy",
+                createdBy, getPolicyModel("org.onap.policy.drools.legacy", "yaml", "1.0.0", "DroolsLegacy")));
+        loopTemplateRepository.save(template);
         return template;
     }
 
-    private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
-            String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
+    private Loop getLoop(String name, String blueprint, String globalPropertiesJson,
+                         String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
         Loop loop = new Loop();
         loop.setName(name);
-        loop.setSvgRepresentation(svgRepresentation);
-        loop.setBlueprint(blueprint);
         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
         loop.setLastComputedState(LoopState.DESIGN);
         loop.setDcaeDeploymentId(dcaeId);
         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
-        loop.setDcaeBlueprintId(dcaeBlueprintId);
-        loop.setLoopTemplate(getLoopTemplate("templateName", "yaml", "svg", "toto", 1));
+        loop.setLoopTemplate(getLoopTemplates("templateName", "yaml", "toto", 1));
         return loop;
     }
 
-    private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
-            String policyTosca, String jsonProperties, boolean shared) {
-        MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared,
-                gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
-        microService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
+    private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String jsonProperties,
+                                                     boolean shared, PolicyModel policyModel) {
+        MicroServicePolicy microService = new MicroServicePolicy(name, policyModel, shared,
+                gson.fromJson(jsonRepresentation, JsonObject.class), null, null, null);
+        microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
         return microService;
     }
 
@@ -142,17 +141,20 @@ public class LoopRepositoriesItCase {
         return new LoopLog(message, type, "CLAMP", loop);
     }
 
+    /**
+     * This method does a crud test and save a loop template and a loop object in db.
+     */
     @Test
     @Transactional
     public void crudTest() {
         // Setup
-        Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
+        Loop loopTest = getLoop("ControlLoopTest", "yamlcontent", "{\"testname\":\"testvalue\"}",
                 "123456789", "https://dcaetest.org", "UUID-blueprint");
-        OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
+        OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest",
+                getPolicyModel("org.onap.policy.drools.legacy", "yaml", "1.0.0", "DroolsLegacy"));
         loopTest.addOperationalPolicy(opPolicy);
-        MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
-                "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
-                "{\"param1\":\"value1\"}", true);
+        MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}",
+                "{\"param1\":\"value1\"}", true, getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools"));
         loopTest.addMicroServicePolicy(microServicePolicy);
         LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
         loopTest.addLog(loopLog);
@@ -182,13 +184,13 @@ public class LoopRepositoriesItCase {
         assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
         assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true);
         assertThat(microServiceModelsRepository.existsById(
-                loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getName()))
-                        .isEqualTo(true);
+                loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getName()))
+                .isEqualTo(true);
         assertThat(policyModelsRepository.existsById(new PolicyModelId(
-                loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel()
-                        .getPolicyModelType(),
-                loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel()
-                        .getVersion()))).isEqualTo(true);
+                loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
+                        .first().getPolicyModelType(),
+                loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
+                        .first().getVersion()))).isEqualTo(true);
 
         // Now attempt to read from database
         Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get();
@@ -198,18 +200,26 @@ public class LoopRepositoriesItCase {
                 "createdBy", "updatedBy");
         assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog);
         assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0])
-                .isEqualToComparingFieldByField(opPolicy);
+                .isEqualToIgnoringGivenFields(opPolicy, "createdDate", "updatedDate", "createdBy", "updatedBy");
+        assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getCreatedDate())
+                .isNotNull();
+        assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getUpdatedDate())
+                .isNotNull();
+        assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getCreatedBy())
+                .isNotNull();
+        assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getUpdatedBy())
+                .isNotNull();
+
         assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0])
                 .isEqualToIgnoringGivenFields(microServicePolicy, "createdDate", "updatedDate", "createdBy",
                         "updatedBy");
 
         // Attempt an update
         ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now());
-        loopInDbRetrieved.setBlueprint("yaml2");
+        loopInDbRetrieved.setLastComputedState(LoopState.RUNNING);
         Loop loopInDbRetrievedUpdated = loopRepository.saveAndFlush(loopInDbRetrieved);
         // Loop loopInDbRetrievedUpdated =
         // loopRepository.findById(loopTest.getName()).get();
-        assertThat(loopInDbRetrievedUpdated.getBlueprint()).isEqualTo("yaml2");
         assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0])
                 .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]);
         // UpdatedDate should have been changed
@@ -217,8 +227,8 @@ public class LoopRepositoriesItCase {
         // createdDate should have NOT been changed
         assertThat(loopInDbRetrievedUpdated.getCreatedDate()).isEqualTo(loopInDb.getCreatedDate());
         // other audit are the same
-        assertThat(loopInDbRetrievedUpdated.getCreatedBy()).isEqualTo("");
-        assertThat(loopInDbRetrievedUpdated.getUpdatedBy()).isEqualTo("");
+        assertThat(loopInDbRetrievedUpdated.getCreatedBy()).isEqualTo("Not found");
+        assertThat(loopInDbRetrievedUpdated.getUpdatedBy()).isEqualTo("Not found");
 
         // Attempt to delete the object and check it has well been cascaded
 
@@ -230,23 +240,14 @@ public class LoopRepositoriesItCase {
         assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
         assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true);
         assertThat(microServiceModelsRepository.existsById(
-                loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getName()))
-                        .isEqualTo(true);
+                loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getName()))
+                .isEqualTo(true);
 
         assertThat(policyModelsRepository.existsById(new PolicyModelId(
-                loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel()
-                        .getPolicyModelType(),
-                loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel()
-                        .getVersion()))).isEqualTo(true);
-
-        // Cleanup
-        // microServiceModelsRepository
-        // .delete(loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel());
-        //
-        // policyModelsRepository.delete(
-        // loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel());
-        // loopTemplateRepository.delete(loopInDb.getLoopTemplate());
-        // servicesRepository.delete(service);
+                loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
+                        .first().getPolicyModelType(),
+                loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
+                        .first().getVersion()))).isEqualTo(true);
 
     }
 }