1936aaa7b4f287015ab0ccd4f7e1e4ed67c336cb
[sdc.git] /
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 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;
35
36 import java.io.FileNotFoundException;
37 import java.net.URL;
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;
63
64 class VendorSoftwareProductsImplTest {
65
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";
70
71     private final Path testResourcesPath = Paths.get("src", "test", "resources");
72
73     @Mock
74     private AsdcItemManager itemManager;
75     @Mock
76     private PermissionsManager permissionsManager;
77     @Mock
78     private VersioningManager versioningManager;
79     @Mock
80     private VendorSoftwareProductManager vendorSoftwareProductManager;
81     @Mock
82     private ActivityLogManager activityLogManager;
83     @Mock
84     private NotificationPropagationManager notificationPropagationManager;
85     @Mock
86     private UniqueValueUtil uniqueValueUtil;
87     @Mock
88     private ArtifactStorageManager artifactStorageManager;
89     @Mock
90     private CatalogVspClient catalogVspClient;
91     @Mock
92     private StorageFactory storageFactory;
93
94     @InjectMocks
95     private VendorSoftwareProductsImpl vendorSoftwareProducts;
96
97     private Item item;
98
99     @BeforeEach
100     public void setUp() {
101         openMocks(this);
102
103         System.setProperty("configuration.yaml", Paths.get(testResourcesPath.toString(), "configuration.yaml").toAbsolutePath().toString());
104
105         item = new Item();
106         item.setType("vsp");
107         item.setId(vspId);
108         when(itemManager.get(vspId)).thenReturn(item);
109         when(storageFactory.createArtifactStorageManager()).thenReturn(artifactStorageManager);
110     }
111
112     @Test
113     void deleteNotCertifiedVspOk() {
114         Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
115         assertEquals(HttpStatus.SC_OK, rsp.getStatus());
116         assertNull(rsp.getEntity());
117     }
118
119     @Test
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());
125     }
126
127     @Test
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));
135     }
136
137     @Test
138     void deleteCertifiedVsp() {
139         item.addVersionStatus(VersionStatus.Certified);
140         when(itemManager.get(vspId)).thenReturn(item);
141
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());
146     }
147
148     @Test
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());
159     }
160
161     @Test
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());
171     }
172
173     @Test
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));
185     }
186
187     @Test
188     void deleteVspUsedInVfKo() throws Exception {
189         Item item = new Item();
190         item.setType("vsp");
191         item.setId(vspId);
192         item.addVersionStatus(VersionStatus.Certified);
193         when(itemManager.get(vspId)).thenReturn(item);
194         when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.of(VF_NAME));
195
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));
200     }
201
202     @Test
203     void deleteVspUsedInVfThrowsExceptionKo() throws Exception {
204         Item item = new Item();
205         item.setType("vsp");
206         item.setId(vspId);
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));
211
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));
216     }
217
218     @Test
219     void deleteCertifiedArchivedVspNotInVfOk() throws Exception {
220         String configPath = getConfigPath("configuration.yaml");
221         System.setProperty(CONFIG_FILE_PROPERTY, configPath);
222         Item item = new Item();
223         item.setType("vsp");
224         item.setId(vspId);
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());
233     }
234
235     private String getConfigPath(String classpathFile) throws FileNotFoundException {
236
237         URL resource = Thread.currentThread().getContextClassLoader().getResource(classpathFile);
238         if (resource == null) {
239             throw new FileNotFoundException("Cannot find resource: " + classpathFile);
240         }
241         return resource.getPath();
242     }
243 }