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.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;
40 import static org.mockito.Matchers.anyObject;
41 import static org.mockito.Mockito.*;
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";
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";
61 private VendorLicenseFacade vendorLicenseFacadeMcok;
64 private LimitDao limitDaoMcok;
67 private VendorLicenseModelDao vendorLicenseModelDao;
70 private LicenseAgreementDao licenseAgreementDao;
73 private FeatureGroupDao featureGroupDao;
76 private EntitlementPoolDao entitlementPoolDao;
79 private LicenseKeyGroupDao licenseKeyGroupDao;
82 private VersioningManager versioningManager;
86 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
90 public void tearDown(){
91 vendorLicenseManagerImpl = null;
94 public FeatureGroupEntity updateFeatureGroup(String vlmId, Version version, String id,
95 String name, String desc,
97 String manufacturerReferenceNumber, Set<String>
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);
117 public void setUp() throws Exception {
118 MockitoAnnotations.openMocks(this);
122 public void testUpdate() {
123 Set<String> licenseKeyGroupIds;
124 licenseKeyGroupIds = new HashSet<>();
125 licenseKeyGroupIds.add("lkg1");
127 Set<String> entitlementPoolIds;
128 entitlementPoolIds = new HashSet<>();
129 entitlementPoolIds.add("ep1");
131 Set<String> referencingLicenseAgreements;
132 referencingLicenseAgreements = new HashSet<>();
133 referencingLicenseAgreements.add("la1");
135 FeatureGroupEntity featureGroupEntity =
136 updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
137 "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds,
138 referencingLicenseAgreements);
140 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
142 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
143 featureGroupDao.update(featureGroupEntity);
144 verify(featureGroupDao).update(anyObject());*/
148 public void testUpdateWithoutManufacturingReferenceNumber() {
149 Set<String> licenseKeyGroupIds;
150 licenseKeyGroupIds = new HashSet<>();
151 licenseKeyGroupIds.add("lkg1");
153 Set<String> entitlementPoolIds;
154 entitlementPoolIds = new HashSet<>();
155 entitlementPoolIds.add("ep1");
157 Set<String> referencingLicenseAgreements;
158 referencingLicenseAgreements = new HashSet<>();
159 referencingLicenseAgreements.add("la1");
161 FeatureGroupEntity featureGroupEntity =
162 updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
163 "partNumber", null, licenseKeyGroupIds, entitlementPoolIds,
164 referencingLicenseAgreements);
166 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
168 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
169 featureGroupDao.update(featureGroupEntity);
170 verify(featureGroupDao, never()).update(anyObject());*/
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);
182 Collection<FeatureGroupEntity> FGs =
183 vendorLicenseManagerImpl.listFeatureGroups(vlm1_id, VERSION01);
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)));
191 public void testCreateFeatureGroup() {
192 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id, VERSION01,
195 doReturn(featureGroupEntity).when(vendorLicenseFacadeMcok).createFeatureGroup
196 (featureGroupEntity);
198 vendorLicenseManagerImpl.createFeatureGroup(featureGroupEntity);
200 verify(vendorLicenseFacadeMcok).createFeatureGroup(featureGroupEntity);
205 public void testUpdateFeatureGroup() {
206 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
208 existingFG.setEntitlementPoolIds(new HashSet<String>());
209 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
211 doReturn(existingFG).when(featureGroupDao).get(existingFG);
213 Set<String> removedEPs = new HashSet<>();
214 Set<String> addedEPs = new HashSet<>();
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);
223 Set<String> removedLKGs = new HashSet<>();
224 Set<String> addedLKGs = new HashSet<>();
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);
233 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
234 anyObject(), anyObject(), anyObject());
236 vendorLicenseManagerImpl.updateFeatureGroup(existingFG, addedLKGs, removedLKGs, addedEPs,
239 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
241 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
243 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs, existingFG);
244 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
247 verify(featureGroupDao)
248 .updateFeatureGroup(existingFG, addedEPs, removedEPs, addedLKGs, removedLKGs);
252 public void testUpdateFeatureGroupWithAddedEpsLkgs(){
253 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
255 existingFG.setEntitlementPoolIds(new HashSet<String>());
256 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
258 doReturn(existingFG).when(featureGroupDao).get(anyObject());
260 Set<String> removedEPs = new HashSet<>();
261 Set<String> addedEPs = new HashSet<>();
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);
270 Set<String> removedLKGs = new HashSet<>();
271 Set<String> addedLKGs = new HashSet<>();
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);
280 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
281 anyObject(),anyObject(), anyObject());
283 FeatureGroupEntity fg = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
285 vendorLicenseManagerImpl.updateFeatureGroup(fg,addedLKGs,removedLKGs, addedEPs,
288 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
290 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
292 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,fg);
293 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
296 verify(featureGroupDao)
297 .updateFeatureGroup(fg,addedEPs,removedEPs, addedLKGs, removedLKGs);
301 public void testUpdateFeatureGroupWithNoAddedEpsLkgs(){
302 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
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);
309 doReturn(existingFG).when(featureGroupDao).get(anyObject());
311 EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
312 doReturn(ep1).when(entitlementPoolDao).get(ep1);
314 LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
315 doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
317 Set<String> removedEPs = new HashSet<>();
318 Set<String> addedEPs = new HashSet<>();
319 Set<String> removedLKGs = new HashSet<>();
320 Set<String> addedLKGs = new HashSet<>();
322 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
323 anyObject(),anyObject(), anyObject());
325 FeatureGroupEntity fg = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
327 vendorLicenseManagerImpl.updateFeatureGroup(fg,addedLKGs,removedLKGs, addedEPs,
330 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
332 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
334 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,fg);
335 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
338 verify(featureGroupDao)
339 .updateFeatureGroup(fg,addedEPs,removedEPs, addedLKGs, removedLKGs);
345 public void testGetFeatureGroup() {
346 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
347 vendorLicenseManagerImpl.getFeatureGroupModel(featureGroupEntity);
348 verify(vendorLicenseFacadeMcok).getFeatureGroupModel(featureGroupEntity);
352 public void deleteFeatureGroupTest() {
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>());
360 doReturn(existingFG).when(featureGroupDao).get(anyObject());
362 doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
363 .UniqueValues.FEATURE_GROUP_NAME, vlm1_id, VERSION01.toString(), existingFG.getName());
365 vendorLicenseManagerImpl.deleteFeatureGroup(existingFG);
367 verify(featureGroupDao).delete(existingFG);
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());
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);