1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2022 Nordix Foundation
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*
*
*/
package org.openecomp.sdcrests.vsp.rest.services;
import static ch.qos.logback.classic.util.ContextInitializer.CONFIG_FILE_PROPERTY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;
import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_FROM_STORAGE_ERROR;
import java.io.FileNotFoundException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import javax.ws.rs.core.Response;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.openecomp.core.util.UniqueValueUtil;
import org.openecomp.sdc.activitylog.ActivityLogManager;
import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
import org.openecomp.sdc.activitylog.dao.type.ActivityType;
import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager;
import org.openecomp.sdc.be.csar.storage.StorageFactory;
import org.openecomp.sdc.common.errors.CatalogRestClientException;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.common.errors.ErrorCode;
import org.openecomp.sdc.datatypes.model.ItemType;
import org.openecomp.sdc.itempermissions.PermissionsManager;
import org.openecomp.sdc.notification.services.NotificationPropagationManager;
import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
import org.openecomp.sdc.versioning.AsdcItemManager;
import org.openecomp.sdc.versioning.VersioningManager;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.sdc.versioning.dao.types.VersionStatus;
import org.openecomp.sdc.versioning.types.Item;
import org.openecomp.sdc.versioning.types.ItemStatus;
import org.openecomp.sdcrests.vsp.rest.CatalogVspClient;
import org.openecomp.sdcrests.vsp.rest.exception.VendorSoftwareProductsExceptionSupplier;
class VendorSoftwareProductsImplTest {
public static final String SOME_INTERNAL_ERROR = "Some internal error";
public static final String VF_NAME = "Vf_name";
private final String vspId = UUID.randomUUID().toString();
private final String user = "cs0008";
private final Path testResourcesPath = Paths.get("src", "test", "resources");
@Mock
private AsdcItemManager itemManager;
@Mock
private PermissionsManager permissionsManager;
@Mock
private VersioningManager versioningManager;
@Mock
private VendorSoftwareProductManager vendorSoftwareProductManager;
@Mock
private ActivityLogManager activityLogManager;
@Mock
private NotificationPropagationManager notificationPropagationManager;
@Mock
private UniqueValueUtil uniqueValueUtil;
@Mock
private ArtifactStorageManager artifactStorageManager;
@Mock
private CatalogVspClient catalogVspClient;
@Mock
private StorageFactory storageFactory;
@InjectMocks
private VendorSoftwareProductsImpl vendorSoftwareProducts;
private Item item;
@BeforeEach
public void setUp() {
openMocks(this);
System.setProperty("configuration.yaml", Paths.get(testResourcesPath.toString(), "configuration.yaml").toAbsolutePath().toString());
item = new Item();
item.setType(ItemType.vsp.getName());
item.setId(vspId);
when(itemManager.get(vspId)).thenReturn(item);
when(storageFactory.createArtifactStorageManager()).thenReturn(artifactStorageManager);
}
@Test
void deleteNotCertifiedVspOk() {
when(itemManager.list(any())).thenReturn(List.of(item));
Response actualResponse = vendorSoftwareProducts.deleteVsp(vspId, user);
assertEquals(HttpStatus.SC_OK, actualResponse.getStatus());
assertNull(actualResponse.getEntity());
}
@Test
void deleteVspWithS3Ok() {
when(artifactStorageManager.isEnabled()).thenReturn(true);
Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
assertEquals(HttpStatus.SC_OK, rsp.getStatus());
assertNull(rsp.getEntity());
}
@Test
void deleteVspWithS3Fail() {
when(artifactStorageManager.isEnabled()).thenReturn(true);
doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString());
final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.deleteVspFromStorageFailure(vspId).get();
assertErrorCode(actualException.code(), expectedException.code());
}
@Test
void deleteCertifiedVsp() {
item.addVersionStatus(VersionStatus.Certified);
when(itemManager.get(vspId)).thenReturn(item);
final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.deleteNotArchivedVsp(vspId).get();
assertErrorCode(actualException.code(), expectedException.code());
}
@Test
void deleteCertifiedArchivedVsp() throws FileNotFoundException {
String configPath = getConfigPath("configuration.yaml");
System.setProperty(CONFIG_FILE_PROPERTY, configPath);
item.setStatus(ItemStatus.ARCHIVED);
item.addVersionStatus(VersionStatus.Certified);
when(itemManager.get(vspId)).thenReturn(item);
when(itemManager.list(any())).thenReturn(List.of(item));
Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
assertEquals(HttpStatus.SC_OK, rsp.getStatus());
assertNull(rsp.getEntity());
}
@Test
void deleteCertifiedAndNotArchivedVsp() {
item.setStatus(ItemStatus.ACTIVE);
item.addVersionStatus(VersionStatus.Certified);
when(itemManager.get(vspId)).thenReturn(item);
final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.deleteNotArchivedVsp(vspId).get();
assertErrorCode(actualException.code(), expectedException.code());
}
@Test
void deleteVspNotFoundTest() {
when(itemManager.get(vspId)).thenReturn(new Item());
final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.vspNotFound(vspId).get();
assertEquals(expectedException.code().id(), actualException.code().id());
assertEquals(expectedException.code().message(), actualException.code().message());
}
@Test
void deleteCertifiedArchivedVspWithS3OK() {
when(artifactStorageManager.isEnabled()).thenReturn(true);
item.setStatus(ItemStatus.ARCHIVED);
item.addVersionStatus(VersionStatus.Certified);
when(itemManager.get(vspId)).thenReturn(item);
final Version version1 = new Version("version1Id");
final Version version2 = new Version("version2Id");
final List<Version> versionList = List.of(version1, version2);
when(versioningManager.list(vspId)).thenReturn(versionList);
when(itemManager.list(any())).thenReturn(List.of(item));
Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
assertEquals(HttpStatus.SC_OK, rsp.getStatus());
assertNull(rsp.getEntity());
final ArgumentCaptor<ActivityLogEntity> logActivityArgument = ArgumentCaptor.forClass(ActivityLogEntity.class);
verify(activityLogManager, times(2)).logActivity(logActivityArgument.capture());
final List<ActivityLogEntity> logActivityArgumentList = logActivityArgument.getAllValues();
assertEquals(versionList.size(), logActivityArgumentList.size());
for (int i = 0; i < versionList.size(); i++) {
final Version expectedVersion = versionList.get(i);
final ActivityLogEntity actualLogActivityArgument = logActivityArgumentList.get(i);
assertTrue(actualLogActivityArgument.isSuccess());
assertEquals(vspId, actualLogActivityArgument.getItemId());
assertEquals(expectedVersion.getId(), actualLogActivityArgument.getVersionId());
assertEquals(user, actualLogActivityArgument.getUser());
assertEquals(ActivityType.Delete_From_Storage, actualLogActivityArgument.getType());
}
}
@Test
void deleteCertifiedArchivedVspWithS3Fail() {
//given
when(artifactStorageManager.isEnabled()).thenReturn(true);
doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString());
item.setStatus(ItemStatus.ARCHIVED);
item.addVersionStatus(VersionStatus.Certified);
when(itemManager.get(vspId)).thenReturn(item);
when(itemManager.list(any())).thenReturn(List.of(item));
final Version version1 = new Version("version1Id");
final Version version2 = new Version("version2Id");
final List<Version> versionList = List.of(version1, version2);
when(versioningManager.list(vspId)).thenReturn(versionList);
//when
final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
//then
final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.deleteVspFromStorageFailure(vspId).get();
assertErrorCode(actualException.code(), expectedException.code());
final ArgumentCaptor<ActivityLogEntity> logActivityArgument = ArgumentCaptor.forClass(ActivityLogEntity.class);
verify(activityLogManager, times(2)).logActivity(logActivityArgument.capture());
final List<ActivityLogEntity> logActivityArgumentList = logActivityArgument.getAllValues();
assertEquals(versionList.size(), logActivityArgumentList.size());
for (int i = 0; i < versionList.size(); i++) {
final Version expectedVersion = versionList.get(i);
final ActivityLogEntity actualLogActivityArgument = logActivityArgumentList.get(i);
assertFalse(actualLogActivityArgument.isSuccess());
assertEquals(vspId, actualLogActivityArgument.getItemId());
assertEquals(expectedVersion.getId(), actualLogActivityArgument.getVersionId());
assertEquals(user, actualLogActivityArgument.getUser());
assertEquals(ActivityType.Delete_From_Storage, actualLogActivityArgument.getType());
assertEquals(DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId), actualLogActivityArgument.getMessage());
assertEquals(DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId), actualLogActivityArgument.getComment());
}
}
@Test
void deleteVspUsedInVfKo() {
item.addVersionStatus(VersionStatus.Certified);
when(itemManager.get(vspId)).thenReturn(item);
when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.of(VF_NAME));
final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.vspInUseByVf(VF_NAME).get();
assertErrorCode(actualException.code(), expectedException.code());
}
@Test
void deleteVspUsedInVfThrowsExceptionKo() {
item.addVersionStatus(VersionStatus.Certified);
when(itemManager.get(vspId)).thenReturn(item);
when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenThrow(new CatalogRestClientException(SOME_INTERNAL_ERROR));
final CoreException actualException = assertThrows(CoreException.class, () -> vendorSoftwareProducts.deleteVsp(vspId, user));
final CoreException expectedException = VendorSoftwareProductsExceptionSupplier.deleteGenericError(vspId).get();
assertErrorCode(actualException.code(), expectedException.code());
}
@Test
void deleteCertifiedArchivedVspNotInVfOk() throws FileNotFoundException {
String configPath = getConfigPath("configuration.yaml");
System.setProperty(CONFIG_FILE_PROPERTY, configPath);
Item item = new Item();
item.setType("vsp");
item.setId(vspId);
item.setStatus(ItemStatus.ARCHIVED);
item.addVersionStatus(VersionStatus.Certified);
when(itemManager.get(vspId)).thenReturn(item);
when(itemManager.list(any())).thenReturn(List.of(item));
when(catalogVspClient.findNameOfVfUsingVsp(vspId, user)).thenReturn(Optional.empty());
Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user);
assertEquals(HttpStatus.SC_OK, rsp.getStatus());
assertNull(rsp.getEntity());
}
private String getConfigPath(String classpathFile) throws FileNotFoundException {
URL resource = Thread.currentThread().getContextClassLoader().getResource(classpathFile);
if (resource == null) {
throw new FileNotFoundException("Cannot find resource: " + classpathFile);
}
return resource.getPath();
}
private void assertErrorCode(final ErrorCode actualErrorCode, final ErrorCode expectedErrorCode) {
assertEquals(expectedErrorCode.id(), actualErrorCode.id());
assertEquals(expectedErrorCode.category(), actualErrorCode.category());
assertEquals(expectedErrorCode.message(), actualErrorCode.message());
}
}
|