Fix blueprint installation
[clamp.git] / src / test / java / org / onap / clamp / loop / CsarInstallerItCase.java
index fd215dd..f986ca9 100644 (file)
@@ -42,15 +42,15 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mockito;
 import org.onap.clamp.clds.Application;
-import org.onap.clamp.clds.config.LegacyOperationalPolicy;
 import org.onap.clamp.clds.exception.sdc.controller.BlueprintParserException;
 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
 import org.onap.clamp.clds.util.JsonUtils;
-import org.onap.clamp.clds.util.ResourceFileUtil;
-import org.onap.clamp.loop.service.ServiceRepository;
+import org.onap.clamp.clds.util.ResourceFileUtils;
+import org.onap.clamp.loop.cds.CdsDataInstaller;
+import org.onap.clamp.loop.service.ServicesRepository;
 import org.onap.clamp.loop.template.LoopTemplate;
 import org.onap.clamp.loop.template.LoopTemplateLoopElementModel;
 import org.onap.clamp.loop.template.LoopTemplatesRepository;
@@ -73,10 +73,11 @@ import org.springframework.test.context.junit4.SpringRunner;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest(classes = Application.class)
-@ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller,legacy-operational-policy")
+@ActiveProfiles({"clamp-default", "clamp-default-user", "clamp-sdc-controller"})
 public class CsarInstallerItCase {
 
-    private static final String CSAR_ARTIFACT_NAME = "example/sdc/service_Vloadbalancerms_csar.csar";
+    private static final String CSAR_ARTIFACT_NAME_CDS = "example/sdc/service_Vloadbalancerms_cds.csar";
+    private static final String CSAR_ARTIFACT_NAME_NO_CDS = "example/sdc/service_Vloadbalancerms_no_cds.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";
@@ -87,7 +88,7 @@ public class CsarInstallerItCase {
     private LoopTemplatesRepository loopTemplatesRepo;
 
     @Autowired
-    ServiceRepository serviceRepository;
+    ServicesRepository serviceRepository;
 
     @Autowired
     PolicyModelsRepository policyModelsRepository;
@@ -97,20 +98,64 @@ public class CsarInstallerItCase {
     private CsarInstaller csarInstaller;
 
     private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
-            String blueprintFilePath, String artifactName, String invariantServiceUuid) throws IOException {
+                                                          String blueprintFilePath, String artifactName,
+                                                          String invariantServiceUuid) throws IOException {
         IResourceInstance resource = Mockito.mock(IResourceInstance.class);
         Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
         Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
         BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
         Mockito.when(blueprintArtifact.getDcaeBlueprint())
-                .thenReturn(ResourceFileUtil.getResourceAsString(blueprintFilePath));
+                .thenReturn(ResourceFileUtils.getResourceAsString(blueprintFilePath));
         Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(artifactName);
         Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
         Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
         return blueprintArtifact;
     }
 
-    private CsarHandler buildFakeCsarHandler(String generatedName) throws IOException, SdcToscaParserException {
+    private CsarHandler buildBadFakeCsarHandler(String generatedName, String csarFileName) throws IOException,
+            SdcToscaParserException {
+
+        // Build a Bad csar because the blueprint contains a link to a microservice that does not exist in the emulator
+        // Create fake notification
+        INotificationData notificationData = Mockito.mock(INotificationData.class);
+        Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
+        // Create fake resource in notification
+        CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
+        List<IResourceInstance> listResources = new ArrayList<>();
+        Mockito.when(notificationData.getResources()).thenReturn(listResources);
+        Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
+        Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
+        // Create fake blueprint artifact 1 on resource1
+        BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1,
+                INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca-guilin.yaml", "tca-guilin.yaml",
+                INVARIANT_SERVICE_UUID);
+        listResources.add(blueprintArtifact.getResourceAttached());
+        blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
+
+        // Build fake csarhandler
+        Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
+        // Build fake csar Helper
+        ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
+        Metadata data = Mockito.mock(Metadata.class);
+        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();
+        String path = Thread.currentThread().getContextClassLoader().getResource(csarFileName).getFile();
+        ISdcCsarHelper sdcHelper = factory.getSdcCsarHelper(path);
+        Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcHelper);
+
+        // Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
+        Mockito.when(csarHandler.getPolicyModelYaml())
+                .thenReturn(Optional.ofNullable(ResourceFileUtils.getResourceAsString("tosca/tosca_example.yaml")));
+        return csarHandler;
+    }
+
+    private CsarHandler buildFakeCsarHandler(String generatedName, String csarFileName) throws IOException,
+            SdcToscaParserException {
         // Create fake notification
         INotificationData notificationData = Mockito.mock(INotificationData.class);
         Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
@@ -149,23 +194,16 @@ public class CsarInstallerItCase {
         // Create helper based on real csar to test policy yaml and global properties
         // set
         SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
-        String path = Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile();
+        String path = Thread.currentThread().getContextClassLoader().getResource(csarFileName).getFile();
         ISdcCsarHelper sdcHelper = factory.getSdcCsarHelper(path);
         Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcHelper);
 
         // Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
         Mockito.when(csarHandler.getPolicyModelYaml())
-                .thenReturn(Optional.ofNullable(ResourceFileUtil.getResourceAsString("tosca/tosca_example.yaml")));
+                .thenReturn(Optional.ofNullable(ResourceFileUtils.getResourceAsString("tosca/tosca_example.yaml")));
         return csarHandler;
     }
 
-    @Test
-    @Transactional
-    public void testPolicyModelAddedAtStartup() {
-        assertThat(policyModelsRepository.findByPolicyModelType(
-                LegacyOperationalPolicy.OPERATIONAL_POLICY_LEGACY).get(0)).isNotNull();
-    }
-
     @Test
     @Transactional
     public void testGetPolicyModelYaml() throws IOException, SdcToscaParserException, CsarHandlerException {
@@ -178,10 +216,10 @@ public class CsarInstallerItCase {
 
         CsarHandler csarHandler = new CsarHandler(notificationData, "", "");
         csarHandler
-                .setFilePath(Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile());
-        Optional<String> testyaml = csarHandler.getPolicyModelYaml();
-        Assert.assertEquals(testyaml, Optional
-                .ofNullable(ResourceFileUtil.getResourceAsString("example/sdc/expected-result/policy-data.yaml")));
+                .setFilePath(Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME_CDS).
+                        getFile());
+        Assert.assertEquals(csarHandler.getPolicyModelYaml(), Optional
+                .ofNullable(ResourceFileUtils.getResourceAsString("example/sdc/expected-result/policy-data.yaml")));
     }
 
     @Test
@@ -189,21 +227,48 @@ public class CsarInstallerItCase {
     public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException,
             CsarHandlerException, IOException, InterruptedException, BlueprintParserException {
         String generatedName = RandomStringUtils.randomAlphanumeric(5);
-        CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
+        CsarHandler csarHandler = buildFakeCsarHandler(generatedName, CSAR_ARTIFACT_NAME_CDS);
         assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isFalse();
         csarInstaller.installTheCsar(csarHandler);
         assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isTrue();
     }
 
+    @Test
+    @Transactional
+    public void testWithoutCdsTca() throws SdcArtifactInstallerException, SdcToscaParserException,
+            CsarHandlerException, IOException, InterruptedException, BlueprintParserException {
+        String generatedName = RandomStringUtils.randomAlphanumeric(5);
+        CsarHandler csarHandler = buildFakeCsarHandler(generatedName, CSAR_ARTIFACT_NAME_NO_CDS);
+
+        assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isFalse();
+        csarInstaller.installTheCsar(csarHandler);
+        assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isTrue();
+    }
+
+    @Test(expected = SdcArtifactInstallerException.class)
+    @Transactional
+    public void testInstallTheBadCsarTca()
+            throws IOException, SdcToscaParserException, InterruptedException, BlueprintParserException,
+            SdcArtifactInstallerException {
+        // This test validates that the blueprint is well rejected because the blueprint contains a link
+        // to a policy that does not exist on the policy engine emulator.
+        String generatedName = RandomStringUtils.randomAlphanumeric(5);
+        csarInstaller.installTheCsar(buildBadFakeCsarHandler(generatedName, CSAR_ARTIFACT_NAME_NO_CDS));
+    }
+
     @Test
     @Transactional
     @Commit
     public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException,
             CsarHandlerException, IOException, JSONException, InterruptedException, BlueprintParserException {
         String generatedName = RandomStringUtils.randomAlphanumeric(5);
-        CsarHandler csar = buildFakeCsarHandler(generatedName);
-        csarInstaller.installTheCsar(csar);
+        csarInstaller.installTheCsar(buildFakeCsarHandler(generatedName, CSAR_ARTIFACT_NAME_CDS));
+
         assertThat(serviceRepository.existsById("63cac700-ab9a-4115-a74f-7eac85e3fce0")).isTrue();
+        // We should have CDS info
+        assertThat(serviceRepository.findById("63cac700-ab9a-4115-a74f-7eac85e3fce0").get().getResourceByType("VF")
+                .getAsJsonObject("vLoadBalancerMS 0").getAsJsonObject(
+                        CdsDataInstaller.CONTROLLER_PROPERTIES)).isNotNull();
         assertThat(loopTemplatesRepo.existsById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0",
                 RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml"))).isTrue();
         assertThat(loopTemplatesRepo.existsById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0",
@@ -214,14 +279,13 @@ public class CsarInstallerItCase {
         // set
         LoopTemplate loopTemplate = loopTemplatesRepo.findById(LoopTemplate.generateLoopTemplateName(generatedName,
                 "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get();
-        assertThat(loopTemplate.getSvgRepresentation()).startsWith("<svg ");
         assertThat(loopTemplate.getLoopElementModelsUsed()).hasSize(1);
         assertThat(loopTemplate.getModelService().getServiceUuid()).isEqualTo("63cac700-ab9a-4115-a74f-7eac85e3fce0");
-        JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/model-properties.json"),
+        JSONAssert.assertEquals(ResourceFileUtils.getResourceAsString("tosca/model-properties.json"),
                 JsonUtils.GSON_JPA_MODEL.toJson(loopTemplate.getModelService()), true);
-        JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/service-details.json"),
+        JSONAssert.assertEquals(ResourceFileUtils.getResourceAsString("tosca/service-details.json"),
                 JsonUtils.GSON_JPA_MODEL.toJson(loopTemplate.getModelService().getServiceDetails()), true);
-        JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/resource-details.json"),
+        JSONAssert.assertEquals(ResourceFileUtils.getResourceAsString("tosca/resource-details.json"),
                 JsonUtils.GSON_JPA_MODEL.toJson(loopTemplate.getModelService().getResourceDetails()), true);
         assertThat(((LoopTemplateLoopElementModel) (loopTemplate.getLoopElementModelsUsed().toArray()[0]))
                 .getLoopElementModel().getName()).isNotEmpty();