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>());
259 doReturn(existingFG).when(featureGroupDao).get(anyObject());
261 Set<String> removedEPs = new HashSet<>();
262 Set<String> addedEPs = new HashSet<>();
264 addedEPs.add(ep1_id);
265 addedEPs.add(ep2_id);
266 EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
267 EntitlementPoolEntity ep2 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep2_id);
268 doReturn(ep1).when(entitlementPoolDao).get(ep1);
269 doReturn(ep2).when(entitlementPoolDao).get(ep2);
271 Set<String> removedLKGs = new HashSet<>();
272 Set<String> addedLKGs = new HashSet<>();
274 addedLKGs.add(lkg1_id);
275 addedLKGs.add(lkg2_id);
276 LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
277 LicenseKeyGroupEntity lkg2 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg2_id);
278 doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
279 doReturn(lkg2).when(licenseKeyGroupDao).get(lkg2);
281 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
282 anyObject(),anyObject(), anyObject());
284 FeatureGroupEntity fg = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
286 vendorLicenseManagerImpl.updateFeatureGroup(fg,addedLKGs,removedLKGs, addedEPs,
289 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
291 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
293 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,fg);
294 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
297 verify(featureGroupDao)
298 .updateFeatureGroup(fg,addedEPs,removedEPs, addedLKGs, removedLKGs);
302 public void testUpdateFeatureGroupWithNoAddedEpsLkgs(){
303 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
305 HashSet<String> epSet = new HashSet<String>(); epSet.add(ep1_id);
306 HashSet<String> lkgSet = new HashSet<String>(); lkgSet.add(lkg1_id);
307 existingFG.setEntitlementPoolIds(epSet);
308 existingFG.setLicenseKeyGroupIds(lkgSet);
310 doReturn(existingFG).when(featureGroupDao).get(anyObject());
312 EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
313 doReturn(ep1).when(entitlementPoolDao).get(ep1);
315 LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
316 doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
318 Set<String> removedEPs = new HashSet<>();
319 Set<String> addedEPs = new HashSet<>();
320 Set<String> removedLKGs = new HashSet<>();
321 Set<String> addedLKGs = new HashSet<>();
323 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
324 anyObject(),anyObject(), anyObject());
326 FeatureGroupEntity fg = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
328 vendorLicenseManagerImpl.updateFeatureGroup(fg,addedLKGs,removedLKGs, addedEPs,
331 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
333 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
335 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,fg);
336 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
339 verify(featureGroupDao)
340 .updateFeatureGroup(fg,addedEPs,removedEPs, addedLKGs, removedLKGs);
346 public void testGetFeatureGroup() {
347 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
348 vendorLicenseManagerImpl.getFeatureGroupModel(featureGroupEntity);
349 verify(vendorLicenseFacadeMcok).getFeatureGroupModel(featureGroupEntity);
353 public void deleteFeatureGroupTest() {
355 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
356 existingFG.setName("FG_name");
357 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
358 existingFG.setEntitlementPoolIds(new HashSet<String>());
359 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
361 doReturn(existingFG).when(featureGroupDao).get(anyObject());
363 doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
364 .UniqueValues.FEATURE_GROUP_NAME, vlm1_id, VERSION01.toString(), existingFG.getName());
366 vendorLicenseManagerImpl.deleteFeatureGroup(existingFG);
368 verify(featureGroupDao).delete(existingFG);
370 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(existingFG
371 .getLicenseKeyGroupIds(), existingFG);
372 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(existingFG
373 .getEntitlementPoolIds(), existingFG);
374 verify(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
375 .UniqueValues.FEATURE_GROUP_NAME, vlm1_id, VERSION01.toString(), existingFG.getName());
378 private FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
379 String name, String description,
380 Set<String> entitlementPoolIds,
381 Set<String> licenseKeyGroupIds) {
382 FeatureGroupEntity featureGroup = new FeatureGroupEntity();
383 featureGroup.setVendorLicenseModelId(vendorId);
384 featureGroup.setVersion(version);
385 featureGroup.setId(id);
386 featureGroup.setName(name);
387 featureGroup.setDescription(description);
388 featureGroup.setEntitlementPoolIds(entitlementPoolIds);
389 featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);