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.be.csar.storage.StorageFactory;
54 import org.openecomp.sdc.itempermissions.PermissionsManager;
55 import org.openecomp.sdc.notification.services.NotificationPropagationManager;
56 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
57 import org.openecomp.sdc.versioning.AsdcItemManager;
58 import org.openecomp.sdc.versioning.VersioningManager;
59 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
60 import org.openecomp.sdc.versioning.types.Item;
61 import org.openecomp.sdc.versioning.types.ItemStatus;
62 import org.openecomp.sdcrests.vsp.rest.CatalogVspClient;
64 class VendorSoftwareProductsImplTest {
66 public static final String SOME_INTERNAL_ERROR = "Some internal error";
67 public static final String VF_NAME = "Vf_name";
68 private final String vspId = UUID.randomUUID().toString();
69 private final String user = "cs0008";
71 private final Path testResourcesPath = Paths.get("src", "test", "resources");
74 private AsdcItemManager itemManager;
76 private PermissionsManager permissionsManager;
78 private VersioningManager versioningManager;
80 private VendorSoftwareProductManager vendorSoftwareProductManager;
82 private ActivityLogManager activityLogManager;
84 private NotificationPropagationManager notificationPropagationManager;
86 private UniqueValueUtil uniqueValueUtil;
88 private ArtifactStorageManager artifactStorageManager;
90 private CatalogVspClient catalogVspClient;
92 private StorageFactory storageFactory;
95 private VendorSoftwareProductsImpl vendorSoftwareProducts;
100 public void setUp() {
103 System.setProperty("configuration.yaml", Paths.get(testResourcesPath.toString(), "configuration.yaml").toAbsolutePath().toString());
108 when(itemManager.get(vspId)).thenReturn(item);
109 when(storageFactory.createArtifactStorageManager()).thenReturn(artifactStorageManager);
113 void deleteNotCertifiedVspOk() {
114 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
115 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
116 assertNull(rsp.getEntity());
120 void deleteVspWithS3Ok() {
121 when(artifactStorageManager.isEnabled()).thenReturn(true);
122 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
123 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
124 assertNull(rsp.getEntity());
128 void deleteVspWithS3Fail() {
129 when(artifactStorageManager.isEnabled()).thenReturn(true);
130 doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString());
131 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
132 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus());
133 assertEquals(rsp.getEntity().getClass(), Exception.class);
134 assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId));
138 void deleteCertifiedVsp() {
139 item.addVersionStatus(VersionStatus.Certified);
140 when(itemManager.get(vspId)).thenReturn(item);
142 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
143 assertEquals(HttpStatus.SC_FORBIDDEN, rsp.getStatus());
144 assertEquals(rsp.getEntity().getClass(), Exception.class);
145 assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_ERROR.getErrorMessage());
149 void deleteCertifiedArchivedVsp() throws FileNotFoundException {
150 String configPath = getConfigPath("configuration.yaml");
151 System.setProperty(CONFIG_FILE_PROPERTY, configPath);
152 item.setStatus(ItemStatus.ARCHIVED);
153 item.addVersionStatus(VersionStatus.Certified);
154 when(itemManager.get(vspId)).thenReturn(item);
155 when(itemManager.list(any())).thenReturn(List.of(item));
156 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
157 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
158 assertNull(rsp.getEntity());
162 void deleteCertifiedArchivedVspWithS3OK() {
163 when(artifactStorageManager.isEnabled()).thenReturn(true);
164 item.setStatus(ItemStatus.ARCHIVED);
165 item.addVersionStatus(VersionStatus.Certified);
166 when(itemManager.get(vspId)).thenReturn(item);
167 when(itemManager.list(any())).thenReturn(List.of(item));
168 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
169 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
170 assertNull(rsp.getEntity());
174 void deleteCertifiedArchivedVspWithS3Fail() {
175 when(artifactStorageManager.isEnabled()).thenReturn(true);
176 doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString());
177 item.setStatus(ItemStatus.ARCHIVED);
178 item.addVersionStatus(VersionStatus.Certified);
179 when(itemManager.get(vspId)).thenReturn(item);
180 when(itemManager.list(any())).thenReturn(List.of(item));
181 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
182 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus());
183 assertEquals(rsp.getEntity().getClass(), Exception.class);
184 assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId));
188 void deleteVspUsedInVfKo() throws Exception {
189 Item item = new Item();
192 item.addVersionStatus(VersionStatus.Certified);
193 when(itemManager.get(vspId)).thenReturn(item);
194 when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.of(VF_NAME));
196 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
197 assertEquals(HttpStatus.SC_FORBIDDEN, rsp.getStatus());
198 assertEquals(rsp.getEntity().getClass(), Exception.class);
199 assertEquals(((Exception)rsp.getEntity()).getLocalizedMessage(), String.format(DELETE_VSP_ERROR_USED_BY_VF.getErrorMessage(), VF_NAME, VF_NAME));
203 void deleteVspUsedInVfThrowsExceptionKo() throws Exception {
204 Item item = new Item();
207 item.addVersionStatus(VersionStatus.Certified);
208 when(itemManager.get(vspId)).thenReturn(item);
209 final String vf_name = "Vf_name";
210 when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenThrow(new Exception(SOME_INTERNAL_ERROR));
212 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
213 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus());
214 assertEquals(rsp.getEntity().getClass(), CoreException.class);
215 assertEquals(((Exception)rsp.getEntity()).getLocalizedMessage(), String.format("Vsp with id %s cannot be deleted due to error %s.", vspId, SOME_INTERNAL_ERROR));
219 void deleteCertifiedArchivedVspNotInVfOk() throws Exception {
220 String configPath = getConfigPath("configuration.yaml");
221 System.setProperty(CONFIG_FILE_PROPERTY, configPath);
222 Item item = new Item();
225 item.setStatus(ItemStatus.ARCHIVED);
226 item.addVersionStatus(VersionStatus.Certified);
227 when(itemManager.get(vspId)).thenReturn(item);
228 when(itemManager.list(any())).thenReturn(List.of(item));
229 when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.empty());
230 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
231 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
232 assertNull(rsp.getEntity());
235 private String getConfigPath(String classpathFile) throws FileNotFoundException {
237 URL resource = Thread.currentThread().getContextClassLoader().getResource(classpathFile);
238 if (resource == null) {
239 throw new FileNotFoundException("Cannot find resource: " + classpathFile);
241 return resource.getPath();