Add unit tests 31/82031/2
authorsebdet <sebastien.determe@intl.att.com>
Tue, 12 Mar 2019 14:08:11 +0000 (15:08 +0100)
committersebdet <sebastien.determe@intl.att.com>
Tue, 12 Mar 2019 14:25:11 +0000 (15:25 +0100)
Add unit tests and fix code to support them, columns modified and csar
installer fixed as well

Issue-ID: CLAMP-306
Change-Id: I946ef1aa957ca36bbb00357308ac67a3f07dcdce
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
extra/sql/bulkload/create-tables.sql
src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java
src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java
src/main/java/org/onap/clamp/loop/Loop.java
src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java
src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java
src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandlerTest.java
src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java
src/test/resources/example/sdc/service-Simsfoimap0112.csar

index 6d490c3..93c80cb 100644 (file)
@@ -16,7 +16,7 @@
 
     create table loops (
        name varchar(255) not null,
-        blueprint_yaml varchar(255) not null,
+        blueprint_yaml MEDIUMTEXT not null,
         dcae_blueprint_id varchar(255),
         dcae_deployment_id varchar(255),
         dcae_deployment_status_url varchar(255),
@@ -36,7 +36,7 @@
     create table micro_service_policies (
        name varchar(255) not null,
         json_representation json not null,
-        policy_tosca varchar(255) not null,
+        policy_tosca MEDIUMTEXT not null,
         properties json,
         shared bit not null,
         primary key (name)
index 784d95e..8a172ab 100644 (file)
@@ -82,13 +82,15 @@ public class ToscaYamlToJsonConvertor {
         this.cldsDao = cldsDao;
     }
 
-    @SuppressWarnings("unchecked")
     public String parseToscaYaml(String yamlString) {
 
         Yaml yaml = new Yaml();
-        LinkedHashMap<String, Object> loadedYaml = (LinkedHashMap<String, Object>) yaml.load(yamlString);
-        LinkedHashMap<String, Object> nodeTypes = new LinkedHashMap<String, Object>();
-        LinkedHashMap<String, Object> dataNodes = new LinkedHashMap<String, Object>();
+        LinkedHashMap<String, Object> loadedYaml = yaml.load(yamlString);
+        if (loadedYaml == null) {
+            return "";
+        }
+        LinkedHashMap<String, Object> nodeTypes = new LinkedHashMap<>();
+        LinkedHashMap<String, Object> dataNodes = new LinkedHashMap<>();
         JSONObject jsonEditorObject = new JSONObject();
         JSONObject jsonParentObject = new JSONObject();
         JSONObject jsonTempObject = new JSONObject();
index 9627445..6e12f29 100644 (file)
@@ -33,7 +33,6 @@ import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Optional;
 
 import org.json.simple.parser.ParseException;
 import org.onap.clamp.clds.client.DcaeInventoryServices;
@@ -53,6 +52,7 @@ import org.onap.clamp.policy.operational.OperationalPolicy;
 import org.onap.sdc.tosca.parser.enums.SdcTypes;
 import org.onap.sdc.toscaparser.api.NodeTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 import org.yaml.snakeyaml.Yaml;
 
@@ -71,63 +71,40 @@ public class CsarInstallerImpl implements CsarInstaller {
     public static final String MODEL_NAME_PREFIX = "Loop_";
 
     @Autowired
-    protected LoopsRepository loopRepository;
+    LoopsRepository loopRepository;
 
     @Autowired
-    private BlueprintParser blueprintParser;
+    BlueprintParser blueprintParser;
 
     @Autowired
-    private ChainGenerator chainGenerator;
+    ChainGenerator chainGenerator;
 
     @Autowired
     DcaeInventoryServices dcaeInventoryService;
 
-    @Autowired
-    public void CsarInstallerImpl(LoopsRepository loopRepository, BlueprintParser blueprintParser,
-        ChainGenerator chainGenerator, DcaeInventoryServices dcaeInventoryService) {
-        this.loopRepository = loopRepository;
-        this.blueprintParser = blueprintParser;
-        this.chainGenerator = chainGenerator;
-        this.dcaeInventoryService = dcaeInventoryService;
-    }
-
     @Override
     public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
         boolean alreadyInstalled = true;
         for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
             alreadyInstalled = alreadyInstalled
-                && loopRepository.existsById(buildModelName(csar, blueprint.getValue()));
+                && loopRepository.existsById(Loop.generateLoopName(csar.getSdcNotification().getServiceName(),
+                    csar.getSdcNotification().getServiceVersion(),
+                    blueprint.getValue().getResourceAttached().getResourceInstanceName(),
+                    blueprint.getValue().getBlueprintArtifactName()));
         }
         return alreadyInstalled;
     }
 
-    public static String buildModelName(CsarHandler csar, BlueprintArtifact artifact) {
-
-        return (MODEL_NAME_PREFIX + "_" + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v"
-            + csar.getSdcNotification().getServiceVersion() + "_"
-            + artifact.getResourceAttached().getResourceInstanceName().replaceAll(" ", "") + "_"
-            + artifact.getBlueprintArtifactName().replace(".yaml", "")).replace('.', '_');
-    }
-
-    public static String buildOperationalPolicyName(CsarHandler csar, BlueprintArtifact artifact) {
-
-        return (MODEL_NAME_PREFIX + "_" + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v"
-            + csar.getSdcNotification().getServiceVersion() + "_"
-            + artifact.getResourceAttached().getResourceInstanceName().replaceAll(" ", "") + "_"
-            + artifact.getBlueprintArtifactName().replace(".yaml", "")).replace('.', '_');
-    }
-
     @Override
-    @Transactional
+    @Transactional(propagation = Propagation.REQUIRED)
     public void installTheCsar(CsarHandler csar)
         throws SdcArtifactInstallerException, InterruptedException, PolicyModelException {
         try {
             logger.info("Installing the CSAR " + csar.getFilePath());
             for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
                 logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
-                createLoopFromBlueprint(csar, blueprint.getValue());
+                loopRepository.save(createLoopFromBlueprint(csar, blueprint.getValue()));
             }
-            createPolicyModel(csar);
             logger.info("Successfully installed the CSAR " + csar.getFilePath());
         } catch (IOException e) {
             throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
@@ -136,15 +113,6 @@ public class CsarInstallerImpl implements CsarInstaller {
         }
     }
 
-    private void createPolicyModel(CsarHandler csar) throws PolicyModelException {
-        try {
-            Optional<String> policyModelYaml = csar.getPolicyModelYaml();
-            // save policy model into the database
-        } catch (IOException e) {
-            throw new PolicyModelException("TransformerException when decoding the YamlText", e);
-        }
-    }
-
     private Loop createLoopFromBlueprint(CsarHandler csar, BlueprintArtifact blueprintArtifact)
         throws IOException, ParseException, InterruptedException {
         Loop newLoop = new Loop();
@@ -154,15 +122,8 @@ public class CsarInstallerImpl implements CsarInstaller {
             blueprintArtifact.getResourceAttached().getResourceInstanceName(),
             blueprintArtifact.getBlueprintArtifactName()));
         newLoop.setLastComputedState(LoopState.DESIGN);
-        for (MicroService microService : blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint())) {
-            newLoop.getMicroServicePolicies().add(new MicroServicePolicy(microService.getName(),
-                csar.getPolicyModelYaml().orElse(""), false, new JsonObject(), new HashSet<>(Arrays.asList(newLoop))));
-        }
-        newLoop.setOperationalPolicies(
-            new HashSet<>(Arrays.asList(new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL",
-                csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(),
-                blueprintArtifact.getResourceAttached().getResourceInstanceName(),
-                blueprintArtifact.getBlueprintArtifactName()), newLoop, new JsonObject()))));
+        newLoop.setMicroServicePolicies(createMicroServicePolicies(csar, blueprintArtifact, newLoop));
+        newLoop.setOperationalPolicies(createOperationalPolicies(csar, blueprintArtifact, newLoop));
         // Set SVG XML computed
         // newLoop.setSvgRepresentation(svgRepresentation);
         newLoop.setGlobalPropertiesJson(createGlobalPropertiesJson(csar, blueprintArtifact));
@@ -172,6 +133,24 @@ public class CsarInstallerImpl implements CsarInstaller {
         return newLoop;
     }
 
+    private HashSet<OperationalPolicy> createOperationalPolicies(CsarHandler csar, BlueprintArtifact blueprintArtifact,
+        Loop newLoop) {
+        return new HashSet<>(Arrays.asList(new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL",
+            csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(),
+            blueprintArtifact.getResourceAttached().getResourceInstanceName(),
+            blueprintArtifact.getBlueprintArtifactName()), newLoop, new JsonObject())));
+    }
+
+    private HashSet<MicroServicePolicy> createMicroServicePolicies(CsarHandler csar,
+        BlueprintArtifact blueprintArtifact, Loop newLoop) throws IOException {
+        HashSet<MicroServicePolicy> newSet = new HashSet<>();
+        for (MicroService microService : blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint())) {
+            newSet.add(new MicroServicePolicy(microService.getName(), csar.getPolicyModelYaml().orElse(""), false,
+                new HashSet<>(Arrays.asList(newLoop))));
+        }
+        return newSet;
+    }
+
     private JsonObject createGlobalPropertiesJson(CsarHandler csar, BlueprintArtifact blueprintArtifact) {
         JsonObject globalProperties = new JsonObject();
         globalProperties.add("dcaeDeployParameters", getAllBlueprintParametersInJson(blueprintArtifact));
index cc7f180..a4cd86d 100644 (file)
@@ -91,7 +91,7 @@ public class Loop implements Serializable {
     @Column(columnDefinition = "json", name = "model_properties_json")
     private JsonObject modelPropertiesJson;
 
-    @Column(nullable = false, name = "blueprint_yaml")
+    @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
     private String blueprint;
 
     @Expose
index 7ebe0ed..857a3d7 100644 (file)
@@ -39,6 +39,8 @@ import javax.persistence.Table;
 import org.hibernate.annotations.Type;
 import org.hibernate.annotations.TypeDef;
 import org.hibernate.annotations.TypeDefs;
+import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
+import org.onap.clamp.clds.util.JsonUtils;
 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
 import org.onap.clamp.loop.Loop;
 import org.onap.clamp.policy.Policy;
@@ -66,7 +68,7 @@ public class MicroServicePolicy implements Serializable, Policy {
     @Column(name = "shared", nullable = false)
     private Boolean shared;
 
-    @Column(name = "policy_tosca", nullable = false)
+    @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca", nullable = false)
     private String policyTosca;
 
     @Expose
@@ -81,13 +83,22 @@ public class MicroServicePolicy implements Serializable, Policy {
         // serialization
     }
 
+    public MicroServicePolicy(String name, String policyTosca, Boolean shared, Set<Loop> usedByLoops) {
+        this.name = name;
+        this.policyTosca = policyTosca;
+        this.shared = shared;
+        this.jsonRepresentation = JsonUtils.GSON_JPA_MODEL
+            .fromJson(new ToscaYamlToJsonConvertor(null).parseToscaYaml(policyTosca), JsonObject.class);
+        this.usedByLoops = usedByLoops;
+    }
+
     public MicroServicePolicy(String name, String policyTosca, Boolean shared, JsonObject jsonRepresentation,
         Set<Loop> usedByLoops) {
         this.name = name;
         this.policyTosca = policyTosca;
         this.shared = shared;
-        this.jsonRepresentation = jsonRepresentation;
         this.usedByLoops = usedByLoops;
+        this.jsonRepresentation = jsonRepresentation;
     }
 
     @Override
index ce8a493..d3a823f 100644 (file)
@@ -30,6 +30,7 @@ import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -68,7 +69,6 @@ import org.springframework.test.context.junit4.SpringRunner;
 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller")
 public class CsarInstallerItCase {
 
-    private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar";
     private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92";
     private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec";
     private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad";
@@ -89,7 +89,8 @@ public class CsarInstallerItCase {
         blueprintMap.put("resourceid", blueprintArtifact);
         Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
         Mockito.when(blueprintArtifact.getDcaeBlueprint()).thenReturn(
-            IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/not-recognized.yaml")));
+            IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/not-recognized.yaml"),
+                StandardCharsets.UTF_8));
         csarInstaller.installTheCsar(csarHandler);
         fail("Should have raised an SdcArtifactInstallerException");
     }
@@ -164,16 +165,17 @@ public class CsarInstallerItCase {
         csarInstaller.installTheCsar(csar);
         CldsModel cldsModel1 = verifyClosedLoopModelLoadedInDb(csar, "tca.yaml");
         JSONAssert.assertEquals(
-            IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json")),
+            IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json"),
+                StandardCharsets.UTF_8),
             cldsModel1.getPropText(), true);
         CldsModel cldsModel2 = verifyClosedLoopModelLoadedInDb(csar, "tca_2.yaml");
-        JSONAssert.assertEquals(
-            IOUtils
-                .toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca-2.json")),
-            cldsModel2.getPropText(), true);
+        JSONAssert.assertEquals(IOUtils.toString(
+            ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca-2.json"),
+            StandardCharsets.UTF_8), cldsModel2.getPropText(), true);
         CldsModel cldsModel3 = verifyClosedLoopModelLoadedInDb(csar, "tca_3.yaml");
         JSONAssert.assertEquals(
-            IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json")),
+            IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json"),
+                StandardCharsets.UTF_8),
             cldsModel3.getPropText(), true);
     }
 
index 544c8ca..e008874 100644 (file)
@@ -161,7 +161,7 @@ public class CsarHandlerTest {
         CsarHandler csar = new CsarHandler(buildFakeSdcNotification(), "test-controller", "/tmp/csar-handler-tests");
         csar.save(buildFakeSdcResut());
         String policyModelYaml = csar.getPolicyModelYaml().get();
-        assertTrue(policyModelYaml.contains("tosca_simple_yaml_1_1"));
+        assertTrue(policyModelYaml.contains("tosca_simple_yaml_1_0_0"));
     }
 
     @Test
index 6bfee4c..d1a4bdc 100644 (file)
@@ -23,8 +23,7 @@
 
 package org.onap.clamp.loop;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -33,6 +32,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 
+import javax.transaction.Transactional;
+
 import org.apache.commons.lang3.RandomStringUtils;
 import org.json.JSONException;
 import org.junit.Test;
@@ -62,13 +63,16 @@ import org.springframework.test.context.junit4.SpringRunner;
 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller-new")
 public class CsarInstallerItCase {
 
-    private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar";
+    private static final String CSAR_ARTIFACT_NAME = "example/sdc/service-Simsfoimap0112.csar";
     private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92";
     private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec";
     private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad";
     private static final String RESOURCE_INSTANCE_NAME_RESOURCE1 = "ResourceInstanceName1";
     private static final String RESOURCE_INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2";
 
+    @Autowired
+    private LoopsRepository loopsRepo;
+
     @Autowired
     private CsarInstaller csarInstaller;
 
@@ -113,10 +117,6 @@ public class CsarInstallerItCase {
             "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID);
         blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
 
-        SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
-        ISdcCsarHelper sdcHelper = factory.getSdcCsarHelper(Thread.currentThread().getContextClassLoader()
-            .getResource("example/sdc/service-Simsfoimap0112.csar").getFile());
-
         // Build fake csarhandler
         Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
         // Build fake csar Helper
@@ -125,28 +125,54 @@ public class CsarInstallerItCase {
         Mockito.when(data.getValue("name")).thenReturn(generatedName);
         Mockito.when(notificationData.getServiceName()).thenReturn(generatedName);
         Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
+
+        // Create helper based on real csar to test policy yaml and global properties
+        // set
+        SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
+        ISdcCsarHelper sdcHelper = factory
+            .getSdcCsarHelper(Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile());
         Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcHelper);
+
         // Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
-        Mockito.when(csarHandler.getPolicyModelYaml()).thenReturn(Optional.ofNullable(""));
+        Mockito.when(csarHandler.getPolicyModelYaml())
+            .thenReturn(Optional.ofNullable(ResourceFileUtil.getResourceAsString("tosca/tca-policy-test.yaml")));
         return csarHandler;
     }
 
+    @Test
+    @Transactional
     public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException,
         CsarHandlerException, IOException, InterruptedException, PolicyModelException {
         String generatedName = RandomStringUtils.randomAlphanumeric(5);
         CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
-        assertFalse(csarInstaller.isCsarAlreadyDeployed(csarHandler));
+        assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isFalse();
         csarInstaller.installTheCsar(csarHandler);
-        assertTrue(csarInstaller.isCsarAlreadyDeployed(csarHandler));
+        assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isTrue();
     }
 
     @Test
+    @Transactional
     public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException,
         CsarHandlerException, IOException, JSONException, InterruptedException, PolicyModelException {
         String generatedName = RandomStringUtils.randomAlphanumeric(5);
         CsarHandler csar = buildFakeCsarHandler(generatedName);
         csarInstaller.installTheCsar(csar);
-
+        assertThat(loopsRepo
+            .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")))
+                .isTrue();
+        assertThat(loopsRepo
+            .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca_3.yaml")))
+                .isTrue();
+        assertThat(loopsRepo
+            .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE2, "tca_2.yaml")))
+                .isTrue();
+        // Verify now that policy and json representation, global properties are well
+        // set
+        Loop loop = loopsRepo
+            .findById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get();
+
+        assertThat(loop.getModelPropertiesJson().get("serviceDetails")).isNotNull();
+        assertThat(loop.getModelPropertiesJson().get("resourceDetails")).isNotNull();
     }
 
 }
index ea0e44a..8c16d31 100644 (file)
Binary files a/src/test/resources/example/sdc/service-Simsfoimap0112.csar and b/src/test/resources/example/sdc/service-Simsfoimap0112.csar differ