2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.vendorlicense.impl;
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;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.HashSet;
41 import static org.mockito.Matchers.anyObject;
42 import static org.mockito.Mockito.*;
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";
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";
62 private VendorLicenseFacade vendorLicenseFacadeMcok;
65 private LimitDao limitDaoMcok;
68 private VendorLicenseModelDao vendorLicenseModelDao;
71 private LicenseAgreementDao licenseAgreementDao;
74 private FeatureGroupDao featureGroupDao;
77 private EntitlementPoolDao entitlementPoolDao;
80 private LicenseKeyGroupDao licenseKeyGroupDao;
83 private VersioningManager versioningManager;
87 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
91 public void tearDown(){
92 vendorLicenseManagerImpl = null;
95 public FeatureGroupEntity updateFeatureGroup(String vlmId, Version version, String id,
96 String name, String desc,
98 String manufacturerReferenceNumber, Set<String>
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);
118 public void setUp() throws Exception {
119 MockitoAnnotations.initMocks(this);
123 public void testUpdate() {
124 Set<String> licenseKeyGroupIds;
125 licenseKeyGroupIds = new HashSet<>();
126 licenseKeyGroupIds.add("lkg1");
128 Set<String> entitlementPoolIds;
129 entitlementPoolIds = new HashSet<>();
130 entitlementPoolIds.add("ep1");
132 Set<String> referencingLicenseAgreements;
133 referencingLicenseAgreements = new HashSet<>();
134 referencingLicenseAgreements.add("la1");
136 FeatureGroupEntity featureGroupEntity =
137 updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
138 "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds,
139 referencingLicenseAgreements);
141 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
143 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
144 featureGroupDao.update(featureGroupEntity);
145 verify(featureGroupDao).update(anyObject());*/
149 public void testUpdateWithoutManufacturingReferenceNumber() {
150 Set<String> licenseKeyGroupIds;
151 licenseKeyGroupIds = new HashSet<>();
152 licenseKeyGroupIds.add("lkg1");
154 Set<String> entitlementPoolIds;
155 entitlementPoolIds = new HashSet<>();
156 entitlementPoolIds.add("ep1");
158 Set<String> referencingLicenseAgreements;
159 referencingLicenseAgreements = new HashSet<>();
160 referencingLicenseAgreements.add("la1");
162 FeatureGroupEntity featureGroupEntity =
163 updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
164 "partNumber", null, licenseKeyGroupIds, entitlementPoolIds,
165 referencingLicenseAgreements);
167 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
169 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
170 featureGroupDao.update(featureGroupEntity);
171 verify(featureGroupDao, never()).update(anyObject());*/
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);
183 Collection<FeatureGroupEntity> FGs =
184 vendorLicenseManagerImpl.listFeatureGroups(vlm1_id, VERSION01);
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)));
192 public void testCreateFeatureGroup() {
193 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id, VERSION01,
196 doReturn(featureGroupEntity).when(vendorLicenseFacadeMcok).createFeatureGroup
197 (featureGroupEntity);
199 vendorLicenseManagerImpl.createFeatureGroup(featureGroupEntity);
201 verify(vendorLicenseFacadeMcok).createFeatureGroup(featureGroupEntity);
206 public void testUpdateFeatureGroup() {
207 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
209 existingFG.setEntitlementPoolIds(new HashSet<String>());
210 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
212 doReturn(existingFG).when(featureGroupDao).get(existingFG);
214 Set<String> removedEPs = new HashSet<>();
215 Set<String> addedEPs = new HashSet<>();
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);
224 Set<String> removedLKGs = new HashSet<>();
225 Set<String> addedLKGs = new HashSet<>();
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);
234 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
235 anyObject(), anyObject(), anyObject());
237 vendorLicenseManagerImpl.updateFeatureGroup(existingFG, addedLKGs, removedLKGs, addedEPs,
240 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
242 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
244 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs, existingFG);
245 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
248 verify(featureGroupDao)
249 .updateFeatureGroup(existingFG, addedEPs, removedEPs, addedLKGs, removedLKGs);
253 public void testUpdateFeatureGroupWithAddedEpsLkgs(){
254 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
256 existingFG.setEntitlementPoolIds(new HashSet<String>());
257 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
258 existingFG.setManufacturerReferenceNumber("MRN");
260 doReturn(existingFG).when(featureGroupDao).get(anyObject());
262 Set<String> removedEPs = new HashSet<>();
263 Set<String> addedEPs = new HashSet<>();
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);
272 Set<String> removedLKGs = new HashSet<>();
273 Set<String> addedLKGs = new HashSet<>();
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);
282 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
283 anyObject(),anyObject(), anyObject());
285 FeatureGroupEntity fg = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
286 fg.setManufacturerReferenceNumber("MRN_UPD");
288 vendorLicenseManagerImpl.updateFeatureGroup(fg,addedLKGs,removedLKGs, addedEPs,
291 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
293 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
295 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,fg);
296 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
299 verify(featureGroupDao)
300 .updateFeatureGroup(fg,addedEPs,removedEPs, addedLKGs, removedLKGs);
302 verify(entitlementPoolDao, times(2)).update(anyObject());
303 verify(licenseKeyGroupDao,times(2)).update(anyObject());
307 public void testUpdateFeatureGroupWithNoAddedEpsLkgs(){
308 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
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");
316 doReturn(existingFG).when(featureGroupDao).get(anyObject());
318 EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
319 doReturn(ep1).when(entitlementPoolDao).get(ep1);
321 LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
322 doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
324 Set<String> removedEPs = new HashSet<>();
325 Set<String> addedEPs = new HashSet<>();
326 Set<String> removedLKGs = new HashSet<>();
327 Set<String> addedLKGs = new HashSet<>();
329 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
330 anyObject(),anyObject(), anyObject());
332 FeatureGroupEntity fg = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
333 fg.setManufacturerReferenceNumber("MRN_UPD");
335 vendorLicenseManagerImpl.updateFeatureGroup(fg,addedLKGs,removedLKGs, addedEPs,
338 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
340 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
342 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,fg);
343 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
346 verify(featureGroupDao)
347 .updateFeatureGroup(fg,addedEPs,removedEPs, addedLKGs, removedLKGs);
349 verify(entitlementPoolDao, times(1)).update(anyObject());
350 verify(licenseKeyGroupDao,times(1)).update(anyObject());
355 public void testGetFeatureGroup() {
356 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
357 vendorLicenseManagerImpl.getFeatureGroupModel(featureGroupEntity);
358 verify(vendorLicenseFacadeMcok).getFeatureGroupModel(featureGroupEntity);
362 public void deleteFeatureGroupTest() {
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>());
370 doReturn(existingFG).when(featureGroupDao).get(anyObject());
372 doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
373 .UniqueValues.FEATURE_GROUP_NAME, vlm1_id, VERSION01.toString(), existingFG.getName());
375 vendorLicenseManagerImpl.deleteFeatureGroup(existingFG);
377 verify(featureGroupDao).delete(existingFG);
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());
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);