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.impl;
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.EntitlementPoolDao;
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.versioning.dao.types.Version;
35 import org.openecomp.sdc.versioning.types.VersionInfo;
36 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
37 import org.testng.Assert;
38 import org.testng.annotations.BeforeMethod;
39 import org.testng.annotations.Test;
41 import java.time.LocalDate;
42 import java.time.LocalDateTime;
43 import java.time.format.DateTimeFormatter;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Collection;
47 import java.util.HashSet;
50 import static org.mockito.Matchers.anyObject;
51 import static org.mockito.Mockito.doNothing;
52 import static org.mockito.Mockito.doReturn;
53 import static org.mockito.Mockito.verify;
55 public class EntitlementPoolTest {
57 //JUnit Test Cases using Mockito
58 private final String USER1 = "epTestUser1";
59 private final String EP1_NAME = "EP1 name";
60 private final String EP2_NAME = "EP2 name";
61 private final String LT1_NAME = "LT1 name";
62 private static String vlm1_id = "vlm1_id";
63 private static String ep1_id = "ep1_id";
64 private static String ep2_id = "ep2_id";
65 public static final Version VERSION01 = new Version(0, 1);
68 private VendorLicenseFacade vendorLicenseFacade;
71 private EntitlementPoolDao entitlementPoolDao;
73 private LimitDao limitDao;
77 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
79 public EntitlementPoolEntity createEntitlementPool(String vlmId, Version version,String id,
80 String name, String desc, int threshold,
81 ThresholdUnit thresholdUnit,
82 EntitlementMetric entitlementMetricChoice,
83 String entitlementMetricOther,
85 AggregationFunction aggregationFunctionChoice,
86 String aggregationFunctionOther,
87 Set<OperationalScope> operationalScopeChoices,
88 String operationalScopeOther,
89 EntitlementTime timeChoice,
90 String timeOther, String sku) {
91 EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity();
92 entitlementPool.setVendorLicenseModelId(vlmId);
93 entitlementPool.setId(id);
94 entitlementPool.setVersion(version);
95 entitlementPool.setName(name);
96 entitlementPool.setDescription(desc);
97 entitlementPool.setThresholdValue(threshold);
98 entitlementPool.setThresholdUnit(thresholdUnit);
99 entitlementPool.setIncrements(increments);
100 entitlementPool.setOperationalScope(
101 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
102 return entitlementPool;
106 public void setUp() throws Exception {
107 MockitoAnnotations.initMocks(this);
111 public void createTest() {
112 Set<OperationalScope> opScopeChoices;
113 opScopeChoices = new HashSet<>();
114 opScopeChoices.add(OperationalScope.Core);
115 opScopeChoices.add(OperationalScope.CPU);
116 opScopeChoices.add(OperationalScope.Network_Wide);
117 EntitlementPoolEntity ep2 =
118 createEntitlementPool("vlm1Id", null, ep1_id,EP1_NAME, "EP2 dec", 70, ThresholdUnit
120 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
121 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
122 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
123 ep2.setStartDate(LocalDate.now().format(formatter));
124 ep2.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
126 vendorLicenseManagerImpl.createEntitlementPool(ep2, USER1);
127 verify(vendorLicenseFacade).createEntitlementPool(ep2,USER1);
131 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
132 "license model with id vlm1_id has invalid date range.")
133 public void createWithInvalidStartExpiryDateTest() {
135 Set<OperationalScope> opScopeChoices;
136 opScopeChoices = new HashSet<>();
137 opScopeChoices.add(OperationalScope.Core);
138 opScopeChoices.add(OperationalScope.CPU);
139 opScopeChoices.add(OperationalScope.Network_Wide);
140 EntitlementPoolEntity ep2 =
141 createEntitlementPool("vlm2Id", null, ep1_id,EP1_NAME, "EP2 dec", 70,
142 ThresholdUnit.Absolute,
143 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
144 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
145 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
146 ep2.setStartDate(LocalDate.now().format(formatter));
147 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
148 ep2.setVendorLicenseModelId(vlm1_id);
149 vendorLicenseManagerImpl.createEntitlementPool(ep2, USER1).getId();
154 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
155 "license model with id vlm1_id has invalid date range.")
156 public void createWithoutStartDateTest() {
158 Set<OperationalScope> opScopeChoices;
159 opScopeChoices = new HashSet<>();
160 opScopeChoices.add(OperationalScope.Core);
161 opScopeChoices.add(OperationalScope.CPU);
162 opScopeChoices.add(OperationalScope.Network_Wide);
163 EntitlementPoolEntity ep2 =
164 createEntitlementPool("vlm3Id", null, ep1_id,EP1_NAME, "EP2 dec", 70,
165 ThresholdUnit.Absolute,
166 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
167 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
168 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
169 ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
170 ep2.setVendorLicenseModelId(vlm1_id);
171 vendorLicenseManagerImpl.createEntitlementPool(ep2, USER1).getId();
176 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
177 "license model with id vlm1_id has invalid date range.")
178 public void createWithSameStartExpiryDateTest() {
180 Set<OperationalScope> opScopeChoices;
181 opScopeChoices = new HashSet<>();
182 opScopeChoices.add(OperationalScope.Core);
183 opScopeChoices.add(OperationalScope.CPU);
184 opScopeChoices.add(OperationalScope.Network_Wide);
185 EntitlementPoolEntity ep2 =
186 createEntitlementPool("vlm4Id", null, ep1_id,EP1_NAME, "EP2 dec", 70,
187 ThresholdUnit.Absolute,
188 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
189 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
190 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
191 ep2.setStartDate(LocalDate.now().format(formatter));
192 ep2.setExpiryDate(LocalDate.now().format(formatter));
193 ep2.setVendorLicenseModelId(vlm1_id);
194 vendorLicenseManagerImpl.createEntitlementPool(ep2, USER1).getId();
199 public void testUpdate() {
200 Set<OperationalScope> opScopeChoices;
201 opScopeChoices = new HashSet<>();
202 opScopeChoices.add(OperationalScope.Core);
203 opScopeChoices.add(OperationalScope.CPU);
204 opScopeChoices.add(OperationalScope.Network_Wide);
205 EntitlementPoolEntity ep2 =
206 createEntitlementPool(vlm1_id, VERSION01, ep1_id,EP1_NAME, "EP2 dec", 70,
209 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
210 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
211 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
212 ep2.setStartDate(LocalDate.now().minusDays(3L).format(formatter));
213 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
215 VersionInfo info = new VersionInfo();
216 info.getViewableVersions().add(VERSION01);
217 info.setActiveVersion(VERSION01);
218 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
220 vendorLicenseManagerImpl.updateEntitlementPool(ep2, USER1);
221 verify(vendorLicenseFacade).updateEntitlementPool(ep2,USER1);
222 verify(vendorLicenseFacade).updateVlmLastModificationTime(vlm1_id,VERSION01);
225 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
226 "license model with id vlm1_id has invalid date range.")
227 public void updateWithInvalidStartExpiryDateTest() {
229 Set<OperationalScope> opScopeChoices;
230 opScopeChoices = new HashSet<>();
231 opScopeChoices.add(OperationalScope.Core);
232 opScopeChoices.add(OperationalScope.CPU);
233 opScopeChoices.add(OperationalScope.Network_Wide);
234 EntitlementPoolEntity ep2 =
235 createEntitlementPool("vlm2Id", null, ep1_id,EP1_NAME, "EP2 dec", 70,
236 ThresholdUnit.Absolute,
237 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
238 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
239 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
240 ep2.setStartDate(LocalDate.now().format(formatter));
241 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
242 ep2.setVendorLicenseModelId(vlm1_id);
243 vendorLicenseManagerImpl.updateEntitlementPool(ep2, USER1);
249 public void updateWithoutStartDateTest() {
252 Set<OperationalScope> opScopeChoices;
253 opScopeChoices = new HashSet<>();
254 opScopeChoices.add(OperationalScope.Core);
255 opScopeChoices.add(OperationalScope.CPU);
256 opScopeChoices.add(OperationalScope.Network_Wide);
257 EntitlementPoolEntity ep2 =
258 createEntitlementPool("vlm3Id", null, ep1_id,EP1_NAME, "EP2 dec", 70,
259 ThresholdUnit.Absolute,
260 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
261 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
262 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
263 ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
264 vendorLicenseManagerImpl.updateEntitlementPool(ep2, USER1);
266 } catch (CoreException exception) {
267 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
272 public void updateWithSameStartExpiryDateTest() {
275 Set<OperationalScope> opScopeChoices;
276 opScopeChoices = new HashSet<>();
277 opScopeChoices.add(OperationalScope.Core);
278 opScopeChoices.add(OperationalScope.CPU);
279 opScopeChoices.add(OperationalScope.Network_Wide);
280 EntitlementPoolEntity ep2 =
281 createEntitlementPool("vlm4Id", null, ep1_id,EP1_NAME, "EP2 dec", 70,
282 ThresholdUnit.Absolute,
283 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
284 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
285 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
286 ep2.setStartDate(LocalDate.now().format(formatter));
287 ep2.setExpiryDate(LocalDate.now().format(formatter));
288 vendorLicenseManagerImpl.updateEntitlementPool(ep2, USER1);
290 } catch (CoreException exception) {
291 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
296 public void deleteEntitlementPoolTest() {
298 VersionInfo versionInfo = new VersionInfo();
299 versionInfo.setActiveVersion(VERSION01);
300 versionInfo.setLockingUser(USER1);
301 ArrayList<Version> viewable = new ArrayList<Version>();
302 viewable.add(VERSION01);
303 versionInfo.setViewableVersions(viewable);
305 doReturn(versionInfo).when(vendorLicenseManagerImpl).getVersionInfo(vlm1_id,
306 VersionableEntityAction.Write, USER1);
308 Set<OperationalScope> opScopeChoices;
309 opScopeChoices = new HashSet<>();
310 opScopeChoices.add(OperationalScope.Core);
311 opScopeChoices.add(OperationalScope.CPU);
312 opScopeChoices.add(OperationalScope.Network_Wide);
314 EntitlementPoolEntity entitlementPool =
315 createEntitlementPool(vlm1_id,VERSION01, ep1_id,EP1_NAME, "EP2 dec", 70,
316 ThresholdUnit.Absolute,EntitlementMetric.Other, "exception metric2", "inc2",
317 AggregationFunction.Average, null,
318 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
319 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
320 entitlementPool.setStartDate(LocalDate.now().format(formatter));
321 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
323 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
325 doNothing().when(vendorLicenseManagerImpl).deleteChildLimits(vlm1_id,VERSION01,ep1_id,USER1);
327 doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(anyObject(),anyObject(),
328 anyObject(),anyObject());
330 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
332 verify(entitlementPoolDao).delete(entitlementPool);
333 verify(vendorLicenseFacade).updateVlmLastModificationTime(vlm1_id,VERSION01);
338 public void testGetEntitlementPool(){
340 VersionInfo versionInfo = new VersionInfo();
341 versionInfo.setActiveVersion(VERSION01);
342 versionInfo.setLockingUser(USER1);
343 ArrayList<Version> viewable = new ArrayList<Version>();
344 viewable.add(VERSION01);
345 versionInfo.setViewableVersions(viewable);
346 versionInfo.setActiveVersion(VERSION01);
348 doReturn(versionInfo).when(vendorLicenseManagerImpl).getVersionInfo(vlm1_id,
349 VersionableEntityAction.Read, USER1);
351 Set<OperationalScope> opScopeChoices;
352 opScopeChoices = new HashSet<>();
353 opScopeChoices.add(OperationalScope.Core);
354 opScopeChoices.add(OperationalScope.CPU);
355 opScopeChoices.add(OperationalScope.Network_Wide);
357 EntitlementPoolEntity entitlementPool =
358 createEntitlementPool(vlm1_id,VERSION01, ep1_id,EP1_NAME, "EP2 dec", 70,
359 ThresholdUnit.Absolute,EntitlementMetric.Other, "exception metric2", "inc2",
360 AggregationFunction.Average, null,
361 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
362 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'");
364 entitlementPool.setStartDate(LocalDateTime.now().format(formatter));
365 entitlementPool.setExpiryDate(LocalDateTime.now().plusDays(1L).format(formatter));
367 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
369 EntitlementPoolEntity retrived = vendorLicenseManagerImpl.getEntitlementPool
370 (entitlementPool,USER1);
372 Assert.assertEquals(retrived.getId(),entitlementPool.getId());
373 Assert.assertEquals(retrived.getVendorLicenseModelId(),entitlementPool.getVendorLicenseModelId());
374 Assert.assertEquals(retrived.getVersion(),entitlementPool.getVersion());
378 public void testListEntitlmentPool(){
380 Set<OperationalScope> opScopeChoices;
381 opScopeChoices = new HashSet<>();
382 opScopeChoices.add(OperationalScope.Core);
383 opScopeChoices.add(OperationalScope.CPU);
384 opScopeChoices.add(OperationalScope.Network_Wide);
386 doReturn(Arrays.asList(
387 createEntitlementPool(vlm1_id,VERSION01, ep1_id, EP1_NAME,"EP1 dec", 70,
388 ThresholdUnit.Absolute,EntitlementMetric.Other, "exception metric1",
389 "inc1", AggregationFunction.Average, null,
390 opScopeChoices, null, EntitlementTime.Other, "time1", "sku1"),
391 createEntitlementPool(vlm1_id,VERSION01, ep2_id, EP2_NAME,"EP2 dec", 70,
392 ThresholdUnit.Absolute,EntitlementMetric.Other, "exception metric2",
393 "inc2", AggregationFunction.Average, null,
394 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2")))
395 .when(vendorLicenseFacade).listEntitlementPools(vlm1_id, VERSION01, USER1);
397 Collection<EntitlementPoolEntity> EPs =
398 vendorLicenseManagerImpl.listEntitlementPools(vlm1_id, VERSION01, USER1);
400 verify(vendorLicenseFacade).listEntitlementPools(vlm1_id, VERSION01, USER1);
401 Assert.assertEquals(EPs.size(), 2);
402 EPs.forEach(ep -> Assert.assertTrue(ep.getId().matches(ep1_id + "|" + ep2_id)));
407 public void deleteEntitlementPoolTest() {
408 Set<OperationalScope> opScopeChoices;
409 opScopeChoices = new HashSet<>();
410 opScopeChoices.add(OperationalScope.Core);
411 opScopeChoices.add(OperationalScope.CPU);
412 opScopeChoices.add(OperationalScope.Network_Wide);
414 EntitlementPoolEntity entitlementPool =
415 createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
416 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
417 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
418 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
419 entitlementPool.setStartDate(LocalDate.now().format(formatter));
420 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
422 VersionInfo info = new VersionInfo();
423 Version version = new Version();
424 info.getViewableVersions().add(version);
425 info.setActiveVersion(version);
426 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
428 LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
429 "Core",AggregationFunction.Average,10,"Hour");
431 ArrayList<LimitEntity> limitEntityList = new ArrayList();
432 limitEntityList.add(limitEntity);
434 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
435 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
436 doReturn(true).when(limitDao).isLimitPresent(anyObject());
437 doReturn(limitEntity).when(limitDao).get(anyObject());
439 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
440 limitField.setAccessible(true);
441 Field modifiersField = Field.class.getDeclaredField("modifiers");
442 modifiersField.setAccessible(true);
443 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
444 limitField.set(null, limitDao);
446 Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao");
447 epField.setAccessible(true);
448 modifiersField = Field.class.getDeclaredField("modifiers");
449 modifiersField.setAccessible(true);
450 modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
451 epField.set(null, entitlementPoolDao);
452 } catch(NoSuchFieldException | IllegalAccessException e)
457 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
459 verify(limitDao).delete(anyObject());
463 public void deleteEntitlementPoolInvalidTest() {
465 Set<OperationalScope> opScopeChoices;
466 opScopeChoices = new HashSet<>();
467 opScopeChoices.add(OperationalScope.Core);
468 opScopeChoices.add(OperationalScope.CPU);
469 opScopeChoices.add(OperationalScope.Network_Wide);
471 EntitlementPoolEntity entitlementPool =
472 createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
473 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
474 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
475 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
476 entitlementPool.setStartDate(LocalDate.now().format(formatter));
477 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
479 VersionInfo info = new VersionInfo();
480 Version version = new Version();
481 info.getViewableVersions().add(version);
482 info.setActiveVersion(version);
483 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
485 LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
486 "Core",AggregationFunction.Average,10,"Hour");
488 ArrayList<LimitEntity> limitEntityList = new ArrayList();
489 limitEntityList.add(limitEntity);
491 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
492 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
493 doReturn(false).when(limitDao).isLimitPresent(anyObject());
496 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
497 limitField.setAccessible(true);
498 Field modifiersField = Field.class.getDeclaredField("modifiers");
499 modifiersField.setAccessible(true);
500 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
501 limitField.set(null, limitDao);
503 Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao");
504 epField.setAccessible(true);
505 modifiersField = Field.class.getDeclaredField("modifiers");
506 modifiersField.setAccessible(true);
507 modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
508 epField.set(null, entitlementPoolDao);
509 } catch(NoSuchFieldException | IllegalAccessException e)
514 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
515 } catch (CoreException exception) {
516 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
520 /* private static final String USER1 = "epTestUser1";
521 private static final String USER2 = "epTestUser2";
522 private static final String EP1_V01_DESC = "EP1 desc";
523 private static final Version VERSION01 = new Version(0, 1);
524 private static final Version VERSION03 = new Version(0, 3);
525 private static final String EP1_NAME = "EP1 name";
526 private static final String EP2_NAME = "EP2 name";
528 private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
529 private static EntitlementPoolDao entitlementPoolDao;
531 private static String vlm1Id;
532 private static String vlm2Id;
533 private static String ep1Id;
534 private static String ep2Id;
536 public static EntitlementPoolEntity createEntitlementPool(String vlmId, Version version,
537 String name, String desc, int threshold,
538 ThresholdUnit thresholdUnit,
539 EntitlementMetric entitlementMetricChoice,
540 String entitlementMetricOther,
542 AggregationFunction aggregationFunctionChoice,
543 String aggregationFunctionOther,
544 Set<OperationalScope> operationalScopeChoices,
545 String operationalScopeOther,
546 EntitlementTime timeChoice,
547 String timeOther, String sku) {
548 EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity();
549 entitlementPool.setVendorLicenseModelId(vlmId);
550 entitlementPool.setVersion(version);
551 entitlementPool.setName(name);
552 entitlementPool.setDescription(desc);
553 entitlementPool.setThresholdValue(threshold);
554 entitlementPool.setThresholdUnit(thresholdUnit);
556 .setEntitlementMetric(new ChoiceOrOther<>(entitlementMetricChoice, entitlementMetricOther));
557 entitlementPool.setIncrements(increments);
558 entitlementPool.setAggregationFunction(
559 new ChoiceOrOther<>(aggregationFunctionChoice, aggregationFunctionOther));
560 entitlementPool.setOperationalScope(
561 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
562 entitlementPool.setTime(new ChoiceOrOther<>(timeChoice, timeOther));
563 return entitlementPool;
566 private static void assertEntitlementPoolsEquals(EntitlementPoolEntity actual,
567 EntitlementPoolEntity expected) {
568 Assert.assertEquals(actual.getVendorLicenseModelId(), expected.getVendorLicenseModelId());
569 Assert.assertEquals(actual.getVersion(), expected.getVersion());
570 Assert.assertEquals(actual.getId(), expected.getId());
571 Assert.assertEquals(actual.getName(), expected.getName());
572 Assert.assertEquals(actual.getDescription(), expected.getDescription());
573 Assert.assertEquals(actual.getThresholdValue(), expected.getThresholdValue());
574 Assert.assertEquals(actual.getThresholdUnit(), expected.getThresholdUnit());
575 Assert.assertEquals(actual.getEntitlementMetric(), expected.getEntitlementMetric());
576 Assert.assertEquals(actual.getIncrements(), expected.getIncrements());
577 Assert.assertEquals(actual.getAggregationFunction(), expected.getAggregationFunction());
578 Assert.assertEquals(actual.getOperationalScope(), expected.getOperationalScope());
579 Assert.assertEquals(actual.getTime(), expected.getTime());
583 private void init() {
584 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
585 vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
586 .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1 dec", "icon1"),
588 vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
589 .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
594 public void emptyListTest() {
595 Collection<EntitlementPoolEntity> entitlementPools =
596 vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
597 Assert.assertEquals(entitlementPools.size(), 0);
600 @Test(dependsOnMethods = "emptyListTest")
601 public void createTest() {
602 ep1Id = testCreate(vlm1Id, EP1_NAME);
604 Set<OperationalScope> opScopeChoices;
605 opScopeChoices = new HashSet<>();
606 opScopeChoices.add(OperationalScope.Core);
607 opScopeChoices.add(OperationalScope.CPU);
608 opScopeChoices.add(OperationalScope.Network_Wide);
609 EntitlementPoolEntity ep2 =
610 createEntitlementPool(vlm1Id, null, EP2_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
611 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
612 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
613 ep2Id = vendorLicenseManager.createEntitlementPool(ep2, USER1).getId();
617 private String testCreate(String vlmId, String name) {
618 Set<OperationalScope> opScopeChoices = new HashSet<>();
619 opScopeChoices.add(OperationalScope.Other);
620 EntitlementPoolEntity ep1 =
621 createEntitlementPool(vlmId, null, name, EP1_V01_DESC, 80, ThresholdUnit.Percentage,
622 EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
623 opScopeChoices, "op scope1", EntitlementTime.Other, "time1", "sku1");
624 String ep1Id = vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
627 EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(ep1);
628 Assert.assertTrue(loadedEp1.equals(ep1));
632 @Test(dependsOnMethods = {"createTest"})
633 public void testCreateWithExistingName_negative() {
634 testCreateWithExistingName_negative(vlm1Id, EP1_NAME);
637 @Test(dependsOnMethods = {"createTest"})
638 public void testCreateWithExistingNameUnderOtherVlm() {
639 testCreate(vlm2Id, EP1_NAME);
642 @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
643 public void updateAndGetTest() {
644 EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, VERSION01, ep1Id);
646 EntitlementPoolEntity ep1 = entitlementPoolDao.get(emptyEp1);
647 ep1.setEntitlementMetric(new ChoiceOrOther<>(EntitlementMetric.Other, "exception metric1 updated"));
648 ep1.setAggregationFunction(new ChoiceOrOther<>(AggregationFunction.Other, "agg func1 updated"));
650 vendorLicenseManager.updateEntitlementPool(ep1, USER1);
652 EntitlementPoolEntity loadedEp1 = vendorLicenseManager.getEntitlementPool(emptyEp1, USER1);
653 assertEntitlementPoolsEquals(loadedEp1, ep1);
656 @Test(dependsOnMethods = {"updateAndGetTest"})
657 public void testGetNonExistingVersion_negative() {
660 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(48, 83), ep1Id), USER1);
661 Assert.assertTrue(false);
662 } catch (CoreException exception) {
663 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
667 @Test(dependsOnMethods = {"updateAndGetTest"})
668 public void testGetOtherUserCandidateVersion_negative() {
669 vendorLicenseManager.checkin(vlm1Id, USER1);
670 vendorLicenseManager.checkout(vlm1Id, USER2);
673 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id), USER1);
674 Assert.assertTrue(false);
675 } catch (CoreException exception) {
676 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
680 @Test(dependsOnMethods = {"testGetOtherUserCandidateVersion_negative"})
681 public void testGetCandidateVersion() {
682 EntitlementPoolEntity ep = new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id);
683 ep.setDescription("updated!");
684 vendorLicenseManager.updateEntitlementPool(ep, USER2);
686 EntitlementPoolEntity actualEp = vendorLicenseManager.getEntitlementPool(ep, USER2);
687 EntitlementPoolEntity expectedEp = entitlementPoolDao.get(ep);
689 Assert.assertEquals(actualEp.getDescription(), ep.getDescription());
690 assertEntitlementPoolsEquals(actualEp, expectedEp);
693 @Test(dependsOnMethods = {"testGetCandidateVersion"})
694 public void testGetOldVersion() {
695 vendorLicenseManager.checkin(vlm1Id, USER2);
696 EntitlementPoolEntity actualEp = vendorLicenseManager
697 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 1), ep1Id), USER2);
698 Assert.assertEquals(actualEp.getDescription(), EP1_V01_DESC);
701 @Test(dependsOnMethods = {"testGetOldVersion"})
702 public void listTest() {
703 Collection<EntitlementPoolEntity> loadedEps =
704 vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
705 Assert.assertEquals(loadedEps.size(), 2);
707 int existingCounter = 0;
708 for (EntitlementPoolEntity loadedEp : loadedEps) {
709 if (ep2Id.equals(loadedEp.getId()) || ep1Id.equals(loadedEp.getId())) {
714 Assert.assertEquals(existingCounter, 2);
717 @Test(dependsOnMethods = {"listTest"})
718 public void deleteTest() {
719 vendorLicenseManager.checkout(vlm1Id, USER1);
720 EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, null, ep1Id);
721 vendorLicenseManager.deleteEntitlementPool(emptyEp1, USER1);
723 emptyEp1.setVersion(VERSION03);
724 EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(emptyEp1);
725 Assert.assertEquals(loadedEp1, null);
727 Collection<EntitlementPoolEntity> loadedEps =
728 entitlementPoolDao.list(new EntitlementPoolEntity(vlm1Id, VERSION03, null));
729 Assert.assertEquals(loadedEps.size(), 1);
730 Assert.assertEquals(loadedEps.iterator().next().getId(), ep2Id);
733 @Test(dependsOnMethods = "deleteTest")
734 public void listOldVersionTest() {
735 Collection<EntitlementPoolEntity> loadedEps =
736 vendorLicenseManager.listEntitlementPools(vlm1Id, VERSION01, USER1);
737 Assert.assertEquals(loadedEps.size(), 2);
740 @Test(dependsOnMethods = "deleteTest")
741 public void testCreateWithRemovedName() {
742 testCreate(vlm1Id, EP1_NAME);
745 @Test(dependsOnMethods = "deleteTest")
746 public void testCreateWithExistingNameAfterCheckout_negative() {
747 testCreateWithExistingName_negative(vlm1Id, EP2_NAME);
750 private void testCreateWithExistingName_negative(String vlmId, String epName) {
752 EntitlementPoolEntity ep1 =
753 createEntitlementPool(vlmId, null, epName, EP1_V01_DESC, 80, ThresholdUnit.Percentage,
754 EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
755 Collections.singleton(OperationalScope.Other), "op scope1", EntitlementTime.Other,
757 vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
759 } catch (CoreException exception) {
760 Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);