38f0ebff86d7a040504c63cde5930e4dc17d907c
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 package org.openecomp.sdc.vendorlicense.impl;
19
20 import static org.mockito.Matchers.anyObject;
21 import static org.mockito.Mockito.doNothing;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.verify;
24
25 import java.time.LocalDate;
26 import java.time.LocalDateTime;
27 import java.time.format.DateTimeFormatter;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.HashSet;
31 import java.util.Set;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.mockito.Spy;
36 import org.openecomp.sdc.common.errors.CoreException;
37 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
38 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
39 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
40 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric;
41 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
42 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime;
43 import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther;
44 import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
45 import org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit;
46 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes;
47 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
48 import org.openecomp.sdc.versioning.dao.types.Version;
49 import org.testng.Assert;
50 import org.testng.annotations.AfterMethod;
51 import org.testng.annotations.BeforeMethod;
52 import org.testng.annotations.Test;
53
54 public class EntitlementPoolTest {
55
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);
65
66   @Mock
67   private VendorLicenseFacade vendorLicenseFacade;
68
69   @Mock
70   private EntitlementPoolDao entitlementPoolDao;
71   @Mock
72   private LimitDao limitDao;
73
74   @InjectMocks
75   @Spy
76   private VendorLicenseManagerImpl vendorLicenseManagerImpl;
77
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,
83                                                      String increments,
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;
102   }
103
104   @BeforeMethod
105   public void setUp() throws Exception {
106     MockitoAnnotations.initMocks(this);
107   }
108
109
110   @AfterMethod
111   public void tearDown(){
112     vendorLicenseManagerImpl = null;
113   }
114
115   @Test
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
124                 .Absolute,
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));
130
131     vendorLicenseManagerImpl.createEntitlementPool(ep2);
132     verify(vendorLicenseFacade).createEntitlementPool(ep2);
133
134   }
135
136
137   @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
138       "license model with id vlm1_id has invalid date range.")
139   public void createWithInvalidStartExpiryDateTest() {
140
141     Set<OperationalScope> opScopeChoices;
142     opScopeChoices = new HashSet<>();
143     opScopeChoices.add(OperationalScope.Core);
144     opScopeChoices.add(OperationalScope.CPU);
145     opScopeChoices.add(OperationalScope.Network_Wide);
146     EntitlementPoolEntity ep2 =
147         createEntitlementPool("vlm2Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
148             ThresholdUnit.Absolute,
149             EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
150             opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
151     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
152     ep2.setStartDate(LocalDate.now().format(formatter));
153     ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
154     ep2.setVendorLicenseModelId(vlm1_id);
155     vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
156     Assert.fail();
157
158   }
159
160   @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
161       "license model with id vlm1_id has invalid date range.")
162   public void createWithoutStartDateTest() {
163
164     Set<OperationalScope> opScopeChoices;
165     opScopeChoices = new HashSet<>();
166     opScopeChoices.add(OperationalScope.Core);
167     opScopeChoices.add(OperationalScope.CPU);
168     opScopeChoices.add(OperationalScope.Network_Wide);
169     EntitlementPoolEntity ep2 =
170         createEntitlementPool("vlm3Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
171             ThresholdUnit.Absolute,
172             EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
173             opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
174     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
175     ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
176     ep2.setVendorLicenseModelId(vlm1_id);
177     vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
178     Assert.fail();
179
180   }
181
182   @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
183       "license model with id vlm1_id has invalid date range.")
184   public void createWithSameStartExpiryDateTest() {
185
186     Set<OperationalScope> opScopeChoices;
187     opScopeChoices = new HashSet<>();
188     opScopeChoices.add(OperationalScope.Core);
189     opScopeChoices.add(OperationalScope.CPU);
190     opScopeChoices.add(OperationalScope.Network_Wide);
191     EntitlementPoolEntity ep2 =
192         createEntitlementPool("vlm4Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
193             ThresholdUnit.Absolute,
194             EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
195             opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
196     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
197     ep2.setStartDate(LocalDate.now().format(formatter));
198     ep2.setExpiryDate(LocalDate.now().format(formatter));
199     ep2.setVendorLicenseModelId(vlm1_id);
200     vendorLicenseManagerImpl.createEntitlementPool(ep2).getId();
201     Assert.fail();
202   }
203
204   @Test
205   public void testUpdate() {
206     Set<OperationalScope> opScopeChoices;
207     opScopeChoices = new HashSet<>();
208     opScopeChoices.add(OperationalScope.Core);
209     opScopeChoices.add(OperationalScope.CPU);
210     opScopeChoices.add(OperationalScope.Network_Wide);
211     EntitlementPoolEntity ep2 =
212         createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
213             ThresholdUnit
214                 .Absolute,
215             EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
216             opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
217     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
218     ep2.setStartDate(LocalDate.now().minusDays(3L).format(formatter));
219     ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
220
221     vendorLicenseManagerImpl.updateEntitlementPool(ep2);
222   }
223
224   @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Vendor " +
225       "license model with id vlm1_id has invalid date range.")
226   public void updateWithInvalidStartExpiryDateTest() {
227
228     Set<OperationalScope> opScopeChoices;
229     opScopeChoices = new HashSet<>();
230     opScopeChoices.add(OperationalScope.Core);
231     opScopeChoices.add(OperationalScope.CPU);
232     opScopeChoices.add(OperationalScope.Network_Wide);
233     EntitlementPoolEntity ep2 =
234         createEntitlementPool("vlm2Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
235             ThresholdUnit.Absolute,
236             EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
237             opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
238     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
239     ep2.setStartDate(LocalDate.now().format(formatter));
240     ep2.setExpiryDate(LocalDate.now().minusDays(2L).format(formatter));
241     ep2.setVendorLicenseModelId(vlm1_id);
242     vendorLicenseManagerImpl.updateEntitlementPool(ep2);
243     Assert.fail();
244
245   }
246
247   @Test
248   public void updateWithoutStartDateTest() {
249     try {
250
251       Set<OperationalScope> opScopeChoices;
252       opScopeChoices = new HashSet<>();
253       opScopeChoices.add(OperationalScope.Core);
254       opScopeChoices.add(OperationalScope.CPU);
255       opScopeChoices.add(OperationalScope.Network_Wide);
256       EntitlementPoolEntity ep2 =
257           createEntitlementPool("vlm3Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
258               ThresholdUnit.Absolute,
259               EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average,
260               null,
261               opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
262       DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
263       ep2.setExpiryDate(LocalDate.now().plusDays(2L).format(formatter));
264       vendorLicenseManagerImpl.updateEntitlementPool(ep2);
265       Assert.fail();
266     } catch (CoreException exception) {
267       Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
268     }
269   }
270
271   @Test
272   public void updateWithSameStartExpiryDateTest() {
273     try {
274
275       Set<OperationalScope> opScopeChoices;
276       opScopeChoices = new HashSet<>();
277       opScopeChoices.add(OperationalScope.Core);
278       opScopeChoices.add(OperationalScope.CPU);
279       opScopeChoices.add(OperationalScope.Network_Wide);
280       EntitlementPoolEntity ep2 =
281           createEntitlementPool("vlm4Id", null, ep1_id, EP1_NAME, "EP2 dec", 70,
282               ThresholdUnit.Absolute,
283               EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average,
284               null,
285               opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
286       DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
287       ep2.setStartDate(LocalDate.now().format(formatter));
288       ep2.setExpiryDate(LocalDate.now().format(formatter));
289       vendorLicenseManagerImpl.updateEntitlementPool(ep2);
290       Assert.fail();
291     } catch (CoreException exception) {
292       Assert.assertEquals(exception.code().id(), VendorLicenseErrorCodes.DATE_RANGE_INVALID);
293     }
294   }
295
296   @Test
297   public void deleteEntitlementPoolTest() {
298     Set<OperationalScope> opScopeChoices;
299     opScopeChoices = new HashSet<>();
300     opScopeChoices.add(OperationalScope.Core);
301     opScopeChoices.add(OperationalScope.CPU);
302     opScopeChoices.add(OperationalScope.Network_Wide);
303
304     EntitlementPoolEntity entitlementPool =
305         createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
306             ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2", "inc2",
307             AggregationFunction.Average, null,
308             opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
309     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
310     entitlementPool.setStartDate(LocalDate.now().format(formatter));
311     entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
312
313     doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
314
315     doNothing().when(vendorLicenseManagerImpl).deleteChildLimits(vlm1_id, VERSION01, ep1_id);
316
317     doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(anyObject(), anyObject(),
318         anyObject(), anyObject());
319
320     vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool);
321
322     verify(entitlementPoolDao).delete(entitlementPool);
323   }
324
325   @Test
326   public void testGetEntitlementPool() {
327     Set<OperationalScope> opScopeChoices;
328     opScopeChoices = new HashSet<>();
329     opScopeChoices.add(OperationalScope.Core);
330     opScopeChoices.add(OperationalScope.CPU);
331     opScopeChoices.add(OperationalScope.Network_Wide);
332
333     EntitlementPoolEntity entitlementPool =
334         createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP2 dec", 70,
335             ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2", "inc2",
336             AggregationFunction.Average, null,
337             opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
338     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy'T'HH:mm:ss'Z'");
339
340     entitlementPool.setStartDate(LocalDateTime.now().format(formatter));
341     entitlementPool.setExpiryDate(LocalDateTime.now().plusDays(1L).format(formatter));
342
343     doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
344
345     EntitlementPoolEntity retrived = vendorLicenseManagerImpl.getEntitlementPool(entitlementPool);
346
347     Assert.assertEquals(retrived.getId(), entitlementPool.getId());
348     Assert.assertEquals(retrived.getVendorLicenseModelId(),
349         entitlementPool.getVendorLicenseModelId());
350     Assert.assertEquals(retrived.getVersion(), entitlementPool.getVersion());
351   }
352
353   @Test
354   public void testListEntitlmentPool() {
355
356     Set<OperationalScope> opScopeChoices;
357     opScopeChoices = new HashSet<>();
358     opScopeChoices.add(OperationalScope.Core);
359     opScopeChoices.add(OperationalScope.CPU);
360     opScopeChoices.add(OperationalScope.Network_Wide);
361
362     doReturn(Arrays.asList(
363         createEntitlementPool(vlm1_id, VERSION01, ep1_id, EP1_NAME, "EP1 dec", 70,
364             ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric1",
365             "inc1", AggregationFunction.Average, null,
366             opScopeChoices, null, EntitlementTime.Other, "time1", "sku1"),
367         createEntitlementPool(vlm1_id, VERSION01, ep2_id, EP2_NAME, "EP2 dec", 70,
368             ThresholdUnit.Absolute, EntitlementMetric.Other, "exception metric2",
369             "inc2", AggregationFunction.Average, null,
370             opScopeChoices, null, EntitlementTime.Other, "time2", "sku2")))
371         .when(vendorLicenseFacade).listEntitlementPools(vlm1_id, VERSION01);
372
373     Collection<EntitlementPoolEntity> EPs =
374         vendorLicenseManagerImpl.listEntitlementPools(vlm1_id, VERSION01);
375
376     verify(vendorLicenseFacade).listEntitlementPools(vlm1_id, VERSION01);
377     Assert.assertEquals(EPs.size(), 2);
378     EPs.forEach(ep -> Assert.assertTrue(ep.getId().matches(ep1_id + "|" + ep2_id)));
379   }
380
381
382   /*  @Test
383     public void deleteEntitlementPoolTest() {
384         Set<OperationalScope> opScopeChoices;
385         opScopeChoices = new HashSet<>();
386         opScopeChoices.add(OperationalScope.Core);
387         opScopeChoices.add(OperationalScope.CPU);
388         opScopeChoices.add(OperationalScope.Network_Wide);
389
390         EntitlementPoolEntity entitlementPool =
391                 createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
392                         EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
393                         opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
394         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
395         entitlementPool.setStartDate(LocalDate.now().format(formatter));
396         entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
397
398         VersionInfo info = new VersionInfo();
399         Version version = new Version();
400         info.getViewableVersions().add(version);
401         info.setActiveVersion(version);
402         doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
403
404         LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
405                 "Core",AggregationFunction.Average,10,"Hour");
406
407         ArrayList<LimitEntity> limitEntityList = new ArrayList();
408         limitEntityList.add(limitEntity);
409
410         doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
411         doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
412         doReturn(true).when(limitDao).isLimitPresent(anyObject());
413         doReturn(limitEntity).when(limitDao).get(anyObject());
414         try {
415             Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
416             limitField.setAccessible(true);
417             Field modifiersField = Field.class.getDeclaredField("modifiers");
418             modifiersField.setAccessible(true);
419             modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
420             limitField.set(null, limitDao);
421
422             Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao");
423             epField.setAccessible(true);
424             modifiersField = Field.class.getDeclaredField("modifiers");
425             modifiersField.setAccessible(true);
426             modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
427             epField.set(null, entitlementPoolDao);
428         } catch(NoSuchFieldException | IllegalAccessException e)
429         {
430             Assert.fail();
431         }
432
433         vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
434
435         verify(limitDao).delete(anyObject());
436     }
437
438     @Test
439     public void deleteEntitlementPoolInvalidTest() {
440         try {
441             Set<OperationalScope> opScopeChoices;
442             opScopeChoices = new HashSet<>();
443             opScopeChoices.add(OperationalScope.Core);
444             opScopeChoices.add(OperationalScope.CPU);
445             opScopeChoices.add(OperationalScope.Network_Wide);
446
447             EntitlementPoolEntity entitlementPool =
448                 createEntitlementPool("vlm1Id", null, EP1_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
449                     EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
450                     opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
451             DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
452             entitlementPool.setStartDate(LocalDate.now().format(formatter));
453             entitlementPool.setExpiryDate(LocalDate.now().plusDays(1L).format(formatter));
454
455             VersionInfo info = new VersionInfo();
456             Version version = new Version();
457             info.getViewableVersions().add(version);
458             info.setActiveVersion(version);
459             doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
460
461             LimitEntity limitEntity = LimitTest.createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
462                 "Core",AggregationFunction.Average,10,"Hour");
463
464             ArrayList<LimitEntity> limitEntityList = new ArrayList();
465             limitEntityList.add(limitEntity);
466
467             doReturn(entitlementPool).when(entitlementPoolDao).get(anyObject());
468             doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
469             doReturn(false).when(limitDao).isLimitPresent(anyObject());
470
471             try {
472                 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
473                 limitField.setAccessible(true);
474                 Field modifiersField = Field.class.getDeclaredField("modifiers");
475                 modifiersField.setAccessible(true);
476                 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
477                 limitField.set(null, limitDao);
478
479                 Field epField = VendorLicenseManagerImpl.class.getDeclaredField("entitlementPoolDao");
480                 epField.setAccessible(true);
481                 modifiersField = Field.class.getDeclaredField("modifiers");
482                 modifiersField.setAccessible(true);
483                 modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
484                 epField.set(null, entitlementPoolDao);
485             } catch(NoSuchFieldException | IllegalAccessException e)
486             {
487                 Assert.fail();
488             }
489
490             vendorLicenseManagerImpl.deleteEntitlementPool(entitlementPool, USER1);
491         } catch (CoreException exception) {
492             Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
493         }
494     } */
495
496  /* private static final String USER1 = "epTestUser1";
497   private static final String USER2 = "epTestUser2";
498   private static final String EP1_V01_DESC = "EP1 desc";
499   private static final Version VERSION01 = new Version(0, 1);
500   private static final Version VERSION03 = new Version(0, 3);
501   private static final String EP1_NAME = "EP1 name";
502   private static final String EP2_NAME = "EP2 name";
503
504   private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
505   private static EntitlementPoolDao entitlementPoolDao;
506
507   private static String vlm1Id;
508   private static String vlm2Id;
509   private static String ep1Id;
510   private static String ep2Id;
511
512   public static EntitlementPoolEntity createEntitlementPool(String vlmId, Version version,
513                                                             String name, String desc, int threshold,
514                                                             ThresholdUnit thresholdUnit,
515                                                             EntitlementMetric entitlementMetricChoice,
516                                                             String entitlementMetricOther,
517                                                             String increments,
518                                                             AggregationFunction aggregationFunctionChoice,
519                                                             String aggregationFunctionOther,
520                                                             Set<OperationalScope> operationalScopeChoices,
521                                                             String operationalScopeOther,
522                                                             EntitlementTime timeChoice,
523                                                             String timeOther, String sku) {
524     EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity();
525     entitlementPool.setVendorLicenseModelId(vlmId);
526     entitlementPool.setVersion(version);
527     entitlementPool.setName(name);
528     entitlementPool.setDescription(desc);
529     entitlementPool.setThresholdValue(threshold);
530     entitlementPool.setThresholdUnit(thresholdUnit);
531     entitlementPool
532         .setEntitlementMetric(new ChoiceOrOther<>(entitlementMetricChoice, entitlementMetricOther));
533     entitlementPool.setIncrements(increments);
534     entitlementPool.setAggregationFunction(
535         new ChoiceOrOther<>(aggregationFunctionChoice, aggregationFunctionOther));
536     entitlementPool.setOperationalScope(
537         new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
538     entitlementPool.setTime(new ChoiceOrOther<>(timeChoice, timeOther));
539     return entitlementPool;
540   }
541
542   private static void assertEntitlementPoolsEquals(EntitlementPoolEntity actual,
543                                                    EntitlementPoolEntity expected) {
544     Assert.assertEquals(actual.getVendorLicenseModelId(), expected.getVendorLicenseModelId());
545     Assert.assertEquals(actual.getVersion(), expected.getVersion());
546     Assert.assertEquals(actual.getId(), expected.getId());
547     Assert.assertEquals(actual.getName(), expected.getName());
548     Assert.assertEquals(actual.getDescription(), expected.getDescription());
549     Assert.assertEquals(actual.getThresholdValue(), expected.getThresholdValue());
550     Assert.assertEquals(actual.getThresholdUnit(), expected.getThresholdUnit());
551     Assert.assertEquals(actual.getEntitlementMetric(), expected.getEntitlementMetric());
552     Assert.assertEquals(actual.getIncrements(), expected.getIncrements());
553     Assert.assertEquals(actual.getAggregationFunction(), expected.getAggregationFunction());
554     Assert.assertEquals(actual.getOperationalScope(), expected.getOperationalScope());
555     Assert.assertEquals(actual.getTime(), expected.getTime());
556   }
557
558   @BeforeClass
559   private void init() {
560     entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
561     vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
562             .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1 dec", "icon1"),
563         USER1).getId();
564     vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
565             .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
566         USER1).getId();
567   }
568
569   @Test
570   public void emptyListTest() {
571     Collection<EntitlementPoolEntity> entitlementPools =
572         vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
573     Assert.assertEquals(entitlementPools.size(), 0);
574   }
575
576   @Test(dependsOnMethods = "emptyListTest")
577   public void createTest() {
578     ep1Id = testCreate(vlm1Id, EP1_NAME);
579
580     Set<OperationalScope> opScopeChoices;
581     opScopeChoices = new HashSet<>();
582     opScopeChoices.add(OperationalScope.Core);
583     opScopeChoices.add(OperationalScope.CPU);
584     opScopeChoices.add(OperationalScope.Network_Wide);
585     EntitlementPoolEntity ep2 =
586         createEntitlementPool(vlm1Id, null, EP2_NAME, "EP2 dec", 70, ThresholdUnit.Absolute,
587             EntitlementMetric.Other, "exception metric2", "inc2", AggregationFunction.Average, null,
588             opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
589     ep2Id = vendorLicenseManager.createEntitlementPool(ep2, USER1).getId();
590     ep2.setId(ep2Id);
591   }
592
593   private String testCreate(String vlmId, String name) {
594     Set<OperationalScope> opScopeChoices = new HashSet<>();
595     opScopeChoices.add(OperationalScope.Other);
596     EntitlementPoolEntity ep1 =
597         createEntitlementPool(vlmId, null, name, EP1_V01_DESC, 80, ThresholdUnit.Percentage,
598             EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
599             opScopeChoices, "op scope1", EntitlementTime.Other, "time1", "sku1");
600     String ep1Id = vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
601     ep1.setId(ep1Id);
602
603     EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(ep1);
604     Assert.assertTrue(loadedEp1.equals(ep1));
605     return ep1Id;
606   }
607
608   @Test(dependsOnMethods = {"createTest"})
609   public void testCreateWithExistingName_negative() {
610     testCreateWithExistingName_negative(vlm1Id, EP1_NAME);
611   }
612
613   @Test(dependsOnMethods = {"createTest"})
614   public void testCreateWithExistingNameUnderOtherVlm() {
615     testCreate(vlm2Id, EP1_NAME);
616   }
617
618   @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
619   public void updateAndGetTest() {
620     EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, VERSION01, ep1Id);
621
622     EntitlementPoolEntity ep1 = entitlementPoolDao.get(emptyEp1);
623     ep1.setEntitlementMetric(new ChoiceOrOther<>(EntitlementMetric.Other, "exception metric1 updated"));
624     ep1.setAggregationFunction(new ChoiceOrOther<>(AggregationFunction.Other, "agg func1 updated"));
625
626     vendorLicenseManager.updateEntitlementPool(ep1, USER1);
627
628     EntitlementPoolEntity loadedEp1 = vendorLicenseManager.getEntitlementPool(emptyEp1, USER1);
629     assertEntitlementPoolsEquals(loadedEp1, ep1);
630   }
631
632   @Test(dependsOnMethods = {"updateAndGetTest"})
633   public void testGetNonExistingVersion_negative() {
634     try {
635       vendorLicenseManager
636           .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(48, 83), ep1Id), USER1);
637       Assert.assertTrue(false);
638     } catch (CoreException exception) {
639       Assert.assertEquals(exception.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
640     }
641   }
642
643   @Test(dependsOnMethods = {"updateAndGetTest"})
644   public void testGetOtherUserCandidateVersion_negative() {
645     vendorLicenseManager.checkin(vlm1Id, USER1);
646     vendorLicenseManager.checkout(vlm1Id, USER2);
647     try {
648       vendorLicenseManager
649           .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id), USER1);
650       Assert.assertTrue(false);
651     } catch (CoreException exception) {
652       Assert.assertEquals(exception.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
653     }
654   }
655
656   @Test(dependsOnMethods = {"testGetOtherUserCandidateVersion_negative"})
657   public void testGetCandidateVersion() {
658     EntitlementPoolEntity ep = new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id);
659     ep.setDescription("updated!");
660     vendorLicenseManager.updateEntitlementPool(ep, USER2);
661
662     EntitlementPoolEntity actualEp = vendorLicenseManager.getEntitlementPool(ep, USER2);
663     EntitlementPoolEntity expectedEp = entitlementPoolDao.get(ep);
664
665     Assert.assertEquals(actualEp.getDescription(), ep.getDescription());
666     assertEntitlementPoolsEquals(actualEp, expectedEp);
667   }
668
669   @Test(dependsOnMethods = {"testGetCandidateVersion"})
670   public void testGetOldVersion() {
671     vendorLicenseManager.checkin(vlm1Id, USER2);
672     EntitlementPoolEntity actualEp = vendorLicenseManager
673         .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 1), ep1Id), USER2);
674     Assert.assertEquals(actualEp.getDescription(), EP1_V01_DESC);
675   }
676
677   @Test(dependsOnMethods = {"testGetOldVersion"})
678   public void listTest() {
679     Collection<EntitlementPoolEntity> loadedEps =
680         vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
681     Assert.assertEquals(loadedEps.size(), 2);
682
683     int existingCounter = 0;
684     for (EntitlementPoolEntity loadedEp : loadedEps) {
685       if (ep2Id.equals(loadedEp.getId()) || ep1Id.equals(loadedEp.getId())) {
686         existingCounter++;
687       }
688     }
689
690     Assert.assertEquals(existingCounter, 2);
691   }
692
693   @Test(dependsOnMethods = {"listTest"})
694   public void deleteTest() {
695     vendorLicenseManager.checkout(vlm1Id, USER1);
696     EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, null, ep1Id);
697     vendorLicenseManager.deleteEntitlementPool(emptyEp1, USER1);
698
699     emptyEp1.setVersion(VERSION03);
700     EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(emptyEp1);
701     Assert.assertEquals(loadedEp1, null);
702
703     Collection<EntitlementPoolEntity> loadedEps =
704         entitlementPoolDao.list(new EntitlementPoolEntity(vlm1Id, VERSION03, null));
705     Assert.assertEquals(loadedEps.size(), 1);
706     Assert.assertEquals(loadedEps.iterator().next().getId(), ep2Id);
707   }
708
709   @Test(dependsOnMethods = "deleteTest")
710   public void listOldVersionTest() {
711     Collection<EntitlementPoolEntity> loadedEps =
712         vendorLicenseManager.listEntitlementPools(vlm1Id, VERSION01, USER1);
713     Assert.assertEquals(loadedEps.size(), 2);
714   }
715
716   @Test(dependsOnMethods = "deleteTest")
717   public void testCreateWithRemovedName() {
718     testCreate(vlm1Id, EP1_NAME);
719   }
720
721   @Test(dependsOnMethods = "deleteTest")
722   public void testCreateWithExistingNameAfterCheckout_negative() {
723     testCreateWithExistingName_negative(vlm1Id, EP2_NAME);
724   }
725
726   private void testCreateWithExistingName_negative(String vlmId, String epName) {
727     try {
728       EntitlementPoolEntity ep1 =
729           createEntitlementPool(vlmId, null, epName, EP1_V01_DESC, 80, ThresholdUnit.Percentage,
730               EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
731               Collections.singleton(OperationalScope.Other), "op scope1", EntitlementTime.Other,
732               "time1", "sku1");
733       vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
734       Assert.fail();
735     } catch (CoreException exception) {
736       Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
737     }
738   }
739 */
740 }
741