Add collaboration feature
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / test / java / org / openecomp / sdc / vendorlicense / impl / EntitlementPoolTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21
22 package org.openecomp.sdc.vendorlicense.impl;
23
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;
44
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;
51 import java.util.Set;
52
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;
57
58 public class EntitlementPoolTest {
59
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);
69
70   @Mock
71   private VendorLicenseFacade vendorLicenseFacade;
72
73   @Mock
74   private EntitlementPoolDao entitlementPoolDao;
75   @Mock
76   private LimitDao limitDao;
77
78   @InjectMocks
79   @Spy
80   private VendorLicenseManagerImpl vendorLicenseManagerImpl;
81
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,
87                                                      String increments,
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;
106   }
107
108   @BeforeMethod
109   public void setUp() throws Exception {
110     MockitoAnnotations.initMocks(this);
111   }
112
113   @Test
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
122                 .Absolute,
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));
128
129     vendorLicenseManagerImpl.createEntitlementPool(ep2);
130     verify(vendorLicenseFacade).createEntitlementPool(ep2);
131
132   }
133
134   @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
135       "license model with id vlm1_id has invalid date range.")
136   public void createWithInvalidStartExpiryDateTest() {
137
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();
153     Assert.fail();
154
155   }
156
157   @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
158       "license model with id vlm1_id has invalid date range.")
159   public void createWithoutStartDateTest() {
160
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();
175     Assert.fail();
176
177   }
178
179   @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
180       "license model with id vlm1_id has invalid date range.")
181   public void createWithSameStartExpiryDateTest() {
182
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();
199   }
200
201   @Test
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,
210             ThresholdUnit
211                 .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().minusDays(3L).format(formatter));
216     ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
217
218     vendorLicenseManagerImpl.updateEntitlementPool(ep2);
219   }
220
221   @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
222       "license model with id vlm1_id has invalid date range.")
223   public void updateWithInvalidStartExpiryDateTest() {
224
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);
240     Assert.fail();
241
242   }
243
244   @Test
245   public void updateWithoutStartDateTest() {
246     try {
247
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,
257               null,
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);
262       Assert.fail();
263     } catch (CoreException exception) {
264       Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
265     }
266   }
267
268   @Test
269   public void updateWithSameStartExpiryDateTest() {
270     try {
271
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,
281               null,
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);
287       Assert.fail();
288     } catch (CoreException exception) {
289       Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
290     }
291   }
292
293   @Test
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);
300
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));
309
310     doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
311
312     doNothing().when(vendorLicenseManagerImpl).deleteChildLimits(vlm1_id, VERSION01, ep1_id);
313
314     doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(anyObject(), anyObject(),
315         anyObject(), anyObject());
316
317     vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool);
318
319     verify(entitlementPoolDao).delete(entitlementPool);
320   }
321
322   @Test
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);
329
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'");
336
337     entitlementPool.setStartDate(LocalDateTime.now().format(formatter));
338     entitlementPool.setExpiryDate(LocalDateTime.now().plusDays(1L).format(formatter));
339
340     doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
341
342     EntitlementPoolEntity retrived = vendorLicenseManagerImpl.getEntitlementPool(entitlementPool);
343
344     Assert.assertEquals(retrived.getId(), entitlementPool.getId());
345     Assert.assertEquals(retrived.getVendorLicenseModelId(),
346         entitlementPool.getVendorLicenseModelId());
347     Assert.assertEquals(retrived.getVersion(), entitlementPool.getVersion());
348   }
349
350   @Test
351   public void testListEntitlmentPool() {
352
353     Set<OperationalScope> opScopeChoices;
354     opScopeChoices = new HashSet<>();
355     opScopeChoices.add(OperationalScope.Core);
356     opScopeChoices.add(OperationalScope.CPU);
357     opScopeChoices.add(OperationalScope.Network_Wide);
358
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);
369
370     Collection<EntitlementPoolEntity> EPs =
371         vendorLicenseManagerImpl.listEntitlementPools(vlm1_id, VERSION01);
372
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)));
376   }
377
378
379   /*  @Test
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);
386
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));
394
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());
400
401         LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
402                 "Core",AggregationFunction.Average,10,"Hour");
403
404         ArrayList<LimitEntity> limitEntityList = new ArrayList();
405         limitEntityList.add(limitEntity);
406
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());
411         try {
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);
418
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)
426         {
427             Assert.fail();
428         }
429
430         vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
431
432         verify(limitDao).delete(anyObject());
433     }
434
435     @Test
436     public void deleteEntitlementPoolInvalidTest() {
437         try {
438             Set<OperationalScope> opScopeChoices;
439             opScopeChoices = new HashSet<>();
440             opScopeChoices.add(OperationalScope.Core);
441             opScopeChoices.add(OperationalScope.CPU);
442             opScopeChoices.add(OperationalScope.Network_Wide);
443
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));
451
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());
457
458             LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
459                 "Core",AggregationFunction.Average,10,"Hour");
460
461             ArrayList<LimitEntity> limitEntityList = new ArrayList();
462             limitEntityList.add(limitEntity);
463
464             doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
465             doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
466             doReturn(false).when(limitDao).isLimitPresent(anyObject());
467
468             try {
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);
475
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)
483             {
484                 Assert.fail();
485             }
486
487             vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
488         } catch (CoreException exception) {
489             Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
490         }
491     } */
492
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";
500
501   private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
502   private static EntitlementPoolDao entitlementPoolDao;
503
504   private static String vlm1Id;
505   private static String vlm2Id;
506   private static String ep1Id;
507   private static String ep2Id;
508
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,
514                                                             String increments,
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);
528     entitlementPool
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;
537   }
538
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());
553   }
554
555   @BeforeClass
556   private void init() {
557     entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
558     vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
559             .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1 dec", "icon1"),
560         USER1).getId();
561     vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
562             .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
563         USER1).getId();
564   }
565
566   @Test
567   public void emptyListTest() {
568     Collection<EntitlementPoolEntity> entitlementPools =
569         vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
570     Assert.assertEquals(entitlementPools.size(), 0);
571   }
572
573   @Test(dependsOnMethods = "emptyListTest")
574   public void createTest() {
575     ep1Id = testCreate(vlm1Id, EP1_NAME);
576
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();
587     ep2.setId(ep2Id);
588   }
589
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();
598     ep1.setId(ep1Id);
599
600     EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(ep1);
601     Assert.assertTrue(loadedEp1.equals(ep1));
602     return ep1Id;
603   }
604
605   @Test(dependsOnMethods = {"createTest"})
606   public void testCreateWithExistingName_negative() {
607     testCreateWithExistingName_negative(vlm1Id, EP1_NAME);
608   }
609
610   @Test(dependsOnMethods = {"createTest"})
611   public void testCreateWithExistingNameUnderOtherVlm() {
612     testCreate(vlm2Id, EP1_NAME);
613   }
614
615   @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
616   public void updateAndGetTest() {
617     EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, VERSION01, ep1Id);
618
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"));
622
623     vendorLicenseManager.updateEntitlementPool(ep1, USER1);
624
625     EntitlementPoolEntity loadedEp1 = vendorLicenseManager.getEntitlementPool(emptyEp1, USER1);
626     assertEntitlementPoolsEquals(loadedEp1, ep1);
627   }
628
629   @Test(dependsOnMethods = {"updateAndGetTest"})
630   public void testGetNonExistingVersion_negative() {
631     try {
632       vendorLicenseManager
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);
637     }
638   }
639
640   @Test(dependsOnMethods = {"updateAndGetTest"})
641   public void testGetOtherUserCandidateVersion_negative() {
642     vendorLicenseManager.checkin(vlm1Id, USER1);
643     vendorLicenseManager.checkout(vlm1Id, USER2);
644     try {
645       vendorLicenseManager
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);
650     }
651   }
652
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);
658
659     EntitlementPoolEntity actualEp = vendorLicenseManager.getEntitlementPool(ep, USER2);
660     EntitlementPoolEntity expectedEp = entitlementPoolDao.get(ep);
661
662     Assert.assertEquals(actualEp.getDescription(), ep.getDescription());
663     assertEntitlementPoolsEquals(actualEp, expectedEp);
664   }
665
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);
672   }
673
674   @Test(dependsOnMethods = {"testGetOldVersion"})
675   public void listTest() {
676     Collection<EntitlementPoolEntity> loadedEps =
677         vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
678     Assert.assertEquals(loadedEps.size(), 2);
679
680     int existingCounter = 0;
681     for (EntitlementPoolEntity loadedEp : loadedEps) {
682       if (ep2Id.equals(loadedEp.getId()) || ep1Id.equals(loadedEp.getId())) {
683         existingCounter++;
684       }
685     }
686
687     Assert.assertEquals(existingCounter, 2);
688   }
689
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);
695
696     emptyEp1.setVersion(VERSION03);
697     EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(emptyEp1);
698     Assert.assertEquals(loadedEp1, null);
699
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);
704   }
705
706   @Test(dependsOnMethods = "deleteTest")
707   public void listOldVersionTest() {
708     Collection<EntitlementPoolEntity> loadedEps =
709         vendorLicenseManager.listEntitlementPools(vlm1Id, VERSION01, USER1);
710     Assert.assertEquals(loadedEps.size(), 2);
711   }
712
713   @Test(dependsOnMethods = "deleteTest")
714   public void testCreateWithRemovedName() {
715     testCreate(vlm1Id, EP1_NAME);
716   }
717
718   @Test(dependsOnMethods = "deleteTest")
719   public void testCreateWithExistingNameAfterCheckout_negative() {
720     testCreateWithExistingName_negative(vlm1Id, EP2_NAME);
721   }
722
723   private void testCreateWithExistingName_negative(String vlmId, String epName) {
724     try {
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,
729               "time1", "sku1");
730       vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
731       Assert.fail();
732     } catch (CoreException exception) {
733       Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
734     }
735   }
736 */
737 }
738