re base code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / test / java / org / openecomp / sdc / vendorlicense / impl / FeatureGroupTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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
17 package org.openecomp.sdc.vendorlicense.impl;
18
19 import org.mockito.InjectMocks;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.mockito.Spy;
23 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
24 import org.openecomp.sdc.vendorlicense.dao.*;
25 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
26 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
27 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
28 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
29 import org.openecomp.sdc.versioning.VersioningManager;
30 import org.openecomp.sdc.versioning.dao.types.Version;
31 import org.testng.Assert;
32 import org.testng.annotations.AfterMethod;
33 import org.testng.annotations.BeforeMethod;
34 import org.testng.annotations.Test;
35
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.HashSet;
39 import java.util.Set;
40
41 import static org.mockito.Matchers.anyObject;
42 import static org.mockito.Mockito.*;
43
44 public class FeatureGroupTest {
45   //JUnit Test Cases using Mockito
46   private static final Version VERSION01 = new Version(0, 1);
47   private static final Version VERSION10 = new Version(1, 0);
48   private final String FG1_NAME = "FG1 name";
49   private static final String USER1 = "TestUser1";
50   private static final String USER2 = "TestUser2";
51
52   private static String vlm1_id = "vlm1_id";
53   private static String vlm2_id = "vlm2_id";
54   private static String lkg1_id = "lkg1_id";
55   private static String lkg2_id = "lkg2_id";
56   private static String fg1_id = "fg1_id";
57   private static String fg2_id = "fg2_id";
58   private static String ep1_id = "ep1_id";
59   private static String ep2_id = "ep2_id";
60
61   @Mock
62   private VendorLicenseFacade vendorLicenseFacadeMcok;
63
64   @Mock
65   private LimitDao limitDaoMcok;
66
67   @Mock
68   private VendorLicenseModelDao vendorLicenseModelDao;
69
70   @Mock
71   private LicenseAgreementDao licenseAgreementDao;
72
73   @Mock
74   private FeatureGroupDao featureGroupDao;
75
76   @Mock
77   private EntitlementPoolDao entitlementPoolDao;
78
79   @Mock
80   private LicenseKeyGroupDao licenseKeyGroupDao;
81
82   @Mock
83   private VersioningManager versioningManager;
84
85   @InjectMocks
86   @Spy
87   private VendorLicenseManagerImpl vendorLicenseManagerImpl;
88
89
90   @AfterMethod
91   public void tearDown(){
92     vendorLicenseManagerImpl = null;
93   }
94
95   public FeatureGroupEntity updateFeatureGroup(String vlmId, Version version, String id,
96                                                String name, String desc,
97                                                String partNumber,
98                                                String manufacturerReferenceNumber, Set<String>
99                                                    licenseKeyGroupIds,
100                                                Set<String> entitlementPoolIds, Set<String>
101                                                    referencingLicenseAgreements) {
102     FeatureGroupEntity featureGroup = new FeatureGroupEntity(vlmId, version, id);
103     featureGroup.setVendorLicenseModelId(vlmId);
104     featureGroup.setVersion(version);
105     featureGroup.setId(id);
106     featureGroup.setName(name);
107     featureGroup.setDescription(desc);
108     featureGroup.setPartNumber(partNumber);
109     //featureGroup.setManufacturerReferenceNumber(manufacturerReferenceNumber);
110     featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
111     featureGroup.setEntitlementPoolIds(entitlementPoolIds);
112     featureGroup.setReferencingLicenseAgreements(referencingLicenseAgreements);
113
114     return featureGroup;
115   }
116
117   @BeforeMethod
118   public void setUp() throws Exception {
119     MockitoAnnotations.initMocks(this);
120   }
121
122   @Test
123   public void testUpdate() {
124     Set<String> licenseKeyGroupIds;
125     licenseKeyGroupIds = new HashSet<>();
126     licenseKeyGroupIds.add("lkg1");
127
128     Set<String> entitlementPoolIds;
129     entitlementPoolIds = new HashSet<>();
130     entitlementPoolIds.add("ep1");
131
132     Set<String> referencingLicenseAgreements;
133     referencingLicenseAgreements = new HashSet<>();
134     referencingLicenseAgreements.add("la1");
135
136     FeatureGroupEntity featureGroupEntity =
137         updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
138             "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds,
139             referencingLicenseAgreements);
140
141     doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
142
143         /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
144             featureGroupDao.update(featureGroupEntity);
145         verify(featureGroupDao).update(anyObject());*/
146   }
147
148   @Test
149   public void testUpdateWithoutManufacturingReferenceNumber() {
150     Set<String> licenseKeyGroupIds;
151     licenseKeyGroupIds = new HashSet<>();
152     licenseKeyGroupIds.add("lkg1");
153
154     Set<String> entitlementPoolIds;
155     entitlementPoolIds = new HashSet<>();
156     entitlementPoolIds.add("ep1");
157
158     Set<String> referencingLicenseAgreements;
159     referencingLicenseAgreements = new HashSet<>();
160     referencingLicenseAgreements.add("la1");
161
162     FeatureGroupEntity featureGroupEntity =
163         updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
164             "partNumber", null, licenseKeyGroupIds, entitlementPoolIds,
165             referencingLicenseAgreements);
166
167     doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
168
169         /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
170             featureGroupDao.update(featureGroupEntity);
171         verify(featureGroupDao, never()).update(anyObject());*/
172   }
173
174   @Test
175   public void testListFeatureGroups() {
176     doReturn(Arrays.asList(
177         createFeatureGroup(vlm1_id, VERSION01, fg1_id, "FG1", "FG1 desc", new HashSet<String>(),
178             new HashSet<String>()),
179         createFeatureGroup(vlm1_id, VERSION01, fg2_id, "FG2", "FG2 desc", new HashSet<String>(),
180             new HashSet<String>())))
181         .when(vendorLicenseFacadeMcok).listFeatureGroups(vlm1_id, VERSION01);
182
183     Collection<FeatureGroupEntity> FGs =
184         vendorLicenseManagerImpl.listFeatureGroups(vlm1_id, VERSION01);
185
186     verify(vendorLicenseFacadeMcok).listFeatureGroups(vlm1_id, VERSION01);
187     Assert.assertEquals(FGs.size(), 2);
188     FGs.forEach(fg -> Assert.assertTrue(fg.getId().matches(fg1_id + "|" + fg2_id)));
189   }
190
191   @Test
192   public void testCreateFeatureGroup() {
193     FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id, VERSION01,
194         fg1_id);
195
196     doReturn(featureGroupEntity).when(vendorLicenseFacadeMcok).createFeatureGroup
197         (featureGroupEntity);
198
199     vendorLicenseManagerImpl.createFeatureGroup(featureGroupEntity);
200
201     verify(vendorLicenseFacadeMcok).createFeatureGroup(featureGroupEntity);
202   }
203
204
205   @Test
206   public void testUpdateFeatureGroup() {
207     FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
208
209     existingFG.setEntitlementPoolIds(new HashSet<String>());
210     existingFG.setLicenseKeyGroupIds(new HashSet<String>());
211
212     doReturn(existingFG).when(featureGroupDao).get(existingFG);
213
214     Set<String> removedEPs = new HashSet<>();
215     Set<String> addedEPs = new HashSet<>();
216
217     addedEPs.add(ep1_id);
218     addedEPs.add(ep2_id);
219     EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
220     EntitlementPoolEntity ep2 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep2_id);
221     doReturn(ep1).when(entitlementPoolDao).get(ep1);
222     doReturn(ep2).when(entitlementPoolDao).get(ep2);
223
224     Set<String> removedLKGs = new HashSet<>();
225     Set<String> addedLKGs = new HashSet<>();
226
227     addedLKGs.add(lkg1_id);
228     addedLKGs.add(lkg2_id);
229     LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
230     LicenseKeyGroupEntity lkg2 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg2_id);
231     doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
232     doReturn(lkg2).when(licenseKeyGroupDao).get(lkg2);
233
234     doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
235         anyObject(), anyObject(), anyObject());
236
237     vendorLicenseManagerImpl.updateFeatureGroup(existingFG, addedLKGs, removedLKGs, addedEPs,
238         removedEPs);
239
240     verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
241         existingFG);
242     verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
243         existingFG);
244     verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs, existingFG);
245     verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
246         existingFG);
247
248     verify(featureGroupDao)
249         .updateFeatureGroup(existingFG, addedEPs, removedEPs, addedLKGs, removedLKGs);
250   }
251
252     @Test
253     public void testUpdateFeatureGroupWithAddedEpsLkgs(){
254         FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
255
256         existingFG.setEntitlementPoolIds(new HashSet<String>());
257         existingFG.setLicenseKeyGroupIds(new HashSet<String>());
258         existingFG.setManufacturerReferenceNumber("MRN");
259
260         doReturn(existingFG).when(featureGroupDao).get(anyObject());
261
262         Set<String> removedEPs = new HashSet<>();
263         Set<String> addedEPs = new HashSet<>();
264
265         addedEPs.add(ep1_id);
266         addedEPs.add(ep2_id);
267         EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
268         EntitlementPoolEntity ep2 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep2_id);
269         doReturn(ep1).when(entitlementPoolDao).get(ep1);
270         doReturn(ep2).when(entitlementPoolDao).get(ep2);
271
272         Set<String> removedLKGs = new HashSet<>();
273         Set<String> addedLKGs = new HashSet<>();
274
275         addedLKGs.add(lkg1_id);
276         addedLKGs.add(lkg2_id);
277         LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
278         LicenseKeyGroupEntity lkg2 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg2_id);
279         doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
280         doReturn(lkg2).when(licenseKeyGroupDao).get(lkg2);
281
282         doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
283             anyObject(),anyObject(), anyObject());
284
285         FeatureGroupEntity fg = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
286         fg.setManufacturerReferenceNumber("MRN_UPD");
287
288         vendorLicenseManagerImpl.updateFeatureGroup(fg,addedLKGs,removedLKGs, addedEPs,
289             removedEPs);
290
291         verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
292             fg);
293         verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
294             fg);
295         verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,fg);
296         verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
297             fg);
298
299         verify(featureGroupDao)
300             .updateFeatureGroup(fg,addedEPs,removedEPs, addedLKGs, removedLKGs);
301
302         verify(entitlementPoolDao, times(2)).update(anyObject());
303         verify(licenseKeyGroupDao,times(2)).update(anyObject());
304     }
305
306     @Test
307     public void testUpdateFeatureGroupWithNoAddedEpsLkgs(){
308         FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
309
310         HashSet<String> epSet = new HashSet<String>(); epSet.add(ep1_id);
311         HashSet<String> lkgSet = new HashSet<String>(); lkgSet.add(lkg1_id);
312         existingFG.setEntitlementPoolIds(epSet);
313         existingFG.setLicenseKeyGroupIds(lkgSet);
314         existingFG.setManufacturerReferenceNumber("MRN");
315
316         doReturn(existingFG).when(featureGroupDao).get(anyObject());
317
318         EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
319         doReturn(ep1).when(entitlementPoolDao).get(ep1);
320
321         LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
322         doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
323
324         Set<String> removedEPs = new HashSet<>();
325         Set<String> addedEPs = new HashSet<>();
326         Set<String> removedLKGs = new HashSet<>();
327         Set<String> addedLKGs = new HashSet<>();
328
329         doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
330             anyObject(),anyObject(), anyObject());
331
332         FeatureGroupEntity fg = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
333         fg.setManufacturerReferenceNumber("MRN_UPD");
334
335         vendorLicenseManagerImpl.updateFeatureGroup(fg,addedLKGs,removedLKGs, addedEPs,
336             removedEPs);
337
338         verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
339             fg);
340         verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
341             fg);
342         verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,fg);
343         verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
344             fg);
345
346         verify(featureGroupDao)
347             .updateFeatureGroup(fg,addedEPs,removedEPs, addedLKGs, removedLKGs);
348
349         verify(entitlementPoolDao, times(1)).update(anyObject());
350         verify(licenseKeyGroupDao,times(1)).update(anyObject());
351     }
352
353
354   @Test
355   public void testGetFeatureGroup() {
356     FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
357     vendorLicenseManagerImpl.getFeatureGroupModel(featureGroupEntity);
358     verify(vendorLicenseFacadeMcok).getFeatureGroupModel(featureGroupEntity);
359   }
360
361   @Test
362   public void deleteFeatureGroupTest() {
363
364     FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
365     existingFG.setName("FG_name");
366     existingFG.setLicenseKeyGroupIds(new HashSet<String>());
367     existingFG.setEntitlementPoolIds(new HashSet<String>());
368     existingFG.setLicenseKeyGroupIds(new HashSet<String>());
369
370     doReturn(existingFG).when(featureGroupDao).get(anyObject());
371
372     doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
373         .UniqueValues.FEATURE_GROUP_NAME, vlm1_id, VERSION01.toString(), existingFG.getName());
374
375     vendorLicenseManagerImpl.deleteFeatureGroup(existingFG);
376
377     verify(featureGroupDao).delete(existingFG);
378
379     verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(existingFG
380         .getLicenseKeyGroupIds(), existingFG);
381     verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(existingFG
382         .getEntitlementPoolIds(), existingFG);
383     verify(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
384         .UniqueValues.FEATURE_GROUP_NAME, vlm1_id, VERSION01.toString(), existingFG.getName());
385   }
386
387   private FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
388                                                 String name, String description,
389                                                 Set<String> entitlementPoolIds,
390                                                 Set<String> licenseKeyGroupIds) {
391     FeatureGroupEntity featureGroup = new FeatureGroupEntity();
392     featureGroup.setVendorLicenseModelId(vendorId);
393     featureGroup.setVersion(version);
394     featureGroup.setId(id);
395     featureGroup.setName(name);
396     featureGroup.setDescription(description);
397     featureGroup.setEntitlementPoolIds(entitlementPoolIds);
398     featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
399     return featureGroup;
400   }
401 }
402