Fix the security issue
[clamp.git] / src / test / java / org / onap / clamp / clds / sdc / controller / installer / LoopCsarInstallerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.clds.sdc.controller.installer;
26
27 import com.google.gson.JsonObject;
28
29 import java.io.IOException;
30 import java.io.InputStream;
31
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;
54
55 @RunWith(MockitoJUnitRunner.class)
56 public class LoopCsarInstallerTest {
57
58     @Mock
59     private CsarHandler csarHandler;
60
61     @Mock
62     private ApplicationContext applicationContext;
63
64     @Mock
65     private DcaeInventoryServices dcaeInventoryServices;
66
67     @Mock
68     private IResourceInstance resourceInstance;
69
70     @Mock
71     private CldsService cldsService;
72
73     @Mock
74     private INotificationData notificationData;
75
76     @Mock
77     private Metadata metadata;
78
79     @Mock
80     private ISdcCsarHelper sdcCsarHelper;
81
82     private CsarInstallerImpl csarInstaller;
83     private BlueprintArtifact artifact;
84
85     /**
86      * Set up method. throws: Exception
87      */
88     @Before
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());
94     }
95
96     @Test
97     public void shouldReturnInputParametersFromBlueprint() {
98         // given
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\"}";
107
108         JsonObject expectedBlueprintInputs = JsonUtils.GSON.fromJson(expectedBlueprintInputsText, JsonObject.class);
109         // when
110         String parametersInJson = csarInstaller.getAllBlueprintParametersInJson(artifact);
111         // then
112         Assertions.assertThat(JsonUtils.GSON.fromJson(parametersInJson, JsonObject.class))
113             .isEqualTo(expectedBlueprintInputs);
114     }
115
116     @Test
117     public void shouldReturnBuildModelName() throws SdcArtifactInstallerException {
118         // given
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");
123         // when
124         String actualModelName = CsarInstallerImpl.buildModelName(csarHandler, artifact);
125         // then
126         Assertions.assertThat(actualModelName).isEqualTo(expectedModelName);
127     }
128
129     @Test
130     public void shouldReturnRightMapping() throws SdcArtifactInstallerException, IOException {
131         // given
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();
142         // when
143         BlueprintParserFilesConfiguration configuration = csarInstaller.searchForRightMapping(artifact);
144
145         // then
146         Assertions.assertThat(configuration.getBpmnXmlFilePath()).isEqualTo("samplePath");
147         Assertions.assertThat(configuration.getSvgXmlFilePath()).isEqualTo("samplePath");
148     }
149
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);
156         return artifact;
157     }
158
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);
166     }
167 }