2 * Copyright © 2016-2017 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.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;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collection;
43 import java.util.HashSet;
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;
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";
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";
70 private VendorLicenseFacade vendorLicenseFacadeMcok;
73 private LimitDao limitDaoMcok;
76 private VendorLicenseModelDao vendorLicenseModelDao;
79 private LicenseAgreementDao licenseAgreementDao;
82 private FeatureGroupDao featureGroupDao;
85 private EntitlementPoolDao entitlementPoolDao;
88 private LicenseKeyGroupDao licenseKeyGroupDao;
91 private VersioningManager versioningManager;
95 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
97 public FeatureGroupEntity updateFeatureGroup(String vlmId, Version version, String id,
98 String name, String desc,
100 String manufacturerReferenceNumber, Set<String>
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);
120 public void setUp() throws Exception {
121 MockitoAnnotations.initMocks(this);
125 public void testUpdate() {
126 Set<String> licenseKeyGroupIds;
127 licenseKeyGroupIds = new HashSet<>();
128 licenseKeyGroupIds.add("lkg1");
130 Set<String> entitlementPoolIds;
131 entitlementPoolIds = new HashSet<>();
132 entitlementPoolIds.add("ep1");
134 Set<String> referencingLicenseAgreements;
135 referencingLicenseAgreements = new HashSet<>();
136 referencingLicenseAgreements.add("la1");
138 FeatureGroupEntity featureGroupEntity =
139 updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
140 "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds,
141 referencingLicenseAgreements);
143 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
145 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
146 featureGroupDao.update(featureGroupEntity);
147 verify(featureGroupDao).update(anyObject());*/
151 public void testUpdateWithoutManufacturingReferenceNumber() {
152 Set<String> licenseKeyGroupIds;
153 licenseKeyGroupIds = new HashSet<>();
154 licenseKeyGroupIds.add("lkg1");
156 Set<String> entitlementPoolIds;
157 entitlementPoolIds = new HashSet<>();
158 entitlementPoolIds.add("ep1");
160 Set<String> referencingLicenseAgreements;
161 referencingLicenseAgreements = new HashSet<>();
162 referencingLicenseAgreements.add("la1");
164 FeatureGroupEntity featureGroupEntity =
165 updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
166 "partNumber", null, licenseKeyGroupIds, entitlementPoolIds,
167 referencingLicenseAgreements);
169 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
171 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
172 featureGroupDao.update(featureGroupEntity);
173 verify(featureGroupDao, never()).update(anyObject());*/
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);
185 Collection<FeatureGroupEntity> FGs =
186 vendorLicenseManagerImpl.listFeatureGroups(vlm1_id, VERSION01);
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)));
194 public void testCreateFeatureGroup() {
195 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id, VERSION01,
198 doReturn(featureGroupEntity).when(vendorLicenseFacadeMcok).createFeatureGroup
199 (featureGroupEntity);
201 vendorLicenseManagerImpl.createFeatureGroup(featureGroupEntity);
203 verify(vendorLicenseFacadeMcok).createFeatureGroup(featureGroupEntity);
208 public void testUpdateFeatureGroup() {
209 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
211 existingFG.setEntitlementPoolIds(new HashSet<String>());
212 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
214 doReturn(existingFG).when(featureGroupDao).get(existingFG);
216 Set<String> removedEPs = new HashSet<>();
217 Set<String> addedEPs = new HashSet<>();
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);
226 Set<String> removedLKGs = new HashSet<>();
227 Set<String> addedLKGs = new HashSet<>();
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);
236 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
237 anyObject(), anyObject(), anyObject());
239 vendorLicenseManagerImpl.updateFeatureGroup(existingFG, addedLKGs, removedLKGs, addedEPs,
242 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
244 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
246 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs, existingFG);
247 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
250 verify(featureGroupDao)
251 .updateFeatureGroup(existingFG, addedEPs, removedEPs, addedLKGs, removedLKGs);
255 public void testUpdateFeatureGroupWithAddedEpsLkgs(){
256 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
258 existingFG.setEntitlementPoolIds(new HashSet<String>());
259 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
260 existingFG.setManufacturerReferenceNumber("MRN");
262 doReturn(existingFG).when(featureGroupDao).get(anyObject());
264 Set<String> removedEPs = new HashSet<>();
265 Set<String> addedEPs = new HashSet<>();
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);
274 Set<String> removedLKGs = new HashSet<>();
275 Set<String> addedLKGs = new HashSet<>();
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);
284 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
285 anyObject(),anyObject(), anyObject());
287 FeatureGroupEntity fg = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
288 fg.setManufacturerReferenceNumber("MRN_UPD");
290 vendorLicenseManagerImpl.updateFeatureGroup(fg,addedLKGs,removedLKGs, addedEPs,
293 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
295 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
297 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,fg);
298 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
301 verify(featureGroupDao)
302 .updateFeatureGroup(fg,addedEPs,removedEPs, addedLKGs, removedLKGs);
304 verify(entitlementPoolDao, times(2)).update(anyObject());
305 verify(licenseKeyGroupDao,times(2)).update(anyObject());
309 public void testUpdateFeatureGroupWithNoAddedEpsLkgs(){
310 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
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");
318 doReturn(existingFG).when(featureGroupDao).get(anyObject());
320 EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
321 doReturn(ep1).when(entitlementPoolDao).get(ep1);
323 LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
324 doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
326 Set<String> removedEPs = new HashSet<>();
327 Set<String> addedEPs = new HashSet<>();
328 Set<String> removedLKGs = new HashSet<>();
329 Set<String> addedLKGs = new HashSet<>();
331 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
332 anyObject(),anyObject(), anyObject());
334 FeatureGroupEntity fg = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
335 fg.setManufacturerReferenceNumber("MRN_UPD");
337 vendorLicenseManagerImpl.updateFeatureGroup(fg,addedLKGs,removedLKGs, addedEPs,
340 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
342 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
344 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,fg);
345 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
348 verify(featureGroupDao)
349 .updateFeatureGroup(fg,addedEPs,removedEPs, addedLKGs, removedLKGs);
351 verify(entitlementPoolDao, times(1)).update(anyObject());
352 verify(licenseKeyGroupDao,times(1)).update(anyObject());
357 public void testGetFeatureGroup() {
358 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
359 vendorLicenseManagerImpl.getFeatureGroupModel(featureGroupEntity);
360 verify(vendorLicenseFacadeMcok).getFeatureGroupModel(featureGroupEntity);
364 public void deleteFeatureGroupTest() {
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>());
372 doReturn(existingFG).when(featureGroupDao).get(anyObject());
374 doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
375 .UniqueValues.FEATURE_GROUP_NAME, vlm1_id, VERSION01.toString(), existingFG.getName());
377 vendorLicenseManagerImpl.deleteFeatureGroup(existingFG);
379 verify(featureGroupDao).delete(existingFG);
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());
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);