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 * ===================================================================
26 package org.onap.clamp.clds.sdc.controller.installer;
28 import com.google.gson.JsonObject;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import org.apache.commons.io.IOUtils;
32 import org.assertj.core.api.Assertions;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.runners.MockitoJUnitRunner;
39 import org.onap.clamp.clds.client.DcaeInventoryServices;
40 import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration;
41 import org.onap.clamp.clds.dao.CldsDao;
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;
90 public void setUp() throws Exception {
91 String dceaBlueprint = ResourceFileUtil.getResourceAsString("tosca/dcea_blueprint.yml");
92 artifact = prepareBlueprintArtifact(dceaBlueprint);
93 csarInstaller = new CsarInstallerImpl(applicationContext, new CldsDao(), new CldsTemplateService(),
94 cldsService, dcaeInventoryServices, new XslTransformer());
98 public void shouldReturnInputParametersFromBlueprint() {
100 String expectedBlueprintInputsText = "{\"aaiEnrichmentHost\":\"aai.onap.svc.cluster.local\""
101 + ",\"aaiEnrichmentPort\":\"8443\""
102 + ",\"enableAAIEnrichment\":true"
103 + ",\"dmaap_host\":\"message-router\""
104 + ",\"dmaap_port\":\"3904\""
105 + ",\"enableRedisCaching\":false"
106 + ",\"redisHosts\":\"dcae-redis:6379\""
107 + ",\"tag_version\":"
108 + "\"nexus3.onap.org:10001/onap/org.onap.dcaegen2.deployments.tca-cdap-container:1.1.0\""
109 + ",\"consul_host\":\"consul-server\""
110 + ",\"consul_port\":\"8500\",\"cbs_host\":\"{\\\"test\\\":"
111 + "{\\\"test\\\":\\\"test\\\"}}\",\"cbs_port\":\"10000\""
112 + ",\"external_port\":\"32010\",\"policy_id\":\"AUTO_GENERATED_POLICY_ID_AT_SUBMIT\"}";
114 JsonObject expectedBlueprintInputs = JsonUtils.GSON.fromJson(expectedBlueprintInputsText, JsonObject.class);
116 String parametersInJson = csarInstaller.getAllBlueprintParametersInJson(artifact);
118 Assertions.assertThat(JsonUtils.GSON.fromJson(parametersInJson, JsonObject.class))
119 .isEqualTo(expectedBlueprintInputs);
123 public void shouldReturnBuildModelName() throws SdcArtifactInstallerException {
125 String expectedModelName = "CLAMP_test_name_"
126 + "vtest_service_version_"
127 + "test_resource_instance_name_"
128 + "test_artifact_name";
129 prepareMockCsarHandler("name", "test_name",
130 "test_service_version");
131 Mockito.when(resourceInstance.getResourceInstanceName()).thenReturn("test_resource_instance_name");
133 String actualModelName = CsarInstallerImpl.buildModelName(csarHandler, artifact);
135 Assertions.assertThat(actualModelName).isEqualTo(expectedModelName);
139 public void shouldReturnRightMapping() throws SdcArtifactInstallerException, IOException {
141 String input = "[{\"blueprintKey\":\"tca_k8s\","
142 + "\"dcaeDeployable\":false,"
143 + "\"files\":{\"svgXmlFilePath\":\"samplePath\",\"bpmnXmlFilePath\":\"samplePath\"}}]";
144 BlueprintParserFilesConfiguration filesConfiguration = new BlueprintParserFilesConfiguration();
145 filesConfiguration.setBpmnXmlFilePath("samplePath");
146 filesConfiguration.setSvgXmlFilePath("samplePath");
147 Resource resource = Mockito.mock(Resource.class);
148 InputStream inputStream = IOUtils.toInputStream(input, "UTF-8");
149 Mockito.when(applicationContext.getResource(Mockito.any(String.class))).thenReturn(resource);
150 Mockito.when(resource.getInputStream()).thenReturn(inputStream);
151 csarInstaller.loadConfiguration();
153 BlueprintParserFilesConfiguration configuration = csarInstaller.searchForRightMapping(artifact);
156 Assertions.assertThat(configuration.getBpmnXmlFilePath()).isEqualTo("samplePath");
157 Assertions.assertThat(configuration.getSvgXmlFilePath()).isEqualTo("samplePath");
160 private BlueprintArtifact prepareBlueprintArtifact(String dceaBlueprint) {
161 artifact = new BlueprintArtifact();
162 artifact.setBlueprintArtifactName("test_artifact_name");
163 artifact.setBlueprintInvariantServiceUuid("test_inv_uuid");
164 artifact.setResourceAttached(resourceInstance);
165 artifact.setDcaeBlueprint(dceaBlueprint);
169 private void prepareMockCsarHandler(String metadataNameMockInput, String metadataNameMockOutput,
170 String serviceVersion) {
171 Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcCsarHelper);
172 Mockito.when(sdcCsarHelper.getServiceMetadata()).thenReturn(metadata);
173 Mockito.when(metadata.getValue(metadataNameMockInput)).thenReturn(metadataNameMockOutput);
174 Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
175 Mockito.when(notificationData.getServiceVersion()).thenReturn(serviceVersion);