2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 package org.openecomp.sdc.vendorlicense.impl;
20 import org.junit.After;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
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 java.time.LocalDate;
36 import java.time.LocalDateTime;
37 import java.time.format.DateTimeFormatter;
38 import java.util.Arrays;
39 import java.util.Collection;
40 import java.util.HashSet;
43 import static org.mockito.Matchers.anyObject;
44 import static org.mockito.Mockito.*;
46 public class EntitlementPoolTest {
48 //JUnit Test Cases using Mockito
49 private final String USER1 = "epTestUser1";
50 private final String EP1_NAME = "EP1 name";
51 private final String EP2_NAME = "EP2 name";
52 private final String LT1_NAME = "LT1 name";
53 private static String vlm1_id = "vlm1_id";
54 private static String ep1_id = "ep1_id";
55 private static String ep2_id = "ep2_id";
56 public static final Version VERSION01 = new Version(0, 1);
59 private VendorLicenseFacade vendorLicenseFacade;
62 private EntitlementPoolDao entitlementPoolDao;
64 private LimitDao limitDao;
68 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
70 public EntitlementPoolEntity createEntitlementPool(String vlmId, Version version, String id,
71 String name, String desc, int threshold,
72 ThresholdUnit thresholdUnit,
73 EntitlementMetric entitlementMetricChoice,
74 String entitlementMetricOther,
76 AggregationFunction aggregationFunctionChoice,
77 String aggregationFunctionOther,
78 Set<OperationalScope> operationalScopeChoices,
79 String operationalScopeOther,
80 EntitlementTime timeChoice,
81 String timeOther, String sku) {
82 EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity();
83 entitlementPool.setVendorLicenseModelId(vlmId);
84 entitlementPool.setId(id);
85 entitlementPool.setVersion(version);
86 entitlementPool.setName(name);
87 entitlementPool.setDescription(desc);
88 entitlementPool.setThresholdValue(threshold);
89 entitlementPool.setThresholdUnit(thresholdUnit);
90 entitlementPool.setIncrements(increments);
91 entitlementPool.setOperationalScope(
92 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
93 return entitlementPool;
97 public void setUp() throws Exception {
98 MockitoAnnotations.initMocks(this);
103 public void tearDown(){
104 vendorLicenseManagerImpl = null;
108 public void createTest() {
109 Set<OperationalScope> opScopeChoices;
110 opScopeChoices = new HashSet<>();
111 opScopeChoices.add(OperationalScope.Core);
112 opScopeChoices.add(OperationalScope.CPU);
113 opScopeChoices.add(OperationalScope.Network_Wide);
114 EntitlementPoolEntity ep2 =
115 createEntitlementPool("vlm1Id", null, ep1_id, EP1_NAME, "EP2 dec", 70, ThresholdUnit
117 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
118 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
119 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
120 ep2.setStartDate(LocalDate.now().format(formatter));
121 ep2.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
123 vendorLicenseManagerImpl.createEntitlementPool(ep2);
124 verify(vendorLicenseFacade).createEntitlementPool(ep2);
129 @Test(expected = CoreException.class)
130 public void createWithInvalidStartExpiryDateTest() {
132 Set<OperationalScope> opScopeChoices;
133 opScopeChoices = new HashSet<>();
134 opScopeChoices.add(OperationalScope.Core);
135 opScopeChoices.add(OperationalScope.CPU);
136 opScopeChoices.add(OperationalScope.Network_Wide);
137 EntitlementPoolEntity ep2 =
138 createEntitlementPool("vlm2Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
139 ThresholdUnit.Absolute,
140 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
141 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
142 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
143 ep2.setStartDate(LocalDate.now().format(formatter));
144 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
145 ep2.setVendorLicenseModelId(vlm1_id);
146 vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
147 Assert.fail("Vendor license model with id vlm1_id has invalid date range.");
151 @Test(expected = CoreException.class)
152 public void createWithoutStartDateTest() {
154 Set<OperationalScope> opScopeChoices;
155 opScopeChoices = new HashSet<>();
156 opScopeChoices.add(OperationalScope.Core);
157 opScopeChoices.add(OperationalScope.CPU);
158 opScopeChoices.add(OperationalScope.Network_Wide);
159 EntitlementPoolEntity ep2 =
160 createEntitlementPool("vlm3Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
161 ThresholdUnit.Absolute,
162 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
163 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
164 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
165 ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
166 ep2.setVendorLicenseModelId(vlm1_id);
167 vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
168 Assert.fail("Vendor license model with id vlm1_id has invalid date range.");
172 @Test(expected = CoreException.class)
173 public void createWithSameStartExpiryDateTest() {
175 Set<OperationalScope> opScopeChoices;
176 opScopeChoices = new HashSet<>();
177 opScopeChoices.add(OperationalScope.Core);
178 opScopeChoices.add(OperationalScope.CPU);
179 opScopeChoices.add(OperationalScope.Network_Wide);
180 EntitlementPoolEntity ep2 =
181 createEntitlementPool("vlm4Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
182 ThresholdUnit.Absolute,
183 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
184 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
185 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
186 ep2.setStartDate(LocalDate.now().format(formatter));
187 ep2.setExpiryDate(LocalDate.now().format(formatter));
188 ep2.setVendorLicenseModelId(vlm1_id);
189 vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
190 Assert.fail("Vendor license model with id vlm1_id has invalid date range.");
194 public void testUpdate() {
195 Set<OperationalScope> opScopeChoices;
196 opScopeChoices = new HashSet<>();
197 opScopeChoices.add(OperationalScope.Core);
198 opScopeChoices.add(OperationalScope.CPU);
199 opScopeChoices.add(OperationalScope.Network_Wide);
200 EntitlementPoolEntity ep2 =
201 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
204 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
205 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
206 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
207 ep2.setStartDate(LocalDate.now().minusDays(3L).format(formatter));
208 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
210 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
213 @Test(expected = CoreException.class)
214 public void updateWithInvalidStartExpiryDateTest() {
216 Set<OperationalScope> opScopeChoices;
217 opScopeChoices = new HashSet<>();
218 opScopeChoices.add(OperationalScope.Core);
219 opScopeChoices.add(OperationalScope.CPU);
220 opScopeChoices.add(OperationalScope.Network_Wide);
221 EntitlementPoolEntity ep2 =
222 createEntitlementPool("vlm2Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
223 ThresholdUnit.Absolute,
224 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
225 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
226 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
227 ep2.setStartDate(LocalDate.now().format(formatter));
228 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
229 ep2.setVendorLicenseModelId(vlm1_id);
230 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
231 Assert.fail("Vendor license model with id vlm1_id has invalid date range.");
236 public void updateWithoutStartDateTest() {
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 EntitlementPoolEntity ep2 =
245 createEntitlementPool("vlm3Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
246 ThresholdUnit.Absolute,
247 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average,
249 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
250 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
251 ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
252 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
254 } catch (CoreException exception) {
255 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
260 public void updateWithSameStartExpiryDateTest() {
263 Set<OperationalScope> opScopeChoices;
264 opScopeChoices = new HashSet<>();
265 opScopeChoices.add(OperationalScope.Core);
266 opScopeChoices.add(OperationalScope.CPU);
267 opScopeChoices.add(OperationalScope.Network_Wide);
268 EntitlementPoolEntity ep2 =
269 createEntitlementPool("vlm4Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
270 ThresholdUnit.Absolute,
271 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average,
273 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
274 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
275 ep2.setStartDate(LocalDate.now().format(formatter));
276 ep2.setExpiryDate(LocalDate.now().format(formatter));
277 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
279 } catch (CoreException exception) {
280 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
285 public void deleteEntitlementPoolTest() {
286 Set<OperationalScope> opScopeChoices;
287 opScopeChoices = new HashSet<>();
288 opScopeChoices.add(OperationalScope.Core);
289 opScopeChoices.add(OperationalScope.CPU);
290 opScopeChoices.add(OperationalScope.Network_Wide);
292 EntitlementPoolEntity entitlementPool =
293 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
294 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2", "inc2",
295 AggregationFunction.Average, null,
296 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
297 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
298 entitlementPool.setStartDate(LocalDate.now().format(formatter));
299 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
301 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
303 doNothing().when(vendorLicenseManagerImpl).deleteChildLimits(vlm1_id, VERSION01, ep1_id);
305 doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(anyObject(), anyObject(),
306 anyObject(), anyObject());
308 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool);
310 verify(entitlementPoolDao).delete(entitlementPool);
314 public void testGetEntitlementPool() {
315 Set<OperationalScope> opScopeChoices;
316 opScopeChoices = new HashSet<>();
317 opScopeChoices.add(OperationalScope.Core);
318 opScopeChoices.add(OperationalScope.CPU);
319 opScopeChoices.add(OperationalScope.Network_Wide);
321 EntitlementPoolEntity entitlementPool =
322 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
323 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2", "inc2",
324 AggregationFunction.Average, null,
325 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
326 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'");
328 entitlementPool.setStartDate(LocalDateTime.now().format(formatter));
329 entitlementPool.setExpiryDate(LocalDateTime.now().plusDays(1L).format(formatter));
331 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
333 EntitlementPoolEntity retrived = vendorLicenseManagerImpl.getEntitlementPool(entitlementPool);
335 Assert.assertEquals(retrived.getId(), entitlementPool.getId());
336 Assert.assertEquals(retrived.getVendorLicenseModelId(),
337 entitlementPool.getVendorLicenseModelId());
338 Assert.assertEquals(retrived.getVersion(), entitlementPool.getVersion());
342 public void testListEntitlmentPool() {
344 Set<OperationalScope> opScopeChoices;
345 opScopeChoices = new HashSet<>();
346 opScopeChoices.add(OperationalScope.Core);
347 opScopeChoices.add(OperationalScope.CPU);
348 opScopeChoices.add(OperationalScope.Network_Wide);
350 doReturn(Arrays.asList(
351 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP1 dec", 70,
352 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric1",
353 "inc1", AggregationFunction.Average, null,
354 opScopeChoices, null, EntitlementTime.Other, "time1", "sku1"),
355 createEntitlementPool(vlm1_id, VERSION01, ep2_id, EP2_NAME, "EP2 dec", 70,
356 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2",
357 "inc2", AggregationFunction.Average, null,
358 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2")))
359 .when(vendorLicenseFacade).listEntitlementPools(vlm1_id, VERSION01);
361 Collection<EntitlementPoolEntity> EPs =
362 vendorLicenseManagerImpl.listEntitlementPools(vlm1_id, VERSION01);
364 verify(vendorLicenseFacade).listEntitlementPools(vlm1_id, VERSION01);
365 Assert.assertEquals(EPs.size(), 2);
366 EPs.forEach(ep -> Assert.assertTrue(ep.getId().matches(ep1_id + "|" + ep2_id)));
371 public void deleteEntitlementPoolTest() {
372 Set<OperationalScope> opScopeChoices;
373 opScopeChoices = new HashSet<>();
374 opScopeChoices.add(OperationalScope.Core);
375 opScopeChoices.add(OperationalScope.CPU);
376 opScopeChoices.add(OperationalScope.Network_Wide);
378 EntitlementPoolEntity entitlementPool =
379 createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
380 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
381 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
382 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
383 entitlementPool.setStartDate(LocalDate.now().format(formatter));
384 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
386 VersionInfo info = new VersionInfo();
387 Version version = new Version();
388 info.getViewableVersions().add(version);
389 info.setActiveVersion(version);
390 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
392 LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
393 "Core",AggregationFunction.Average,10,"Hour");
395 ArrayList<LimitEntity> limitEntityList = new ArrayList();
396 limitEntityList.add(limitEntity);
398 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
399 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
400 doReturn(true).when(limitDao).isLimitPresent(anyObject());
401 doReturn(limitEntity).when(limitDao).get(anyObject());
403 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
404 limitField.setAccessible(true);
405 Field modifiersField = Field.class.getDeclaredField("modifiers");
406 modifiersField.setAccessible(true);
407 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
408 limitField.set(null, limitDao);
410 Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao");
411 epField.setAccessible(true);
412 modifiersField = Field.class.getDeclaredField("modifiers");
413 modifiersField.setAccessible(true);
414 modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
415 epField.set(null, entitlementPoolDao);
416 } catch(NoSuchFieldException | IllegalAccessException e)
421 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
423 verify(limitDao).delete(anyObject());
427 public void deleteEntitlementPoolInvalidTest() {
429 Set<OperationalScope> opScopeChoices;
430 opScopeChoices = new HashSet<>();
431 opScopeChoices.add(OperationalScope.Core);
432 opScopeChoices.add(OperationalScope.CPU);
433 opScopeChoices.add(OperationalScope.Network_Wide);
435 EntitlementPoolEntity entitlementPool =
436 createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
437 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
438 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
439 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
440 entitlementPool.setStartDate(LocalDate.now().format(formatter));
441 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
443 VersionInfo info = new VersionInfo();
444 Version version = new Version();
445 info.getViewableVersions().add(version);
446 info.setActiveVersion(version);
447 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
449 LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
450 "Core",AggregationFunction.Average,10,"Hour");
452 ArrayList<LimitEntity> limitEntityList = new ArrayList();
453 limitEntityList.add(limitEntity);
455 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
456 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
457 doReturn(false).when(limitDao).isLimitPresent(anyObject());
460 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
461 limitField.setAccessible(true);
462 Field modifiersField = Field.class.getDeclaredField("modifiers");
463 modifiersField.setAccessible(true);
464 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
465 limitField.set(null, limitDao);
467 Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao");
468 epField.setAccessible(true);
469 modifiersField = Field.class.getDeclaredField("modifiers");
470 modifiersField.setAccessible(true);
471 modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
472 epField.set(null, entitlementPoolDao);
473 } catch(NoSuchFieldException | IllegalAccessException e)
478 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
479 } catch (CoreException exception) {
480 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
484 /* private static final String USER1 = "epTestUser1";
485 private static final String USER2 = "epTestUser2";
486 private static final String EP1_V01_DESC = "EP1 desc";
487 private static final Version VERSION01 = new Version(0, 1);
488 private static final Version VERSION03 = new Version(0, 3);
489 private static final String EP1_NAME = "EP1 name";
490 private static final String EP2_NAME = "EP2 name";
492 private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
493 private static EntitlementPoolDao entitlementPoolDao;
495 private static String vlm1Id;
496 private static String vlm2Id;
497 private static String ep1Id;
498 private static String ep2Id;
500 public static EntitlementPoolEntity createEntitlementPool(String vlmId, Version version,
501 String name, String desc, int threshold,
502 ThresholdUnit thresholdUnit,
503 EntitlementMetric entitlementMetricChoice,
504 String entitlementMetricOther,
506 AggregationFunction aggregationFunctionChoice,
507 String aggregationFunctionOther,
508 Set<OperationalScope> operationalScopeChoices,
509 String operationalScopeOther,
510 EntitlementTime timeChoice,
511 String timeOther, String sku) {
512 EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity();
513 entitlementPool.setVendorLicenseModelId(vlmId);
514 entitlementPool.setVersion(version);
515 entitlementPool.setName(name);
516 entitlementPool.setDescription(desc);
517 entitlementPool.setThresholdValue(threshold);
518 entitlementPool.setThresholdUnit(thresholdUnit);
520 .setEntitlementMetric(new ChoiceOrOther<>(entitlementMetricChoice, entitlementMetricOther));
521 entitlementPool.setIncrements(increments);
522 entitlementPool.setAggregationFunction(
523 new ChoiceOrOther<>(aggregationFunctionChoice, aggregationFunctionOther));
524 entitlementPool.setOperationalScope(
525 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
526 entitlementPool.setTime(new ChoiceOrOther<>(timeChoice, timeOther));
527 return entitlementPool;
530 private static void assertEntitlementPoolsEquals(EntitlementPoolEntity actual,
531 EntitlementPoolEntity expected) {
532 Assert.assertEquals(actual.getVendorLicenseModelId(), expected.getVendorLicenseModelId());
533 Assert.assertEquals(actual.getVersion(), expected.getVersion());
534 Assert.assertEquals(actual.getId(), expected.getId());
535 Assert.assertEquals(actual.getName(), expected.getName());
536 Assert.assertEquals(actual.getDescription(), expected.getDescription());
537 Assert.assertEquals(actual.getThresholdValue(), expected.getThresholdValue());
538 Assert.assertEquals(actual.getThresholdUnit(), expected.getThresholdUnit());
539 Assert.assertEquals(actual.getEntitlementMetric(), expected.getEntitlementMetric());
540 Assert.assertEquals(actual.getIncrements(), expected.getIncrements());
541 Assert.assertEquals(actual.getAggregationFunction(), expected.getAggregationFunction());
542 Assert.assertEquals(actual.getOperationalScope(), expected.getOperationalScope());
543 Assert.assertEquals(actual.getTime(), expected.getTime());
547 private void init() {
548 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
549 vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
550 .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1 dec", "icon1"),
552 vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
553 .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
558 public void emptyListTest() {
559 Collection<EntitlementPoolEntity> entitlementPools =
560 vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
561 Assert.assertEquals(entitlementPools.size(), 0);
564 @Test(dependsOnMethods = "emptyListTest")
565 public void createTest() {
566 ep1Id = testCreate(vlm1Id, EP1_NAME);
568 Set<OperationalScope> opScopeChoices;
569 opScopeChoices = new HashSet<>();
570 opScopeChoices.add(OperationalScope.Core);
571 opScopeChoices.add(OperationalScope.CPU);
572 opScopeChoices.add(OperationalScope.Network_Wide);
573 EntitlementPoolEntity ep2 =
574 createEntitlementPool(vlm1Id, null, EP2_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
575 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
576 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
577 ep2Id = vendorLicenseManager.createEntitlementPool(ep2, USER1).getId();
581 private String testCreate(String vlmId, String name) {
582 Set<OperationalScope> opScopeChoices = new HashSet<>();
583 opScopeChoices.add(OperationalScope.Other);
584 EntitlementPoolEntity ep1 =
585 createEntitlementPool(vlmId, null, name, EP1_V01_DESC, 80, ThresholdUnit.Percentage,
586 EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
587 opScopeChoices, "op scope1", EntitlementTime.Other, "time1", "sku1");
588 String ep1Id = vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
591 EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(ep1);
592 Assert.assertTrue(loadedEp1.equals(ep1));
596 @Test(dependsOnMethods = {"createTest"})
597 public void testCreateWithExistingName_negative() {
598 testCreateWithExistingName_negative(vlm1Id, EP1_NAME);
601 @Test(dependsOnMethods = {"createTest"})
602 public void testCreateWithExistingNameUnderOtherVlm() {
603 testCreate(vlm2Id, EP1_NAME);
606 @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
607 public void updateAndGetTest() {
608 EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, VERSION01, ep1Id);
610 EntitlementPoolEntity ep1 = entitlementPoolDao.get(emptyEp1);
611 ep1.setEntitlementMetric(new ChoiceOrOther<>(EntitlementMetric.Other, "exception metric1 updated"));
612 ep1.setAggregationFunction(new ChoiceOrOther<>(AggregationFunction.Other, "agg func1 updated"));
614 vendorLicenseManager.updateEntitlementPool(ep1, USER1);
616 EntitlementPoolEntity loadedEp1 = vendorLicenseManager.getEntitlementPool(emptyEp1, USER1);
617 assertEntitlementPoolsEquals(loadedEp1, ep1);
620 @Test(dependsOnMethods = {"updateAndGetTest"})
621 public void testGetNonExistingVersion_negative() {
624 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(48, 83), ep1Id), USER1);
625 Assert.assertTrue(false);
626 } catch (CoreException exception) {
627 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
631 @Test(dependsOnMethods = {"updateAndGetTest"})
632 public void testGetOtherUserCandidateVersion_negative() {
633 vendorLicenseManager.checkin(vlm1Id, USER1);
634 vendorLicenseManager.checkout(vlm1Id, USER2);
637 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id), USER1);
638 Assert.assertTrue(false);
639 } catch (CoreException exception) {
640 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
644 @Test(dependsOnMethods = {"testGetOtherUserCandidateVersion_negative"})
645 public void testGetCandidateVersion() {
646 EntitlementPoolEntity ep = new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id);
647 ep.setDescription("updated!");
648 vendorLicenseManager.updateEntitlementPool(ep, USER2);
650 EntitlementPoolEntity actualEp = vendorLicenseManager.getEntitlementPool(ep, USER2);
651 EntitlementPoolEntity expectedEp = entitlementPoolDao.get(ep);
653 Assert.assertEquals(actualEp.getDescription(), ep.getDescription());
654 assertEntitlementPoolsEquals(actualEp, expectedEp);
657 @Test(dependsOnMethods = {"testGetCandidateVersion"})
658 public void testGetOldVersion() {
659 vendorLicenseManager.checkin(vlm1Id, USER2);
660 EntitlementPoolEntity actualEp = vendorLicenseManager
661 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 1), ep1Id), USER2);
662 Assert.assertEquals(actualEp.getDescription(), EP1_V01_DESC);
665 @Test(dependsOnMethods = {"testGetOldVersion"})
666 public void listTest() {
667 Collection<EntitlementPoolEntity> loadedEps =
668 vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
669 Assert.assertEquals(loadedEps.size(), 2);
671 int existingCounter = 0;
672 for (EntitlementPoolEntity loadedEp : loadedEps) {
673 if (ep2Id.equals(loadedEp.getId()) || ep1Id.equals(loadedEp.getId())) {
678 Assert.assertEquals(existingCounter, 2);
681 @Test(dependsOnMethods = {"listTest"})
682 public void deleteTest() {
683 vendorLicenseManager.checkout(vlm1Id, USER1);
684 EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, null, ep1Id);
685 vendorLicenseManager.deleteEntitlementPool(emptyEp1, USER1);
687 emptyEp1.setVersion(VERSION03);
688 EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(emptyEp1);
689 Assert.assertEquals(loadedEp1, null);
691 Collection<EntitlementPoolEntity> loadedEps =
692 entitlementPoolDao.list(new EntitlementPoolEntity(vlm1Id, VERSION03, null));
693 Assert.assertEquals(loadedEps.size(), 1);
694 Assert.assertEquals(loadedEps.iterator().next().getId(), ep2Id);
697 @Test(dependsOnMethods = "deleteTest")
698 public void listOldVersionTest() {
699 Collection<EntitlementPoolEntity> loadedEps =
700 vendorLicenseManager.listEntitlementPools(vlm1Id, VERSION01, USER1);
701 Assert.assertEquals(loadedEps.size(), 2);
704 @Test(dependsOnMethods = "deleteTest")
705 public void testCreateWithRemovedName() {
706 testCreate(vlm1Id, EP1_NAME);
709 @Test(dependsOnMethods = "deleteTest")
710 public void testCreateWithExistingNameAfterCheckout_negative() {
711 testCreateWithExistingName_negative(vlm1Id, EP2_NAME);
714 private void testCreateWithExistingName_negative(String vlmId, String epName) {
716 EntitlementPoolEntity ep1 =
717 createEntitlementPool(vlmId, null, epName, EP1_V01_DESC, 80, ThresholdUnit.Percentage,
718 EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
719 Collections.singleton(OperationalScope.Other), "op scope1", EntitlementTime.Other,
721 vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
723 } catch (CoreException exception) {
724 Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);