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.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNull;
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.Mockito.doThrow;
33 import static org.mockito.Mockito.times;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.when;
36 import static org.mockito.MockitoAnnotations.openMocks;
37 import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_FROM_STORAGE_ERROR;
39 import java.io.FileNotFoundException;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.util.List;
44 import java.util.Optional;
45 import java.util.UUID;
46 import javax.ws.rs.core.Response;
47 import org.apache.http.HttpStatus;
48 import org.junit.jupiter.api.BeforeEach;
49 import org.junit.jupiter.api.Test;
50 import org.mockito.ArgumentCaptor;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.openecomp.core.util.UniqueValueUtil;
54 import org.openecomp.sdc.activitylog.ActivityLogManager;
55 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
56 import org.openecomp.sdc.activitylog.dao.type.ActivityType;
57 import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager;
58 import org.openecomp.sdc.be.csar.storage.StorageFactory;
59 import org.openecomp.sdc.common.errors.CatalogRestClientException;
60 import org.openecomp.sdc.common.errors.CoreException;
61 import org.openecomp.sdc.common.errors.ErrorCode;
62 import org.openecomp.sdc.datatypes.model.ItemType;
63 import org.openecomp.sdc.itempermissions.PermissionsManager;
64 import org.openecomp.sdc.notification.services.NotificationPropagationManager;
65 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
66 import org.openecomp.sdc.versioning.AsdcItemManager;
67 import org.openecomp.sdc.versioning.VersioningManager;
68 import org.openecomp.sdc.versioning.dao.types.Version;
69 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
70 import org.openecomp.sdc.versioning.types.Item;
71 import org.openecomp.sdc.versioning.types.ItemStatus;
72 import org.openecomp.sdcrests.vsp.rest.CatalogVspClient;
73 import org.openecomp.sdcrests.vsp.rest.exception.VendorSoftwareProductsExceptionSupplier;
75 class VendorSoftwareProductsImplTest {
77 public static final String SOME_INTERNAL_ERROR = "Some internal error";
78 public static final String VF_NAME = "Vf_name";
79 private final String vspId = UUID.randomUUID().toString();
80 private final String user = "cs0008";
82 private final Path testResourcesPath = Paths.get("src", "test", "resources");
85 private AsdcItemManager itemManager;
87 private PermissionsManager permissionsManager;
89 private VersioningManager versioningManager;
91 private VendorSoftwareProductManager vendorSoftwareProductManager;
93 private ActivityLogManager activityLogManager;
95 private NotificationPropagationManager notificationPropagationManager;
97 private UniqueValueUtil uniqueValueUtil;
99 private ArtifactStorageManager artifactStorageManager;
101 private CatalogVspClient catalogVspClient;
103 private StorageFactory storageFactory;
106 private VendorSoftwareProductsImpl vendorSoftwareProducts;
111 public void setUp() {
114 System.setProperty("configuration.yaml", Paths.get(testResourcesPath.toString(), "configuration.yaml").toAbsolutePath().toString());
117 item.setType(ItemType.vsp.getName());
119 when(itemManager.get(vspId)).thenReturn(item);
120 when(storageFactory.createArtifactStorageManager()).thenReturn(artifactStorageManager);
124 void deleteNotCertifiedVspOk() {
125 when(itemManager.list(any())).thenReturn(List.of(item));
126 Response actualResponse = vendorSoftwareProducts.deleteVsp(vspId, user);
127 assertEquals(HttpStatus.SC_OK, actualResponse.getStatus());
128 assertNull(actualResponse.getEntity());
132 void deleteVspWithS3Ok() {
133 when(artifactStorageManager.isEnabled()).thenReturn(true);
134 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
135 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
136 assertNull(rsp.getEntity());
140 void deleteVspWithS3Fail() {
141 when(artifactStorageManager.isEnabled()).thenReturn(true);
142 doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString());
143 final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
144 final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.deleteVspFromStorageFailure(vspId).get();
145 assertErrorCode(actualException.code(), expectedException.code());
149 void deleteCertifiedVsp() {
150 item.addVersionStatus(VersionStatus.Certified);
151 when(itemManager.get(vspId)).thenReturn(item);
153 final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
154 final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.deleteNotArchivedVsp(vspId).get();
155 assertErrorCode(actualException.code(), expectedException.code());
159 void deleteCertifiedArchivedVsp() throws FileNotFoundException {
160 String configPath = getConfigPath("configuration.yaml");
161 System.setProperty(CONFIG_FILE_PROPERTY, configPath);
162 item.setStatus(ItemStatus.ARCHIVED);
163 item.addVersionStatus(VersionStatus.Certified);
164 when(itemManager.get(vspId)).thenReturn(item);
165 when(itemManager.list(any())).thenReturn(List.of(item));
166 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
167 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
168 assertNull(rsp.getEntity());
172 void deleteCertifiedAndNotArchivedVsp() {
173 item.setStatus(ItemStatus.ACTIVE);
174 item.addVersionStatus(VersionStatus.Certified);
175 when(itemManager.get(vspId)).thenReturn(item);
177 final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
178 final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.deleteNotArchivedVsp(vspId).get();
179 assertErrorCode(actualException.code(), expectedException.code());
183 void deleteVspNotFoundTest() {
184 when(itemManager.get(vspId)).thenReturn(new Item());
185 final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
186 final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.vspNotFound(vspId).get();
187 assertEquals(expectedException.code().id(), actualException.code().id());
188 assertEquals(expectedException.code().message(), actualException.code().message());
192 void deleteCertifiedArchivedVspWithS3OK() {
193 when(artifactStorageManager.isEnabled()).thenReturn(true);
194 item.setStatus(ItemStatus.ARCHIVED);
195 item.addVersionStatus(VersionStatus.Certified);
196 when(itemManager.get(vspId)).thenReturn(item);
198 final Version version1 = new Version("version1Id");
199 final Version version2 = new Version("version2Id");
200 final List<Version> versionList = List.of(version1, version2);
201 when(versioningManager.list(vspId)).thenReturn(versionList);
203 when(itemManager.list(any())).thenReturn(List.of(item));
204 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
205 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
206 assertNull(rsp.getEntity());
208 final ArgumentCaptor<ActivityLogEntity> logActivityArgument = ArgumentCaptor.forClass(ActivityLogEntity.class);
209 verify(activityLogManager, times(2)).logActivity(logActivityArgument.capture());
210 final List<ActivityLogEntity> logActivityArgumentList = logActivityArgument.getAllValues();
211 assertEquals(versionList.size(), logActivityArgumentList.size());
212 for (int i = 0; i < versionList.size(); i++) {
213 final Version expectedVersion = versionList.get(i);
214 final ActivityLogEntity actualLogActivityArgument = logActivityArgumentList.get(i);
215 assertTrue(actualLogActivityArgument.isSuccess());
216 assertEquals(vspId, actualLogActivityArgument.getItemId());
217 assertEquals(expectedVersion.getId(), actualLogActivityArgument.getVersionId());
218 assertEquals(user, actualLogActivityArgument.getUser());
219 assertEquals(ActivityType.Delete_From_Storage, actualLogActivityArgument.getType());
224 void deleteCertifiedArchivedVspWithS3Fail() {
226 when(artifactStorageManager.isEnabled()).thenReturn(true);
227 doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString());
228 item.setStatus(ItemStatus.ARCHIVED);
229 item.addVersionStatus(VersionStatus.Certified);
230 when(itemManager.get(vspId)).thenReturn(item);
231 when(itemManager.list(any())).thenReturn(List.of(item));
233 final Version version1 = new Version("version1Id");
234 final Version version2 = new Version("version2Id");
235 final List<Version> versionList = List.of(version1, version2);
236 when(versioningManager.list(vspId)).thenReturn(versionList);
239 final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
242 final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.deleteVspFromStorageFailure(vspId).get();
243 assertErrorCode(actualException.code(), expectedException.code());
245 final ArgumentCaptor<ActivityLogEntity> logActivityArgument = ArgumentCaptor.forClass(ActivityLogEntity.class);
246 verify(activityLogManager, times(2)).logActivity(logActivityArgument.capture());
247 final List<ActivityLogEntity> logActivityArgumentList = logActivityArgument.getAllValues();
248 assertEquals(versionList.size(), logActivityArgumentList.size());
249 for (int i = 0; i < versionList.size(); i++) {
250 final Version expectedVersion = versionList.get(i);
251 final ActivityLogEntity actualLogActivityArgument = logActivityArgumentList.get(i);
252 assertFalse(actualLogActivityArgument.isSuccess());
253 assertEquals(vspId, actualLogActivityArgument.getItemId());
254 assertEquals(expectedVersion.getId(), actualLogActivityArgument.getVersionId());
255 assertEquals(user, actualLogActivityArgument.getUser());
256 assertEquals(ActivityType.Delete_From_Storage, actualLogActivityArgument.getType());
257 assertEquals(DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId), actualLogActivityArgument.getMessage());
258 assertEquals(DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId), actualLogActivityArgument.getComment());
263 void deleteVspUsedInVfKo() {
264 item.addVersionStatus(VersionStatus.Certified);
265 when(itemManager.get(vspId)).thenReturn(item);
266 when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.of(VF_NAME));
268 final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
269 final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.vspInUseByVf(VF_NAME).get();
270 assertErrorCode(actualException.code(), expectedException.code());
274 void deleteVspUsedInVfThrowsExceptionKo() {
275 item.addVersionStatus(VersionStatus.Certified);
276 when(itemManager.get(vspId)).thenReturn(item);
277 when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenThrow(new CatalogRestClientException(SOME_INTERNAL_ERROR));
279 final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
280 final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.deleteGenericError(vspId).get();
281 assertErrorCode(actualException.code(), expectedException.code());
285 void deleteCertifiedArchivedVspNotInVfOk() throws FileNotFoundException {
286 String configPath = getConfigPath("configuration.yaml");
287 System.setProperty(CONFIG_FILE_PROPERTY, configPath);
288 Item item = new Item();
291 item.setStatus(ItemStatus.ARCHIVED);
292 item.addVersionStatus(VersionStatus.Certified);
293 when(itemManager.get(vspId)).thenReturn(item);
294 when(itemManager.list(any())).thenReturn(List.of(item));
295 when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.empty());
296 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
297 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
298 assertNull(rsp.getEntity());
301 private String getConfigPath(String classpathFile) throws FileNotFoundException {
303 URL resource = Thread.currentThread().getContextClassLoader().getResource(classpathFile);
304 if (resource == null) {
305 throw new FileNotFoundException("Cannot find resource: " + classpathFile);
307 return resource.getPath();
310 private void assertErrorCode(final ErrorCode actualErrorCode, final ErrorCode expectedErrorCode) {
311 assertEquals(expectedErrorCode.id(), actualErrorCode.id());
312 assertEquals(expectedErrorCode.category(), actualErrorCode.category());
313 assertEquals(expectedErrorCode.message(), actualErrorCode.message());