push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / test / java / org / openecomp / sdc / vendorlicense / EntitlementPoolTest.java
1 package org.openecomp.sdc.vendorlicense;
2
3 import org.openecomp.sdc.common.errors.CoreException;
4 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
5 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
6 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
7 import org.openecomp.sdc.versioning.dao.types.Version;
8 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
9 import org.openecomp.core.util.UniqueValueUtil;
10 import org.openecomp.core.utilities.CommonMethods;
11
12 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
13 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric;
14 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
15 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime;
16 import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
17 import org.testng.Assert;
18 import org.testng.annotations.BeforeClass;
19 import org.testng.annotations.Test;
20
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.Set;
25
26 public class EntitlementPoolTest {
27
28   private static final String USER1 = "epTestUser1";
29   private static final String USER2 = "epTestUser2";
30   private static final String EP1_V01_DESC = "EP1 desc";
31   private static final Version VERSION01 = new Version(0, 1);
32   private static final Version VERSION03 = new Version(0, 3);
33   private static final String EP1_NAME = "EP1 name";
34   private static final String EP2_NAME = "EP2 name";
35
36   private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
37   private static EntitlementPoolDao entitlementPoolDao;
38
39   private static String vlm1Id;
40   private static String vlm2Id;
41   private static String ep1Id;
42   private static String ep2Id;
43
44   public static EntitlementPoolEntity createEntitlementPool(String vlmId, Version version,
45                                                             String name, String desc, int threshold,
46                                                             org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit thresholdUnit,
47                                                             EntitlementMetric entitlementMetricChoice,
48                                                             String entitlementMetricOther,
49                                                             String increments,
50                                                             AggregationFunction aggregationFunctionChoice,
51                                                             String aggregationFunctionOther,
52                                                             Set<OperationalScope> operationalScopeChoices,
53                                                             String operationalScopeOther,
54                                                             EntitlementTime timeChoice,
55                                                             String timeOther, String sku) {
56     EntitlementPoolEntity entitlementPool = new EntitlementPoolEntity();
57     entitlementPool.setVendorLicenseModelId(vlmId);
58     entitlementPool.setVersion(version);
59     entitlementPool.setName(name);
60     entitlementPool.setDescription(desc);
61     entitlementPool.setThresholdValue(threshold);
62     entitlementPool.setThresholdUnit(thresholdUnit);
63     entitlementPool
64         .setEntitlementMetric(new org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther<>(entitlementMetricChoice, entitlementMetricOther));
65     entitlementPool.setIncrements(increments);
66     entitlementPool.setAggregationFunction(
67         new org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther<>(aggregationFunctionChoice, aggregationFunctionOther));
68     entitlementPool.setOperationalScope(
69         new org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
70     entitlementPool.setTime(new org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther<>(timeChoice, timeOther));
71     entitlementPool.setManufacturerReferenceNumber(sku);
72     return entitlementPool;
73   }
74
75   private static void assertEntitlementPoolsEquals(EntitlementPoolEntity actual,
76                                                    EntitlementPoolEntity expected) {
77     Assert.assertEquals(actual.getVendorLicenseModelId(), expected.getVendorLicenseModelId());
78     Assert.assertEquals(actual.getVersion(), expected.getVersion());
79     Assert.assertEquals(actual.getId(), expected.getId());
80     Assert.assertEquals(actual.getName(), expected.getName());
81     Assert.assertEquals(actual.getDescription(), expected.getDescription());
82     Assert.assertEquals(actual.getThresholdValue(), expected.getThresholdValue());
83     Assert.assertEquals(actual.getThresholdUnit(), expected.getThresholdUnit());
84     Assert.assertEquals(actual.getEntitlementMetric(), expected.getEntitlementMetric());
85     Assert.assertEquals(actual.getIncrements(), expected.getIncrements());
86     Assert.assertEquals(actual.getAggregationFunction(), expected.getAggregationFunction());
87     Assert.assertEquals(actual.getOperationalScope(), expected.getOperationalScope());
88     Assert.assertEquals(actual.getTime(), expected.getTime());
89     Assert.assertEquals(actual.getManufacturerReferenceNumber(),
90         expected.getManufacturerReferenceNumber());
91   }
92
93   @BeforeClass
94   private void init() {
95     entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
96     vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
97             .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1 dec", "icon1"),
98         USER1).getId();
99     vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
100             .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
101         USER1).getId();
102   }
103
104   @Test
105   public void emptyListTest() {
106     Collection<EntitlementPoolEntity> entitlementPools =
107         vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
108     Assert.assertEquals(entitlementPools.size(), 0);
109   }
110
111   @Test(dependsOnMethods = "emptyListTest")
112   public void createTest() {
113     ep1Id = testCreate(vlm1Id, EP1_NAME);
114
115     Set<OperationalScope> opScopeChoices;
116     opScopeChoices = new HashSet<>();
117     opScopeChoices.add(OperationalScope.Core);
118     opScopeChoices.add(OperationalScope.CPU);
119     opScopeChoices.add(OperationalScope.Network_Wide);
120     EntitlementPoolEntity ep2 =
121         createEntitlementPool(vlm1Id, null, EP2_NAME, "EP2 dec", 70, org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit.Absolute,
122             EntitlementMetric.Other, "e metric2", "inc2", AggregationFunction.Average, null,
123             opScopeChoices, null, EntitlementTime.Other, "time2", "sku2");
124     ep2Id = vendorLicenseManager.createEntitlementPool(ep2, USER1).getId();
125     ep2.setId(ep2Id);
126   }
127
128   private String testCreate(String vlmId, String name) {
129     Set<OperationalScope> opScopeChoices = new HashSet<>();
130     opScopeChoices.add(OperationalScope.Other);
131     EntitlementPoolEntity ep1 =
132         createEntitlementPool(vlmId, null, name, EP1_V01_DESC, 80, org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit.Percentage,
133             EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
134             opScopeChoices, "op scope1", EntitlementTime.Other, "time1", "sku1");
135     String ep1Id = vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
136     ep1.setId(ep1Id);
137
138     EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(ep1);
139     Assert.assertTrue(loadedEp1.equals(ep1));
140     return ep1Id;
141   }
142
143   @Test(dependsOnMethods = {"createTest"})
144   public void testCreateWithExistingName_negative() {
145     testCreateWithExistingName_negative(vlm1Id, EP1_NAME);
146   }
147
148   @Test(dependsOnMethods = {"createTest"})
149   public void testCreateWithExistingNameUnderOtherVlm() {
150     testCreate(vlm2Id, EP1_NAME);
151   }
152
153   @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
154   public void updateAndGetTest() {
155     EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, VERSION01, ep1Id);
156
157     EntitlementPoolEntity ep1 = entitlementPoolDao.get(emptyEp1);
158     ep1.setEntitlementMetric(new org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther<>(EntitlementMetric.Other, "e metric1 updated"));
159     ep1.setAggregationFunction(new org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther<>(AggregationFunction.Other, "agg func1 updated"));
160
161     vendorLicenseManager.updateEntitlementPool(ep1, USER1);
162
163     EntitlementPoolEntity loadedEp1 = vendorLicenseManager.getEntitlementPool(emptyEp1, USER1);
164     assertEntitlementPoolsEquals(loadedEp1, ep1);
165   }
166
167   @Test(dependsOnMethods = {"updateAndGetTest"})
168   public void testGetNonExistingVersion_negative() {
169     try {
170       vendorLicenseManager
171           .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(48, 83), ep1Id), USER1);
172       Assert.assertTrue(false);
173     } catch (CoreException e) {
174       Assert.assertEquals(e.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
175     }
176   }
177
178   @Test(dependsOnMethods = {"updateAndGetTest"})
179   public void testGetOtherUserCandidateVersion_negative() {
180     vendorLicenseManager.checkin(vlm1Id, USER1);
181     vendorLicenseManager.checkout(vlm1Id, USER2);
182     try {
183       vendorLicenseManager
184           .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id), USER1);
185       Assert.assertTrue(false);
186     } catch (CoreException e) {
187       Assert.assertEquals(e.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
188     }
189   }
190
191   @Test(dependsOnMethods = {"testGetOtherUserCandidateVersion_negative"})
192   public void testGetCandidateVersion() {
193     EntitlementPoolEntity ep = new EntitlementPoolEntity(vlm1Id, new Version(0, 2), ep1Id);
194     ep.setDescription("updated!");
195     vendorLicenseManager.updateEntitlementPool(ep, USER2);
196
197     EntitlementPoolEntity actualEp = vendorLicenseManager.getEntitlementPool(ep, USER2);
198     EntitlementPoolEntity expectedEp = entitlementPoolDao.get(ep);
199
200     Assert.assertEquals(actualEp.getDescription(), ep.getDescription());
201     assertEntitlementPoolsEquals(actualEp, expectedEp);
202   }
203
204   @Test(dependsOnMethods = {"testGetCandidateVersion"})
205   public void testGetOldVersion() {
206     vendorLicenseManager.checkin(vlm1Id, USER2);
207     EntitlementPoolEntity actualEp = vendorLicenseManager
208         .getEntitlementPool(new EntitlementPoolEntity(vlm1Id, new Version(0, 1), ep1Id), USER2);
209     Assert.assertEquals(actualEp.getDescription(), EP1_V01_DESC);
210   }
211
212   @Test(dependsOnMethods = {"testGetOldVersion"})
213   public void listTest() {
214     Collection<EntitlementPoolEntity> loadedEps =
215         vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
216     Assert.assertEquals(loadedEps.size(), 2);
217
218     int existingCounter = 0;
219     for (EntitlementPoolEntity loadedEp : loadedEps) {
220       if (ep2Id.equals(loadedEp.getId()) || ep1Id.equals(loadedEp.getId())) {
221         existingCounter++;
222       }
223     }
224
225     Assert.assertEquals(existingCounter, 2);
226   }
227
228   @Test(dependsOnMethods = {"listTest"})
229   public void deleteTest() {
230     vendorLicenseManager.checkout(vlm1Id, USER1);
231     EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, null, ep1Id);
232     vendorLicenseManager.deleteEntitlementPool(emptyEp1, USER1);
233
234     emptyEp1.setVersion(VERSION03);
235     EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(emptyEp1);
236     Assert.assertEquals(loadedEp1, null);
237
238     Collection<EntitlementPoolEntity> loadedEps =
239         entitlementPoolDao.list(new EntitlementPoolEntity(vlm1Id, VERSION03, null));
240     Assert.assertEquals(loadedEps.size(), 1);
241     Assert.assertEquals(loadedEps.iterator().next().getId(), ep2Id);
242   }
243
244   @Test(dependsOnMethods = "deleteTest")
245   public void listOldVersionTest() {
246     Collection<EntitlementPoolEntity> loadedEps =
247         vendorLicenseManager.listEntitlementPools(vlm1Id, VERSION01, USER1);
248     Assert.assertEquals(loadedEps.size(), 2);
249   }
250
251   @Test(dependsOnMethods = "deleteTest")
252   public void testCreateWithRemovedName() {
253     testCreate(vlm1Id, EP1_NAME);
254   }
255
256   @Test(dependsOnMethods = "deleteTest")
257   public void testCreateWithExistingNameAfterCheckout_negative() {
258     testCreateWithExistingName_negative(vlm1Id, EP2_NAME);
259   }
260
261   private void testCreateWithExistingName_negative(String vlmId, String epName) {
262     try {
263       EntitlementPoolEntity ep1 =
264           createEntitlementPool(vlmId, null, epName, EP1_V01_DESC, 80, org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit.Percentage,
265               EntitlementMetric.Core, null, "inc1", AggregationFunction.Other, "agg func1",
266               Collections.singleton(OperationalScope.Other), "op scope1", EntitlementTime.Other,
267               "time1", "sku1");
268       vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
269       Assert.fail();
270     } catch (CoreException e) {
271       Assert.assertEquals(e.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
272     }
273   }
274
275 }