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.AggregationFunction;
32 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric;
33 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
34 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime;
35 import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther;
36 import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
37 import org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit;
38 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes;
39 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
40 import org.openecomp.sdc.versioning.dao.types.Version;
41 import org.testng.Assert;
42 import org.testng.annotations.BeforeMethod;
43 import org.testng.annotations.Test;
45 import java.time.LocalDate;
46 import java.time.LocalDateTime;
47 import java.time.format.DateTimeFormatter;
48 import java.util.Arrays;
49 import java.util.Collection;
50 import java.util.HashSet;
53 import static org.mockito.Matchers.anyObject;
54 import static org.mockito.Mockito.doNothing;
55 import static org.mockito.Mockito.doReturn;
56 import static org.mockito.Mockito.verify;
58 public class EntitlementPoolTest {
60 //JUnit Test Cases using Mockito
61 private final String USER1 = "epTestUser1";
62 private final String EP1_NAME = "EP1 name";
63 private final String EP2_NAME = "EP2 name";
64 private final String LT1_NAME = "LT1 name";
65 private static String vlm1_id = "vlm1_id";
66 private static String ep1_id = "ep1_id";
67 private static String ep2_id = "ep2_id";
68 public static final Version VERSION01 = new Version(0, 1);
71 private VendorLicenseFacade vendorLicenseFacade;
74 private EntitlementPoolDao entitlementPoolDao;
76 private LimitDao limitDao;
80 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
82 public EntitlementPoolEntity createEntitlementPool(String vlmId, Version version, String id,
83 String name, String desc, int threshold,
84 ThresholdUnit thresholdUnit,
85 EntitlementMetric entitlementMetricChoice,
86 String entitlementMetricOther,
88 AggregationFunction aggregationFunctionChoice,
89 String aggregationFunctionOther,
90 Set<OperationalScope> operationalScopeChoices,
91 String operationalScopeOther,
92 EntitlementTime timeChoice,
93 String timeOther, String sku) {
94 EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity();
95 entitlementPool.setVendorLicenseModelId(vlmId);
96 entitlementPool.setId(id);
97 entitlementPool.setVersion(version);
98 entitlementPool.setName(name);
99 entitlementPool.setDescription(desc);
100 entitlementPool.setThresholdValue(threshold);
101 entitlementPool.setThresholdUnit(thresholdUnit);
102 entitlementPool.setIncrements(increments);
103 entitlementPool.setOperationalScope(
104 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
105 return entitlementPool;
109 public void setUp() throws Exception {
110 MockitoAnnotations.initMocks(this);
114 public void createTest() {
115 Set<OperationalScope> opScopeChoices;
116 opScopeChoices = new HashSet<>();
117 opScopeChoices.add(OperationalScope.Core);
118 opScopeChoices.add(OperationalScope.CPU);
119 opScopeChoices.add(OperationalScope.Network_Wide);
120 EntitlementPoolEntity ep2 =
121 createEntitlementPool("vlm1Id", null, ep1_id, EP1_NAME, "EP2 dec", 70, ThresholdUnit
123 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
124 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
125 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
126 ep2.setStartDate(LocalDate.now().format(formatter));
127 ep2.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
129 vendorLicenseManagerImpl.createEntitlementPool(ep2);
130 verify(vendorLicenseFacade).createEntitlementPool(ep2);
134 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
135 "license model with id vlm1_id has invalid date range.")
136 public void createWithInvalidStartExpiryDateTest() {
138 Set<OperationalScope> opScopeChoices;
139 opScopeChoices = new HashSet<>();
140 opScopeChoices.add(OperationalScope.Core);
141 opScopeChoices.add(OperationalScope.CPU);
142 opScopeChoices.add(OperationalScope.Network_Wide);
143 EntitlementPoolEntity ep2 =
144 createEntitlementPool("vlm2Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
145 ThresholdUnit.Absolute,
146 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
147 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
148 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
149 ep2.setStartDate(LocalDate.now().format(formatter));
150 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
151 ep2.setVendorLicenseModelId(vlm1_id);
152 vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
157 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
158 "license model with id vlm1_id has invalid date range.")
159 public void createWithoutStartDateTest() {
161 Set<OperationalScope> opScopeChoices;
162 opScopeChoices = new HashSet<>();
163 opScopeChoices.add(OperationalScope.Core);
164 opScopeChoices.add(OperationalScope.CPU);
165 opScopeChoices.add(OperationalScope.Network_Wide);
166 EntitlementPoolEntity ep2 =
167 createEntitlementPool("vlm3Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
168 ThresholdUnit.Absolute,
169 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
170 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
171 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
172 ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
173 ep2.setVendorLicenseModelId(vlm1_id);
174 vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
179 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
180 "license model with id vlm1_id has invalid date range.")
181 public void createWithSameStartExpiryDateTest() {
183 Set<OperationalScope> opScopeChoices;
184 opScopeChoices = new HashSet<>();
185 opScopeChoices.add(OperationalScope.Core);
186 opScopeChoices.add(OperationalScope.CPU);
187 opScopeChoices.add(OperationalScope.Network_Wide);
188 EntitlementPoolEntity ep2 =
189 createEntitlementPool("vlm4Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
190 ThresholdUnit.Absolute,
191 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
192 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
193 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
194 ep2.setStartDate(LocalDate.now().format(formatter));
195 ep2.setExpiryDate(LocalDate.now().format(formatter));
196 ep2.setVendorLicenseModelId(vlm1_id);
197 vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
202 public void testUpdate() {
203 Set<OperationalScope> opScopeChoices;
204 opScopeChoices = new HashSet<>();
205 opScopeChoices.add(OperationalScope.Core);
206 opScopeChoices.add(OperationalScope.CPU);
207 opScopeChoices.add(OperationalScope.Network_Wide);
208 EntitlementPoolEntity ep2 =
209 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
212 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
213 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
214 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
215 ep2.setStartDate(LocalDate.now().minusDays(3L).format(formatter));
216 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
218 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
221 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
222 "license model with id vlm1_id has invalid date range.")
223 public void updateWithInvalidStartExpiryDateTest() {
225 Set<OperationalScope> opScopeChoices;
226 opScopeChoices = new HashSet<>();
227 opScopeChoices.add(OperationalScope.Core);
228 opScopeChoices.add(OperationalScope.CPU);
229 opScopeChoices.add(OperationalScope.Network_Wide);
230 EntitlementPoolEntity ep2 =
231 createEntitlementPool("vlm2Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
232 ThresholdUnit.Absolute,
233 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
234 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
235 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
236 ep2.setStartDate(LocalDate.now().format(formatter));
237 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
238 ep2.setVendorLicenseModelId(vlm1_id);
239 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
245 public void updateWithoutStartDateTest() {
248 Set<OperationalScope> opScopeChoices;
249 opScopeChoices = new HashSet<>();
250 opScopeChoices.add(OperationalScope.Core);
251 opScopeChoices.add(OperationalScope.CPU);
252 opScopeChoices.add(OperationalScope.Network_Wide);
253 EntitlementPoolEntity ep2 =
254 createEntitlementPool("vlm3Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
255 ThresholdUnit.Absolute,
256 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average,
258 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
259 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
260 ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
261 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
263 } catch (CoreException exception) {
264 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
269 public void updateWithSameStartExpiryDateTest() {
272 Set<OperationalScope> opScopeChoices;
273 opScopeChoices = new HashSet<>();
274 opScopeChoices.add(OperationalScope.Core);
275 opScopeChoices.add(OperationalScope.CPU);
276 opScopeChoices.add(OperationalScope.Network_Wide);
277 EntitlementPoolEntity ep2 =
278 createEntitlementPool("vlm4Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
279 ThresholdUnit.Absolute,
280 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average,
282 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
283 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
284 ep2.setStartDate(LocalDate.now().format(formatter));
285 ep2.setExpiryDate(LocalDate.now().format(formatter));
286 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
288 } catch (CoreException exception) {
289 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
294 public void deleteEntitlementPoolTest() {
295 Set<OperationalScope> opScopeChoices;
296 opScopeChoices = new HashSet<>();
297 opScopeChoices.add(OperationalScope.Core);
298 opScopeChoices.add(OperationalScope.CPU);
299 opScopeChoices.add(OperationalScope.Network_Wide);
301 EntitlementPoolEntity entitlementPool =
302 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
303 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2", "inc2",
304 AggregationFunction.Average, null,
305 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
306 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
307 entitlementPool.setStartDate(LocalDate.now().format(formatter));
308 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
310 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
312 doNothing().when(vendorLicenseManagerImpl).deleteChildLimits(vlm1_id, VERSION01, ep1_id);
314 doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(anyObject(), anyObject(),
315 anyObject(), anyObject());
317 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool);
319 verify(entitlementPoolDao).delete(entitlementPool);
323 public void testGetEntitlementPool() {
324 Set<OperationalScope> opScopeChoices;
325 opScopeChoices = new HashSet<>();
326 opScopeChoices.add(OperationalScope.Core);
327 opScopeChoices.add(OperationalScope.CPU);
328 opScopeChoices.add(OperationalScope.Network_Wide);
330 EntitlementPoolEntity entitlementPool =
331 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
332 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2", "inc2",
333 AggregationFunction.Average, null,
334 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
335 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'");
337 entitlementPool.setStartDate(LocalDateTime.now().format(formatter));
338 entitlementPool.setExpiryDate(LocalDateTime.now().plusDays(1L).format(formatter));
340 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
342 EntitlementPoolEntity retrived = vendorLicenseManagerImpl.getEntitlementPool(entitlementPool);
344 Assert.assertEquals(retrived.getId(), entitlementPool.getId());
345 Assert.assertEquals(retrived.getVendorLicenseModelId(),
346 entitlementPool.getVendorLicenseModelId());
347 Assert.assertEquals(retrived.getVersion(), entitlementPool.getVersion());
351 public void testListEntitlmentPool() {
353 Set<OperationalScope> opScopeChoices;
354 opScopeChoices = new HashSet<>();
355 opScopeChoices.add(OperationalScope.Core);
356 opScopeChoices.add(OperationalScope.CPU);
357 opScopeChoices.add(OperationalScope.Network_Wide);
359 doReturn(Arrays.asList(
360 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP1 dec", 70,
361 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric1",
362 "inc1", AggregationFunction.Average, null,
363 opScopeChoices, null, EntitlementTime.Other, "time1", "sku1"),
364 createEntitlementPool(vlm1_id, VERSION01, ep2_id, EP2_NAME, "EP2 dec", 70,
365 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2",
366 "inc2", AggregationFunction.Average, null,
367 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2")))
368 .when(vendorLicenseFacade).listEntitlementPools(vlm1_id, VERSION01);
370 Collection<EntitlementPoolEntity> EPs =
371 vendorLicenseManagerImpl.listEntitlementPools(vlm1_id, VERSION01);
373 verify(vendorLicenseFacade).listEntitlementPools(vlm1_id, VERSION01);
374 Assert.assertEquals(EPs.size(), 2);
375 EPs.forEach(ep -> Assert.assertTrue(ep.getId().matches(ep1_id + "|" + ep2_id)));
380 public void deleteEntitlementPoolTest() {
381 Set<OperationalScope> opScopeChoices;
382 opScopeChoices = new HashSet<>();
383 opScopeChoices.add(OperationalScope.Core);
384 opScopeChoices.add(OperationalScope.CPU);
385 opScopeChoices.add(OperationalScope.Network_Wide);
387 EntitlementPoolEntity entitlementPool =
388 createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
389 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
390 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
391 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
392 entitlementPool.setStartDate(LocalDate.now().format(formatter));
393 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
395 VersionInfo info = new VersionInfo();
396 Version version = new Version();
397 info.getViewableVersions().add(version);
398 info.setActiveVersion(version);
399 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
401 LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
402 "Core",AggregationFunction.Average,10,"Hour");
404 ArrayList<LimitEntity> limitEntityList = new ArrayList();
405 limitEntityList.add(limitEntity);
407 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
408 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
409 doReturn(true).when(limitDao).isLimitPresent(anyObject());
410 doReturn(limitEntity).when(limitDao).get(anyObject());
412 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
413 limitField.setAccessible(true);
414 Field modifiersField = Field.class.getDeclaredField("modifiers");
415 modifiersField.setAccessible(true);
416 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
417 limitField.set(null, limitDao);
419 Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao");
420 epField.setAccessible(true);
421 modifiersField = Field.class.getDeclaredField("modifiers");
422 modifiersField.setAccessible(true);
423 modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
424 epField.set(null, entitlementPoolDao);
425 } catch(NoSuchFieldException | IllegalAccessException e)
430 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
432 verify(limitDao).delete(anyObject());
436 public void deleteEntitlementPoolInvalidTest() {
438 Set<OperationalScope> opScopeChoices;
439 opScopeChoices = new HashSet<>();
440 opScopeChoices.add(OperationalScope.Core);
441 opScopeChoices.add(OperationalScope.CPU);
442 opScopeChoices.add(OperationalScope.Network_Wide);
444 EntitlementPoolEntity entitlementPool =
445 createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
446 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
447 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
448 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
449 entitlementPool.setStartDate(LocalDate.now().format(formatter));
450 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
452 VersionInfo info = new VersionInfo();
453 Version version = new Version();
454 info.getViewableVersions().add(version);
455 info.setActiveVersion(version);
456 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
458 LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
459 "Core",AggregationFunction.Average,10,"Hour");
461 ArrayList<LimitEntity> limitEntityList = new ArrayList();
462 limitEntityList.add(limitEntity);
464 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
465 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
466 doReturn(false).when(limitDao).isLimitPresent(anyObject());
469 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
470 limitField.setAccessible(true);
471 Field modifiersField = Field.class.getDeclaredField("modifiers");
472 modifiersField.setAccessible(true);
473 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
474 limitField.set(null, limitDao);
476 Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao");
477 epField.setAccessible(true);
478 modifiersField = Field.class.getDeclaredField("modifiers");
479 modifiersField.setAccessible(true);
480 modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
481 epField.set(null, entitlementPoolDao);
482 } catch(NoSuchFieldException | IllegalAccessException e)
487 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
488 } catch (CoreException exception) {
489 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
493 /* private static final String USER1 = "epTestUser1";
494 private static final String USER2 = "epTestUser2";
495 private static final String EP1_V01_DESC = "EP1 desc";
496 private static final Version VERSION01 = new Version(0, 1);
497 private static final Version VERSION03 = new Version(0, 3);
498 private static final String EP1_NAME = "EP1 name";
499 private static final String EP2_NAME = "EP2 name";
501 private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
502 private static EntitlementPoolDao entitlementPoolDao;
504 private static String vlm1Id;
505 private static String vlm2Id;
506 private static String ep1Id;
507 private static String ep2Id;
509 public static EntitlementPoolEntity createEntitlementPool(String vlmId, Version version,
510 String name, String desc, int threshold,
511 ThresholdUnit thresholdUnit,
512 EntitlementMetric entitlementMetricChoice,
513 String entitlementMetricOther,
515 AggregationFunction aggregationFunctionChoice,
516 String aggregationFunctionOther,
517 Set<OperationalScope> operationalScopeChoices,
518 String operationalScopeOther,
519 EntitlementTime timeChoice,
520 String timeOther, String sku) {
521 EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity();
522 entitlementPool.setVendorLicenseModelId(vlmId);
523 entitlementPool.setVersion(version);
524 entitlementPool.setName(name);
525 entitlementPool.setDescription(desc);
526 entitlementPool.setThresholdValue(threshold);
527 entitlementPool.setThresholdUnit(thresholdUnit);
529 .setEntitlementMetric(new ChoiceOrOther<>(entitlementMetricChoice, entitlementMetricOther));
530 entitlementPool.setIncrements(increments);
531 entitlementPool.setAggregationFunction(
532 new ChoiceOrOther<>(aggregationFunctionChoice, aggregationFunctionOther));
533 entitlementPool.setOperationalScope(
534 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
535 entitlementPool.setTime(new ChoiceOrOther<>(timeChoice, timeOther));
536 return entitlementPool;
539 private static void assertEntitlementPoolsEquals(EntitlementPoolEntity actual,
540 EntitlementPoolEntity expected) {
541 Assert.assertEquals(actual.getVendorLicenseModelId(), expected.getVendorLicenseModelId());
542 Assert.assertEquals(actual.getVersion(), expected.getVersion());
543 Assert.assertEquals(actual.getId(), expected.getId());
544 Assert.assertEquals(actual.getName(), expected.getName());
545 Assert.assertEquals(actual.getDescription(), expected.getDescription());
546 Assert.assertEquals(actual.getThresholdValue(), expected.getThresholdValue());
547 Assert.assertEquals(actual.getThresholdUnit(), expected.getThresholdUnit());
548 Assert.assertEquals(actual.getEntitlementMetric(), expected.getEntitlementMetric());
549 Assert.assertEquals(actual.getIncrements(), expected.getIncrements());
550 Assert.assertEquals(actual.getAggregationFunction(), expected.getAggregationFunction());
551 Assert.assertEquals(actual.getOperationalScope(), expected.getOperationalScope());
552 Assert.assertEquals(actual.getTime(), expected.getTime());
556 private void init() {
557 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
558 vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
559 .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1 dec", "icon1"),
561 vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
562 .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
567 public void emptyListTest() {
568 Collection<EntitlementPoolEntity> entitlementPools =
569 vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
570 Assert.assertEquals(entitlementPools.size(), 0);
573 @Test(dependsOnMethods = "emptyListTest")
574 public void createTest() {
575 ep1Id = testCreate(vlm1Id, EP1_NAME);
577 Set<OperationalScope> opScopeChoices;
578 opScopeChoices = new HashSet<>();
579 opScopeChoices.add(OperationalScope.Core);
580 opScopeChoices.add(OperationalScope.CPU);
581 opScopeChoices.add(OperationalScope.Network_Wide);
582 EntitlementPoolEntity ep2 =
583 createEntitlementPool(vlm1Id, null, EP2_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
584 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
585 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
586 ep2Id = vendorLicenseManager.createEntitlementPool(ep2, USER1).getId();
590 private String testCreate(String vlmId, String name) {
591 Set<OperationalScope> opScopeChoices = new HashSet<>();
592 opScopeChoices.add(OperationalScope.Other);
593 EntitlementPoolEntity ep1 =
594 createEntitlementPool(vlmId, null, name, EP1_V01_DESC, 80, ThresholdUnit.Percentage,
595 EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
596 opScopeChoices, "op scope1", EntitlementTime.Other, "time1", "sku1");
597 String ep1Id = vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
600 EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(ep1);
601 Assert.assertTrue(loadedEp1.equals(ep1));
605 @Test(dependsOnMethods = {"createTest"})
606 public void testCreateWithExistingName_negative() {
607 testCreateWithExistingName_negative(vlm1Id, EP1_NAME);
610 @Test(dependsOnMethods = {"createTest"})
611 public void testCreateWithExistingNameUnderOtherVlm() {
612 testCreate(vlm2Id, EP1_NAME);
615 @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
616 public void updateAndGetTest() {
617 EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, VERSION01, ep1Id);
619 EntitlementPoolEntity ep1 = entitlementPoolDao.get(emptyEp1);
620 ep1.setEntitlementMetric(new ChoiceOrOther<>(EntitlementMetric.Other, "exception metric1 updated"));
621 ep1.setAggregationFunction(new ChoiceOrOther<>(AggregationFunction.Other, "agg func1 updated"));
623 vendorLicenseManager.updateEntitlementPool(ep1, USER1);
625 EntitlementPoolEntity loadedEp1 = vendorLicenseManager.getEntitlementPool(emptyEp1, USER1);
626 assertEntitlementPoolsEquals(loadedEp1, ep1);
629 @Test(dependsOnMethods = {"updateAndGetTest"})
630 public void testGetNonExistingVersion_negative() {
633 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(48, 83), ep1Id), USER1);
634 Assert.assertTrue(false);
635 } catch (CoreException exception) {
636 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
640 @Test(dependsOnMethods = {"updateAndGetTest"})
641 public void testGetOtherUserCandidateVersion_negative() {
642 vendorLicenseManager.checkin(vlm1Id, USER1);
643 vendorLicenseManager.checkout(vlm1Id, USER2);
646 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id), USER1);
647 Assert.assertTrue(false);
648 } catch (CoreException exception) {
649 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
653 @Test(dependsOnMethods = {"testGetOtherUserCandidateVersion_negative"})
654 public void testGetCandidateVersion() {
655 EntitlementPoolEntity ep = new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id);
656 ep.setDescription("updated!");
657 vendorLicenseManager.updateEntitlementPool(ep, USER2);
659 EntitlementPoolEntity actualEp = vendorLicenseManager.getEntitlementPool(ep, USER2);
660 EntitlementPoolEntity expectedEp = entitlementPoolDao.get(ep);
662 Assert.assertEquals(actualEp.getDescription(), ep.getDescription());
663 assertEntitlementPoolsEquals(actualEp, expectedEp);
666 @Test(dependsOnMethods = {"testGetCandidateVersion"})
667 public void testGetOldVersion() {
668 vendorLicenseManager.checkin(vlm1Id, USER2);
669 EntitlementPoolEntity actualEp = vendorLicenseManager
670 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 1), ep1Id), USER2);
671 Assert.assertEquals(actualEp.getDescription(), EP1_V01_DESC);
674 @Test(dependsOnMethods = {"testGetOldVersion"})
675 public void listTest() {
676 Collection<EntitlementPoolEntity> loadedEps =
677 vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
678 Assert.assertEquals(loadedEps.size(), 2);
680 int existingCounter = 0;
681 for (EntitlementPoolEntity loadedEp : loadedEps) {
682 if (ep2Id.equals(loadedEp.getId()) || ep1Id.equals(loadedEp.getId())) {
687 Assert.assertEquals(existingCounter, 2);
690 @Test(dependsOnMethods = {"listTest"})
691 public void deleteTest() {
692 vendorLicenseManager.checkout(vlm1Id, USER1);
693 EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, null, ep1Id);
694 vendorLicenseManager.deleteEntitlementPool(emptyEp1, USER1);
696 emptyEp1.setVersion(VERSION03);
697 EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(emptyEp1);
698 Assert.assertEquals(loadedEp1, null);
700 Collection<EntitlementPoolEntity> loadedEps =
701 entitlementPoolDao.list(new EntitlementPoolEntity(vlm1Id, VERSION03, null));
702 Assert.assertEquals(loadedEps.size(), 1);
703 Assert.assertEquals(loadedEps.iterator().next().getId(), ep2Id);
706 @Test(dependsOnMethods = "deleteTest")
707 public void listOldVersionTest() {
708 Collection<EntitlementPoolEntity> loadedEps =
709 vendorLicenseManager.listEntitlementPools(vlm1Id, VERSION01, USER1);
710 Assert.assertEquals(loadedEps.size(), 2);
713 @Test(dependsOnMethods = "deleteTest")
714 public void testCreateWithRemovedName() {
715 testCreate(vlm1Id, EP1_NAME);
718 @Test(dependsOnMethods = "deleteTest")
719 public void testCreateWithExistingNameAfterCheckout_negative() {
720 testCreateWithExistingName_negative(vlm1Id, EP2_NAME);
723 private void testCreateWithExistingName_negative(String vlmId, String epName) {
725 EntitlementPoolEntity ep1 =
726 createEntitlementPool(vlmId, null, epName, EP1_V01_DESC, 80, ThresholdUnit.Percentage,
727 EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
728 Collections.singleton(OperationalScope.Other), "op scope1", EntitlementTime.Other,
730 vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
732 } catch (CoreException exception) {
733 Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);