2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.sdc.vendorlicense;
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;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.HashSet;
49 public class EntitlementPoolTest {
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";
59 private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
60 private static EntitlementPoolDao entitlementPoolDao;
62 private static String vlm1Id;
63 private static String vlm2Id;
64 private static String ep1Id;
65 private static String ep2Id;
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,
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);
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;
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());
117 private void init() {
118 entitlementPoolDao = EntitlementPoolDaoFactory.getInstance().createInterface();
119 vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
120 .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1 dec", "icon1"),
122 vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
123 .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
128 public void emptyListTest() {
129 Collection<EntitlementPoolEntity> entitlementPools =
130 vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
131 Assert.assertEquals(entitlementPools.size(), 0);
134 @Test(dependsOnMethods = "emptyListTest")
135 public void createTest() {
136 ep1Id = testCreate(vlm1Id, EP1_NAME);
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();
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();
161 EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(ep1);
162 Assert.assertTrue(loadedEp1.equals(ep1));
166 @Test(dependsOnMethods = {"createTest"})
167 public void testCreateWithExistingName_negative() {
168 testCreateWithExistingName_negative(vlm1Id, EP1_NAME);
171 @Test(dependsOnMethods = {"createTest"})
172 public void testCreateWithExistingNameUnderOtherVlm() {
173 testCreate(vlm2Id, EP1_NAME);
176 @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
177 public void updateAndGetTest() {
178 EntitlementPoolEntity emptyEp1 = new EntitlementPoolEntity(vlm1Id, VERSION01, ep1Id);
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"));
184 vendorLicenseManager.updateEntitlementPool(ep1, USER1);
186 EntitlementPoolEntity loadedEp1 = vendorLicenseManager.getEntitlementPool(emptyEp1, USER1);
187 assertEntitlementPoolsEquals(loadedEp1, ep1);
190 @Test(dependsOnMethods = {"updateAndGetTest"})
191 public void testGetNonExistingVersion_negative() {
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);
201 @Test(dependsOnMethods = {"updateAndGetTest"})
202 public void testGetOtherUserCandidateVersion_negative() {
203 vendorLicenseManager.checkin(vlm1Id, USER1);
204 vendorLicenseManager.checkout(vlm1Id, USER2);
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);
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);
220 EntitlementPoolEntity actualEp = vendorLicenseManager.getEntitlementPool(ep, USER2);
221 EntitlementPoolEntity expectedEp = entitlementPoolDao.get(ep);
223 Assert.assertEquals(actualEp.getDescription(), ep.getDescription());
224 assertEntitlementPoolsEquals(actualEp, expectedEp);
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);
235 @Test(dependsOnMethods = {"testGetOldVersion"})
236 public void listTest() {
237 Collection<EntitlementPoolEntity> loadedEps =
238 vendorLicenseManager.listEntitlementPools(vlm1Id, null, USER1);
239 Assert.assertEquals(loadedEps.size(), 2);
241 int existingCounter = 0;
242 for (EntitlementPoolEntity loadedEp : loadedEps) {
243 if (ep2Id.equals(loadedEp.getId()) || ep1Id.equals(loadedEp.getId())) {
248 Assert.assertEquals(existingCounter, 2);
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);
257 emptyEp1.setVersion(VERSION03);
258 EntitlementPoolEntity loadedEp1 = entitlementPoolDao.get(emptyEp1);
259 Assert.assertEquals(loadedEp1, null);
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);
267 @Test(dependsOnMethods = "deleteTest")
268 public void listOldVersionTest() {
269 Collection<EntitlementPoolEntity> loadedEps =
270 vendorLicenseManager.listEntitlementPools(vlm1Id, VERSION01, USER1);
271 Assert.assertEquals(loadedEps.size(), 2);
274 @Test(dependsOnMethods = "deleteTest")
275 public void testCreateWithRemovedName() {
276 testCreate(vlm1Id, EP1_NAME);
279 @Test(dependsOnMethods = "deleteTest")
280 public void testCreateWithExistingNameAfterCheckout_negative() {
281 testCreateWithExistingName_negative(vlm1Id, EP2_NAME);
284 private void testCreateWithExistingName_negative(String vlmId, String epName) {
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,
291 vendorLicenseManager.createEntitlementPool(ep1, USER1).getId();
293 } catch (CoreException exception) {
294 Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);