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