2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
22 package org.openecomp.sdcrests.vsp.rest.services;
24 import static ch.qos.logback.classic.util.ContextInitializer.CONFIG_FILE_PROPERTY;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.when;
31 import static org.mockito.MockitoAnnotations.openMocks;
32 import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_ERROR;
33 import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_ERROR_USED_BY_VF;
34 import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_FROM_STORAGE_ERROR;
36 import java.io.FileNotFoundException;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.util.List;
41 import java.util.Optional;
42 import java.util.UUID;
43 import javax.ws.rs.core.Response;
44 import org.apache.http.HttpStatus;
45 import org.junit.jupiter.api.BeforeEach;
46 import org.junit.jupiter.api.Test;
47 import org.mockito.InjectMocks;
48 import org.mockito.Mock;
49 import org.openecomp.core.util.UniqueValueUtil;
50 import org.openecomp.sdc.activitylog.ActivityLogManager;
51 import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager;
52 import org.openecomp.sdc.common.errors.CoreException;
53 import org.openecomp.sdc.itempermissions.PermissionsManager;
54 import org.openecomp.sdc.notification.services.NotificationPropagationManager;
55 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
56 import org.openecomp.sdc.versioning.AsdcItemManager;
57 import org.openecomp.sdc.versioning.VersioningManager;
58 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
59 import org.openecomp.sdc.versioning.types.Item;
60 import org.openecomp.sdc.versioning.types.ItemStatus;
61 import org.openecomp.sdcrests.vsp.rest.CatalogVspClient;
63 class VendorSoftwareProductsImplTest {
65 public static final String SOME_INTERNAL_ERROR = "Some internal error";
66 public static final String VF_NAME = "Vf_name";
67 private final String vspId = UUID.randomUUID().toString();
68 private final String user = "cs0008";
70 private final Path testResourcesPath = Paths.get("src", "test", "resources");
73 private AsdcItemManager itemManager;
75 private PermissionsManager permissionsManager;
77 private VersioningManager versioningManager;
79 private VendorSoftwareProductManager vendorSoftwareProductManager;
81 private ActivityLogManager activityLogManager;
83 private NotificationPropagationManager notificationPropagationManager;
85 private UniqueValueUtil uniqueValueUtil;
87 private ArtifactStorageManager artifactStorageManager;
89 private CatalogVspClient catalogVspClient;
92 private VendorSoftwareProductsImpl vendorSoftwareProducts;
100 System.setProperty("configuration.yaml", Paths.get(testResourcesPath.toString(), "configuration.yaml").toAbsolutePath().toString());
105 when(itemManager.get(vspId)).thenReturn(item);
109 void deleteNotCertifiedVspOk() {
110 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
111 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
112 assertNull(rsp.getEntity());
116 void deleteVspWithS3Ok() {
117 when(artifactStorageManager.isEnabled()).thenReturn(true);
118 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
119 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
120 assertNull(rsp.getEntity());
124 void deleteVspWithS3Fail() {
125 when(artifactStorageManager.isEnabled()).thenReturn(true);
126 doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString());
127 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
128 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus());
129 assertEquals(rsp.getEntity().getClass(), Exception.class);
130 assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId));
134 void deleteCertifiedVsp() {
135 item.addVersionStatus(VersionStatus.Certified);
136 when(itemManager.get(vspId)).thenReturn(item);
138 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
139 assertEquals(HttpStatus.SC_FORBIDDEN, rsp.getStatus());
140 assertEquals(rsp.getEntity().getClass(), Exception.class);
141 assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_ERROR.getErrorMessage());
145 void deleteCertifiedArchivedVsp() throws FileNotFoundException {
146 String configPath = getConfigPath("configuration.yaml");
147 System.setProperty(CONFIG_FILE_PROPERTY, configPath);
148 item.setStatus(ItemStatus.ARCHIVED);
149 item.addVersionStatus(VersionStatus.Certified);
150 when(itemManager.get(vspId)).thenReturn(item);
151 when(itemManager.list(any())).thenReturn(List.of(item));
152 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
153 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
154 assertNull(rsp.getEntity());
158 void deleteCertifiedArchivedVspWithS3OK() {
159 when(artifactStorageManager.isEnabled()).thenReturn(true);
160 item.setStatus(ItemStatus.ARCHIVED);
161 item.addVersionStatus(VersionStatus.Certified);
162 when(itemManager.get(vspId)).thenReturn(item);
163 when(itemManager.list(any())).thenReturn(List.of(item));
164 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
165 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
166 assertNull(rsp.getEntity());
170 void deleteCertifiedArchivedVspWithS3Fail() {
171 when(artifactStorageManager.isEnabled()).thenReturn(true);
172 doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString());
173 item.setStatus(ItemStatus.ARCHIVED);
174 item.addVersionStatus(VersionStatus.Certified);
175 when(itemManager.get(vspId)).thenReturn(item);
176 when(itemManager.list(any())).thenReturn(List.of(item));
177 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
178 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus());
179 assertEquals(rsp.getEntity().getClass(), Exception.class);
180 assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId));
184 void deleteVspUsedInVfKo() throws Exception {
185 Item item = new Item();
188 item.addVersionStatus(VersionStatus.Certified);
189 when(itemManager.get(vspId)).thenReturn(item);
190 when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.of(VF_NAME));
192 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
193 assertEquals(HttpStatus.SC_FORBIDDEN, rsp.getStatus());
194 assertEquals(rsp.getEntity().getClass(), Exception.class);
195 assertEquals(((Exception)rsp.getEntity()).getLocalizedMessage(), String.format(DELETE_VSP_ERROR_USED_BY_VF.getErrorMessage(), VF_NAME, VF_NAME));
199 void deleteVspUsedInVfThrowsExceptionKo() throws Exception {
200 Item item = new Item();
203 item.addVersionStatus(VersionStatus.Certified);
204 when(itemManager.get(vspId)).thenReturn(item);
205 final String vf_name = "Vf_name";
206 when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenThrow(new Exception(SOME_INTERNAL_ERROR));
208 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
209 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus());
210 assertEquals(rsp.getEntity().getClass(), CoreException.class);
211 assertEquals(((Exception)rsp.getEntity()).getLocalizedMessage(), String.format("Vsp with id %s cannot be deleted due to error %s.", vspId, SOME_INTERNAL_ERROR));
215 void deleteCertifiedArchivedVspNotInVfOk() throws Exception {
216 String configPath = getConfigPath("configuration.yaml");
217 System.setProperty(CONFIG_FILE_PROPERTY, configPath);
218 Item item = new Item();
221 item.setStatus(ItemStatus.ARCHIVED);
222 item.addVersionStatus(VersionStatus.Certified);
223 when(itemManager.get(vspId)).thenReturn(item);
224 when(itemManager.list(any())).thenReturn(List.of(item));
225 when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.empty());
226 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
227 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
228 assertNull(rsp.getEntity());
231 private String getConfigPath(String classpathFile) throws FileNotFoundException {
233 URL resource = Thread.currentThread().getContextClassLoader().getResource(classpathFile);
234 if (resource == null) {
235 throw new FileNotFoundException("Cannot find resource: " + classpathFile);
237 return resource.getPath();