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 org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNull;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.when;
30 import static org.mockito.MockitoAnnotations.openMocks;
31 import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_ERROR;
32 import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_FROM_STORAGE_ERROR;
34 import java.util.List;
35 import java.util.UUID;
36 import javax.ws.rs.core.Response;
37 import org.apache.http.HttpStatus;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.openecomp.core.util.UniqueValueUtil;
43 import org.openecomp.sdc.activitylog.ActivityLogManager;
44 import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager;
45 import org.openecomp.sdc.itempermissions.PermissionsManager;
46 import org.openecomp.sdc.notification.services.NotificationPropagationManager;
47 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
48 import org.openecomp.sdc.versioning.AsdcItemManager;
49 import org.openecomp.sdc.versioning.VersioningManager;
50 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
51 import org.openecomp.sdc.versioning.types.Item;
52 import org.openecomp.sdc.versioning.types.ItemStatus;
54 class VendorSoftwareProductsImplTest {
56 private final String vspId = UUID.randomUUID().toString();
57 private final String user = "cs0008";
60 private AsdcItemManager itemManager;
62 private PermissionsManager permissionsManager;
64 private VersioningManager versioningManager;
66 private VendorSoftwareProductManager vendorSoftwareProductManager;
68 private ActivityLogManager activityLogManager;
70 private NotificationPropagationManager notificationPropagationManager;
72 private UniqueValueUtil uniqueValueUtil;
74 private ArtifactStorageManager artifactStorageManager;
77 private VendorSoftwareProductsImpl vendorSoftwareProducts;
88 when(itemManager.get(vspId)).thenReturn(item);
93 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
94 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
95 assertNull(rsp.getEntity());
99 void deleteVspWithS3Ok() {
100 when(artifactStorageManager.isEnabled()).thenReturn(true);
101 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
102 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
103 assertNull(rsp.getEntity());
107 void deleteVspWithS3Fail() {
108 when(artifactStorageManager.isEnabled()).thenReturn(true);
109 doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString());
110 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
111 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus());
112 assertEquals(rsp.getEntity().getClass(), Exception.class);
113 assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId));
117 void deleteCertifiedVsp() {
118 item.addVersionStatus(VersionStatus.Certified);
119 when(itemManager.get(vspId)).thenReturn(item);
121 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
122 assertEquals(HttpStatus.SC_FORBIDDEN, rsp.getStatus());
123 assertEquals(rsp.getEntity().getClass(), Exception.class);
124 assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_ERROR.getErrorMessage());
128 void deleteCertifiedArchivedVsp() {
129 item.setStatus(ItemStatus.ARCHIVED);
130 item.addVersionStatus(VersionStatus.Certified);
131 when(itemManager.get(vspId)).thenReturn(item);
132 when(itemManager.list(any())).thenReturn(List.of(item));
133 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
134 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
135 assertNull(rsp.getEntity());
139 void deleteCertifiedArchivedVspWithS3OK() {
140 when(artifactStorageManager.isEnabled()).thenReturn(true);
141 item.setStatus(ItemStatus.ARCHIVED);
142 item.addVersionStatus(VersionStatus.Certified);
143 when(itemManager.get(vspId)).thenReturn(item);
144 when(itemManager.list(any())).thenReturn(List.of(item));
145 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
146 assertEquals(HttpStatus.SC_OK, rsp.getStatus());
147 assertNull(rsp.getEntity());
151 void deleteCertifiedArchivedVspWithS3Fail() {
152 when(artifactStorageManager.isEnabled()).thenReturn(true);
153 doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString());
154 item.setStatus(ItemStatus.ARCHIVED);
155 item.addVersionStatus(VersionStatus.Certified);
156 when(itemManager.get(vspId)).thenReturn(item);
157 when(itemManager.list(any())).thenReturn(List.of(item));
158 Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
159 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus());
160 assertEquals(rsp.getEntity().getClass(), Exception.class);
161 assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId));