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
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.openecomp.sdc.be.components.csar;
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;
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;
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;
70 class CsarBusinessLogicTest extends BaseBusinessLogicMock {
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);
79 private final CsarBusinessLogic csarBusinessLogic = new CsarBusinessLogic(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
80 interfaceOperation, interfaceLifecycleTypeOperation, yamlHandler, artifactToscaOperation, modelOperation);
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";
91 private static final String RESOURCE_NAME = "resourceName";
92 private static final String PAYLOAD_NAME = "mock_vf.csar";
95 void setUp() throws Exception {
96 csarBusinessLogic.setCsarOperation(csarOperation);
97 csarBusinessLogic.setToscaOperationFacade(toscaOperationFacade);
98 csarBusinessLogic.setComponentsUtils(componentsUtils);
102 void testGetCsarInfo() {
104 Resource resource = new Resource();
105 resource.setName(RESOURCE_NAME);
106 resource.setCsarUUID(CSAR_UUID);
107 resource.setCsarVersionId("csarVersionId");
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));
119 final CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID, null);
122 assertNotNull(csarInfo);
124 assertEquals(resource.getCsarUUID(), csarInfo.getCsarUUID());
125 assertEquals(resource.getCsarVersionId(), csarInfo.getCsarVersionId());
126 assertEquals(CSAR_ENTRY, csarInfo.getMainTemplateName());
127 assertEquals(RESOURCE_NAME, csarInfo.getVfResourceName());
129 assertEquals(CSAR_ENTRY_CONTENT, csarInfo.getMainTemplateContent());
130 assertTrue(csarInfo.getCsar().keySet().containsAll(Arrays.asList(CSAR_ENTRY, CSAR_METADATA)));
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);
144 when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenReturn(Optional.of(vendorSoftwareProduct));
146 final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class,
147 () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID, null));
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]);
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);
166 when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenReturn(Optional.of(vendorSoftwareProduct));
168 final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class,
169 () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID, null));
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]);
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);
187 when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenReturn(Optional.of(vendorSoftwareProduct));
189 final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class,
190 () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID, null));
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]);
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());
209 final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class,
210 () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID, null));
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]);
218 void testGetCsarInfoWithPayload() throws IOException, URISyntaxException, ZipException {
220 Resource resource = new Resource();
221 resource.setName(RESOURCE_NAME);
223 Map<String, byte[]> payload = loadPayload(PAYLOAD_NAME);
226 CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, payload, CSAR_UUID, null);
229 assertNotNull(csarInfo);
231 assertEquals(CSAR_UUID, csarInfo.getCsarUUID());
232 assertEquals(CSAR_ENTRY, csarInfo.getMainTemplateName());
233 assertEquals(RESOURCE_NAME, csarInfo.getVfResourceName());
235 assertTrue(csarInfo.getMainTemplateContent().startsWith(CSAR_ENTRY_CONTENT));
236 assertTrue(csarInfo.getCsar().keySet().containsAll(Arrays.asList(CSAR_ENTRY, CSAR_METADATA)));
240 void testGetCsarInfoWithBadData(){
242 Resource resource = new Resource();
243 resource.setName(RESOURCE_NAME);
245 Map<String, byte[]> csar_data = new HashMap<>();
246 when(csarOperation.findVspLatestPackage(anyString(), any(User.class))).thenReturn(Either.left(csar_data));
249 assertThrows(ComponentException.class, () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID, null));
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);
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));
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);
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);
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"));
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);
304 return ZipUtils.readZip(data, false);