dd3560615ef6d5178cbcd05d8f5e11c54eafc454
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21
22 package org.openecomp.sdc.vendorlicense.impl;
23
24 import static org.mockito.Matchers.anyObject;
25 import static org.mockito.Mockito.doReturn;
26
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.mockito.Spy;
31 import org.openecomp.sdc.activityLog.ActivityLogManager;
32 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
33 import org.openecomp.sdc.vendorlicense.dao.*;
34 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
35 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
36 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
37 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
38 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
39 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
40 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
41 import org.openecomp.sdc.vendorlicense.facade.impl.VendorLicenseFacadeImpl;
42 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
43 import org.openecomp.sdc.versioning.VersioningManager;
44 import org.openecomp.sdc.versioning.dao.types.Version;
45 import org.openecomp.sdc.versioning.types.VersionInfo;
46 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
47 import org.testng.Assert;
48 import org.testng.AssertJUnit.*;
49 import org.testng.annotations.BeforeMethod;
50 import org.testng.annotations.Test;
51
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.Collection;
55 import java.util.HashSet;
56 import java.util.Set;
57
58 import static org.mockito.Matchers.anyObject;
59 import static org.mockito.Mockito.doNothing;
60 import static org.mockito.Mockito.doReturn;
61 import static org.mockito.Mockito.never;
62 import static org.mockito.Mockito.verify;
63
64 /**
65  * Created by KATYR on 4/10/2016
66  */
67
68 public class FeatureGroupTest {
69     //JUnit Test Cases using Mockito
70     private static final Version VERSION01 = new Version(0, 1);
71     private static final Version VERSION10 = new Version(1, 0);
72     private final String FG1_NAME = "FG1 name";
73     private static final String USER1 = "TestUser1";
74     private static final String USER2 = "TestUser2";
75
76     private static String vlm1_id = "vlm1_id";
77     private static String vlm2_id = "vlm2_id";
78     private static String lkg1_id = "lkg1_id";
79     private static String lkg2_id = "lkg2_id";
80     private static String fg1_id = "fg1_id";
81     private static String fg2_id = "fg2_id";
82     private static String ep1_id = "ep1_id";
83     private static String ep2_id = "ep2_id";
84
85     @Mock
86     private VendorLicenseFacade vendorLicenseFacadeMcok;
87
88     @Mock
89     private LimitDao limitDaoMcok;
90     @Mock
91     private ActivityLogManager activityLogManagerMcok;
92
93     @Mock
94     private VendorLicenseModelDao vendorLicenseModelDao;
95
96     @Mock
97     private LicenseAgreementDao licenseAgreementDao;
98
99     @Mock
100     private FeatureGroupDao featureGroupDao;
101
102     @Mock
103     private EntitlementPoolDao entitlementPoolDao;
104
105     @Mock
106     private LicenseKeyGroupDao licenseKeyGroupDao;
107
108     @Mock
109     private VersioningManager versioningManager;
110
111     @InjectMocks
112     @Spy
113     private VendorLicenseManagerImpl vendorLicenseManagerImpl;
114
115     public FeatureGroupEntity updateFeatureGroup(String vlmId, Version version, String id, String name, String desc,
116                                                  String partNumber, String manufacturerReferenceNumber, Set<String>
117                                                      licenseKeyGroupIds, Set<String> entitlementPoolIds, Set<String>
118                                                      referencingLicenseAgreements){
119         FeatureGroupEntity featureGroup = new FeatureGroupEntity(vlmId, version, id);
120         featureGroup.setVendorLicenseModelId(vlmId);
121         featureGroup.setVersion(version);
122         featureGroup.setId(id);
123         featureGroup.setName(name);
124         featureGroup.setDescription(desc);
125         featureGroup.setPartNumber(partNumber);
126         //featureGroup.setManufacturerReferenceNumber(manufacturerReferenceNumber);
127         featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
128         featureGroup.setEntitlementPoolIds(entitlementPoolIds);
129         featureGroup.setReferencingLicenseAgreements(referencingLicenseAgreements);
130
131         return featureGroup;
132     }
133
134     @BeforeMethod
135     public void setUp() {
136         MockitoAnnotations.initMocks(this);
137     }
138
139     @Test
140     public void testUpdate(){
141         Set<String> licenseKeyGroupIds;
142         licenseKeyGroupIds = new HashSet<>();
143         licenseKeyGroupIds.add("lkg1");
144
145         Set<String> entitlementPoolIds;
146         entitlementPoolIds = new HashSet<>();
147         entitlementPoolIds.add("ep1");
148
149         Set<String> referencingLicenseAgreements;
150         referencingLicenseAgreements = new HashSet<>();
151         referencingLicenseAgreements.add("la1");
152
153         FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
154             "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds,
155             referencingLicenseAgreements);
156
157         doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
158
159         /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
160             featureGroupDao.update(featureGroupEntity);
161         verify(featureGroupDao).update(anyObject());*/
162     }
163
164     @Test
165     public void testUpdateWithoutManufacturingReferenceNumber(){
166         Set<String> licenseKeyGroupIds;
167         licenseKeyGroupIds = new HashSet<>();
168         licenseKeyGroupIds.add("lkg1");
169
170         Set<String> entitlementPoolIds;
171         entitlementPoolIds = new HashSet<>();
172         entitlementPoolIds.add("ep1");
173
174         Set<String> referencingLicenseAgreements;
175         referencingLicenseAgreements = new HashSet<>();
176         referencingLicenseAgreements.add("la1");
177
178         FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
179             "partNumber", null, licenseKeyGroupIds, entitlementPoolIds,
180             referencingLicenseAgreements);
181
182         doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
183
184         /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
185             featureGroupDao.update(featureGroupEntity);
186         verify(featureGroupDao, never()).update(anyObject());*/
187     }
188
189     @Test
190     public void testListFeatureGroups() {
191         doReturn(Arrays.asList(
192             createFeatureGroup(vlm1_id, VERSION01, fg1_id, "FG1", "FG1 desc", new HashSet<String>(),
193                 new HashSet<String>()),
194             createFeatureGroup(vlm1_id, VERSION01, fg2_id, "FG2", "FG2 desc", new HashSet<String>(),
195                 new HashSet<String>())))
196             .when(vendorLicenseFacadeMcok).listFeatureGroups(vlm1_id, VERSION01, USER1);
197
198         Collection<FeatureGroupEntity> FGs =
199             vendorLicenseManagerImpl.listFeatureGroups(vlm1_id, VERSION01, USER1);
200
201         verify(vendorLicenseFacadeMcok).listFeatureGroups(vlm1_id, VERSION01, USER1);
202         Assert.assertEquals(FGs.size(), 2);
203         FGs.forEach(fg -> Assert.assertTrue(fg.getId().matches(fg1_id + "|" + fg2_id)));
204     }
205
206     @Test
207     public void testCreateFeatureGroup() {
208         FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id,VERSION01,
209             fg1_id);
210
211         doReturn(featureGroupEntity).when(vendorLicenseFacadeMcok).createFeatureGroup
212             (featureGroupEntity,USER1);
213
214         vendorLicenseManagerImpl.createFeatureGroup(featureGroupEntity,USER1);
215
216         verify(vendorLicenseFacadeMcok).createFeatureGroup(featureGroupEntity,USER1);
217     }
218
219
220     @Test
221     public void testUpdateFeatureGroup(){
222         VersionInfo versionInfo = new VersionInfo();
223         versionInfo.setActiveVersion(VERSION01);
224         versionInfo.setLockingUser(USER1);
225         ArrayList<Version> viewable = new ArrayList<Version>();
226         viewable.add(VERSION01);
227         versionInfo.setViewableVersions(viewable);
228
229         doReturn(versionInfo).when(vendorLicenseManagerImpl).getVersionInfo(vlm1_id,
230             VersionableEntityAction.Write, USER1);
231         FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
232
233         existingFG.setEntitlementPoolIds(new HashSet<String>());
234         existingFG.setLicenseKeyGroupIds(new HashSet<String>());
235
236         doReturn(existingFG).when(featureGroupDao).get(existingFG);
237
238         Set<String> removedEPs = new HashSet<>();
239         Set<String> addedEPs = new HashSet<>();
240
241         addedEPs.add(ep1_id);
242         addedEPs.add(ep2_id);
243         EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
244         EntitlementPoolEntity ep2 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep2_id);
245         doReturn(ep1).when(entitlementPoolDao).get(ep1);
246         doReturn(ep2).when(entitlementPoolDao).get(ep2);
247
248         Set<String> removedLKGs = new HashSet<>();
249         Set<String> addedLKGs = new HashSet<>();
250
251         addedLKGs.add(lkg1_id);
252         addedLKGs.add(lkg2_id);
253         LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
254         LicenseKeyGroupEntity lkg2 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg2_id);
255         doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
256         doReturn(lkg2).when(licenseKeyGroupDao).get(lkg2);
257
258         doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
259             anyObject(),anyObject(), anyObject());
260
261         vendorLicenseManagerImpl.updateFeatureGroup(existingFG,addedLKGs,removedLKGs, addedEPs,
262             removedEPs, USER1);
263
264         verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
265             existingFG);
266         verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
267             existingFG);
268         verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,existingFG);
269         verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
270             existingFG);
271
272         verify(featureGroupDao)
273             .updateFeatureGroup(existingFG,addedEPs,removedEPs, addedLKGs, removedLKGs);
274         verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id,VERSION01);
275     }
276
277
278     @Test
279     public void testGetFeatureGroup(){
280         FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id,VERSION01,fg1_id);
281         vendorLicenseManagerImpl.getFeatureGroupModel(featureGroupEntity,USER1);
282         verify(vendorLicenseFacadeMcok).getFeatureGroupModel(featureGroupEntity,USER1);
283     }
284
285     @Test
286     public void deleteFeatureGroupTest() {
287
288         VersionInfo versionInfo = new VersionInfo();
289         versionInfo.setActiveVersion(VERSION01);
290         versionInfo.setLockingUser(USER1);
291         ArrayList<Version> viewable = new ArrayList<Version>();
292         viewable.add(VERSION01);
293         versionInfo.setViewableVersions(viewable);
294
295         doReturn(versionInfo).when(vendorLicenseManagerImpl).getVersionInfo(vlm1_id,
296             VersionableEntityAction.Write, USER1);
297
298         FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
299         existingFG.setName("FG_name");
300         existingFG.setLicenseKeyGroupIds(new HashSet<String>());
301         existingFG.setEntitlementPoolIds(new HashSet<String>());
302         existingFG.setLicenseKeyGroupIds(new HashSet<String>());
303
304         doReturn(existingFG).when(featureGroupDao).get(anyObject());
305
306         doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
307             .UniqueValues.FEATURE_GROUP_NAME,vlm1_id,VERSION01.toString(),existingFG.getName());
308
309         vendorLicenseManagerImpl.deleteFeatureGroup(existingFG, USER1);
310
311         verify(featureGroupDao).delete(existingFG);
312         verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id,VERSION01);
313
314         verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(existingFG
315             .getLicenseKeyGroupIds(),existingFG);
316         verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(existingFG
317             .getEntitlementPoolIds(),existingFG);
318         verify(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
319             .UniqueValues.FEATURE_GROUP_NAME,vlm1_id,VERSION01.toString(),existingFG.getName());
320     }
321
322     private FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
323                                                   String name, String description,
324                                                   Set<String> entitlementPoolIds,
325                                                   Set<String> licenseKeyGroupIds) {
326         FeatureGroupEntity featureGroup = new FeatureGroupEntity();
327         featureGroup.setVendorLicenseModelId(vendorId);
328         featureGroup.setVersion(version);
329         featureGroup.setId(id);
330         featureGroup.setName(name);
331         featureGroup.setDescription(description);
332         featureGroup.setEntitlementPoolIds(entitlementPoolIds);
333         featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
334         return featureGroup;
335     }
336 }
337