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