aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/test/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImplTest.java
blob: 48cb42d160053febefe49a1039f9c603e23d1204 (plain)
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
/*
 * -
 *  ============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.vendorlicense.rest.services;


import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openecomp.core.util.UniqueValueUtil;
import org.openecomp.sdc.activitylog.ActivityLogManager;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.datatypes.model.ItemType;
import org.openecomp.sdc.itempermissions.PermissionsManager;
import org.openecomp.sdc.notification.dtos.Event;
import org.openecomp.sdc.notification.services.NotificationPropagationManager;
import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
import org.openecomp.sdc.vendorlicense.VendorLicenseManager;
import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
import org.openecomp.sdc.versioning.AsdcItemManager;
import org.openecomp.sdc.versioning.VersioningManager;
import org.openecomp.sdc.versioning.dao.types.VersionStatus;
import org.openecomp.sdc.versioning.types.Item;
import org.openecomp.sdcrests.vendorlicense.rest.exception.VendorLicenseModelExceptionSupplier;

class VendorLicenseModelsImplTest {

    @Mock
    private PermissionsManager permissionsManager;
    @Mock
    private NotificationPropagationManager notifier;
    @Mock
    private AsdcItemManager asdcItemManager;
    @Mock
    private VersioningManager versioningManager;
    @Mock
    private VendorLicenseManager vendorLicenseManager;
    @Mock
    private ActivityLogManager activityLogManager;
    @Mock
    private UniqueValueUtil uniqueValueUtil;
    @Mock
    private VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao;

    @InjectMocks
    private VendorLicenseModelsImpl vendorLicenseModels;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    void deleteLicenseModelSuccessTest() {
        //given
        final String vlmId = "vlmId";
        final String vlmName = "vlmName";
        final String userId = "userId";

        final Item vlmItem = new Item();
        vlmItem.setId(vlmId);
        vlmItem.setType(ItemType.vlm.getName());
        vlmItem.setName(vlmName);
        when(asdcItemManager.get(vlmId)).thenReturn(vlmItem);

        final VspDetails vspDetailsThatDontUseVlm1 = new VspDetails();
        vspDetailsThatDontUseVlm1.setVendorId("otherVendorId");
        final VspDetails vspDetailsThatDontUseVlm2 = new VspDetails();
        vspDetailsThatDontUseVlm2.setVendorId("otherVendorId");
        final List<VspDetails> vspDetailsList = List.of(vspDetailsThatDontUseVlm1, vspDetailsThatDontUseVlm2);
        when(vendorSoftwareProductInfoDao.list(null)).thenReturn(vspDetailsList);

        //when
        final Response response = vendorLicenseModels.deleteLicenseModel(vlmId, userId);
        //then
        assertEquals(Status.OK.getStatusCode(), response.getStatus());
        verify(asdcItemManager).delete(vlmItem);
        verify(permissionsManager).deleteItemPermissions(vlmItem.getId());
        verify(uniqueValueUtil).deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlmItem.getName());
        verify(notifier).notifySubscribers(any(Event.class), eq(userId));
    }

    @Test
    void deleteLicenseModel_cantDeleteVlmInUseTest() {
        //given
        final String vlmId = "vlmId";
        final String vlmName = "vlmName";
        final String userId = "userId";

        final Item vlmItem = new Item();
        vlmItem.setId(vlmId);
        vlmItem.setType(ItemType.vlm.getName());
        vlmItem.setName(vlmName);
        when(asdcItemManager.get(vlmId)).thenReturn(vlmItem);

        final VspDetails vspDetailsThatUsesVlm = new VspDetails();
        vspDetailsThatUsesVlm.setName("VspThatUsesVlm");
        vspDetailsThatUsesVlm.setVendorId(vlmId);
        final VspDetails vspDetailsThatDontUseVlm = new VspDetails();
        vspDetailsThatDontUseVlm.setName("VspThatDontUseVlm");
        vspDetailsThatDontUseVlm.setVendorId("otherVendorId");
        final List<VspDetails> vspDetailsList = List.of(vspDetailsThatUsesVlm, vspDetailsThatDontUseVlm);
        when(vendorSoftwareProductInfoDao.list(null)).thenReturn(vspDetailsList);

        //when
        final CoreException actualException = assertThrows(CoreException.class, () -> vendorLicenseModels.deleteLicenseModel(vlmId, userId));
        //then
        final CoreException expectedException =
            VendorLicenseModelExceptionSupplier.cantDeleteUsedVlm(vlmId, List.of(vspDetailsThatUsesVlm.getName())).get();
        assertEquals(expectedException.code().id(), actualException.code().id());
        assertEquals(expectedException.code().message(), actualException.code().message());
        assertEquals(expectedException.code().category(), actualException.code().category());
        verify(asdcItemManager, never()).delete(vlmItem);
        verify(permissionsManager, never()).deleteItemPermissions(vlmItem.getId());
        verify(uniqueValueUtil, never()).deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlmItem.getName());
        verify(notifier, never()).notifySubscribers(any(Event.class), eq(userId));
    }

    @Test
    void deleteLicenseModel_cantDeleteCertifiedTest() {
        //given
        final String vlmId = "vlmId";
        final String vlmName = "vlmName";
        final String userId = "userId";

        final Item vlmItem = new Item();
        vlmItem.setId(vlmId);
        vlmItem.setType(ItemType.vlm.getName());
        vlmItem.setName(vlmName);
        vlmItem.setVersionStatusCounters(Map.of(VersionStatus.Certified, 1));
        when(asdcItemManager.get(vlmId)).thenReturn(vlmItem);
        when(vendorSoftwareProductInfoDao.list(null)).thenReturn(Collections.emptyList());

        //when
        final CoreException actualException = assertThrows(CoreException.class, () -> vendorLicenseModels.deleteLicenseModel(vlmId, userId));
        //then
        final CoreException expectedException = VendorLicenseModelExceptionSupplier.cantDeleteCertifiedVlm(vlmId).get();
        assertEquals(expectedException.code().id(), actualException.code().id());
        assertEquals(expectedException.code().message(), actualException.code().message());
        assertEquals(expectedException.code().category(), actualException.code().category());
        verify(asdcItemManager, never()).delete(vlmItem);
        verify(permissionsManager, never()).deleteItemPermissions(vlmItem.getId());
        verify(uniqueValueUtil, never()).deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlmItem.getName());
        verify(notifier, never()).notifySubscribers(any(Event.class), eq(userId));
    }

    @Test
    void deleteLicenseModel_incorrectItemTypeTest() {
        //given
        final String vlmId = "vlmId";

        final Item vlmItem = new Item();
        vlmItem.setId(vlmId);
        vlmItem.setType("incorrectType");
        when(asdcItemManager.get(vlmId)).thenReturn(vlmItem);

        //when/then
        final CoreException actualException = assertThrows(CoreException.class, () -> vendorLicenseModels.deleteLicenseModel(vlmId, "userId"));

        final CoreException expectedException = VendorLicenseModelExceptionSupplier.couldNotFindVlm(vlmId).get();
        assertEquals(expectedException.code().id(), actualException.code().id());
        assertEquals(expectedException.code().message(), actualException.code().message());
    }

}