2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.vendorlicense;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.mockito.Spy;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
30 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
31 import org.openecomp.sdc.vendorlicense.dao.types.*;
32 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes;
33 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
34 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
37 import org.openecomp.sdc.versioning.types.VersionInfo;
38 import org.testng.Assert;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
42 import java.lang.reflect.Field;
43 import java.lang.reflect.Modifier;
44 import java.util.ArrayList;
45 import java.time.LocalDate;
46 import java.time.format.DateTimeFormatter;
47 import java.util.Collection;
48 import java.util.Collections;
49 import java.util.HashSet;
52 import static org.mockito.Matchers.anyObject;
53 import static org.mockito.Mockito.doReturn;
54 import static org.mockito.Mockito.verify;
56 public class LicenseKeyGroupTest {
58 //JUnit Test Cases using Mockito
59 private final String USER = "lkgTestUser";
60 private final String LKG_NAME = "LKG name";
61 private final String LT_NAME = "LT name";
62 private final String LKG1_NAME = "LKG1 name";
63 private final String USER1 = "user1";
66 private VendorLicenseFacade vendorLicenseFacade;
69 private LicenseKeyGroupDao licenseKeyGroupDao;
71 private LimitDao limitDao;
75 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
78 public void setUp() throws Exception {
79 MockitoAnnotations.initMocks(this);
82 private LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyType type, Set<OperationalScope> operationalScopeChoices,
83 String operationalScopeOther)
85 LicenseKeyGroupEntity licenseKeyGroupEntity = new LicenseKeyGroupEntity();
86 licenseKeyGroupEntity.setType(type);
87 licenseKeyGroupEntity.setOperationalScope(
88 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
89 return licenseKeyGroupEntity;
93 public void deleteLicenseKeyGroupTest() {
94 Set<OperationalScope> opScopeChoices;
95 opScopeChoices = new HashSet<>();
96 opScopeChoices.add(OperationalScope.Core);
97 opScopeChoices.add(OperationalScope.CPU);
98 opScopeChoices.add(OperationalScope.Network_Wide);
100 LicenseKeyGroupEntity licenseKeyGroup =
101 createLicenseKeyGroup(LicenseKeyType.Unique, opScopeChoices, null);
103 VersionInfo info = new VersionInfo();
104 Version version = new Version();
105 info.getViewableVersions().add(version);
106 info.setActiveVersion(version);
107 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
109 LimitEntity limitEntity = LimitTest.createLimitEntity(LT_NAME,LimitType.Vendor,"string",version,
110 EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
112 ArrayList<LimitEntity> limitEntityList = new ArrayList();
113 limitEntityList.add(limitEntity);
115 doReturn(licenseKeyGroup).when(licenseKeyGroupDao).get(anyObject());
116 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
117 doReturn(true).when(limitDao).isLimitPresent(anyObject());
118 doReturn(limitEntity).when(limitDao).get(anyObject());
120 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
121 limitField.setAccessible(true);
122 Field modifiersField = Field.class.getDeclaredField("modifiers");
123 modifiersField.setAccessible(true);
124 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
125 limitField.set(null, limitDao);
127 Field lkgField = VendorLicenseManagerImpl.class.getDeclaredField("licenseKeyGroupDao");
128 lkgField.setAccessible(true);
129 modifiersField = Field.class.getDeclaredField("modifiers");
130 modifiersField.setAccessible(true);
131 modifiersField.setInt(lkgField, lkgField.getModifiers() & ~Modifier.FINAL);
132 lkgField.set(null, licenseKeyGroupDao);
133 } catch(NoSuchFieldException | IllegalAccessException e)
138 vendorLicenseManagerImpl.deleteLicenseKeyGroup(licenseKeyGroup, USER);
140 verify(limitDao).delete(anyObject());
144 public void deleteLicenseKeyGroupInvalidTest() {
146 Set<OperationalScope> opScopeChoices;
147 opScopeChoices = new HashSet<>();
148 opScopeChoices.add(OperationalScope.Core);
149 opScopeChoices.add(OperationalScope.CPU);
150 opScopeChoices.add(OperationalScope.Network_Wide);
152 LicenseKeyGroupEntity licenseKeyGroup =
153 createLicenseKeyGroup(LicenseKeyType.Unique, opScopeChoices, null);
155 VersionInfo info = new VersionInfo();
156 Version version = new Version();
157 info.getViewableVersions().add(version);
158 info.setActiveVersion(version);
159 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
161 LimitEntity limitEntity = LimitTest.createLimitEntity(LT_NAME,LimitType.Vendor,"string",version,
162 EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
164 ArrayList<LimitEntity> limitEntityList = new ArrayList();
165 limitEntityList.add(limitEntity);
167 doReturn(licenseKeyGroup).when(licenseKeyGroupDao).get(anyObject());
168 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
169 doReturn(false).when(limitDao).isLimitPresent(anyObject());
172 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
173 limitField.setAccessible(true);
174 Field modifiersField = Field.class.getDeclaredField("modifiers");
175 modifiersField.setAccessible(true);
176 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
177 limitField.set(null, limitDao);
179 Field lkgField = VendorLicenseManagerImpl.class.getDeclaredField("licenseKeyGroupDao");
180 lkgField.setAccessible(true);
181 modifiersField = Field.class.getDeclaredField("modifiers");
182 modifiersField.setAccessible(true);
183 modifiersField.setInt(lkgField, lkgField.getModifiers() & ~Modifier.FINAL);
184 lkgField.set(null, licenseKeyGroupDao);
185 } catch(NoSuchFieldException | IllegalAccessException e)
190 vendorLicenseManagerImpl.deleteLicenseKeyGroup(licenseKeyGroup, USER);
191 } catch (CoreException exception) {
192 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
197 public void createTest() {
198 Set<OperationalScope> opScopeChoices;
199 opScopeChoices = new HashSet<>();
200 opScopeChoices.add(OperationalScope.Core);
201 opScopeChoices.add(OperationalScope.CPU);
202 opScopeChoices.add(OperationalScope.Network_Wide);
203 LicenseKeyGroupEntity lkg =
204 createLicenseKeyGroup("vlm1Id", null, LKG1_NAME, "LKG1 dec",LicenseKeyType.Unique,
205 new MultiChoiceOrOther<>(opScopeChoices, null));
206 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
207 lkg.setStartDate(LocalDate.now().format(formatter));
208 lkg.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
210 vendorLicenseManagerImpl.createLicenseKeyGroup(lkg, USER1);
214 public void createWithInvalidStartExpiryDateTest() {
217 Set<OperationalScope> opScopeChoices;
218 opScopeChoices = new HashSet<>();
219 opScopeChoices.add(OperationalScope.Core);
220 opScopeChoices.add(OperationalScope.CPU);
221 opScopeChoices.add(OperationalScope.Network_Wide);
222 LicenseKeyGroupEntity lkg =
223 createLicenseKeyGroup("vlm1Id", null, LKG1_NAME, "LKG1 dec",LicenseKeyType.Unique,
224 new MultiChoiceOrOther<>(opScopeChoices, null));
225 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
226 lkg.setStartDate(LocalDate.now().format(formatter));
227 lkg.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
228 vendorLicenseManagerImpl.createLicenseKeyGroup(lkg, USER1);
230 } catch (CoreException exception) {
231 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
236 public void createWithoutStartDateTest() {
239 Set<OperationalScope> opScopeChoices;
240 opScopeChoices = new HashSet<>();
241 opScopeChoices.add(OperationalScope.Core);
242 opScopeChoices.add(OperationalScope.CPU);
243 opScopeChoices.add(OperationalScope.Network_Wide);
244 LicenseKeyGroupEntity lkg =
245 createLicenseKeyGroup("vlm1Id", null, LKG1_NAME, "LKG1 dec",LicenseKeyType.Unique,
246 new MultiChoiceOrOther<>(opScopeChoices, null));
247 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
248 lkg.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
249 vendorLicenseManagerImpl.createLicenseKeyGroup(lkg, USER1).getId();
251 } catch (CoreException exception) {
252 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
257 public void createWithSameStartExpiryDateTest() {
260 Set<OperationalScope> opScopeChoices;
261 opScopeChoices = new HashSet<>();
262 opScopeChoices.add(OperationalScope.Core);
263 opScopeChoices.add(OperationalScope.CPU);
264 opScopeChoices.add(OperationalScope.Network_Wide);
265 LicenseKeyGroupEntity lkg =
266 createLicenseKeyGroup("vlm1Id", null, LKG1_NAME, "LKG1 dec",LicenseKeyType.Unique,
267 new MultiChoiceOrOther<>(opScopeChoices, null));
268 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
269 lkg.setStartDate(LocalDate.now().plusDays(2L).format(formatter));
270 lkg.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
271 vendorLicenseManagerImpl.createLicenseKeyGroup(lkg, USER1).getId();
273 } catch (CoreException exception) {
274 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
279 public void createUpdate() {
280 Set<OperationalScope> opScopeChoices;
281 opScopeChoices = new HashSet<>();
282 opScopeChoices.add(OperationalScope.Core);
283 opScopeChoices.add(OperationalScope.CPU);
284 opScopeChoices.add(OperationalScope.Network_Wide);
285 LicenseKeyGroupEntity lkg =
286 createLicenseKeyGroup("vlm1Id", null, LKG1_NAME, "LKG1 dec",LicenseKeyType.Unique,
287 new MultiChoiceOrOther<>(opScopeChoices, null));
288 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
289 lkg.setStartDate(LocalDate.now().minusDays(3L).format(formatter));
290 lkg.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
291 VersionInfo info = new VersionInfo();
292 Version version = new Version();
293 info.getViewableVersions().add(version);
294 info.setActiveVersion(version);
295 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
297 vendorLicenseManagerImpl.updateLicenseKeyGroup(lkg, USER1);
301 public void updateWithInvalidStartExpiryDateTest() {
304 Set<OperationalScope> opScopeChoices;
305 opScopeChoices = new HashSet<>();
306 opScopeChoices.add(OperationalScope.Core);
307 opScopeChoices.add(OperationalScope.CPU);
308 opScopeChoices.add(OperationalScope.Network_Wide);
309 LicenseKeyGroupEntity lkg =
310 createLicenseKeyGroup("vlm1Id", null, LKG1_NAME, "LKG1 dec",LicenseKeyType.Unique,
311 new MultiChoiceOrOther<>(opScopeChoices, null));
312 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
313 lkg.setStartDate(LocalDate.now().format(formatter));
314 lkg.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
315 vendorLicenseManagerImpl.updateLicenseKeyGroup(lkg, USER1);
317 } catch (CoreException exception) {
318 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
323 public void updateWithoutStartDateTest() {
326 Set<OperationalScope> opScopeChoices;
327 opScopeChoices = new HashSet<>();
328 opScopeChoices.add(OperationalScope.Core);
329 opScopeChoices.add(OperationalScope.CPU);
330 opScopeChoices.add(OperationalScope.Network_Wide);
331 LicenseKeyGroupEntity lkg =
332 createLicenseKeyGroup("vlm1Id", null, LKG1_NAME, "LKG1 dec",LicenseKeyType.Unique,
333 new MultiChoiceOrOther<>(opScopeChoices, null));
334 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
335 lkg.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
336 vendorLicenseManagerImpl.updateLicenseKeyGroup(lkg, USER1);
338 } catch (CoreException exception) {
339 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
344 public void updateWithSameStartExpiryDateTest() {
347 Set<OperationalScope> opScopeChoices;
348 opScopeChoices = new HashSet<>();
349 opScopeChoices.add(OperationalScope.Core);
350 opScopeChoices.add(OperationalScope.CPU);
351 opScopeChoices.add(OperationalScope.Network_Wide);
352 LicenseKeyGroupEntity lkg =
353 createLicenseKeyGroup("vlm1Id", null, LKG1_NAME, "LKG1 dec",LicenseKeyType.Unique,
354 new MultiChoiceOrOther<>(opScopeChoices, null));
355 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
356 lkg.setStartDate(LocalDate.now().format(formatter));
357 lkg.setExpiryDate(LocalDate.now().format(formatter));
358 vendorLicenseManagerImpl.updateLicenseKeyGroup(lkg, USER1);
360 } catch (CoreException exception) {
361 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
366 public static LicenseKeyGroupEntity createLicenseKeyGroup(String vlmId, Version version,
367 String name, String desc,
369 MultiChoiceOrOther<OperationalScope> operationalScope) {
370 LicenseKeyGroupEntity licenseKeyGroup = new LicenseKeyGroupEntity();
371 licenseKeyGroup.setVendorLicenseModelId(vlmId);
372 licenseKeyGroup.setVersion(version);
373 licenseKeyGroup.setName(name);
374 licenseKeyGroup.setDescription(desc);
375 licenseKeyGroup.setType(type);
376 licenseKeyGroup.setOperationalScope(operationalScope);
377 return licenseKeyGroup;
380 /*public static final String LKG1_NAME = "LKG1 name";
381 private static final Version VERSION01 = new Version(0, 1);
382 public static final String LKG1_NAME = "LKG1 name";
383 private static final String USER1 = "user1";
384 public static String vlm1Id;
385 public static String vlm2Id;
386 private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
387 private static LicenseKeyGroupDao licenseKeyGroupDao;
388 private static NoSqlDb noSqlDb;
389 private static String lkg1Id;
390 private static String lkg2Id;
392 public static LicenseKeyGroupEntity createLicenseKeyGroup(String vlmId, Version version,
393 String name, String desc,
395 MultiChoiceOrOther<OperationalScope> operationalScope) {
396 LicenseKeyGroupEntity licenseKeyGroup = new LicenseKeyGroupEntity();
397 licenseKeyGroup.setVendorLicenseModelId(vlmId);
398 licenseKeyGroup.setVersion(version);
399 licenseKeyGroup.setName(name);
400 licenseKeyGroup.setDescription(desc);
401 licenseKeyGroup.setType(type);
402 licenseKeyGroup.setOperationalScope(operationalScope);
403 return licenseKeyGroup;
407 public void setUp() throws Exception {
408 MockitoAnnotations.initMocks(this);
412 private void init() {
413 licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
414 noSqlDb = NoSqlDbFactory.getInstance().createInterface();
416 vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
417 .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1Id dec",
418 "icon1"), USER1).getId();
419 vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
420 .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
425 public void createTest() {
426 lkg1Id = testCreate(vlm1Id, LKG1_NAME);
429 private String testCreate(String vlmId, String name) {
430 Set<OperationalScope> opScopeChoices = new HashSet<>();
431 opScopeChoices.add(OperationalScope.CPU);
432 opScopeChoices.add(OperationalScope.VM);
433 opScopeChoices.add(OperationalScope.Tenant);
434 opScopeChoices.add(OperationalScope.Data_Center);
435 LicenseKeyGroupEntity
436 lkg1 = createLicenseKeyGroup(vlmId, VERSION01, name, "LKG1 dec", LicenseKeyType.One_Time,
437 new MultiChoiceOrOther<>(opScopeChoices, null));
438 String lkg1Id = vendorLicenseManager.createLicenseKeyGroup(lkg1, USER1).getId();
441 LicenseKeyGroupEntity loadedLkg1 = licenseKeyGroupDao.get(lkg1);
442 Assert.assertTrue(loadedLkg1.equals(lkg1));
446 @Test(dependsOnMethods = {"createTest"})
447 public void testCreateWithExistingName_negative() {
449 LicenseKeyGroupEntity lkg1 =
450 createLicenseKeyGroup(vlm1Id, VERSION01, LKG1_NAME, "LKG1 dec", LicenseKeyType.One_Time,
451 new MultiChoiceOrOther<>(Collections.singleton(OperationalScope.Other),
453 vendorLicenseManager.createLicenseKeyGroup(lkg1, USER1).getId();
455 } catch (CoreException exception) {
456 Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
460 @Test(dependsOnMethods = {"createTest"})
461 public void testCreateWithExistingNameUnderOtherVlm() {
462 testCreate(vlm2Id, LKG1_NAME);
465 @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
466 public void updateAndGetTest() {
467 LicenseKeyGroupEntity lkg1 =
468 licenseKeyGroupDao.get(new LicenseKeyGroupEntity(vlm1Id, VERSION01, lkg1Id));
469 Set<OperationalScope> opScopeChoices = new HashSet<>();
470 opScopeChoices.add(OperationalScope.Other);
471 lkg1.setOperationalScope(new MultiChoiceOrOther<>(opScopeChoices, "op scope1 updated"));
472 lkg1.setDescription("LKG1 dec updated");
474 vendorLicenseManager.updateLicenseKeyGroup(lkg1, USER1);
476 LicenseKeyGroupEntity loadedLkg1 = vendorLicenseManager.getLicenseKeyGroup(lkg1, USER1);
477 Assert.assertTrue(loadedLkg1.equals(lkg1));
481 @Test(dependsOnMethods = {"updateAndGetTest"})
482 public void listTest() {
483 Set<OperationalScope> opScopeChoices = new HashSet<>();
484 opScopeChoices.add(OperationalScope.Network_Wide);
485 LicenseKeyGroupEntity lkg2 =
486 createLicenseKeyGroup(vlm1Id, VERSION01, "LKG2", "LKG2 dec", LicenseKeyType.Universal,
487 new MultiChoiceOrOther<>(opScopeChoices, null));
488 lkg2Id = vendorLicenseManager.createLicenseKeyGroup(lkg2, USER1).getId();
491 Collection<LicenseKeyGroupEntity> loadedLkgs =
492 vendorLicenseManager.listLicenseKeyGroups(vlm1Id, null, USER1);
493 Assert.assertEquals(loadedLkgs.size(), 2);
494 for (LicenseKeyGroupEntity loadedLkg : loadedLkgs) {
495 if (lkg2Id.equals(loadedLkg.getId())) {
496 Assert.assertTrue(loadedLkg.equals(lkg2));
501 @Test(dependsOnMethods = {"listTest"})
502 public void deleteTest() {
504 .deleteLicenseKeyGroup(new LicenseKeyGroupEntity(vlm1Id, VERSION01, lkg1Id), USER1);
506 LicenseKeyGroupEntity loadedLkg1 =
507 licenseKeyGroupDao.get(new LicenseKeyGroupEntity(vlm1Id, VERSION01, lkg1Id));
508 Assert.assertEquals(loadedLkg1, null);
510 Collection<LicenseKeyGroupEntity> loadedLkgs =
511 licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlm1Id, VERSION01, null));
512 Assert.assertEquals(loadedLkgs.size(), 1);
513 Assert.assertEquals(loadedLkgs.iterator().next().getId(), lkg2Id);
516 @Test(dependsOnMethods = "deleteTest")
517 public void testCreateWithRemovedName() {
518 testCreate(vlm1Id, LKG1_NAME);