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