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