2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * * Modifications copyright (c) 2019 Nokia
21 * ===================================================================
25 package org.onap.clamp.clds.sdc.controller.installer;
27 import com.google.gson.JsonObject;
29 import java.io.IOException;
30 import java.io.InputStream;
32 import org.apache.commons.io.IOUtils;
33 import org.assertj.core.api.Assertions;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.clamp.clds.client.DcaeInventoryServices;
41 import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration;
42 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
43 import org.onap.clamp.clds.service.CldsService;
44 import org.onap.clamp.clds.service.CldsTemplateService;
45 import org.onap.clamp.clds.transform.XslTransformer;
46 import org.onap.clamp.clds.util.JsonUtils;
47 import org.onap.clamp.clds.util.ResourceFileUtil;
48 import org.onap.sdc.api.notification.INotificationData;
49 import org.onap.sdc.api.notification.IResourceInstance;
50 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
51 import org.onap.sdc.toscaparser.api.elements.Metadata;
52 import org.springframework.context.ApplicationContext;
53 import org.springframework.core.io.Resource;
55 @RunWith(MockitoJUnitRunner.class)
56 public class CsarInstallerImplTest {
59 private CsarHandler csarHandler;
62 private ApplicationContext applicationContext;
65 private DcaeInventoryServices dcaeInventoryServices;
68 private IResourceInstance resourceInstance;
71 private CldsService cldsService;
74 private INotificationData notificationData;
77 private Metadata metadata;
80 private ISdcCsarHelper sdcCsarHelper;
82 private CsarInstallerImpl csarInstaller;
83 private BlueprintArtifact artifact;
86 * Set up method. throws: Exception
89 public void setUp() throws Exception {
90 String dceaBlueprint = ResourceFileUtil.getResourceAsString("tosca/dcea_blueprint.yml");
91 artifact = prepareBlueprintArtifact(dceaBlueprint);
92 csarInstaller = new CsarInstallerImpl(applicationContext, null, new CldsTemplateService(), cldsService,
93 dcaeInventoryServices, new XslTransformer());
97 public void shouldReturnInputParametersFromBlueprint() {
99 String expectedBlueprintInputsText = "{\"aaiEnrichmentHost\":\"aai.onap.svc.cluster.local\""
100 + ",\"aaiEnrichmentPort\":\"8443\"" + ",\"enableAAIEnrichment\":true" + ",\"dmaap_host\":\"message-router\""
101 + ",\"dmaap_port\":\"3904\"" + ",\"enableRedisCaching\":false" + ",\"redisHosts\":\"dcae-redis:6379\""
102 + ",\"tag_version\":"
103 + "\"nexus3.onap.org:10001/onap/org.onap.dcaegen2.deployments.tca-cdap-container:1.1.0\""
104 + ",\"consul_host\":\"consul-server\"" + ",\"consul_port\":\"8500\",\"cbs_host\":\"{\\\"test\\\":"
105 + "{\\\"test\\\":\\\"test\\\"}}\",\"cbs_port\":\"10000\""
106 + ",\"external_port\":\"32010\",\"policy_id\":\"AUTO_GENERATED_POLICY_ID_AT_SUBMIT\"}";
108 JsonObject expectedBlueprintInputs = JsonUtils.GSON.fromJson(expectedBlueprintInputsText, JsonObject.class);
110 String parametersInJson = csarInstaller.getAllBlueprintParametersInJson(artifact);
112 Assertions.assertThat(JsonUtils.GSON.fromJson(parametersInJson, JsonObject.class))
113 .isEqualTo(expectedBlueprintInputs);
117 public void shouldReturnBuildModelName() throws SdcArtifactInstallerException {
119 String expectedModelName = "CLAMP_test_name_" + "vtest_service_version_" + "test_resource_instance_name_"
120 + "test_artifact_name";
121 prepareMockCsarHandler("name", "test_name", "test_service_version");
122 Mockito.when(resourceInstance.getResourceInstanceName()).thenReturn("test_resource_instance_name");
124 String actualModelName = CsarInstallerImpl.buildModelName(csarHandler, artifact);
126 Assertions.assertThat(actualModelName).isEqualTo(expectedModelName);
130 public void shouldReturnRightMapping() throws SdcArtifactInstallerException, IOException {
132 String input = "[{\"blueprintKey\":\"tca_k8s\"," + "\"dcaeDeployable\":false,"
133 + "\"files\":{\"svgXmlFilePath\":\"samplePath\",\"bpmnXmlFilePath\":\"samplePath\"}}]";
134 BlueprintParserFilesConfiguration filesConfiguration = new BlueprintParserFilesConfiguration();
135 filesConfiguration.setBpmnXmlFilePath("samplePath");
136 filesConfiguration.setSvgXmlFilePath("samplePath");
137 Resource resource = Mockito.mock(Resource.class);
138 InputStream inputStream = IOUtils.toInputStream(input, "UTF-8");
139 Mockito.when(applicationContext.getResource(Mockito.any(String.class))).thenReturn(resource);
140 Mockito.when(resource.getInputStream()).thenReturn(inputStream);
141 csarInstaller.loadConfiguration();
143 BlueprintParserFilesConfiguration configuration = csarInstaller.searchForRightMapping(artifact);
146 Assertions.assertThat(configuration.getBpmnXmlFilePath()).isEqualTo("samplePath");
147 Assertions.assertThat(configuration.getSvgXmlFilePath()).isEqualTo("samplePath");
150 private BlueprintArtifact prepareBlueprintArtifact(String dceaBlueprint) {
151 artifact = new BlueprintArtifact();
152 artifact.setBlueprintArtifactName("test_artifact_name");
153 artifact.setBlueprintInvariantServiceUuid("test_inv_uuid");
154 artifact.setResourceAttached(resourceInstance);
155 artifact.setDcaeBlueprint(dceaBlueprint);
159 private void prepareMockCsarHandler(String metadataNameMockInput, String metadataNameMockOutput,
160 String serviceVersion) {
161 Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcCsarHelper);
162 Mockito.when(sdcCsarHelper.getServiceMetadata()).thenReturn(metadata);
163 Mockito.when(metadata.getValue(metadataNameMockInput)).thenReturn(metadataNameMockOutput);
164 Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
165 Mockito.when(notificationData.getServiceVersion()).thenReturn(serviceVersion);