diff options
Diffstat (limited to 'src/test/java')
3 files changed, 398 insertions, 22 deletions
diff --git a/src/test/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParserTest.java b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParserTest.java new file mode 100644 index 000000000..2a2ab94e6 --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParserTest.java @@ -0,0 +1,190 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ +package org.onap.clamp.clds.sdc.controller.installer; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.clamp.clds.util.ResourceFileUtil; +import org.yaml.snakeyaml.Yaml; + +public class BlueprintParserTest { + private static final Gson GSON = new Gson(); + private static final String FIRST_APPP = "first_app"; + private static final String SECOND_APPP = "second_app"; + private static final String THIRD_APPP = "third_app"; + + private static String microServiceTheWholeBlueprintValid; + private static String microServiceBlueprintOldStyleTCA; + private static String microServiceBlueprintOldStyleHolmes; + + private static JsonObject jsonObjectBlueprintValid; + private static JsonObject jsonObjectBlueprintWithoutName; + private static JsonObject jsonObjectBlueprintWithoutProperties; + private static JsonObject jsonObjectBlueprintWithoutRelationships; + + @BeforeClass + public static void loadBlueprints() throws IOException { + microServiceTheWholeBlueprintValid = ResourceFileUtil + .getResourceAsString("clds/blueprint-with-microservice-chain.yaml"); + microServiceBlueprintOldStyleTCA = ResourceFileUtil + .getResourceAsString("clds/tca-old-style-ms.yaml"); + microServiceBlueprintOldStyleHolmes = ResourceFileUtil + .getResourceAsString("clds/holmes-old-style-ms.yaml"); + + String microServiceBlueprintValid = ResourceFileUtil + .getResourceAsString("clds/single-microservice-fragment-valid.yaml"); + String microServiceBlueprintWithoutName = ResourceFileUtil + .getResourceAsString("clds/single-microservice-fragment-without-name.yaml"); + String microServiceBlueprintWithoutProperties = ResourceFileUtil + .getResourceAsString("clds/single-microservice-fragment-without-properties.yaml"); + String microServiceBlueprintWithoutRelationships = ResourceFileUtil + .getResourceAsString("clds/single-microservice-fragment-without-relationships.yaml"); + + jsonObjectBlueprintValid = yamlToJson(microServiceBlueprintValid); + jsonObjectBlueprintWithoutName = yamlToJson(microServiceBlueprintWithoutName); + jsonObjectBlueprintWithoutProperties = yamlToJson(microServiceBlueprintWithoutProperties); + jsonObjectBlueprintWithoutRelationships = yamlToJson(microServiceBlueprintWithoutRelationships); + + } + + @Test + public void getNameShouldReturnDefinedName() { + final JsonObject jsonObject = jsonObjectBlueprintValid; + String expectedName = jsonObject.get(jsonObject.keySet().iterator().next()) + .getAsJsonObject().get("properties") + .getAsJsonObject().get("name") + .getAsString(); + Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next(); + String actualName = new BlueprintParser().getName(entry); + + Assert.assertEquals(expectedName, actualName); + } + + @Test + public void getNameShouldReturnServiceNameWhenNoNameDefined() { + final JsonObject jsonObject = jsonObjectBlueprintWithoutName; + + String expectedName = jsonObject.keySet().iterator().next(); + Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next(); + String actualName = new BlueprintParser().getName(entry); + + Assert.assertEquals(expectedName, actualName); + } + + @Test + public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() { + final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties; + + String expectedName = jsonObject.keySet().iterator().next(); + Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next(); + String actualName = new BlueprintParser().getName(entry); + + Assert.assertEquals(expectedName, actualName); + } + + @Test + public void getInputShouldReturnInputWhenPresent() { + final JsonObject jsonObject = jsonObjectBlueprintValid; + + String expected = FIRST_APPP; + Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next(); + String actual = new BlueprintParser().getInput(entry); + + Assert.assertEquals(expected, actual); + } + + @Test + public void getInputShouldReturnEmptyStringWhenAbsent() { + final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships; + + String expected = ""; + Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next(); + String actual = new BlueprintParser().getInput(entry); + + Assert.assertEquals(expected, actual); + } + + @Test + public void getNodeRepresentationFromCompleteYaml() { + final JsonObject jsonObject = jsonObjectBlueprintValid; + + MicroService expected = new MicroService(SECOND_APPP, FIRST_APPP); + Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next(); + MicroService actual = new BlueprintParser().getNodeRepresentation(entry); + + Assert.assertEquals(expected, actual); + } + + @Test + public void getMicroServicesFromBlueprintTest() { + MicroService thirdApp = new MicroService(THIRD_APPP, ""); + MicroService firstApp = new MicroService(FIRST_APPP, THIRD_APPP); + MicroService secondApp = new MicroService(SECOND_APPP, FIRST_APPP); + + Set<MicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp)); + Set<MicroService> actual = new BlueprintParser().getMicroServices(microServiceTheWholeBlueprintValid); + + Assert.assertEquals(expected, actual); + } + + @Test + public void fallBackToOneMicroServiceTCATest() { + MicroService tcaMS = new MicroService(BlueprintParser.TCA, ""); + + List<MicroService> expected = Collections.singletonList(tcaMS); + List<MicroService> actual = new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleTCA); + + Assert.assertEquals(expected, actual); + } + + @Test + public void fallBackToOneMicroServiceHolmesTest() { + MicroService holmesMS = new MicroService(BlueprintParser.HOLMES, ""); + + List<MicroService> expected = Collections.singletonList(holmesMS); + List<MicroService> actual = + new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleHolmes); + + Assert.assertEquals(expected, actual); + } + + private static JsonObject yamlToJson(String yamlString) { + Yaml yaml = new Yaml(); + Map<String, Object> map = yaml.load(yamlString); + JSONObject jsonObject = new JSONObject(map); + return GSON.fromJson(jsonObject.toString(), JsonObject.class); + } +} diff --git a/src/test/java/org/onap/clamp/clds/sdc/controller/installer/ChainGeneratorTest.java b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/ChainGeneratorTest.java new file mode 100644 index 000000000..9573515d2 --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/ChainGeneratorTest.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ +package org.onap.clamp.clds.sdc.controller.installer; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.junit.Assert; +import org.junit.Test; + +public class ChainGeneratorTest { + private static final String FIRST_APPP = "first_app"; + private static final String SECOND_APPP = "second_app"; + private static final String THIRD_APPP = "third_app"; + private static final String FOURTH_APPP = "fourth_app"; + + @Test + public void getChainOfMicroServicesTest() { + MicroService ms1 = new MicroService(FIRST_APPP, ""); + MicroService ms2 = new MicroService(SECOND_APPP, FIRST_APPP); + MicroService ms3 = new MicroService(THIRD_APPP, SECOND_APPP); + MicroService ms4 = new MicroService(FOURTH_APPP, THIRD_APPP); + + List<MicroService> expectedList = Arrays.asList(ms1, ms2, ms3, ms4); + Set<MicroService> inputSet = new HashSet<>(expectedList); + + List<MicroService> actualList = new ChainGenerator().getChainOfMicroServices(inputSet); + Assert.assertEquals(expectedList, actualList); + } + + @Test + public void getChainOfMicroServicesTwiceNoInputTest() { + MicroService ms1 = new MicroService(FIRST_APPP, ""); + MicroService ms2 = new MicroService(SECOND_APPP, ""); + MicroService ms3 = new MicroService(THIRD_APPP, SECOND_APPP); + MicroService ms4 = new MicroService(FOURTH_APPP, FIRST_APPP); + + Set<MicroService> inputSet = new HashSet<>(Arrays.asList(ms1, ms2, ms3, ms4)); + List<MicroService> actualList = new ChainGenerator().getChainOfMicroServices(inputSet); + Assert.assertTrue(actualList.isEmpty()); + } + + @Test + public void getChainOfMicroServicesBranchingTest() { + MicroService ms1 = new MicroService(FIRST_APPP, ""); + MicroService ms2 = new MicroService(SECOND_APPP, FIRST_APPP); + MicroService ms3 = new MicroService(THIRD_APPP, FIRST_APPP); + MicroService ms4 = new MicroService(FOURTH_APPP, FIRST_APPP); + + Set<MicroService> inputSet = new HashSet<>(Arrays.asList(ms1, ms2, ms3, ms4)); + List<MicroService> actualList = new ChainGenerator().getChainOfMicroServices(inputSet); + Assert.assertTrue(actualList.isEmpty()); + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImplTest.java b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImplTest.java index a995c523d..1fe3ff3d9 100644 --- a/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImplTest.java +++ b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImplTest.java @@ -17,50 +17,161 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END============================================ + * * Modifications copyright (c) 2019 Nokia * =================================================================== * */ -package org.onap.clamp.clds.sdc.controller.installer; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +package org.onap.clamp.clds.sdc.controller.installer; import com.google.gson.JsonObject; import java.io.IOException; +import java.io.InputStream; +import org.apache.commons.io.IOUtils; +import org.assertj.core.api.Assertions; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.clamp.clds.client.DcaeInventoryServices; +import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration; +import org.onap.clamp.clds.dao.CldsDao; +import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException; +import org.onap.clamp.clds.service.CldsService; +import org.onap.clamp.clds.service.CldsTemplateService; +import org.onap.clamp.clds.transform.XslTransformer; import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.clds.util.ResourceFileUtil; +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.toscaparser.api.elements.Metadata; +import org.springframework.context.ApplicationContext; +import org.springframework.core.io.Resource; +@RunWith(MockitoJUnitRunner.class) public class CsarInstallerImplTest { + @Mock + private CsarHandler csarHandler; + + @Mock + private ApplicationContext applicationContext; + + @Mock + private DcaeInventoryServices dcaeInventoryServices; + + @Mock + private IResourceInstance resourceInstance; + + @Mock + private CldsService cldsService; + + @Mock + private INotificationData notificationData; + + @Mock + private Metadata metadata; + + @Mock + private ISdcCsarHelper sdcCsarHelper; + + private CsarInstallerImpl csarInstaller; + private BlueprintArtifact artifact; + + /** + * Set up method. + * throws: Exception + */ + @Before + public void setUp() throws Exception { + String dceaBlueprint = ResourceFileUtil.getResourceAsString("tosca/dcea_blueprint.yml"); + artifact = prepareBlueprintArtifact(dceaBlueprint); + csarInstaller = new CsarInstallerImpl(applicationContext, new CldsDao(), new CldsTemplateService(), + cldsService, dcaeInventoryServices, new XslTransformer()); + } + @Test - public void shouldReturnInputParametersFromBlueprint() throws IOException { + public void shouldReturnInputParametersFromBlueprint() { //given String expectedBlueprintInputsText = "{\"aaiEnrichmentHost\":\"aai.onap.svc.cluster.local\"" - + ",\"aaiEnrichmentPort\":\"8443\"" - + ",\"enableAAIEnrichment\":true" - + ",\"dmaap_host\":\"message-router\"" - + ",\"dmaap_port\":\"3904\"" - + ",\"enableRedisCaching\":false" - + ",\"redisHosts\":\"dcae-redis:6379\"" - + ",\"tag_version\":\"nexus3.onap.org:10001/onap/org.onap.dcaegen2.deployments.tca-cdap-container:1.1.0\"" - + ",\"consul_host\":\"consul-server\"" - + ",\"consul_port\":\"8500\",\"cbs_host\":\"{\\\"test\\\":" - + "{\\\"test\\\":\\\"test\\\"}}\",\"cbs_port\":\"10000\"" - + ",\"external_port\":\"32010\",\"policy_id\":\"AUTO_GENERATED_POLICY_ID_AT_SUBMIT\"}"; + + ",\"aaiEnrichmentPort\":\"8443\"" + + ",\"enableAAIEnrichment\":true" + + ",\"dmaap_host\":\"message-router\"" + + ",\"dmaap_port\":\"3904\"" + + ",\"enableRedisCaching\":false" + + ",\"redisHosts\":\"dcae-redis:6379\"" + + ",\"tag_version\":" + + "\"nexus3.onap.org:10001/onap/org.onap.dcaegen2.deployments.tca-cdap-container:1.1.0\"" + + ",\"consul_host\":\"consul-server\"" + + ",\"consul_port\":\"8500\",\"cbs_host\":\"{\\\"test\\\":" + + "{\\\"test\\\":\\\"test\\\"}}\",\"cbs_port\":\"10000\"" + + ",\"external_port\":\"32010\",\"policy_id\":\"AUTO_GENERATED_POLICY_ID_AT_SUBMIT\"}"; JsonObject expectedBlueprintInputs = JsonUtils.GSON.fromJson(expectedBlueprintInputsText, JsonObject.class); - String dceaBlueprint = ResourceFileUtil.getResourceAsString("tosca/dcea_blueprint.yml"); - BlueprintArtifact blueprintArtifact = mock(BlueprintArtifact.class); - when(blueprintArtifact.getDcaeBlueprint()).thenReturn(dceaBlueprint); - CsarInstallerImpl csarInstaller = new CsarInstallerImpl(); + //when + String parametersInJson = csarInstaller.getAllBlueprintParametersInJson(artifact); + //then + Assertions.assertThat(JsonUtils.GSON.fromJson(parametersInJson, JsonObject.class)) + .isEqualTo(expectedBlueprintInputs); + } + @Test + public void shouldReturnBuildModelName() throws SdcArtifactInstallerException { + //given + String expectedModelName = "CLAMP_test_name_" + + "vtest_service_version_" + + "test_resource_instance_name_" + + "test_artifact_name"; + prepareMockCsarHandler("name", "test_name", + "test_service_version"); + Mockito.when(resourceInstance.getResourceInstanceName()).thenReturn("test_resource_instance_name"); //when - String parametersInJson = csarInstaller.getAllBlueprintParametersInJson(blueprintArtifact); + String actualModelName = CsarInstallerImpl.buildModelName(csarHandler, artifact); + //then + Assertions.assertThat(actualModelName).isEqualTo(expectedModelName); + } + + @Test + public void shouldReturnRightMapping() throws SdcArtifactInstallerException, IOException { + //given + String input = "[{\"blueprintKey\":\"tca_k8s\"," + + "\"dcaeDeployable\":false," + + "\"files\":{\"svgXmlFilePath\":\"samplePath\",\"bpmnXmlFilePath\":\"samplePath\"}}]"; + BlueprintParserFilesConfiguration filesConfiguration = new BlueprintParserFilesConfiguration(); + filesConfiguration.setBpmnXmlFilePath("samplePath"); + filesConfiguration.setSvgXmlFilePath("samplePath"); + Resource resource = Mockito.mock(Resource.class); + InputStream inputStream = IOUtils.toInputStream(input, "UTF-8"); + Mockito.when(applicationContext.getResource(Mockito.any(String.class))).thenReturn(resource); + Mockito.when(resource.getInputStream()).thenReturn(inputStream); + csarInstaller.loadConfiguration(); + //when + BlueprintParserFilesConfiguration configuration = csarInstaller.searchForRightMapping(artifact); //then - assertThat(JsonUtils.GSON.fromJson(parametersInJson, JsonObject.class)).isEqualTo(expectedBlueprintInputs); + Assertions.assertThat(configuration.getBpmnXmlFilePath()).isEqualTo("samplePath"); + Assertions.assertThat(configuration.getSvgXmlFilePath()).isEqualTo("samplePath"); + } + + private BlueprintArtifact prepareBlueprintArtifact(String dceaBlueprint) { + artifact = new BlueprintArtifact(); + artifact.setBlueprintArtifactName("test_artifact_name"); + artifact.setBlueprintInvariantServiceUuid("test_inv_uuid"); + artifact.setResourceAttached(resourceInstance); + artifact.setDcaeBlueprint(dceaBlueprint); + return artifact; + } + + private void prepareMockCsarHandler(String metadataNameMockInput, String metadataNameMockOutput, + String serviceVersion) { + Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcCsarHelper); + Mockito.when(sdcCsarHelper.getServiceMetadata()).thenReturn(metadata); + Mockito.when(metadata.getValue(metadataNameMockInput)).thenReturn(metadataNameMockOutput); + Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData); + Mockito.when(notificationData.getServiceVersion()).thenReturn(serviceVersion); } }
\ No newline at end of file |