fec288a953ceeb2bf22010202d05eb68768f63d4
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / csar / CsarBusinessLogicTest.java
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (c) 2018 Huawei Intellectual Property.
5  *  Modifications Copyright (c) 2019 Samsung.
6  *  Modifications Copyright (c) 2021 Nordix Foundation.
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  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.openecomp.sdc.be.components.csar;
25
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertThrows;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.ArgumentMatchers.anyString;
32 import static org.mockito.ArgumentMatchers.eq;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35
36 import fj.data.Either;
37 import java.io.IOException;
38 import java.net.URISyntaxException;
39 import java.nio.file.Files;
40 import java.nio.file.Paths;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Optional;
48 import org.junit.jupiter.api.BeforeEach;
49 import org.junit.jupiter.api.Test;
50 import org.mockito.Mockito;
51 import org.openecomp.sdc.be.components.impl.BaseBusinessLogicMock;
52 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
53 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
54 import org.openecomp.sdc.be.dao.api.ActionStatus;
55 import org.openecomp.sdc.be.impl.ComponentsUtils;
56 import org.openecomp.sdc.be.model.Resource;
57 import org.openecomp.sdc.be.model.Service;
58 import org.openecomp.sdc.be.model.User;
59 import org.openecomp.sdc.be.model.VendorSoftwareProduct;
60 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
61 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
62 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
63 import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
64 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
65 import org.openecomp.sdc.common.zip.ZipUtils;
66 import org.openecomp.sdc.common.zip.exception.ZipException;
67 import org.openecomp.sdc.exception.ResponseFormat;
68
69
70 class CsarBusinessLogicTest extends BaseBusinessLogicMock {
71
72     private final CsarOperation csarOperation = Mockito.mock(CsarOperation.class);
73     private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
74     private final ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class);
75     private final User user = Mockito.mock(User.class);
76     private final YamlTemplateParsingHandler yamlHandler = Mockito.mock(YamlTemplateParsingHandler.class);
77     private final ModelOperation modelOperation = Mockito.mock(ModelOperation.class);
78
79     private final CsarBusinessLogic csarBusinessLogic = new CsarBusinessLogic(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
80         interfaceOperation, interfaceLifecycleTypeOperation, yamlHandler, artifactToscaOperation, modelOperation);
81
82     private static final String CSAR_UUID = "csarUUID";
83     private static final String CSAR_ENTRY = "Definitions/tosca_mock_vf.yaml";
84     private static final String CSAR_METADATA = "TOSCA-Metadata/TOSCA.meta";
85     private static final String CSAR_METADATA_CONTENT = "TOSCA-Meta-File-Version: 1.0\n" +
86             "CSAR-Version: 1.1\n" +
87             "Created-By: OASIS TOSCA TC\n" +
88             "Entry-Definitions:" + CSAR_ENTRY;
89     private static final String CSAR_ENTRY_CONTENT = "tosca_definitions_version: tosca_simple_yaml_1_0\n";
90
91     private static final String RESOURCE_NAME = "resourceName";
92     private static final String PAYLOAD_NAME = "mock_vf.csar";
93
94     @BeforeEach
95     void setUp() throws Exception {
96         csarBusinessLogic.setCsarOperation(csarOperation);
97         csarBusinessLogic.setToscaOperationFacade(toscaOperationFacade);
98         csarBusinessLogic.setComponentsUtils(componentsUtils);
99     }
100
101     @Test
102     void testGetCsarInfo() {
103         // given
104         Resource resource = new Resource();
105         resource.setName(RESOURCE_NAME);
106         resource.setCsarUUID(CSAR_UUID);
107         resource.setCsarVersionId("csarVersionId");
108
109         Map<String, byte[]> csar_data = new HashMap<>();
110         csar_data.put(CSAR_METADATA, CSAR_METADATA_CONTENT.getBytes());
111         csar_data.put(CSAR_ENTRY, CSAR_ENTRY_CONTENT.getBytes());
112         final var vendorSoftwareProduct = new VendorSoftwareProduct();
113         vendorSoftwareProduct.setFileMap(csar_data);
114         vendorSoftwareProduct.setModelList(Collections.emptyList());
115         when(csarOperation.findVsp(eq(resource.getCsarUUID()), eq(resource.getCsarVersionId()), any(User.class)))
116             .thenReturn(Optional.of(vendorSoftwareProduct));
117
118         // when
119         final CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID);
120
121         // then
122         assertNotNull(csarInfo);
123
124         assertEquals(resource.getCsarUUID(), csarInfo.getCsarUUID());
125         assertEquals(resource.getCsarVersionId(), csarInfo.getCsarVersionId());
126         assertEquals(CSAR_ENTRY, csarInfo.getMainTemplateName());
127         assertEquals(RESOURCE_NAME, csarInfo.getVfResourceName());
128
129         assertEquals(CSAR_ENTRY_CONTENT, csarInfo.getMainTemplateContent());
130         assertTrue(csarInfo.getCsar().keySet().containsAll(Arrays.asList(CSAR_ENTRY, CSAR_METADATA)));
131     }
132
133     @Test
134     void testGetCsarInfo_vspWithModelAndResourceWithInvalidModel() {
135         final var resource = new Resource();
136         resource.setCsarUUID(CSAR_UUID);
137         final String csarVersionId = "csarVersionId";
138         resource.setCsarVersionId(csarVersionId);
139         resource.setModel("model1");
140         var vendorSoftwareProduct = new VendorSoftwareProduct();
141         final List<String> modelList = List.of("model2", "model3");
142         vendorSoftwareProduct.setModelList(modelList);
143
144         when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenReturn(Optional.of(vendorSoftwareProduct));
145
146         final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class,
147             () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID));
148         assertEquals(ActionStatus.VSP_MODEL_NOT_ALLOWED, actualException.getActionStatus());
149         assertEquals(2, actualException.getParams().length);
150         assertEquals(resource.getModel(), actualException.getParams()[0]);
151         assertEquals(String.join(", ", modelList), actualException.getParams()[1]);
152     }
153
154
155     @Test
156     void testGetCsarInfo_vspWithNoModelAndResourceWithInvalidModel() {
157         final var resource = new Resource();
158         resource.setCsarUUID(CSAR_UUID);
159         final String csarVersionId = "csarVersionId";
160         resource.setCsarVersionId(csarVersionId);
161         resource.setModel("model1");
162         var vendorSoftwareProduct = new VendorSoftwareProduct();
163         final List<String> modelList = new ArrayList<>();
164         vendorSoftwareProduct.setModelList(modelList);
165
166         when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenReturn(Optional.of(vendorSoftwareProduct));
167
168         final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class,
169             () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID));
170         assertEquals(ActionStatus.VSP_MODEL_NOT_ALLOWED, actualException.getActionStatus());
171         assertEquals(2, actualException.getParams().length);
172         assertEquals(resource.getModel(), actualException.getParams()[0]);
173         assertEquals("SDC AID", actualException.getParams()[1]);
174     }
175
176     @Test
177     void testGetCsarInfo_vspWithModelAndResourceWithNoModel() {
178         final var resource = new Resource();
179         resource.setCsarUUID(CSAR_UUID);
180         final String csarVersionId = "csarVersionId";
181         resource.setCsarVersionId(csarVersionId);
182         resource.setModel(null);
183         var vendorSoftwareProduct = new VendorSoftwareProduct();
184         final List<String> modelList = List.of("model2", "model3");
185         vendorSoftwareProduct.setModelList(modelList);
186
187         when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenReturn(Optional.of(vendorSoftwareProduct));
188
189         final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class,
190             () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID));
191         assertEquals(ActionStatus.VSP_MODEL_NOT_ALLOWED, actualException.getActionStatus());
192         assertEquals(2, actualException.getParams().length);
193         assertEquals("SDC AID", actualException.getParams()[0]);
194         assertEquals(String.join(", ", modelList), actualException.getParams()[1]);
195     }
196
197     @Test
198     void testGetCsarInfo_vspWithNoModelAndResourceWithNoModel() {
199         final var resource = new Resource();
200         resource.setCsarUUID(CSAR_UUID);
201         final String csarVersionId = "csarVersionId";
202         resource.setCsarVersionId(csarVersionId);
203         resource.setModel(null);
204         var vendorSoftwareProduct = new VendorSoftwareProduct();
205         final List<String> modelList = new ArrayList<>();
206         vendorSoftwareProduct.setModelList(modelList);
207         when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenThrow(new RuntimeException());
208
209         final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class,
210             () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID));
211         assertEquals(ActionStatus.VSP_FIND_ERROR, actualException.getActionStatus());
212         assertEquals(2, actualException.getParams().length);
213         assertEquals(resource.getCsarUUID(), actualException.getParams()[0]);
214         assertEquals(resource.getCsarVersionId(), actualException.getParams()[1]);
215     }
216
217     @Test
218     void testGetCsarInfoWithPayload() throws IOException, URISyntaxException, ZipException {
219         // given
220         Resource resource = new Resource();
221         resource.setName(RESOURCE_NAME);
222
223         Map<String, byte[]> payload = loadPayload(PAYLOAD_NAME);
224
225         // when
226         CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, payload, CSAR_UUID);
227
228         // then
229         assertNotNull(csarInfo);
230
231         assertEquals(CSAR_UUID, csarInfo.getCsarUUID());
232         assertEquals(CSAR_ENTRY, csarInfo.getMainTemplateName());
233         assertEquals(RESOURCE_NAME, csarInfo.getVfResourceName());
234
235         assertTrue(csarInfo.getMainTemplateContent().startsWith(CSAR_ENTRY_CONTENT));
236         assertTrue(csarInfo.getCsar().keySet().containsAll(Arrays.asList(CSAR_ENTRY, CSAR_METADATA)));
237     }
238
239     @Test
240     void testGetCsarInfoWithBadData(){
241         // given
242         Resource resource = new Resource();
243         resource.setName(RESOURCE_NAME);
244
245         Map<String, byte[]> csar_data = new HashMap<>();
246         when(csarOperation.findVspLatestPackage(anyString(), any(User.class))).thenReturn(Either.left(csar_data));
247
248         // when/then
249         assertThrows(ComponentException.class, () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID));
250     }
251
252     @Test
253     void testValidateCsarBeforeCreate() {
254         Resource resource = new Resource();
255         StorageOperationStatus status = StorageOperationStatus.OK;
256         when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status);
257         csarBusinessLogic.validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, CSAR_UUID);
258     }
259
260     @Test
261     void testValidateCsarBeforeCreate_ResourceExists() {
262         Resource resource = new Resource();
263         ResponseFormat responseFormat = new ResponseFormat();
264         StorageOperationStatus status = StorageOperationStatus.ENTITY_ALREADY_EXISTS;
265         when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status);
266         when(componentsUtils.getResponseFormat(ActionStatus.VSP_ALREADY_EXISTS, CSAR_UUID)).thenReturn(responseFormat);
267         assertThrows(ComponentException.class, () -> csarBusinessLogic
268             .validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, CSAR_UUID));
269     }
270
271     @Test
272     void testValidateCsarBeforeCreate_ServiceExists() {
273         final var  service = new Service();
274         final var status = StorageOperationStatus.ENTITY_ALREADY_EXISTS;
275         when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status);
276         csarBusinessLogic.validateCsarBeforeCreate(service, CSAR_UUID);
277         verify(toscaOperationFacade).validateCsarUuidUniqueness(CSAR_UUID);
278     }
279
280     @Test
281     void testValidateCsarBeforeCreate_ServiceValidateError() {
282         final var service = new Service();
283         final var status = StorageOperationStatus.GENERAL_ERROR;
284         when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status);
285         when(componentsUtils.convertFromStorageResponse(status)).thenReturn(ActionStatus.GENERAL_ERROR);
286         assertThrows(ComponentException.class, () -> csarBusinessLogic.validateCsarBeforeCreate(service, CSAR_UUID));
287         verify(toscaOperationFacade).validateCsarUuidUniqueness(CSAR_UUID);
288     }
289
290     @Test
291     void testValidateCsarBeforeCreate_Fail() {
292         Resource resource = new Resource();
293         String csarUUID = "csarUUID";
294         when(toscaOperationFacade.validateCsarUuidUniqueness(csarUUID)).thenReturn(StorageOperationStatus.EXEUCTION_FAILED);
295         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.EXEUCTION_FAILED)).thenReturn(ActionStatus.GENERAL_ERROR);
296         assertThrows(ComponentException.class, () -> csarBusinessLogic
297             .validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, "csarUUID"));
298     }
299
300     private Map<String, byte[]> loadPayload(String payloadName) throws IOException, URISyntaxException, ZipException {
301         var path = Paths.get(getClass().getResource("/" + payloadName).toURI());
302         byte[] data = Files.readAllBytes(path);
303
304         return ZipUtils.readZip(data, false);
305     }
306 }