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