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, String metric,
80 AggregationFunction aggregationFunction, int unit,
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(String.valueOf(unit));
90 limitEntity.setTime(time);
95 public void setUp() throws Exception {
96 MockitoAnnotations.initMocks(this);
100 public void testUpdateLimit() {
101 Version version = new Version();
102 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
103 "Core",AggregationFunction.Average,10,"Hour");
104 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
105 "Tokens",AggregationFunction.Peak,12,"Month");
106 VersionInfo info = new VersionInfo();
107 info.getViewableVersions().add(version);
108 info.setActiveVersion(version);
110 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
111 doReturn(true).when(limitDao).isLimitPresent(anyObject());
112 doReturn(limitEntity1).when(limitDao).get(anyObject());
114 List<LimitEntity> limitEntityList = new ArrayList<>();
115 limitEntityList.add(limitEntity1);
116 limitEntityList.add(limitEntity2);
117 limitEntity1.setId("1234");
118 limitEntity2.setId("1234");
119 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(),anyObject(),
120 anyObject(),anyObject());
122 vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
124 verify(vendorLicenseFacade).updateLimit(anyObject(), anyObject());
128 public void testUpdateLimitErrorWithSameNameType() {
130 Version version = new Version();
131 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
132 "Core",AggregationFunction.Average,10,"Hour");
133 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
134 "Tokens",AggregationFunction.Peak,12,"Month");
135 VersionInfo info = new VersionInfo();
136 info.getViewableVersions().add(version);
137 info.setActiveVersion(version);
139 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
140 doReturn(limitEntity1).when(limitDao).get(anyObject());
142 List<LimitEntity> limitEntityList = new ArrayList<>();
143 limitEntityList.add(limitEntity1);
144 limitEntityList.add(limitEntity2);
145 limitEntity1.setId("1234");
146 limitEntity2.setId("9632");
147 doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(),anyObject(),
148 anyObject(),anyObject());
150 vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
152 } catch (CoreException exception) {
153 Assert.assertEquals(exception.code().id(),
154 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
159 public void testDeleteLimit() {
160 Version version = new Version();
161 LimitEntity limitEntity = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
162 "Core",AggregationFunction.Average,10,"Hour");
163 VersionInfo info = new VersionInfo();
164 info.getViewableVersions().add(version);
165 info.setActiveVersion(version);
167 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
168 doReturn(true).when(limitDao).isLimitPresent(anyObject());
169 doReturn(limitEntity).when(limitDao).get(anyObject());
171 List<LimitEntity> limitEntityList = new ArrayList<>();
172 limitEntityList.add(limitEntity);
173 limitEntity.setId("1234");
175 vendorLicenseManagerImpl.deleteLimit(limitEntity,LT1_NAME);
177 verify(vendorLicenseManagerImpl).deleteLimit(anyObject(), anyObject());
181 public void testUpdateLimitErrorWithInvalidId() {
183 Version version = new Version();
184 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
185 "Core",AggregationFunction.Average,10,"Hour");
186 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
187 "Tokens",AggregationFunction.Peak,12,"Month");
188 VersionInfo info = new VersionInfo();
189 info.getViewableVersions().add(version);
190 info.setActiveVersion(version);
192 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
193 doReturn(null).when(limitDao).get(anyObject());
195 vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
197 } catch (CoreException exception) {
198 Assert.assertEquals(exception.code().id(),
199 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
204 public void testList() {
205 doReturn(Arrays.asList(
206 createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID),
207 createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID)))
208 .when(vendorLicenseFacade).listLimits(VLM_ID, VERSION, EPLKG_ID, USER1);
210 final Collection<LimitEntity> limits =
211 vendorLicenseManagerImpl.listLimits(VLM_ID, VERSION, EPLKG_ID, USER1);
212 Assert.assertEquals(limits.size(), 2);
213 for (LimitEntity limit : limits) {
214 Assert.assertEquals(limit.getName(),
215 LIMIT1_ID.equals(limit.getId()) ? LIMIT1_ID+" name" : LIMIT2_ID+" name" );
220 public void testCreateLimit() {
221 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
222 VersionInfo info = new VersionInfo();
223 info.getViewableVersions().add(VERSION);
224 info.setActiveVersion(VERSION);
226 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
228 vendorLicenseManagerImpl.createLimit(expected, USER1);
229 verify(vendorLicenseFacade).createLimit(expected,USER1);
233 public void testCreateWithDuplicateName() {
234 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
235 expected.setType(LimitType.Vendor);
237 LimitEntity expectedDiffName = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID);
238 expectedDiffName.setName(LIMIT1_ID + " name");
239 expectedDiffName.setType(LimitType.Vendor);
241 List<LimitEntity> vfcImageList = new ArrayList<LimitEntity>();
242 vfcImageList.add(expectedDiffName);
243 doReturn(vfcImageList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(),
246 VersionInfo info = new VersionInfo();
247 info.getViewableVersions().add(VERSION);
248 info.setActiveVersion(VERSION);
250 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
253 vendorLicenseManagerImpl.createLimit(expected, USER1);
256 catch (CoreException ex) {
257 Assert.assertEquals(ex.code().id(),
258 VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED);
263 public void testGetNonExistingLimitId_negative() {
264 LimitEntity limit = createLimit(VLM_ID, VERSION, EPLKG_ID, "non existing limit id");
265 VersionInfo info = new VersionInfo();
266 info.getViewableVersions().add(VERSION);
267 info.setActiveVersion(VERSION);
269 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
272 vendorLicenseManagerImpl.getLimit(limit , USER1);
274 } catch (CoreException exception) {
275 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
280 public void testGet() {
281 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
282 expected.setType(LimitType.Vendor);
283 expected.setValue(String.valueOf(100));
284 expected.setUnit(String.valueOf(10));
285 expected.setAggregationFunction(AggregationFunction.Average);
286 expected.setMetric("BWTH");
287 expected.setTime("Day");
289 doReturn(true).when(limitDao).isLimitPresent(anyObject());
290 doReturn(expected).when(limitDao).get(anyObject());
291 VersionInfo info = new VersionInfo();
292 info.getViewableVersions().add(VERSION);
293 info.setActiveVersion(VERSION);
295 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
297 LimitEntity actual = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
298 vendorLicenseManagerImpl.getLimit(actual, USER1);
299 Assert.assertEquals(actual.getId(), expected.getId());
300 Assert.assertEquals(actual.getName(), expected.getName());
301 Assert.assertEquals(actual.getUnit(), expected.getUnit());
302 Assert.assertEquals(actual.getValue(), expected.getValue());
303 Assert.assertEquals(actual.getAggregationFunction().name(), expected.getAggregationFunction()
305 Assert.assertEquals(actual.getMetric(), expected.getMetric());
309 static LimitEntity createLimit(String vlmId, Version version, String epLkgId, String limitId) {
310 LimitEntity limitEntity = new LimitEntity(vlmId, version, epLkgId, limitId);
311 limitEntity.setName(limitId + " name");
312 limitEntity.setDescription(limitId + " desc");
313 limitEntity.setVersion(version);
314 limitEntity.setMetric("BWTH");
315 limitEntity.setAggregationFunction(AggregationFunction.Average);
316 limitEntity.setUnit(String.valueOf(10));
317 limitEntity.setTime("Day");
318 limitEntity.setValue(String.valueOf(100));