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