6340d0819ae7c06fd857ad161f7cdff65bfd7ebc
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vendor-software-products-rest-services / src / test / java / org / openecomp / sdcrests / vsp / rest / services / VendorSoftwareProductsImplTest.java
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  *
19  *
20  */
21
22 package org.openecomp.sdcrests.vsp.rest.services;
23
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;
33
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;
53
54 class VendorSoftwareProductsImplTest {
55
56     private final String vspId = UUID.randomUUID().toString();
57     private final String user = "cs0008";
58
59     @Mock
60     private AsdcItemManager itemManager;
61     @Mock
62     private PermissionsManager permissionsManager;
63     @Mock
64     private VersioningManager versioningManager;
65     @Mock
66     private VendorSoftwareProductManager vendorSoftwareProductManager;
67     @Mock
68     private ActivityLogManager activityLogManager;
69     @Mock
70     private NotificationPropagationManager notificationPropagationManager;
71     @Mock
72     private UniqueValueUtil uniqueValueUtil;
73     @Mock
74     private ArtifactStorageManager artifactStorageManager;
75
76     @InjectMocks
77     private VendorSoftwareProductsImpl vendorSoftwareProducts;
78
79     private Item item;
80
81     @BeforeEach
82     public void setUp() {
83         openMocks(this);
84
85         item = new Item();
86         item.setType("vsp");
87         item.setId(vspId);
88         when(itemManager.get(vspId)).thenReturn(item);
89     }
90
91     @Test
92     void deleteVspOk() {
93         Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
94         assertEquals(HttpStatus.SC_OK, rsp.getStatus());
95         assertNull(rsp.getEntity());
96     }
97
98     @Test
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());
104     }
105
106     @Test
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));
114     }
115
116     @Test
117     void deleteCertifiedVsp() {
118         item.addVersionStatus(VersionStatus.Certified);
119         when(itemManager.get(vspId)).thenReturn(item);
120
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());
125     }
126
127     @Test
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());
136     }
137
138     @Test
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());
148     }
149
150     @Test
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));
162     }
163 }