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.mockito.InjectMocks;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.mockito.Spy;
24 import org.openecomp.sdc.common.errors.CoreException;
25 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
26 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
27 import org.openecomp.sdc.vendorlicense.dao.types.*;
28 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes;
29 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
30 import org.openecomp.sdc.versioning.dao.types.Version;
31 import org.testng.Assert;
32 import org.testng.annotations.AfterMethod;
33 import org.testng.annotations.BeforeMethod;
34 import org.testng.annotations.Test;
36 import java.time.LocalDate;
37 import java.time.LocalDateTime;
38 import java.time.format.DateTimeFormatter;
39 import java.util.Arrays;
40 import java.util.Collection;
41 import java.util.HashSet;
44 import static org.mockito.Matchers.anyObject;
45 import static org.mockito.Mockito.*;
47 public class EntitlementPoolTest {
49 //JUnit Test Cases using Mockito
50 private final String USER1 = "epTestUser1";
51 private final String EP1_NAME = "EP1 name";
52 private final String EP2_NAME = "EP2 name";
53 private final String LT1_NAME = "LT1 name";
54 private static String vlm1_id = "vlm1_id";
55 private static String ep1_id = "ep1_id";
56 private static String ep2_id = "ep2_id";
57 public static final Version VERSION01 = new Version(0, 1);
60 private VendorLicenseFacade vendorLicenseFacade;
63 private EntitlementPoolDao entitlementPoolDao;
65 private LimitDao limitDao;
69 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
71 public EntitlementPoolEntity createEntitlementPool(String vlmId, Version version, String id,
72 String name, String desc, int threshold,
73 ThresholdUnit thresholdUnit,
74 EntitlementMetric entitlementMetricChoice,
75 String entitlementMetricOther,
77 AggregationFunction aggregationFunctionChoice,
78 String aggregationFunctionOther,
79 Set<OperationalScope> operationalScopeChoices,
80 String operationalScopeOther,
81 EntitlementTime timeChoice,
82 String timeOther, String sku) {
83 EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity();
84 entitlementPool.setVendorLicenseModelId(vlmId);
85 entitlementPool.setId(id);
86 entitlementPool.setVersion(version);
87 entitlementPool.setName(name);
88 entitlementPool.setDescription(desc);
89 entitlementPool.setThresholdValue(threshold);
90 entitlementPool.setThresholdUnit(thresholdUnit);
91 entitlementPool.setIncrements(increments);
92 entitlementPool.setOperationalScope(
93 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
94 return entitlementPool;
98 public void setUp() throws Exception {
99 MockitoAnnotations.initMocks(this);
104 public void tearDown(){
105 vendorLicenseManagerImpl = null;
109 public void createTest() {
110 Set<OperationalScope> opScopeChoices;
111 opScopeChoices = new HashSet<>();
112 opScopeChoices.add(OperationalScope.Core);
113 opScopeChoices.add(OperationalScope.CPU);
114 opScopeChoices.add(OperationalScope.Network_Wide);
115 EntitlementPoolEntity ep2 =
116 createEntitlementPool("vlm1Id", null, ep1_id, EP1_NAME, "EP2 dec", 70, ThresholdUnit
118 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
119 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
120 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
121 ep2.setStartDate(LocalDate.now().format(formatter));
122 ep2.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
124 vendorLicenseManagerImpl.createEntitlementPool(ep2);
125 verify(vendorLicenseFacade).createEntitlementPool(ep2);
130 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
131 "license model with id vlm1_id has invalid date range.")
132 public void createWithInvalidStartExpiryDateTest() {
134 Set<OperationalScope> opScopeChoices;
135 opScopeChoices = new HashSet<>();
136 opScopeChoices.add(OperationalScope.Core);
137 opScopeChoices.add(OperationalScope.CPU);
138 opScopeChoices.add(OperationalScope.Network_Wide);
139 EntitlementPoolEntity ep2 =
140 createEntitlementPool("vlm2Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
141 ThresholdUnit.Absolute,
142 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
143 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
144 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
145 ep2.setStartDate(LocalDate.now().format(formatter));
146 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
147 ep2.setVendorLicenseModelId(vlm1_id);
148 vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
153 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
154 "license model with id vlm1_id has invalid date range.")
155 public void createWithoutStartDateTest() {
157 Set<OperationalScope> opScopeChoices;
158 opScopeChoices = new HashSet<>();
159 opScopeChoices.add(OperationalScope.Core);
160 opScopeChoices.add(OperationalScope.CPU);
161 opScopeChoices.add(OperationalScope.Network_Wide);
162 EntitlementPoolEntity ep2 =
163 createEntitlementPool("vlm3Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
164 ThresholdUnit.Absolute,
165 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
166 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
167 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
168 ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
169 ep2.setVendorLicenseModelId(vlm1_id);
170 vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
175 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
176 "license model with id vlm1_id has invalid date range.")
177 public void createWithSameStartExpiryDateTest() {
179 Set<OperationalScope> opScopeChoices;
180 opScopeChoices = new HashSet<>();
181 opScopeChoices.add(OperationalScope.Core);
182 opScopeChoices.add(OperationalScope.CPU);
183 opScopeChoices.add(OperationalScope.Network_Wide);
184 EntitlementPoolEntity ep2 =
185 createEntitlementPool("vlm4Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
186 ThresholdUnit.Absolute,
187 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
188 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
189 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
190 ep2.setStartDate(LocalDate.now().format(formatter));
191 ep2.setExpiryDate(LocalDate.now().format(formatter));
192 ep2.setVendorLicenseModelId(vlm1_id);
193 vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
198 public void testUpdate() {
199 Set<OperationalScope> opScopeChoices;
200 opScopeChoices = new HashSet<>();
201 opScopeChoices.add(OperationalScope.Core);
202 opScopeChoices.add(OperationalScope.CPU);
203 opScopeChoices.add(OperationalScope.Network_Wide);
204 EntitlementPoolEntity ep2 =
205 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
208 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
209 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
210 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
211 ep2.setStartDate(LocalDate.now().minusDays(3L).format(formatter));
212 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
214 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
217 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
218 "license model with id vlm1_id has invalid date range.")
219 public void updateWithInvalidStartExpiryDateTest() {
221 Set<OperationalScope> opScopeChoices;
222 opScopeChoices = new HashSet<>();
223 opScopeChoices.add(OperationalScope.Core);
224 opScopeChoices.add(OperationalScope.CPU);
225 opScopeChoices.add(OperationalScope.Network_Wide);
226 EntitlementPoolEntity ep2 =
227 createEntitlementPool("vlm2Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
228 ThresholdUnit.Absolute,
229 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
230 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
231 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
232 ep2.setStartDate(LocalDate.now().format(formatter));
233 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
234 ep2.setVendorLicenseModelId(vlm1_id);
235 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
241 public void updateWithoutStartDateTest() {
244 Set<OperationalScope> opScopeChoices;
245 opScopeChoices = new HashSet<>();
246 opScopeChoices.add(OperationalScope.Core);
247 opScopeChoices.add(OperationalScope.CPU);
248 opScopeChoices.add(OperationalScope.Network_Wide);
249 EntitlementPoolEntity ep2 =
250 createEntitlementPool("vlm3Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
251 ThresholdUnit.Absolute,
252 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average,
254 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
255 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
256 ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
257 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
259 } catch (CoreException exception) {
260 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
265 public void updateWithSameStartExpiryDateTest() {
268 Set<OperationalScope> opScopeChoices;
269 opScopeChoices = new HashSet<>();
270 opScopeChoices.add(OperationalScope.Core);
271 opScopeChoices.add(OperationalScope.CPU);
272 opScopeChoices.add(OperationalScope.Network_Wide);
273 EntitlementPoolEntity ep2 =
274 createEntitlementPool("vlm4Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
275 ThresholdUnit.Absolute,
276 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average,
278 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
279 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
280 ep2.setStartDate(LocalDate.now().format(formatter));
281 ep2.setExpiryDate(LocalDate.now().format(formatter));
282 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
284 } catch (CoreException exception) {
285 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
290 public void deleteEntitlementPoolTest() {
291 Set<OperationalScope> opScopeChoices;
292 opScopeChoices = new HashSet<>();
293 opScopeChoices.add(OperationalScope.Core);
294 opScopeChoices.add(OperationalScope.CPU);
295 opScopeChoices.add(OperationalScope.Network_Wide);
297 EntitlementPoolEntity entitlementPool =
298 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
299 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2", "inc2",
300 AggregationFunction.Average, null,
301 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
302 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
303 entitlementPool.setStartDate(LocalDate.now().format(formatter));
304 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
306 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
308 doNothing().when(vendorLicenseManagerImpl).deleteChildLimits(vlm1_id, VERSION01, ep1_id);
310 doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(anyObject(), anyObject(),
311 anyObject(), anyObject());
313 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool);
315 verify(entitlementPoolDao).delete(entitlementPool);
319 public void testGetEntitlementPool() {
320 Set<OperationalScope> opScopeChoices;
321 opScopeChoices = new HashSet<>();
322 opScopeChoices.add(OperationalScope.Core);
323 opScopeChoices.add(OperationalScope.CPU);
324 opScopeChoices.add(OperationalScope.Network_Wide);
326 EntitlementPoolEntity entitlementPool =
327 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
328 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2", "inc2",
329 AggregationFunction.Average, null,
330 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
331 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'");
333 entitlementPool.setStartDate(LocalDateTime.now().format(formatter));
334 entitlementPool.setExpiryDate(LocalDateTime.now().plusDays(1L).format(formatter));
336 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
338 EntitlementPoolEntity retrived = vendorLicenseManagerImpl.getEntitlementPool(entitlementPool);
340 Assert.assertEquals(retrived.getId(), entitlementPool.getId());
341 Assert.assertEquals(retrived.getVendorLicenseModelId(),
342 entitlementPool.getVendorLicenseModelId());
343 Assert.assertEquals(retrived.getVersion(), entitlementPool.getVersion());
347 public void testListEntitlmentPool() {
349 Set<OperationalScope> opScopeChoices;
350 opScopeChoices = new HashSet<>();
351 opScopeChoices.add(OperationalScope.Core);
352 opScopeChoices.add(OperationalScope.CPU);
353 opScopeChoices.add(OperationalScope.Network_Wide);
355 doReturn(Arrays.asList(
356 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP1 dec", 70,
357 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric1",
358 "inc1", AggregationFunction.Average, null,
359 opScopeChoices, null, EntitlementTime.Other, "time1", "sku1"),
360 createEntitlementPool(vlm1_id, VERSION01, ep2_id, EP2_NAME, "EP2 dec", 70,
361 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2",
362 "inc2", AggregationFunction.Average, null,
363 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2")))
364 .when(vendorLicenseFacade).listEntitlementPools(vlm1_id, VERSION01);
366 Collection<EntitlementPoolEntity> EPs =
367 vendorLicenseManagerImpl.listEntitlementPools(vlm1_id, VERSION01);
369 verify(vendorLicenseFacade).listEntitlementPools(vlm1_id, VERSION01);
370 Assert.assertEquals(EPs.size(), 2);
371 EPs.forEach(ep -> Assert.assertTrue(ep.getId().matches(ep1_id + "|" + ep2_id)));
376 public void deleteEntitlementPoolTest() {
377 Set<OperationalScope> opScopeChoices;
378 opScopeChoices = new HashSet<>();
379 opScopeChoices.add(OperationalScope.Core);
380 opScopeChoices.add(OperationalScope.CPU);
381 opScopeChoices.add(OperationalScope.Network_Wide);
383 EntitlementPoolEntity entitlementPool =
384 createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
385 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
386 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
387 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
388 entitlementPool.setStartDate(LocalDate.now().format(formatter));
389 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
391 VersionInfo info = new VersionInfo();
392 Version version = new Version();
393 info.getViewableVersions().add(version);
394 info.setActiveVersion(version);
395 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
397 LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
398 "Core",AggregationFunction.Average,10,"Hour");
400 ArrayList<LimitEntity> limitEntityList = new ArrayList();
401 limitEntityList.add(limitEntity);
403 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
404 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
405 doReturn(true).when(limitDao).isLimitPresent(anyObject());
406 doReturn(limitEntity).when(limitDao).get(anyObject());
408 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
409 limitField.setAccessible(true);
410 Field modifiersField = Field.class.getDeclaredField("modifiers");
411 modifiersField.setAccessible(true);
412 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
413 limitField.set(null, limitDao);
415 Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao");
416 epField.setAccessible(true);
417 modifiersField = Field.class.getDeclaredField("modifiers");
418 modifiersField.setAccessible(true);
419 modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
420 epField.set(null, entitlementPoolDao);
421 } catch(NoSuchFieldException | IllegalAccessException e)
426 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
428 verify(limitDao).delete(anyObject());
432 public void deleteEntitlementPoolInvalidTest() {
434 Set<OperationalScope> opScopeChoices;
435 opScopeChoices = new HashSet<>();
436 opScopeChoices.add(OperationalScope.Core);
437 opScopeChoices.add(OperationalScope.CPU);
438 opScopeChoices.add(OperationalScope.Network_Wide);
440 EntitlementPoolEntity entitlementPool =
441 createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
442 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
443 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
444 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
445 entitlementPool.setStartDate(LocalDate.now().format(formatter));
446 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
448 VersionInfo info = new VersionInfo();
449 Version version = new Version();
450 info.getViewableVersions().add(version);
451 info.setActiveVersion(version);
452 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
454 LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
455 "Core",AggregationFunction.Average,10,"Hour");
457 ArrayList<LimitEntity> limitEntityList = new ArrayList();
458 limitEntityList.add(limitEntity);
460 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
461 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
462 doReturn(false).when(limitDao).isLimitPresent(anyObject());
465 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
466 limitField.setAccessible(true);
467 Field modifiersField = Field.class.getDeclaredField("modifiers");
468 modifiersField.setAccessible(true);
469 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
470 limitField.set(null, limitDao);
472 Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao");
473 epField.setAccessible(true);
474 modifiersField = Field.class.getDeclaredField("modifiers");
475 modifiersField.setAccessible(true);
476 modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
477 epField.set(null, entitlementPoolDao);
478 } catch(NoSuchFieldException | IllegalAccessException e)
483 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
484 } catch (CoreException exception) {
485 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
489 /* private static final String USER1 = "epTestUser1";
490 private static final String USER2 = "epTestUser2";
491 private static final String EP1_V01_DESC = "EP1 desc";
492 private static final Version VERSION01 = new Version(0, 1);
493 private static final Version VERSION03 = new Version(0, 3);
494 private static final String EP1_NAME = "EP1 name";
495 private static final String EP2_NAME = "EP2 name";
497 private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
498 private static EntitlementPoolDao entitlementPoolDao;
500 private static String vlm1Id;
501 private static String vlm2Id;
502 private static String ep1Id;
503 private static String ep2Id;
505 public static EntitlementPoolEntity createEntitlementPool(String vlmId, Version version,
506 String name, String desc, int threshold,
507 ThresholdUnit thresholdUnit,
508 EntitlementMetric entitlementMetricChoice,
509 String entitlementMetricOther,
511 AggregationFunction aggregationFunctionChoice,
512 String aggregationFunctionOther,
513 Set<OperationalScope> operationalScopeChoices,
514 String operationalScopeOther,
515 EntitlementTime timeChoice,
516 String timeOther, String sku) {
517 EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity();
518 entitlementPool.setVendorLicenseModelId(vlmId);
519 entitlementPool.setVersion(version);
520 entitlementPool.setName(name);
521 entitlementPool.setDescription(desc);
522 entitlementPool.setThresholdValue(threshold);
523 entitlementPool.setThresholdUnit(thresholdUnit);
525 .setEntitlementMetric(new ChoiceOrOther<>(entitlementMetricChoice, entitlementMetricOther));
526 entitlementPool.setIncrements(increments);
527 entitlementPool.setAggregationFunction(
528 new ChoiceOrOther<>(aggregationFunctionChoice, aggregationFunctionOther));
529 entitlementPool.setOperationalScope(
530 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
531 entitlementPool.setTime(new ChoiceOrOther<>(timeChoice, timeOther));
532 return entitlementPool;
535 private static void assertEntitlementPoolsEquals(EntitlementPoolEntity actual,
536 EntitlementPoolEntity expected) {
537 Assert.assertEquals(actual.getVendorLicenseModelId(), expected.getVendorLicenseModelId());
538 Assert.assertEquals(actual.getVersion(), expected.getVersion());
539 Assert.assertEquals(actual.getId(), expected.getId());
540 Assert.assertEquals(actual.getName(), expected.getName());
541 Assert.assertEquals(actual.getDescription(), expected.getDescription());
542 Assert.assertEquals(actual.getThresholdValue(), expected.getThresholdValue());
543 Assert.assertEquals(actual.getThresholdUnit(), expected.getThresholdUnit());
544 Assert.assertEquals(actual.getEntitlementMetric(), expected.getEntitlementMetric());
545 Assert.assertEquals(actual.getIncrements(), expected.getIncrements());
546 Assert.assertEquals(actual.getAggregationFunction(), expected.getAggregationFunction());
547 Assert.assertEquals(actual.getOperationalScope(), expected.getOperationalScope());
548 Assert.assertEquals(actual.getTime(), expected.getTime());
552 private void init() {
553 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
554 vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
555 .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1 dec", "icon1"),
557 vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
558 .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
563 public void emptyListTest() {
564 Collection<EntitlementPoolEntity> entitlementPools =
565 vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
566 Assert.assertEquals(entitlementPools.size(), 0);
569 @Test(dependsOnMethods = "emptyListTest")
570 public void createTest() {
571 ep1Id = testCreate(vlm1Id, EP1_NAME);
573 Set<OperationalScope> opScopeChoices;
574 opScopeChoices = new HashSet<>();
575 opScopeChoices.add(OperationalScope.Core);
576 opScopeChoices.add(OperationalScope.CPU);
577 opScopeChoices.add(OperationalScope.Network_Wide);
578 EntitlementPoolEntity ep2 =
579 createEntitlementPool(vlm1Id, null, EP2_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
580 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
581 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
582 ep2Id = vendorLicenseManager.createEntitlementPool(ep2, USER1).getId();
586 private String testCreate(String vlmId, String name) {
587 Set<OperationalScope> opScopeChoices = new HashSet<>();
588 opScopeChoices.add(OperationalScope.Other);
589 EntitlementPoolEntity ep1 =
590 createEntitlementPool(vlmId, null, name, EP1_V01_DESC, 80, ThresholdUnit.Percentage,
591 EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
592 opScopeChoices, "op scope1", EntitlementTime.Other, "time1", "sku1");
593 String ep1Id = vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
596 EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(ep1);
597 Assert.assertTrue(loadedEp1.equals(ep1));
601 @Test(dependsOnMethods = {"createTest"})
602 public void testCreateWithExistingName_negative() {
603 testCreateWithExistingName_negative(vlm1Id, EP1_NAME);
606 @Test(dependsOnMethods = {"createTest"})
607 public void testCreateWithExistingNameUnderOtherVlm() {
608 testCreate(vlm2Id, EP1_NAME);
611 @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
612 public void updateAndGetTest() {
613 EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, VERSION01, ep1Id);
615 EntitlementPoolEntity ep1 = entitlementPoolDao.get(emptyEp1);
616 ep1.setEntitlementMetric(new ChoiceOrOther<>(EntitlementMetric.Other, "exception metric1 updated"));
617 ep1.setAggregationFunction(new ChoiceOrOther<>(AggregationFunction.Other, "agg func1 updated"));
619 vendorLicenseManager.updateEntitlementPool(ep1, USER1);
621 EntitlementPoolEntity loadedEp1 = vendorLicenseManager.getEntitlementPool(emptyEp1, USER1);
622 assertEntitlementPoolsEquals(loadedEp1, ep1);
625 @Test(dependsOnMethods = {"updateAndGetTest"})
626 public void testGetNonExistingVersion_negative() {
629 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(48, 83), ep1Id), USER1);
630 Assert.assertTrue(false);
631 } catch (CoreException exception) {
632 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
636 @Test(dependsOnMethods = {"updateAndGetTest"})
637 public void testGetOtherUserCandidateVersion_negative() {
638 vendorLicenseManager.checkin(vlm1Id, USER1);
639 vendorLicenseManager.checkout(vlm1Id, USER2);
642 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id), USER1);
643 Assert.assertTrue(false);
644 } catch (CoreException exception) {
645 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
649 @Test(dependsOnMethods = {"testGetOtherUserCandidateVersion_negative"})
650 public void testGetCandidateVersion() {
651 EntitlementPoolEntity ep = new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id);
652 ep.setDescription("updated!");
653 vendorLicenseManager.updateEntitlementPool(ep, USER2);
655 EntitlementPoolEntity actualEp = vendorLicenseManager.getEntitlementPool(ep, USER2);
656 EntitlementPoolEntity expectedEp = entitlementPoolDao.get(ep);
658 Assert.assertEquals(actualEp.getDescription(), ep.getDescription());
659 assertEntitlementPoolsEquals(actualEp, expectedEp);
662 @Test(dependsOnMethods = {"testGetCandidateVersion"})
663 public void testGetOldVersion() {
664 vendorLicenseManager.checkin(vlm1Id, USER2);
665 EntitlementPoolEntity actualEp = vendorLicenseManager
666 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 1), ep1Id), USER2);
667 Assert.assertEquals(actualEp.getDescription(), EP1_V01_DESC);
670 @Test(dependsOnMethods = {"testGetOldVersion"})
671 public void listTest() {
672 Collection<EntitlementPoolEntity> loadedEps =
673 vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
674 Assert.assertEquals(loadedEps.size(), 2);
676 int existingCounter = 0;
677 for (EntitlementPoolEntity loadedEp : loadedEps) {
678 if (ep2Id.equals(loadedEp.getId()) || ep1Id.equals(loadedEp.getId())) {
683 Assert.assertEquals(existingCounter, 2);
686 @Test(dependsOnMethods = {"listTest"})
687 public void deleteTest() {
688 vendorLicenseManager.checkout(vlm1Id, USER1);
689 EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, null, ep1Id);
690 vendorLicenseManager.deleteEntitlementPool(emptyEp1, USER1);
692 emptyEp1.setVersion(VERSION03);
693 EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(emptyEp1);
694 Assert.assertEquals(loadedEp1, null);
696 Collection<EntitlementPoolEntity> loadedEps =
697 entitlementPoolDao.list(new EntitlementPoolEntity(vlm1Id, VERSION03, null));
698 Assert.assertEquals(loadedEps.size(), 1);
699 Assert.assertEquals(loadedEps.iterator().next().getId(), ep2Id);
702 @Test(dependsOnMethods = "deleteTest")
703 public void listOldVersionTest() {
704 Collection<EntitlementPoolEntity> loadedEps =
705 vendorLicenseManager.listEntitlementPools(vlm1Id, VERSION01, USER1);
706 Assert.assertEquals(loadedEps.size(), 2);
709 @Test(dependsOnMethods = "deleteTest")
710 public void testCreateWithRemovedName() {
711 testCreate(vlm1Id, EP1_NAME);
714 @Test(dependsOnMethods = "deleteTest")
715 public void testCreateWithExistingNameAfterCheckout_negative() {
716 testCreateWithExistingName_negative(vlm1Id, EP2_NAME);
719 private void testCreateWithExistingName_negative(String vlmId, String epName) {
721 EntitlementPoolEntity ep1 =
722 createEntitlementPool(vlmId, null, epName, EP1_V01_DESC, 80, ThresholdUnit.Percentage,
723 EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
724 Collections.singleton(OperationalScope.Other), "op scope1", EntitlementTime.Other,
726 vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
728 } catch (CoreException exception) {
729 Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);