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 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;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.HashSet;
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;
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";
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";
69 private VendorLicenseFacade vendorLicenseFacadeMcok;
72 private LimitDao limitDaoMcok;
75 private VendorLicenseModelDao vendorLicenseModelDao;
78 private LicenseAgreementDao licenseAgreementDao;
81 private FeatureGroupDao featureGroupDao;
84 private EntitlementPoolDao entitlementPoolDao;
87 private LicenseKeyGroupDao licenseKeyGroupDao;
90 private VersioningManager versioningManager;
94 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
98 public void tearDown(){
99 vendorLicenseManagerImpl = null;
102 public FeatureGroupEntity updateFeatureGroup(String vlmId, Version version, String id,
103 String name, String desc,
105 String manufacturerReferenceNumber, Set<String>
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);
125 public void setUp() throws Exception {
126 MockitoAnnotations.initMocks(this);
130 public void testUpdate() {
131 Set<String> licenseKeyGroupIds;
132 licenseKeyGroupIds = new HashSet<>();
133 licenseKeyGroupIds.add("lkg1");
135 Set<String> entitlementPoolIds;
136 entitlementPoolIds = new HashSet<>();
137 entitlementPoolIds.add("ep1");
139 Set<String> referencingLicenseAgreements;
140 referencingLicenseAgreements = new HashSet<>();
141 referencingLicenseAgreements.add("la1");
143 FeatureGroupEntity featureGroupEntity =
144 updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
145 "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds,
146 referencingLicenseAgreements);
148 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
150 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
151 featureGroupDao.update(featureGroupEntity);
152 verify(featureGroupDao).update(anyObject());*/
156 public void testUpdateWithoutManufacturingReferenceNumber() {
157 Set<String> licenseKeyGroupIds;
158 licenseKeyGroupIds = new HashSet<>();
159 licenseKeyGroupIds.add("lkg1");
161 Set<String> entitlementPoolIds;
162 entitlementPoolIds = new HashSet<>();
163 entitlementPoolIds.add("ep1");
165 Set<String> referencingLicenseAgreements;
166 referencingLicenseAgreements = new HashSet<>();
167 referencingLicenseAgreements.add("la1");
169 FeatureGroupEntity featureGroupEntity =
170 updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
171 "partNumber", null, licenseKeyGroupIds, entitlementPoolIds,
172 referencingLicenseAgreements);
174 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
176 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
177 featureGroupDao.update(featureGroupEntity);
178 verify(featureGroupDao, never()).update(anyObject());*/
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);
190 Collection<FeatureGroupEntity> FGs =
191 vendorLicenseManagerImpl.listFeatureGroups(vlm1_id, VERSION01);
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)));
199 public void testCreateFeatureGroup() {
200 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id, VERSION01,
203 doReturn(featureGroupEntity).when(vendorLicenseFacadeMcok).createFeatureGroup
204 (featureGroupEntity);
206 vendorLicenseManagerImpl.createFeatureGroup(featureGroupEntity);
208 verify(vendorLicenseFacadeMcok).createFeatureGroup(featureGroupEntity);
213 public void testUpdateFeatureGroup() {
214 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
216 existingFG.setEntitlementPoolIds(new HashSet<String>());
217 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
219 doReturn(existingFG).when(featureGroupDao).get(existingFG);
221 Set<String> removedEPs = new HashSet<>();
222 Set<String> addedEPs = new HashSet<>();
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);
231 Set<String> removedLKGs = new HashSet<>();
232 Set<String> addedLKGs = new HashSet<>();
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);
241 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
242 anyObject(), anyObject(), anyObject());
244 vendorLicenseManagerImpl.updateFeatureGroup(existingFG, addedLKGs, removedLKGs, addedEPs,
247 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
249 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
251 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs, existingFG);
252 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
255 verify(featureGroupDao)
256 .updateFeatureGroup(existingFG, addedEPs, removedEPs, addedLKGs, removedLKGs);
260 public void testUpdateFeatureGroupWithAddedEpsLkgs(){
261 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
263 existingFG.setEntitlementPoolIds(new HashSet<String>());
264 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
265 existingFG.setManufacturerReferenceNumber("MRN");
267 doReturn(existingFG).when(featureGroupDao).get(anyObject());
269 Set<String> removedEPs = new HashSet<>();
270 Set<String> addedEPs = new HashSet<>();
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);
279 Set<String> removedLKGs = new HashSet<>();
280 Set<String> addedLKGs = new HashSet<>();
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);
289 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
290 anyObject(),anyObject(), anyObject());
292 FeatureGroupEntity fg = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
293 fg.setManufacturerReferenceNumber("MRN_UPD");
295 vendorLicenseManagerImpl.updateFeatureGroup(fg,addedLKGs,removedLKGs, addedEPs,
298 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
300 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
302 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,fg);
303 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
306 verify(featureGroupDao)
307 .updateFeatureGroup(fg,addedEPs,removedEPs, addedLKGs, removedLKGs);
309 verify(entitlementPoolDao, times(2)).update(anyObject());
310 verify(licenseKeyGroupDao,times(2)).update(anyObject());
314 public void testUpdateFeatureGroupWithNoAddedEpsLkgs(){
315 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
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");
323 doReturn(existingFG).when(featureGroupDao).get(anyObject());
325 EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
326 doReturn(ep1).when(entitlementPoolDao).get(ep1);
328 LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
329 doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
331 Set<String> removedEPs = new HashSet<>();
332 Set<String> addedEPs = new HashSet<>();
333 Set<String> removedLKGs = new HashSet<>();
334 Set<String> addedLKGs = new HashSet<>();
336 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
337 anyObject(),anyObject(), anyObject());
339 FeatureGroupEntity fg = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
340 fg.setManufacturerReferenceNumber("MRN_UPD");
342 vendorLicenseManagerImpl.updateFeatureGroup(fg,addedLKGs,removedLKGs, addedEPs,
345 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
347 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
349 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,fg);
350 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
353 verify(featureGroupDao)
354 .updateFeatureGroup(fg,addedEPs,removedEPs, addedLKGs, removedLKGs);
356 verify(entitlementPoolDao, times(1)).update(anyObject());
357 verify(licenseKeyGroupDao,times(1)).update(anyObject());
362 public void testGetFeatureGroup() {
363 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
364 vendorLicenseManagerImpl.getFeatureGroupModel(featureGroupEntity);
365 verify(vendorLicenseFacadeMcok).getFeatureGroupModel(featureGroupEntity);
369 public void deleteFeatureGroupTest() {
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>());
377 doReturn(existingFG).when(featureGroupDao).get(anyObject());
379 doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
380 .UniqueValues.FEATURE_GROUP_NAME, vlm1_id, VERSION01.toString(), existingFG.getName());
382 vendorLicenseManagerImpl.deleteFeatureGroup(existingFG);
384 verify(featureGroupDao).delete(existingFG);
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());
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);