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.facade.VendorLicenseFacade;
33 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
34 import org.openecomp.sdc.versioning.dao.types.Version;
35 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
36 import org.openecomp.sdc.versioning.types.VersionInfo;
37 import org.testng.Assert;
38 import org.testng.annotations.BeforeMethod;
39 import org.testng.annotations.Test;
41 import java.lang.reflect.Field;
42 import java.lang.reflect.Modifier;
43 import java.util.ArrayList;
44 import java.util.HashSet;
47 import static org.mockito.Matchers.anyObject;
48 import static org.mockito.Mockito.doReturn;
49 import static org.mockito.Mockito.verify;
51 public class LicenseKeyGroupTest {
53 //JUnit Test Cases using Mockito
54 private final String USER = "lkgTestUser";
55 private final String LKG_NAME = "LKG name";
56 private final String LT_NAME = "LT name";
59 private VendorLicenseFacade vendorLicenseFacade;
62 private LicenseKeyGroupDao licenseKeyGroupDao;
64 private LimitDao limitDao;
68 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
71 public void setUp() throws Exception {
72 MockitoAnnotations.initMocks(this);
75 private LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyType type, Set<OperationalScope> operationalScopeChoices,
76 String operationalScopeOther)
78 LicenseKeyGroupEntity licenseKeyGroupEntity = new LicenseKeyGroupEntity();
79 licenseKeyGroupEntity.setType(type);
80 licenseKeyGroupEntity.setOperationalScope(
81 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
82 return licenseKeyGroupEntity;
86 public void deleteLicenseKeyGroupTest() {
87 Set<OperationalScope> opScopeChoices;
88 opScopeChoices = new HashSet<>();
89 opScopeChoices.add(OperationalScope.Core);
90 opScopeChoices.add(OperationalScope.CPU);
91 opScopeChoices.add(OperationalScope.Network_Wide);
93 LicenseKeyGroupEntity licenseKeyGroup =
94 createLicenseKeyGroup(LicenseKeyType.Unique, opScopeChoices, null);
96 VersionInfo info = new VersionInfo();
97 Version version = new Version();
98 info.getViewableVersions().add(version);
99 info.setActiveVersion(version);
100 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
102 LimitEntity limitEntity = LimitTest.createLimitEntity(LT_NAME,LimitType.Vendor,"string",version,
103 EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
105 ArrayList<LimitEntity> limitEntityList = new ArrayList();
106 limitEntityList.add(limitEntity);
108 doReturn(licenseKeyGroup).when(licenseKeyGroupDao).get(anyObject());
109 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
110 doReturn(true).when(limitDao).isLimitPresent(anyObject());
111 doReturn(limitEntity).when(limitDao).get(anyObject());
113 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
114 limitField.setAccessible(true);
115 Field modifiersField = Field.class.getDeclaredField("modifiers");
116 modifiersField.setAccessible(true);
117 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
118 limitField.set(null, limitDao);
120 Field lkgField = VendorLicenseManagerImpl.class.getDeclaredField("licenseKeyGroupDao");
121 lkgField.setAccessible(true);
122 modifiersField = Field.class.getDeclaredField("modifiers");
123 modifiersField.setAccessible(true);
124 modifiersField.setInt(lkgField, lkgField.getModifiers() & ~Modifier.FINAL);
125 lkgField.set(null, licenseKeyGroupDao);
126 } catch(NoSuchFieldException | IllegalAccessException e)
131 vendorLicenseManagerImpl.deleteLicenseKeyGroup(licenseKeyGroup, USER);
133 verify(limitDao).delete(anyObject());
137 public void deleteLicenseKeyGroupInvalidTest() {
139 Set<OperationalScope> opScopeChoices;
140 opScopeChoices = new HashSet<>();
141 opScopeChoices.add(OperationalScope.Core);
142 opScopeChoices.add(OperationalScope.CPU);
143 opScopeChoices.add(OperationalScope.Network_Wide);
145 LicenseKeyGroupEntity licenseKeyGroup =
146 createLicenseKeyGroup(LicenseKeyType.Unique, opScopeChoices, null);
148 VersionInfo info = new VersionInfo();
149 Version version = new Version();
150 info.getViewableVersions().add(version);
151 info.setActiveVersion(version);
152 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
154 LimitEntity limitEntity = LimitTest.createLimitEntity(LT_NAME,LimitType.Vendor,"string",version,
155 EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
157 ArrayList<LimitEntity> limitEntityList = new ArrayList();
158 limitEntityList.add(limitEntity);
160 doReturn(licenseKeyGroup).when(licenseKeyGroupDao).get(anyObject());
161 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
162 doReturn(false).when(limitDao).isLimitPresent(anyObject());
165 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
166 limitField.setAccessible(true);
167 Field modifiersField = Field.class.getDeclaredField("modifiers");
168 modifiersField.setAccessible(true);
169 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
170 limitField.set(null, limitDao);
172 Field lkgField = VendorLicenseManagerImpl.class.getDeclaredField("licenseKeyGroupDao");
173 lkgField.setAccessible(true);
174 modifiersField = Field.class.getDeclaredField("modifiers");
175 modifiersField.setAccessible(true);
176 modifiersField.setInt(lkgField, lkgField.getModifiers() & ~Modifier.FINAL);
177 lkgField.set(null, licenseKeyGroupDao);
178 } catch(NoSuchFieldException | IllegalAccessException e)
183 vendorLicenseManagerImpl.deleteLicenseKeyGroup(licenseKeyGroup, USER);
184 } catch (CoreException exception) {
185 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
189 /*public static final String LKG1_NAME = "LKG1 name";
190 private static final Version VERSION01 = new Version(0, 1);
191 private static final String USER1 = "user1";
192 public static String vlm1Id;
193 public static String vlm2Id;
194 private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
195 private static LicenseKeyGroupDao licenseKeyGroupDao;
196 private static NoSqlDb noSqlDb;
197 private static String lkg1Id;
198 private static String lkg2Id;
200 public static LicenseKeyGroupEntity createLicenseKeyGroup(String vlmId, Version version,
201 String name, String desc,
203 MultiChoiceOrOther<OperationalScope> operationalScope) {
204 LicenseKeyGroupEntity licenseKeyGroup = new LicenseKeyGroupEntity();
205 licenseKeyGroup.setVendorLicenseModelId(vlmId);
206 licenseKeyGroup.setVersion(version);
207 licenseKeyGroup.setName(name);
208 licenseKeyGroup.setDescription(desc);
209 licenseKeyGroup.setType(type);
210 licenseKeyGroup.setOperationalScope(operationalScope);
211 return licenseKeyGroup;
215 private void init() {
216 licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
217 noSqlDb = NoSqlDbFactory.getInstance().createInterface();
219 vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
220 .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1Id dec",
221 "icon1"), USER1).getId();
222 vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
223 .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
228 public void createTest() {
229 lkg1Id = testCreate(vlm1Id, LKG1_NAME);
232 private String testCreate(String vlmId, String name) {
233 Set<OperationalScope> opScopeChoices = new HashSet<>();
234 opScopeChoices.add(OperationalScope.CPU);
235 opScopeChoices.add(OperationalScope.VM);
236 opScopeChoices.add(OperationalScope.Tenant);
237 opScopeChoices.add(OperationalScope.Data_Center);
238 LicenseKeyGroupEntity
239 lkg1 = createLicenseKeyGroup(vlmId, VERSION01, name, "LKG1 dec", LicenseKeyType.One_Time,
240 new MultiChoiceOrOther<>(opScopeChoices, null));
241 String lkg1Id = vendorLicenseManager.createLicenseKeyGroup(lkg1, USER1).getId();
244 LicenseKeyGroupEntity loadedLkg1 = licenseKeyGroupDao.get(lkg1);
245 Assert.assertTrue(loadedLkg1.equals(lkg1));
249 @Test(dependsOnMethods = {"createTest"})
250 public void testCreateWithExistingName_negative() {
252 LicenseKeyGroupEntity lkg1 =
253 createLicenseKeyGroup(vlm1Id, VERSION01, LKG1_NAME, "LKG1 dec", LicenseKeyType.One_Time,
254 new MultiChoiceOrOther<>(Collections.singleton(OperationalScope.Other),
256 vendorLicenseManager.createLicenseKeyGroup(lkg1, USER1).getId();
258 } catch (CoreException exception) {
259 Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
263 @Test(dependsOnMethods = {"createTest"})
264 public void testCreateWithExistingNameUnderOtherVlm() {
265 testCreate(vlm2Id, LKG1_NAME);
268 @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
269 public void updateAndGetTest() {
270 LicenseKeyGroupEntity lkg1 =
271 licenseKeyGroupDao.get(new LicenseKeyGroupEntity(vlm1Id, VERSION01, lkg1Id));
272 Set<OperationalScope> opScopeChoices = new HashSet<>();
273 opScopeChoices.add(OperationalScope.Other);
274 lkg1.setOperationalScope(new MultiChoiceOrOther<>(opScopeChoices, "op scope1 updated"));
275 lkg1.setDescription("LKG1 dec updated");
277 vendorLicenseManager.updateLicenseKeyGroup(lkg1, USER1);
279 LicenseKeyGroupEntity loadedLkg1 = vendorLicenseManager.getLicenseKeyGroup(lkg1, USER1);
280 Assert.assertTrue(loadedLkg1.equals(lkg1));
284 @Test(dependsOnMethods = {"updateAndGetTest"})
285 public void listTest() {
286 Set<OperationalScope> opScopeChoices = new HashSet<>();
287 opScopeChoices.add(OperationalScope.Network_Wide);
288 LicenseKeyGroupEntity lkg2 =
289 createLicenseKeyGroup(vlm1Id, VERSION01, "LKG2", "LKG2 dec", LicenseKeyType.Universal,
290 new MultiChoiceOrOther<>(opScopeChoices, null));
291 lkg2Id = vendorLicenseManager.createLicenseKeyGroup(lkg2, USER1).getId();
294 Collection<LicenseKeyGroupEntity> loadedLkgs =
295 vendorLicenseManager.listLicenseKeyGroups(vlm1Id, null, USER1);
296 Assert.assertEquals(loadedLkgs.size(), 2);
297 for (LicenseKeyGroupEntity loadedLkg : loadedLkgs) {
298 if (lkg2Id.equals(loadedLkg.getId())) {
299 Assert.assertTrue(loadedLkg.equals(lkg2));
304 @Test(dependsOnMethods = {"listTest"})
305 public void deleteTest() {
307 .deleteLicenseKeyGroup(new LicenseKeyGroupEntity(vlm1Id, VERSION01, lkg1Id), USER1);
309 LicenseKeyGroupEntity loadedLkg1 =
310 licenseKeyGroupDao.get(new LicenseKeyGroupEntity(vlm1Id, VERSION01, lkg1Id));
311 Assert.assertEquals(loadedLkg1, null);
313 Collection<LicenseKeyGroupEntity> loadedLkgs =
314 licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlm1Id, VERSION01, null));
315 Assert.assertEquals(loadedLkgs.size(), 1);
316 Assert.assertEquals(loadedLkgs.iterator().next().getId(), lkg2Id);
319 @Test(dependsOnMethods = "deleteTest")
320 public void testCreateWithRemovedName() {
321 testCreate(vlm1Id, LKG1_NAME);