48cb42d160053febefe49a1039f9c603e23d1204
[sdc.git] /
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdcrests.vendorlicense.rest.services;
23
24
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Map;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.Response.Status;
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.mockito.MockitoAnnotations;
43 import org.openecomp.core.util.UniqueValueUtil;
44 import org.openecomp.sdc.activitylog.ActivityLogManager;
45 import org.openecomp.sdc.common.errors.CoreException;
46 import org.openecomp.sdc.datatypes.model.ItemType;
47 import org.openecomp.sdc.itempermissions.PermissionsManager;
48 import org.openecomp.sdc.notification.dtos.Event;
49 import org.openecomp.sdc.notification.services.NotificationPropagationManager;
50 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
51 import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
52 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
53 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
54 import org.openecomp.sdc.versioning.AsdcItemManager;
55 import org.openecomp.sdc.versioning.VersioningManager;
56 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
57 import org.openecomp.sdc.versioning.types.Item;
58 import org.openecomp.sdcrests.vendorlicense.rest.exception.VendorLicenseModelExceptionSupplier;
59
60 class VendorLicenseModelsImplTest {
61
62     @Mock
63     private PermissionsManager permissionsManager;
64     @Mock
65     private NotificationPropagationManager notifier;
66     @Mock
67     private AsdcItemManager asdcItemManager;
68     @Mock
69     private VersioningManager versioningManager;
70     @Mock
71     private VendorLicenseManager vendorLicenseManager;
72     @Mock
73     private ActivityLogManager activityLogManager;
74     @Mock
75     private UniqueValueUtil uniqueValueUtil;
76     @Mock
77     private VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao;
78
79     @InjectMocks
80     private VendorLicenseModelsImpl vendorLicenseModels;
81
82     @BeforeEach
83     void setUp() {
84         MockitoAnnotations.openMocks(this);
85     }
86
87     @Test
88     void deleteLicenseModelSuccessTest() {
89         //given
90         final String vlmId = "vlmId";
91         final String vlmName = "vlmName";
92         final String userId = "userId";
93
94         final Item vlmItem = new Item();
95         vlmItem.setId(vlmId);
96         vlmItem.setType(ItemType.vlm.getName());
97         vlmItem.setName(vlmName);
98         when(asdcItemManager.get(vlmId)).thenReturn(vlmItem);
99
100         final VspDetails vspDetailsThatDontUseVlm1 = new VspDetails();
101         vspDetailsThatDontUseVlm1.setVendorId("otherVendorId");
102         final VspDetails vspDetailsThatDontUseVlm2 = new VspDetails();
103         vspDetailsThatDontUseVlm2.setVendorId("otherVendorId");
104         final List<VspDetails> vspDetailsList = List.of(vspDetailsThatDontUseVlm1, vspDetailsThatDontUseVlm2);
105         when(vendorSoftwareProductInfoDao.list(null)).thenReturn(vspDetailsList);
106
107         //when
108         final Response response = vendorLicenseModels.deleteLicenseModel(vlmId, userId);
109         //then
110         assertEquals(Status.OK.getStatusCode(), response.getStatus());
111         verify(asdcItemManager).delete(vlmItem);
112         verify(permissionsManager).deleteItemPermissions(vlmItem.getId());
113         verify(uniqueValueUtil).deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlmItem.getName());
114         verify(notifier).notifySubscribers(any(Event.class), eq(userId));
115     }
116
117     @Test
118     void deleteLicenseModel_cantDeleteVlmInUseTest() {
119         //given
120         final String vlmId = "vlmId";
121         final String vlmName = "vlmName";
122         final String userId = "userId";
123
124         final Item vlmItem = new Item();
125         vlmItem.setId(vlmId);
126         vlmItem.setType(ItemType.vlm.getName());
127         vlmItem.setName(vlmName);
128         when(asdcItemManager.get(vlmId)).thenReturn(vlmItem);
129
130         final VspDetails vspDetailsThatUsesVlm = new VspDetails();
131         vspDetailsThatUsesVlm.setName("VspThatUsesVlm");
132         vspDetailsThatUsesVlm.setVendorId(vlmId);
133         final VspDetails vspDetailsThatDontUseVlm = new VspDetails();
134         vspDetailsThatDontUseVlm.setName("VspThatDontUseVlm");
135         vspDetailsThatDontUseVlm.setVendorId("otherVendorId");
136         final List<VspDetails> vspDetailsList = List.of(vspDetailsThatUsesVlm, vspDetailsThatDontUseVlm);
137         when(vendorSoftwareProductInfoDao.list(null)).thenReturn(vspDetailsList);
138
139         //when
140         final CoreException actualException = assertThrows(CoreException.class, () -> vendorLicenseModels.deleteLicenseModel(vlmId, userId));
141         //then
142         final CoreException expectedException =
143             VendorLicenseModelExceptionSupplier.cantDeleteUsedVlm(vlmId, List.of(vspDetailsThatUsesVlm.getName())).get();
144         assertEquals(expectedException.code().id(), actualException.code().id());
145         assertEquals(expectedException.code().message(), actualException.code().message());
146         assertEquals(expectedException.code().category(), actualException.code().category());
147         verify(asdcItemManager, never()).delete(vlmItem);
148         verify(permissionsManager, never()).deleteItemPermissions(vlmItem.getId());
149         verify(uniqueValueUtil, never()).deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlmItem.getName());
150         verify(notifier, never()).notifySubscribers(any(Event.class), eq(userId));
151     }
152
153     @Test
154     void deleteLicenseModel_cantDeleteCertifiedTest() {
155         //given
156         final String vlmId = "vlmId";
157         final String vlmName = "vlmName";
158         final String userId = "userId";
159
160         final Item vlmItem = new Item();
161         vlmItem.setId(vlmId);
162         vlmItem.setType(ItemType.vlm.getName());
163         vlmItem.setName(vlmName);
164         vlmItem.setVersionStatusCounters(Map.of(VersionStatus.Certified, 1));
165         when(asdcItemManager.get(vlmId)).thenReturn(vlmItem);
166         when(vendorSoftwareProductInfoDao.list(null)).thenReturn(Collections.emptyList());
167
168         //when
169         final CoreException actualException = assertThrows(CoreException.class, () -> vendorLicenseModels.deleteLicenseModel(vlmId, userId));
170         //then
171         final CoreException expectedException = VendorLicenseModelExceptionSupplier.cantDeleteCertifiedVlm(vlmId).get();
172         assertEquals(expectedException.code().id(), actualException.code().id());
173         assertEquals(expectedException.code().message(), actualException.code().message());
174         assertEquals(expectedException.code().category(), actualException.code().category());
175         verify(asdcItemManager, never()).delete(vlmItem);
176         verify(permissionsManager, never()).deleteItemPermissions(vlmItem.getId());
177         verify(uniqueValueUtil, never()).deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlmItem.getName());
178         verify(notifier, never()).notifySubscribers(any(Event.class), eq(userId));
179     }
180
181     @Test
182     void deleteLicenseModel_incorrectItemTypeTest() {
183         //given
184         final String vlmId = "vlmId";
185
186         final Item vlmItem = new Item();
187         vlmItem.setId(vlmId);
188         vlmItem.setType("incorrectType");
189         when(asdcItemManager.get(vlmId)).thenReturn(vlmItem);
190
191         //when/then
192         final CoreException actualException = assertThrows(CoreException.class, () -> vendorLicenseModels.deleteLicenseModel(vlmId, "userId"));
193
194         final CoreException expectedException = VendorLicenseModelExceptionSupplier.couldNotFindVlm(vlmId).get();
195         assertEquals(expectedException.code().id(), actualException.code().id());
196         assertEquals(expectedException.code().message(), actualException.code().message());
197     }
198
199 }