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.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 java.time.LocalDate;
42 import java.time.LocalDateTime;
43 import java.time.format.DateTimeFormatter;
44 import java.util.Arrays;
45 import java.util.Collection;
46 import java.util.HashSet;
49 import static org.mockito.ArgumentMatchers.any;
50 import static org.mockito.Mockito.doNothing;
51 import static org.mockito.Mockito.doReturn;
52 import static org.mockito.Mockito.verify;
54 public class EntitlementPoolTest {
56 //JUnit Test Cases using Mockito
57 private final String USER1 = "epTestUser1";
58 private final String EP1_NAME = "EP1 name";
59 private final String EP2_NAME = "EP2 name";
60 private final String LT1_NAME = "LT1 name";
61 private static String vlm1_id = "vlm1_id";
62 private static String ep1_id = "ep1_id";
63 private static String ep2_id = "ep2_id";
64 public static final Version VERSION01 = new Version(0, 1);
67 private VendorLicenseFacade vendorLicenseFacade;
70 private EntitlementPoolDao entitlementPoolDao;
72 private LimitDao limitDao;
76 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
78 public EntitlementPoolEntity createEntitlementPool(String vlmId, Version version, String id,
79 String name, String desc, int threshold,
80 ThresholdUnit thresholdUnit,
81 EntitlementMetric entitlementMetricChoice,
82 String entitlementMetricOther,
84 AggregationFunction aggregationFunctionChoice,
85 String aggregationFunctionOther,
86 Set<OperationalScope> operationalScopeChoices,
87 String operationalScopeOther,
88 EntitlementTime timeChoice,
89 String timeOther, String sku) {
90 EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity();
91 entitlementPool.setVendorLicenseModelId(vlmId);
92 entitlementPool.setId(id);
93 entitlementPool.setVersion(version);
94 entitlementPool.setName(name);
95 entitlementPool.setDescription(desc);
96 entitlementPool.setThresholdValue(threshold);
97 entitlementPool.setThresholdUnit(thresholdUnit);
98 entitlementPool.setIncrements(increments);
99 entitlementPool.setOperationalScope(
100 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
101 return entitlementPool;
105 public void setUp() throws Exception {
106 MockitoAnnotations.initMocks(this);
111 public void tearDown(){
112 vendorLicenseManagerImpl = null;
116 public void createTest() {
117 Set<OperationalScope> opScopeChoices;
118 opScopeChoices = new HashSet<>();
119 opScopeChoices.add(OperationalScope.Core);
120 opScopeChoices.add(OperationalScope.CPU);
121 opScopeChoices.add(OperationalScope.Network_Wide);
122 EntitlementPoolEntity ep2 =
123 createEntitlementPool("vlm1Id", null, ep1_id, EP1_NAME, "EP2 dec", 70, ThresholdUnit
125 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
126 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
127 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
128 ep2.setStartDate(LocalDate.now().format(formatter));
129 ep2.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
131 vendorLicenseManagerImpl.createEntitlementPool(ep2);
132 verify(vendorLicenseFacade).createEntitlementPool(ep2);
137 @Test(expected = CoreException.class)
138 public void createWithInvalidStartExpiryDateTest() {
140 Set<OperationalScope> opScopeChoices;
141 opScopeChoices = new HashSet<>();
142 opScopeChoices.add(OperationalScope.Core);
143 opScopeChoices.add(OperationalScope.CPU);
144 opScopeChoices.add(OperationalScope.Network_Wide);
145 EntitlementPoolEntity ep2 =
146 createEntitlementPool("vlm2Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
147 ThresholdUnit.Absolute,
148 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
149 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
150 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
151 ep2.setStartDate(LocalDate.now().format(formatter));
152 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
153 ep2.setVendorLicenseModelId(vlm1_id);
154 vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
155 Assert.fail("Vendor license model with id vlm1_id has invalid date range.");
159 @Test(expected = CoreException.class)
160 public void createWithoutStartDateTest() {
162 Set<OperationalScope> opScopeChoices;
163 opScopeChoices = new HashSet<>();
164 opScopeChoices.add(OperationalScope.Core);
165 opScopeChoices.add(OperationalScope.CPU);
166 opScopeChoices.add(OperationalScope.Network_Wide);
167 EntitlementPoolEntity ep2 =
168 createEntitlementPool("vlm3Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
169 ThresholdUnit.Absolute,
170 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
171 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
172 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
173 ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
174 ep2.setVendorLicenseModelId(vlm1_id);
175 vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
176 Assert.fail("Vendor license model with id vlm1_id has invalid date range.");
180 @Test(expected = CoreException.class)
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();
198 Assert.fail("Vendor license model with id vlm1_id has invalid date range.");
202 public void createWithExpiryDateNullTest() {
204 Set<OperationalScope> opScopeChoices;
205 opScopeChoices = new HashSet<>();
206 opScopeChoices.add(OperationalScope.Core);
207 opScopeChoices.add(OperationalScope.CPU);
208 opScopeChoices.add(OperationalScope.Network_Wide);
209 EntitlementPoolEntity ep2 =
210 createEntitlementPool("vlm2Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
211 ThresholdUnit.Absolute,
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().format(formatter));
216 ep2.setExpiryDate(null);
217 ep2.setVendorLicenseModelId(vlm1_id);
218 vendorLicenseManagerImpl.createEntitlementPool(ep2);
219 verify(vendorLicenseFacade).createEntitlementPool(ep2);
224 public void createWithStartAndExpiryDateNullTest() {
226 Set<OperationalScope> opScopeChoices;
227 opScopeChoices = new HashSet<>();
228 opScopeChoices.add(OperationalScope.Core);
229 opScopeChoices.add(OperationalScope.CPU);
230 opScopeChoices.add(OperationalScope.Network_Wide);
231 EntitlementPoolEntity ep2 =
232 createEntitlementPool("vlm2Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
233 ThresholdUnit.Absolute,
234 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
235 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
236 ep2.setStartDate(null);
237 ep2.setExpiryDate(null);
238 ep2.setVendorLicenseModelId(vlm1_id);
239 vendorLicenseManagerImpl.createEntitlementPool(ep2);
240 verify(vendorLicenseFacade).createEntitlementPool(ep2);
245 public void testUpdate() {
246 Set<OperationalScope> opScopeChoices;
247 opScopeChoices = new HashSet<>();
248 opScopeChoices.add(OperationalScope.Core);
249 opScopeChoices.add(OperationalScope.CPU);
250 opScopeChoices.add(OperationalScope.Network_Wide);
251 EntitlementPoolEntity ep2 =
252 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
255 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
256 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
257 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
258 ep2.setStartDate(LocalDate.now().minusDays(3L).format(formatter));
259 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
261 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
264 @Test(expected = CoreException.class)
265 public void updateWithInvalidStartExpiryDateTest() {
267 Set<OperationalScope> opScopeChoices;
268 opScopeChoices = new HashSet<>();
269 opScopeChoices.add(OperationalScope.Core);
270 opScopeChoices.add(OperationalScope.CPU);
271 opScopeChoices.add(OperationalScope.Network_Wide);
272 EntitlementPoolEntity ep2 =
273 createEntitlementPool("vlm2Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
274 ThresholdUnit.Absolute,
275 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
276 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
277 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
278 ep2.setStartDate(LocalDate.now().format(formatter));
279 ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
280 ep2.setVendorLicenseModelId(vlm1_id);
281 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
282 Assert.fail("Vendor license model with id vlm1_id has invalid date range.");
287 public void updateWithoutStartDateTest() {
290 Set<OperationalScope> opScopeChoices;
291 opScopeChoices = new HashSet<>();
292 opScopeChoices.add(OperationalScope.Core);
293 opScopeChoices.add(OperationalScope.CPU);
294 opScopeChoices.add(OperationalScope.Network_Wide);
295 EntitlementPoolEntity ep2 =
296 createEntitlementPool("vlm3Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
297 ThresholdUnit.Absolute,
298 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average,
300 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
301 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
302 ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
303 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
305 } catch (CoreException exception) {
306 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
311 public void updateWithSameStartExpiryDateTest() {
314 Set<OperationalScope> opScopeChoices;
315 opScopeChoices = new HashSet<>();
316 opScopeChoices.add(OperationalScope.Core);
317 opScopeChoices.add(OperationalScope.CPU);
318 opScopeChoices.add(OperationalScope.Network_Wide);
319 EntitlementPoolEntity ep2 =
320 createEntitlementPool("vlm4Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
321 ThresholdUnit.Absolute,
322 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average,
324 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
325 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
326 ep2.setStartDate(LocalDate.now().format(formatter));
327 ep2.setExpiryDate(LocalDate.now().format(formatter));
328 vendorLicenseManagerImpl.updateEntitlementPool(ep2);
330 } catch (CoreException exception) {
331 Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
336 public void deleteEntitlementPoolTest() {
337 Set<OperationalScope> opScopeChoices;
338 opScopeChoices = new HashSet<>();
339 opScopeChoices.add(OperationalScope.Core);
340 opScopeChoices.add(OperationalScope.CPU);
341 opScopeChoices.add(OperationalScope.Network_Wide);
343 EntitlementPoolEntity entitlementPool =
344 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
345 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2", "inc2",
346 AggregationFunction.Average, null,
347 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
348 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
349 entitlementPool.setStartDate(LocalDate.now().format(formatter));
350 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
352 doReturn(entitlementPool).when(entitlementPoolDao).get(any());
354 doNothing().when(vendorLicenseManagerImpl).deleteChildLimits(vlm1_id, VERSION01, ep1_id);
356 doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(any(), any(),
359 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool);
361 verify(entitlementPoolDao).delete(entitlementPool);
365 public void testGetEntitlementPool() {
366 Set<OperationalScope> opScopeChoices;
367 opScopeChoices = new HashSet<>();
368 opScopeChoices.add(OperationalScope.Core);
369 opScopeChoices.add(OperationalScope.CPU);
370 opScopeChoices.add(OperationalScope.Network_Wide);
372 EntitlementPoolEntity entitlementPool =
373 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
374 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2", "inc2",
375 AggregationFunction.Average, null,
376 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
377 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'");
379 entitlementPool.setStartDate(LocalDateTime.now().format(formatter));
380 entitlementPool.setExpiryDate(LocalDateTime.now().plusDays(1L).format(formatter));
382 doReturn(entitlementPool).when(entitlementPoolDao).get(any());
384 EntitlementPoolEntity retrieved = vendorLicenseManagerImpl.getEntitlementPool(entitlementPool);
386 Assert.assertEquals(retrieved.getId(), entitlementPool.getId());
387 Assert.assertEquals(retrieved.getVendorLicenseModelId(),
388 entitlementPool.getVendorLicenseModelId());
389 Assert.assertEquals(retrieved.getVersion(), entitlementPool.getVersion());
393 public void testListEntitlmentPool() {
395 Set<OperationalScope> opScopeChoices;
396 opScopeChoices = new HashSet<>();
397 opScopeChoices.add(OperationalScope.Core);
398 opScopeChoices.add(OperationalScope.CPU);
399 opScopeChoices.add(OperationalScope.Network_Wide);
401 doReturn(Arrays.asList(
402 createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP1 dec", 70,
403 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric1",
404 "inc1", AggregationFunction.Average, null,
405 opScopeChoices, null, EntitlementTime.Other, "time1", "sku1"),
406 createEntitlementPool(vlm1_id, VERSION01, ep2_id, EP2_NAME, "EP2 dec", 70,
407 ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2",
408 "inc2", AggregationFunction.Average, null,
409 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2")))
410 .when(vendorLicenseFacade).listEntitlementPools(vlm1_id, VERSION01);
412 Collection<EntitlementPoolEntity> EPs =
413 vendorLicenseManagerImpl.listEntitlementPools(vlm1_id, VERSION01);
415 verify(vendorLicenseFacade).listEntitlementPools(vlm1_id, VERSION01);
416 Assert.assertEquals(EPs.size(), 2);
417 EPs.forEach(ep -> Assert.assertTrue(ep.getId().matches(ep1_id + "|" + ep2_id)));
422 public void deleteEntitlementPoolTest() {
423 Set<OperationalScope> opScopeChoices;
424 opScopeChoices = new HashSet<>();
425 opScopeChoices.add(OperationalScope.Core);
426 opScopeChoices.add(OperationalScope.CPU);
427 opScopeChoices.add(OperationalScope.Network_Wide);
429 EntitlementPoolEntity entitlementPool =
430 createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
431 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
432 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
433 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
434 entitlementPool.setStartDate(LocalDate.now().format(formatter));
435 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
437 VersionInfo info = new VersionInfo();
438 Version version = new Version();
439 info.getViewableVersions().add(version);
440 info.setActiveVersion(version);
441 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
443 LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
444 "Core",AggregationFunction.Average,10,"Hour");
446 ArrayList<LimitEntity> limitEntityList = new ArrayList();
447 limitEntityList.add(limitEntity);
449 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
450 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
451 doReturn(true).when(limitDao).isLimitPresent(anyObject());
452 doReturn(limitEntity).when(limitDao).get(anyObject());
454 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
455 limitField.setAccessible(true);
456 Field modifiersField = Field.class.getDeclaredField("modifiers");
457 modifiersField.setAccessible(true);
458 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
459 limitField.set(null, limitDao);
461 Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao");
462 epField.setAccessible(true);
463 modifiersField = Field.class.getDeclaredField("modifiers");
464 modifiersField.setAccessible(true);
465 modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
466 epField.set(null, entitlementPoolDao);
467 } catch(NoSuchFieldException | IllegalAccessException e)
472 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
474 verify(limitDao).delete(anyObject());
478 public void deleteEntitlementPoolInvalidTest() {
480 Set<OperationalScope> opScopeChoices;
481 opScopeChoices = new HashSet<>();
482 opScopeChoices.add(OperationalScope.Core);
483 opScopeChoices.add(OperationalScope.CPU);
484 opScopeChoices.add(OperationalScope.Network_Wide);
486 EntitlementPoolEntity entitlementPool =
487 createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
488 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
489 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
490 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
491 entitlementPool.setStartDate(LocalDate.now().format(formatter));
492 entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
494 VersionInfo info = new VersionInfo();
495 Version version = new Version();
496 info.getViewableVersions().add(version);
497 info.setActiveVersion(version);
498 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
500 LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
501 "Core",AggregationFunction.Average,10,"Hour");
503 ArrayList<LimitEntity> limitEntityList = new ArrayList();
504 limitEntityList.add(limitEntity);
506 doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
507 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
508 doReturn(false).when(limitDao).isLimitPresent(anyObject());
511 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
512 limitField.setAccessible(true);
513 Field modifiersField = Field.class.getDeclaredField("modifiers");
514 modifiersField.setAccessible(true);
515 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
516 limitField.set(null, limitDao);
518 Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao");
519 epField.setAccessible(true);
520 modifiersField = Field.class.getDeclaredField("modifiers");
521 modifiersField.setAccessible(true);
522 modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
523 epField.set(null, entitlementPoolDao);
524 } catch(NoSuchFieldException | IllegalAccessException e)
529 vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
530 } catch (CoreException exception) {
531 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
535 /* private static final String USER1 = "epTestUser1";
536 private static final String USER2 = "epTestUser2";
537 private static final String EP1_V01_DESC = "EP1 desc";
538 private static final Version VERSION01 = new Version(0, 1);
539 private static final Version VERSION03 = new Version(0, 3);
540 private static final String EP1_NAME = "EP1 name";
541 private static final String EP2_NAME = "EP2 name";
543 private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
544 private static EntitlementPoolDao entitlementPoolDao;
546 private static String vlm1Id;
547 private static String vlm2Id;
548 private static String ep1Id;
549 private static String ep2Id;
551 public static EntitlementPoolEntity createEntitlementPool(String vlmId, Version version,
552 String name, String desc, int threshold,
553 ThresholdUnit thresholdUnit,
554 EntitlementMetric entitlementMetricChoice,
555 String entitlementMetricOther,
557 AggregationFunction aggregationFunctionChoice,
558 String aggregationFunctionOther,
559 Set<OperationalScope> operationalScopeChoices,
560 String operationalScopeOther,
561 EntitlementTime timeChoice,
562 String timeOther, String sku) {
563 EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity();
564 entitlementPool.setVendorLicenseModelId(vlmId);
565 entitlementPool.setVersion(version);
566 entitlementPool.setName(name);
567 entitlementPool.setDescription(desc);
568 entitlementPool.setThresholdValue(threshold);
569 entitlementPool.setThresholdUnit(thresholdUnit);
571 .setEntitlementMetric(new ChoiceOrOther<>(entitlementMetricChoice, entitlementMetricOther));
572 entitlementPool.setIncrements(increments);
573 entitlementPool.setAggregationFunction(
574 new ChoiceOrOther<>(aggregationFunctionChoice, aggregationFunctionOther));
575 entitlementPool.setOperationalScope(
576 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
577 entitlementPool.setTime(new ChoiceOrOther<>(timeChoice, timeOther));
578 return entitlementPool;
581 private static void assertEntitlementPoolsEquals(EntitlementPoolEntity actual,
582 EntitlementPoolEntity expected) {
583 Assert.assertEquals(actual.getVendorLicenseModelId(), expected.getVendorLicenseModelId());
584 Assert.assertEquals(actual.getVersion(), expected.getVersion());
585 Assert.assertEquals(actual.getId(), expected.getId());
586 Assert.assertEquals(actual.getName(), expected.getName());
587 Assert.assertEquals(actual.getDescription(), expected.getDescription());
588 Assert.assertEquals(actual.getThresholdValue(), expected.getThresholdValue());
589 Assert.assertEquals(actual.getThresholdUnit(), expected.getThresholdUnit());
590 Assert.assertEquals(actual.getEntitlementMetric(), expected.getEntitlementMetric());
591 Assert.assertEquals(actual.getIncrements(), expected.getIncrements());
592 Assert.assertEquals(actual.getAggregationFunction(), expected.getAggregationFunction());
593 Assert.assertEquals(actual.getOperationalScope(), expected.getOperationalScope());
594 Assert.assertEquals(actual.getTime(), expected.getTime());
598 private void init() {
599 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
600 vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
601 .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1 dec", "icon1"),
603 vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
604 .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
609 public void emptyListTest() {
610 Collection<EntitlementPoolEntity> entitlementPools =
611 vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
612 Assert.assertEquals(entitlementPools.size(), 0);
615 @Test(dependsOnMethods = "emptyListTest")
616 public void createTest() {
617 ep1Id = testCreate(vlm1Id, EP1_NAME);
619 Set<OperationalScope> opScopeChoices;
620 opScopeChoices = new HashSet<>();
621 opScopeChoices.add(OperationalScope.Core);
622 opScopeChoices.add(OperationalScope.CPU);
623 opScopeChoices.add(OperationalScope.Network_Wide);
624 EntitlementPoolEntity ep2 =
625 createEntitlementPool(vlm1Id, null, EP2_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
626 EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
627 opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
628 ep2Id = vendorLicenseManager.createEntitlementPool(ep2, USER1).getId();
632 private String testCreate(String vlmId, String name) {
633 Set<OperationalScope> opScopeChoices = new HashSet<>();
634 opScopeChoices.add(OperationalScope.Other);
635 EntitlementPoolEntity ep1 =
636 createEntitlementPool(vlmId, null, name, EP1_V01_DESC, 80, ThresholdUnit.Percentage,
637 EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
638 opScopeChoices, "op scope1", EntitlementTime.Other, "time1", "sku1");
639 String ep1Id = vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
642 EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(ep1);
643 Assert.assertTrue(loadedEp1.equals(ep1));
647 @Test(dependsOnMethods = {"createTest"})
648 public void testCreateWithExistingName_negative() {
649 testCreateWithExistingName_negative(vlm1Id, EP1_NAME);
652 @Test(dependsOnMethods = {"createTest"})
653 public void testCreateWithExistingNameUnderOtherVlm() {
654 testCreate(vlm2Id, EP1_NAME);
657 @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
658 public void updateAndGetTest() {
659 EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, VERSION01, ep1Id);
661 EntitlementPoolEntity ep1 = entitlementPoolDao.get(emptyEp1);
662 ep1.setEntitlementMetric(new ChoiceOrOther<>(EntitlementMetric.Other, "exception metric1 updated"));
663 ep1.setAggregationFunction(new ChoiceOrOther<>(AggregationFunction.Other, "agg func1 updated"));
665 vendorLicenseManager.updateEntitlementPool(ep1, USER1);
667 EntitlementPoolEntity loadedEp1 = vendorLicenseManager.getEntitlementPool(emptyEp1, USER1);
668 assertEntitlementPoolsEquals(loadedEp1, ep1);
671 @Test(dependsOnMethods = {"updateAndGetTest"})
672 public void testGetNonExistingVersion_negative() {
675 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(48, 83), ep1Id), USER1);
676 Assert.assertTrue(false);
677 } catch (CoreException exception) {
678 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
682 @Test(dependsOnMethods = {"updateAndGetTest"})
683 public void testGetOtherUserCandidateVersion_negative() {
684 vendorLicenseManager.checkin(vlm1Id, USER1);
685 vendorLicenseManager.checkout(vlm1Id, USER2);
688 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id), USER1);
689 Assert.assertTrue(false);
690 } catch (CoreException exception) {
691 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
695 @Test(dependsOnMethods = {"testGetOtherUserCandidateVersion_negative"})
696 public void testGetCandidateVersion() {
697 EntitlementPoolEntity ep = new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id);
698 ep.setDescription("updated!");
699 vendorLicenseManager.updateEntitlementPool(ep, USER2);
701 EntitlementPoolEntity actualEp = vendorLicenseManager.getEntitlementPool(ep, USER2);
702 EntitlementPoolEntity expectedEp = entitlementPoolDao.get(ep);
704 Assert.assertEquals(actualEp.getDescription(), ep.getDescription());
705 assertEntitlementPoolsEquals(actualEp, expectedEp);
708 @Test(dependsOnMethods = {"testGetCandidateVersion"})
709 public void testGetOldVersion() {
710 vendorLicenseManager.checkin(vlm1Id, USER2);
711 EntitlementPoolEntity actualEp = vendorLicenseManager
712 .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 1), ep1Id), USER2);
713 Assert.assertEquals(actualEp.getDescription(), EP1_V01_DESC);
716 @Test(dependsOnMethods = {"testGetOldVersion"})
717 public void listTest() {
718 Collection<EntitlementPoolEntity> loadedEps =
719 vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
720 Assert.assertEquals(loadedEps.size(), 2);
722 int existingCounter = 0;
723 for (EntitlementPoolEntity loadedEp : loadedEps) {
724 if (ep2Id.equals(loadedEp.getId()) || ep1Id.equals(loadedEp.getId())) {
729 Assert.assertEquals(existingCounter, 2);
732 @Test(dependsOnMethods = {"listTest"})
733 public void deleteTest() {
734 vendorLicenseManager.checkout(vlm1Id, USER1);
735 EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, null, ep1Id);
736 vendorLicenseManager.deleteEntitlementPool(emptyEp1, USER1);
738 emptyEp1.setVersion(VERSION03);
739 EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(emptyEp1);
740 Assert.assertEquals(loadedEp1, null);
742 Collection<EntitlementPoolEntity> loadedEps =
743 entitlementPoolDao.list(new EntitlementPoolEntity(vlm1Id, VERSION03, null));
744 Assert.assertEquals(loadedEps.size(), 1);
745 Assert.assertEquals(loadedEps.iterator().next().getId(), ep2Id);
748 @Test(dependsOnMethods = "deleteTest")
749 public void listOldVersionTest() {
750 Collection<EntitlementPoolEntity> loadedEps =
751 vendorLicenseManager.listEntitlementPools(vlm1Id, VERSION01, USER1);
752 Assert.assertEquals(loadedEps.size(), 2);
755 @Test(dependsOnMethods = "deleteTest")
756 public void testCreateWithRemovedName() {
757 testCreate(vlm1Id, EP1_NAME);
760 @Test(dependsOnMethods = "deleteTest")
761 public void testCreateWithExistingNameAfterCheckout_negative() {
762 testCreateWithExistingName_negative(vlm1Id, EP2_NAME);
765 private void testCreateWithExistingName_negative(String vlmId, String epName) {
767 EntitlementPoolEntity ep1 =
768 createEntitlementPool(vlmId, null, epName, EP1_V01_DESC, 80, ThresholdUnit.Percentage,
769 EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
770 Collections.singleton(OperationalScope.Other), "op scope1", EntitlementTime.Other,
772 vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
774 } catch (CoreException exception) {
775 Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);