X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Fclamp%2Floop%2FCsarInstallerItCase.java;h=a57a35ea41af99c5197482d3bf8b41516c9cac06;hb=8604d37f2f6bdd011de62ec474b6883413d30348;hp=d1a4bdc567bb7c13a832863998889795daf20bc4;hpb=d7df3862dd3af9b1b0062bd68bb183dd5a3416ed;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 d1a4bdc5..a57a35ea 100644 --- a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java +++ b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java @@ -5,6 +5,9 @@ * Copyright (C) 2019 AT&T Intellectual Property. All rights * reserved. * ================================================================================ + * Modifications copyright (c) 2019 Nokia + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -24,46 +27,55 @@ package org.onap.clamp.loop; import static org.assertj.core.api.Assertions.assertThat; - import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; 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.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.onap.clamp.clds.Application; -import org.onap.clamp.clds.exception.policy.PolicyModelException; +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.sdc.controller.installer.CsarInstaller; +import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.clds.util.ResourceFileUtil; +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; +import org.onap.clamp.loop.template.PolicyModelId; +import org.onap.clamp.loop.template.PolicyModelsRepository; +import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.notification.INotificationData; import org.onap.sdc.api.notification.IResourceInstance; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException; import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; import org.onap.sdc.toscaparser.api.elements.Metadata; +import org.skyscreamer.jsonassert.JSONAssert; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Commit; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) -@ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller-new") +@ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller,legacy-operational-policy") public class CsarInstallerItCase { - private static final String CSAR_ARTIFACT_NAME = "example/sdc/service-Simsfoimap0112.csar"; + private static final String CSAR_ARTIFACT_NAME = "example/sdc/service_Vloadbalancerms_csar.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"; @@ -71,19 +83,26 @@ public class CsarInstallerItCase { private static final String RESOURCE_INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2"; @Autowired - private LoopsRepository loopsRepo; + private LoopTemplatesRepository loopTemplatesRepo; + + @Autowired + ServicesRepository serviceRepository; + + @Autowired + PolicyModelsRepository policyModelsRepository; @Autowired + @Qualifier("csarInstaller") 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(ResourceFileUtil.getResourceAsString(blueprintFilePath)); Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(artifactName); Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid); Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource); @@ -102,19 +121,19 @@ public class CsarInstallerItCase { 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.yaml", "tca.yaml", INVARIANT_SERVICE_UUID); + INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", "tca.yaml", INVARIANT_SERVICE_UUID); listResources.add(blueprintArtifact.getResourceAttached()); blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact); // Create fake blueprint artifact 2 on resource2 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID, - "example/sdc/blueprint-dcae/tca_2.yaml", "tca_2.yaml", INVARIANT_SERVICE_UUID); + "example/sdc/blueprint-dcae/tca_2.yaml", "tca_2.yaml", INVARIANT_SERVICE_UUID); listResources.add(blueprintArtifact.getResourceAttached()); blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact); // Create fake blueprint artifact 3 on resource 1 so that it's possible to // test multiple CL deployment per Service/vnf blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1, INVARIANT_RESOURCE1_UUID, - "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID); + "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID); blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact); // Build fake csarhandler @@ -129,20 +148,45 @@ public class CsarInstallerItCase { // 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()); + String path = Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).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/tca-policy-test.yaml"))); + .thenReturn(Optional.ofNullable(ResourceFileUtil.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 { + INotificationData notificationData = Mockito.mock(INotificationData.class); + IArtifactInfo serviceArtifacts = Mockito.mock(IArtifactInfo.class); + Mockito.when(serviceArtifacts.getArtifactType()).thenReturn("TOSCA_CSAR"); + List serviceArtifactsList = new ArrayList<>(); + serviceArtifactsList.add(serviceArtifacts); + Mockito.when(notificationData.getServiceArtifacts()).thenReturn(serviceArtifactsList); + + CsarHandler csarHandler = new CsarHandler(notificationData, "", ""); + csarHandler + .setFilePath(Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile()); + Optional testyaml = csarHandler.getPolicyModelYaml(); + Assert.assertEquals(testyaml, Optional + .ofNullable(ResourceFileUtil.getResourceAsString("example/sdc/expected-result/policy-data.yaml"))); + } + @Test @Transactional public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException, - CsarHandlerException, IOException, InterruptedException, PolicyModelException { + CsarHandlerException, IOException, InterruptedException, BlueprintParserException { String generatedName = RandomStringUtils.randomAlphanumeric(5); CsarHandler csarHandler = buildFakeCsarHandler(generatedName); assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isFalse(); @@ -152,27 +196,52 @@ public class CsarInstallerItCase { @Test @Transactional + @Commit public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException, - CsarHandlerException, IOException, JSONException, InterruptedException, PolicyModelException { + CsarHandlerException, IOException, JSONException, InterruptedException, BlueprintParserException { 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(); + assertThat(serviceRepository.existsById("63cac700-ab9a-4115-a74f-7eac85e3fce0")).isTrue(); + assertThat(loopTemplatesRepo.existsById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0", + RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml"))).isTrue(); + assertThat(loopTemplatesRepo.existsById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0", + RESOURCE_INSTANCE_NAME_RESOURCE1, "tca_3.yaml"))).isTrue(); + assertThat(loopTemplatesRepo.existsById(LoopTemplate.generateLoopTemplateName(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(); + LoopTemplate loopTemplate = loopTemplatesRepo.findById(LoopTemplate.generateLoopTemplateName(generatedName, + "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get(); + assertThat(loopTemplate.getSvgRepresentation()).startsWith("