Update cadi to 2.1.10 and updated keystore
[clamp.git] / src / test / java / org / onap / clamp / clds / sdc / controller / installer / CsarInstallerImplTest.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
26 package org.onap.clamp.clds.sdc.controller.installer;
27
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;
54
55 @RunWith(MockitoJUnitRunner.class)
56 public class CsarInstallerImplTest {
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.
87      * throws: Exception
88      */
89     @Before
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());
95     }
96
97     @Test
98     public void shouldReturnInputParametersFromBlueprint() {
99         //given
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\"}";
113
114         JsonObject expectedBlueprintInputs = JsonUtils.GSON.fromJson(expectedBlueprintInputsText, JsonObject.class);
115         //when
116         String parametersInJson = csarInstaller.getAllBlueprintParametersInJson(artifact);
117         //then
118         Assertions.assertThat(JsonUtils.GSON.fromJson(parametersInJson, JsonObject.class))
119                 .isEqualTo(expectedBlueprintInputs);
120     }
121
122     @Test
123     public void shouldReturnBuildModelName() throws SdcArtifactInstallerException {
124         //given
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");
132         //when
133         String actualModelName = CsarInstallerImpl.buildModelName(csarHandler, artifact);
134         //then
135         Assertions.assertThat(actualModelName).isEqualTo(expectedModelName);
136     }
137
138     @Test
139     public void shouldReturnRightMapping() throws SdcArtifactInstallerException, IOException {
140         //given
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();
152         //when
153         BlueprintParserFilesConfiguration configuration = csarInstaller.searchForRightMapping(artifact);
154
155         //then
156         Assertions.assertThat(configuration.getBpmnXmlFilePath()).isEqualTo("samplePath");
157         Assertions.assertThat(configuration.getSvgXmlFilePath()).isEqualTo("samplePath");
158     }
159
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);
166         return artifact;
167     }
168
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);
176     }
177 }