X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Fclamp%2Floop%2FCsarInstallerItCase.java;h=f986ca935bb89060301f5c302756bbde8b0b5dcb;hb=e1d5007e4688bfbbb176f0fc6c119a73e9f19882;hp=1c33d5e0593ef1faaca1cbcc4984ed2fa735c09a;hpb=32bfb481953f057ce460ba10efbf51509a2cec1f;p=clamp.git diff --git a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java index 1c33d5e0..f986ca93 100644 --- a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java +++ b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java @@ -42,14 +42,13 @@ 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.LegacyOperationalPolicyController; 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.clds.util.ResourceFileUtils; import org.onap.clamp.loop.cds.CdsDataInstaller; import org.onap.clamp.loop.service.ServicesRepository; import org.onap.clamp.loop.template.LoopTemplate; @@ -74,7 +73,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) -@ActiveProfiles({"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_CDS = "example/sdc/service_Vloadbalancerms_cds.csar"; @@ -106,13 +105,55 @@ public class CsarInstallerItCase { 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 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 listResources = new ArrayList<>(); + Mockito.when(notificationData.getResources()).thenReturn(listResources); + Map 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 @@ -159,17 +200,10 @@ public class CsarInstallerItCase { // 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( - LegacyOperationalPolicyController.OPERATIONAL_POLICY_LEGACY).get(0)).isNotNull(); - } - @Test @Transactional public void testGetPolicyModelYaml() throws IOException, SdcToscaParserException, CsarHandlerException { @@ -182,9 +216,10 @@ public class CsarInstallerItCase { CsarHandler csarHandler = new CsarHandler(notificationData, "", ""); csarHandler - .setFilePath(Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME_CDS).getFile()); + .setFilePath(Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME_CDS). + getFile()); Assert.assertEquals(csarHandler.getPolicyModelYaml(), Optional - .ofNullable(ResourceFileUtil.getResourceAsString("example/sdc/expected-result/policy-data.yaml"))); + .ofNullable(ResourceFileUtils.getResourceAsString("example/sdc/expected-result/policy-data.yaml"))); } @Test @@ -210,14 +245,25 @@ public class CsarInstallerItCase { 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, CSAR_ARTIFACT_NAME_CDS); - 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") @@ -235,11 +281,11 @@ public class CsarInstallerItCase { "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get(); 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();