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