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