f8af8df1ed568f438182bf5c17da1d792f399226
[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.itempermissions.PermissionsManager;
54 import org.openecomp.sdc.notification.services.NotificationPropagationManager;
55 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
56 import org.openecomp.sdc.versioning.AsdcItemManager;
57 import org.openecomp.sdc.versioning.VersioningManager;
58 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
59 import org.openecomp.sdc.versioning.types.Item;
60 import org.openecomp.sdc.versioning.types.ItemStatus;
61 import org.openecomp.sdcrests.vsp.rest.CatalogVspClient;
62
63 class VendorSoftwareProductsImplTest {
64
65     public static final String SOME_INTERNAL_ERROR = "Some internal error";
66     public static final String VF_NAME = "Vf_name";
67     private final String vspId = UUID.randomUUID().toString();
68     private final String user = "cs0008";
69
70     private final Path testResourcesPath = Paths.get("src", "test", "resources");
71
72     @Mock
73     private AsdcItemManager itemManager;
74     @Mock
75     private PermissionsManager permissionsManager;
76     @Mock
77     private VersioningManager versioningManager;
78     @Mock
79     private VendorSoftwareProductManager vendorSoftwareProductManager;
80     @Mock
81     private ActivityLogManager activityLogManager;
82     @Mock
83     private NotificationPropagationManager notificationPropagationManager;
84     @Mock
85     private UniqueValueUtil uniqueValueUtil;
86     @Mock
87     private ArtifactStorageManager artifactStorageManager;
88     @Mock
89     private CatalogVspClient catalogVspClient;
90
91     @InjectMocks
92     private VendorSoftwareProductsImpl vendorSoftwareProducts;
93
94     private Item item;
95
96     @BeforeEach
97     public void setUp() {
98         openMocks(this);
99
100         System.setProperty("configuration.yaml", Paths.get(testResourcesPath.toString(), "configuration.yaml").toAbsolutePath().toString());
101
102         item = new Item();
103         item.setType("vsp");
104         item.setId(vspId);
105         when(itemManager.get(vspId)).thenReturn(item);
106     }
107
108     @Test
109     void deleteNotCertifiedVspOk() {
110         Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
111         assertEquals(HttpStatus.SC_OK, rsp.getStatus());
112         assertNull(rsp.getEntity());
113     }
114
115     @Test
116     void deleteVspWithS3Ok() {
117         when(artifactStorageManager.isEnabled()).thenReturn(true);
118         Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
119         assertEquals(HttpStatus.SC_OK, rsp.getStatus());
120         assertNull(rsp.getEntity());
121     }
122
123     @Test
124     void deleteVspWithS3Fail() {
125         when(artifactStorageManager.isEnabled()).thenReturn(true);
126         doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString());
127         Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
128         assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus());
129         assertEquals(rsp.getEntity().getClass(), Exception.class);
130         assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId));
131     }
132
133     @Test
134     void deleteCertifiedVsp() {
135         item.addVersionStatus(VersionStatus.Certified);
136         when(itemManager.get(vspId)).thenReturn(item);
137
138         Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
139         assertEquals(HttpStatus.SC_FORBIDDEN, rsp.getStatus());
140         assertEquals(rsp.getEntity().getClass(), Exception.class);
141         assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_ERROR.getErrorMessage());
142     }
143
144     @Test
145     void deleteCertifiedArchivedVsp() throws FileNotFoundException {
146         String configPath = getConfigPath("configuration.yaml");
147         System.setProperty(CONFIG_FILE_PROPERTY, configPath);
148         item.setStatus(ItemStatus.ARCHIVED);
149         item.addVersionStatus(VersionStatus.Certified);
150         when(itemManager.get(vspId)).thenReturn(item);
151         when(itemManager.list(any())).thenReturn(List.of(item));
152         Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
153         assertEquals(HttpStatus.SC_OK, rsp.getStatus());
154         assertNull(rsp.getEntity());
155     }
156
157     @Test
158     void deleteCertifiedArchivedVspWithS3OK() {
159         when(artifactStorageManager.isEnabled()).thenReturn(true);
160         item.setStatus(ItemStatus.ARCHIVED);
161         item.addVersionStatus(VersionStatus.Certified);
162         when(itemManager.get(vspId)).thenReturn(item);
163         when(itemManager.list(any())).thenReturn(List.of(item));
164         Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
165         assertEquals(HttpStatus.SC_OK, rsp.getStatus());
166         assertNull(rsp.getEntity());
167     }
168
169     @Test
170     void deleteCertifiedArchivedVspWithS3Fail() {
171         when(artifactStorageManager.isEnabled()).thenReturn(true);
172         doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString());
173         item.setStatus(ItemStatus.ARCHIVED);
174         item.addVersionStatus(VersionStatus.Certified);
175         when(itemManager.get(vspId)).thenReturn(item);
176         when(itemManager.list(any())).thenReturn(List.of(item));
177         Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
178         assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus());
179         assertEquals(rsp.getEntity().getClass(), Exception.class);
180         assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId));
181     }
182
183     @Test
184     void deleteVspUsedInVfKo() throws Exception {
185         Item item = new Item();
186         item.setType("vsp");
187         item.setId(vspId);
188         item.addVersionStatus(VersionStatus.Certified);
189         when(itemManager.get(vspId)).thenReturn(item);
190         when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.of(VF_NAME));
191
192         Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
193         assertEquals(HttpStatus.SC_FORBIDDEN, rsp.getStatus());
194         assertEquals(rsp.getEntity().getClass(), Exception.class);
195         assertEquals(((Exception)rsp.getEntity()).getLocalizedMessage(), String.format(DELETE_VSP_ERROR_USED_BY_VF.getErrorMessage(), VF_NAME, VF_NAME));
196     }
197
198     @Test
199     void deleteVspUsedInVfThrowsExceptionKo() throws Exception {
200         Item item = new Item();
201         item.setType("vsp");
202         item.setId(vspId);
203         item.addVersionStatus(VersionStatus.Certified);
204         when(itemManager.get(vspId)).thenReturn(item);
205         final String vf_name = "Vf_name";
206         when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenThrow(new Exception(SOME_INTERNAL_ERROR));
207
208         Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
209         assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus());
210         assertEquals(rsp.getEntity().getClass(), CoreException.class);
211         assertEquals(((Exception)rsp.getEntity()).getLocalizedMessage(), String.format("Vsp with id %s cannot be deleted due to error %s.", vspId, SOME_INTERNAL_ERROR));
212     }
213
214     @Test
215     void deleteCertifiedArchivedVspNotInVfOk() throws Exception {
216         String configPath = getConfigPath("configuration.yaml");
217         System.setProperty(CONFIG_FILE_PROPERTY, configPath);
218         Item item = new Item();
219         item.setType("vsp");
220         item.setId(vspId);
221         item.setStatus(ItemStatus.ARCHIVED);
222         item.addVersionStatus(VersionStatus.Certified);
223         when(itemManager.get(vspId)).thenReturn(item);
224         when(itemManager.list(any())).thenReturn(List.of(item));
225         when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.empty());
226         Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
227         assertEquals(HttpStatus.SC_OK, rsp.getStatus());
228         assertNull(rsp.getEntity());
229     }
230
231     private String getConfigPath(String classpathFile) throws FileNotFoundException {
232
233         URL resource = Thread.currentThread().getContextClassLoader().getResource(classpathFile);
234         if (resource == null) {
235             throw new FileNotFoundException("Cannot find resource: " + classpathFile);
236         }
237         return resource.getPath();
238     }
239 }