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=========================================================
21 package org.openecomp.sdc.vendorlicense;
23 import static org.mockito.Matchers.anyObject;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.verify;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.mockito.Spy;
31 import org.openecomp.sdc.common.errors.CoreException;
32 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
33 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
34 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric;
35 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime;
36 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
37 import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
38 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes;
39 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
40 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
41 import org.openecomp.sdc.versioning.dao.types.Version;
42 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
43 import org.openecomp.sdc.versioning.types.VersionInfo;
44 import org.testng.Assert;
45 import org.testng.annotations.BeforeMethod;
46 import org.testng.annotations.Test;
48 import java.lang.reflect.Field;
49 import java.lang.reflect.Modifier;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.Collection;
53 import java.util.List;
55 import static org.mockito.Mockito.when;
57 public class LimitTest {
59 private final String USER1 = "limitTestUser1";
60 private final String LT1_NAME = "LT1 name";
62 private static final String VLM_ID = "VLM_ID";
63 private static final Version VERSION = new Version(0, 1);
64 private static final String EPLKG_ID = "ID";
65 private static final String LIMIT1_ID = "limit1";
66 private static final String LIMIT2_ID = "limit2";
69 private VendorLicenseFacade vendorLicenseFacade;
72 private LimitDao limitDao;
76 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
78 public static LimitEntity createLimitEntity(String name, LimitType type, String description,
79 Version version, EntitlementMetric metric,
80 AggregationFunction aggregationFunction, int unit,
81 EntitlementTime time) {
82 LimitEntity limitEntity = new LimitEntity();
83 limitEntity.setName(name);
84 limitEntity.setType(type);
85 limitEntity.setDescription(description);
86 limitEntity.setVersion(version);
87 limitEntity.setMetric(metric);
88 limitEntity.setAggregationFunction(aggregationFunction);
89 limitEntity.setUnit(unit);
90 limitEntity.setTime(time);
95 public void setUp() throws Exception {
96 MockitoAnnotations.initMocks(this);
98 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
99 limitField.setAccessible(true);
100 Field modifiersField = Field.class.getDeclaredField("modifiers");
101 modifiersField.setAccessible(true);
102 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
103 limitField.set(null, limitDao);
104 } catch(NoSuchFieldException | IllegalAccessException e)
111 public void testUpdateLimit() {
112 Version version = new Version();
113 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
114 EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
115 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
116 EntitlementMetric.Tokens,AggregationFunction.Peak,12,EntitlementTime.Month);
117 VersionInfo info = new VersionInfo();
118 info.getViewableVersions().add(version);
119 info.setActiveVersion(version);
121 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
122 doReturn(true).when(limitDao).isLimitPresent(anyObject());
123 doReturn(limitEntity1).when(limitDao).get(anyObject());
125 List<LimitEntity> limitEntityList = new ArrayList<>();
126 limitEntityList.add(limitEntity1);
127 limitEntityList.add(limitEntity2);
128 limitEntity1.setId("1234");
129 limitEntity2.setId("1234");
130 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(),anyObject(),
131 anyObject(),anyObject());
133 vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
135 verify(vendorLicenseFacade).updateLimit(anyObject(), anyObject());
139 public void testUpdateLimitErrorWithSameNameType() {
141 Version version = new Version();
142 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
143 EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
144 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
145 EntitlementMetric.Tokens,AggregationFunction.Peak,12,EntitlementTime.Month);
146 VersionInfo info = new VersionInfo();
147 info.getViewableVersions().add(version);
148 info.setActiveVersion(version);
150 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
151 doReturn(limitEntity1).when(limitDao).get(anyObject());
153 List<LimitEntity> limitEntityList = new ArrayList<>();
154 limitEntityList.add(limitEntity1);
155 limitEntityList.add(limitEntity2);
156 limitEntity1.setId("1234");
157 limitEntity2.setId("9632");
158 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(),anyObject(),
159 anyObject(),anyObject());
161 vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
163 } catch (CoreException exception) {
164 Assert.assertEquals(exception.code().id(),
165 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
170 public void testDeleteLimit() {
171 Version version = new Version();
172 LimitEntity limitEntity = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
173 EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
174 VersionInfo info = new VersionInfo();
175 info.getViewableVersions().add(version);
176 info.setActiveVersion(version);
178 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
179 doReturn(true).when(limitDao).isLimitPresent(anyObject());
180 doReturn(limitEntity).when(limitDao).get(anyObject());
182 List<LimitEntity> limitEntityList = new ArrayList<>();
183 limitEntityList.add(limitEntity);
184 limitEntity.setId("1234");
186 vendorLicenseManagerImpl.deleteLimit(limitEntity,LT1_NAME);
188 verify(vendorLicenseManagerImpl).deleteLimit(anyObject(), anyObject());
192 public void testUpdateLimitErrorWithInvalidId() {
194 Version version = new Version();
195 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
196 EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
197 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
198 EntitlementMetric.Tokens,AggregationFunction.Peak,12,EntitlementTime.Month);
199 VersionInfo info = new VersionInfo();
200 info.getViewableVersions().add(version);
201 info.setActiveVersion(version);
203 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
204 doReturn(null).when(limitDao).get(anyObject());
206 vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
208 } catch (CoreException exception) {
209 Assert.assertEquals(exception.code().id(),
210 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
215 public void testList() {
216 doReturn(Arrays.asList(
217 createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID),
218 createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID)))
219 .when(vendorLicenseFacade).listLimits(VLM_ID, VERSION, EPLKG_ID, USER1);
221 final Collection<LimitEntity> limits =
222 vendorLicenseManagerImpl.listLimits(VLM_ID, VERSION, EPLKG_ID, USER1);
223 Assert.assertEquals(limits.size(), 2);
224 for (LimitEntity limit : limits) {
225 Assert.assertEquals(limit.getName(),
226 LIMIT1_ID.equals(limit.getId()) ? LIMIT1_ID+" name" : LIMIT2_ID+" name" );
231 public void testCreateLimit() {
232 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
233 VersionInfo info = new VersionInfo();
234 info.getViewableVersions().add(VERSION);
235 info.setActiveVersion(VERSION);
237 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
239 vendorLicenseManagerImpl.createLimit(expected, USER1);
240 verify(vendorLicenseFacade).createLimit(expected,USER1);
244 public void testCreateWithDuplicateName() {
245 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
246 expected.setType(LimitType.Vendor);
248 LimitEntity expectedDiffName = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID);
249 expectedDiffName.setName(LIMIT1_ID + " name");
250 expectedDiffName.setType(LimitType.Vendor);
252 List<LimitEntity> vfcImageList = new ArrayList<LimitEntity>();
253 vfcImageList.add(expectedDiffName);
254 doReturn(vfcImageList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(),
257 VersionInfo info = new VersionInfo();
258 info.getViewableVersions().add(VERSION);
259 info.setActiveVersion(VERSION);
261 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
264 vendorLicenseManagerImpl.createLimit(expected, USER1);
267 catch (CoreException ex) {
268 Assert.assertEquals(ex.code().id(),
269 VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED);
274 public void testGetNonExistingLimitId_negative() {
275 LimitEntity limit = createLimit(VLM_ID, VERSION, EPLKG_ID, "non existing limit id");
276 VersionInfo info = new VersionInfo();
277 info.getViewableVersions().add(VERSION);
278 info.setActiveVersion(VERSION);
280 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
283 vendorLicenseManagerImpl.getLimit(limit , USER1);
285 } catch (CoreException exception) {
286 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
291 public void testGet() {
292 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
293 expected.setType(LimitType.Vendor);
294 expected.setValue(100);
295 expected.setUnit(10);
296 expected.setAggregationFunction(AggregationFunction.Average);
297 expected.setMetric(EntitlementMetric.CPU);
298 expected.setTime(EntitlementTime.Day);
300 doReturn(true).when(limitDao).isLimitPresent(anyObject());
301 doReturn(expected).when(limitDao).get(anyObject());
302 VersionInfo info = new VersionInfo();
303 info.getViewableVersions().add(VERSION);
304 info.setActiveVersion(VERSION);
306 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
308 LimitEntity actual = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
309 vendorLicenseManagerImpl.getLimit(actual, USER1);
310 Assert.assertEquals(actual.getId(), expected.getId());
311 Assert.assertEquals(actual.getName(), expected.getName());
312 Assert.assertEquals(actual.getUnit(), expected.getUnit());
313 Assert.assertEquals(actual.getValue(), expected.getValue());
314 Assert.assertEquals(actual.getAggregationFunction().name(), expected.getAggregationFunction()
316 Assert.assertEquals(actual.getMetric().name(), expected.getMetric().name());
320 static LimitEntity createLimit(String vlmId, Version version, String epLkgId, String limitId) {
321 LimitEntity limitEntity = new LimitEntity(vlmId, version, epLkgId, limitId);
322 limitEntity.setName(limitId + " name");
323 limitEntity.setDescription(limitId + " desc");
324 limitEntity.setVersion(version);
325 limitEntity.setMetric(EntitlementMetric.CPU);
326 limitEntity.setAggregationFunction(AggregationFunction.Average);
327 limitEntity.setUnit(10);
328 limitEntity.setTime(EntitlementTime.Day);
329 limitEntity.setValue(100);