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