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.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
35 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
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 public class LimitTest {
57 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
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);
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 log.debug("",exception);
154 Assert.assertEquals(exception.code().id(),
155 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
160 public void testDeleteLimit() {
161 Version version = new Version();
162 LimitEntity limitEntity = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
163 "Core",AggregationFunction.Average,10,"Hour");
164 VersionInfo info = new VersionInfo();
165 info.getViewableVersions().add(version);
166 info.setActiveVersion(version);
168 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
169 doReturn(true).when(limitDao).isLimitPresent(anyObject());
170 doReturn(limitEntity).when(limitDao).get(anyObject());
172 List<LimitEntity> limitEntityList = new ArrayList<>();
173 limitEntityList.add(limitEntity);
174 limitEntity.setId("1234");
176 vendorLicenseManagerImpl.deleteLimit(limitEntity,LT1_NAME);
178 verify(vendorLicenseManagerImpl).deleteLimit(anyObject(), anyObject());
182 public void testUpdateLimitErrorWithInvalidId() {
184 Version version = new Version();
185 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
186 "Core",AggregationFunction.Average,10,"Hour");
187 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
188 "Tokens",AggregationFunction.Peak,12,"Month");
189 VersionInfo info = new VersionInfo();
190 info.getViewableVersions().add(version);
191 info.setActiveVersion(version);
193 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
194 doReturn(null).when(limitDao).get(anyObject());
196 vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
198 } catch (CoreException exception) {
199 Assert.assertEquals(exception.code().id(),
200 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
205 public void testList() {
206 doReturn(Arrays.asList(
207 createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID),
208 createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID)))
209 .when(vendorLicenseFacade).listLimits(VLM_ID, VERSION, EPLKG_ID, USER1);
211 final Collection<LimitEntity> limits =
212 vendorLicenseManagerImpl.listLimits(VLM_ID, VERSION, EPLKG_ID, USER1);
213 Assert.assertEquals(limits.size(), 2);
214 for (LimitEntity limit : limits) {
215 Assert.assertEquals(limit.getName(),
216 LIMIT1_ID.equals(limit.getId()) ? LIMIT1_ID+" name" : LIMIT2_ID+" name" );
221 public void testCreateLimit() {
222 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
223 VersionInfo info = new VersionInfo();
224 info.getViewableVersions().add(VERSION);
225 info.setActiveVersion(VERSION);
227 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
229 vendorLicenseManagerImpl.createLimit(expected, USER1);
230 verify(vendorLicenseFacade).createLimit(expected,USER1);
234 public void testCreateWithDuplicateName() {
235 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
236 expected.setType(LimitType.Vendor);
238 LimitEntity expectedDiffName = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID);
239 expectedDiffName.setName(LIMIT1_ID + " name");
240 expectedDiffName.setType(LimitType.Vendor);
242 List<LimitEntity> vfcImageList = new ArrayList<LimitEntity>();
243 vfcImageList.add(expectedDiffName);
244 doReturn(vfcImageList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(),
247 VersionInfo info = new VersionInfo();
248 info.getViewableVersions().add(VERSION);
249 info.setActiveVersion(VERSION);
251 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
254 vendorLicenseManagerImpl.createLimit(expected, USER1);
257 catch (CoreException ex) {
258 Assert.assertEquals(ex.code().id(),
259 VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED);
264 public void testGetNonExistingLimitId_negative() {
265 LimitEntity limit = createLimit(VLM_ID, VERSION, EPLKG_ID, "non existing limit id");
266 VersionInfo info = new VersionInfo();
267 info.getViewableVersions().add(VERSION);
268 info.setActiveVersion(VERSION);
270 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
273 vendorLicenseManagerImpl.getLimit(limit , USER1);
275 } catch (CoreException exception) {
276 Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
281 public void testGet() {
282 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
283 expected.setType(LimitType.Vendor);
284 expected.setValue(String.valueOf(100));
285 expected.setUnit(String.valueOf(10));
286 expected.setAggregationFunction(AggregationFunction.Average);
287 expected.setMetric("BWTH");
288 expected.setTime("Day");
290 doReturn(true).when(limitDao).isLimitPresent(anyObject());
291 doReturn(expected).when(limitDao).get(anyObject());
292 VersionInfo info = new VersionInfo();
293 info.getViewableVersions().add(VERSION);
294 info.setActiveVersion(VERSION);
296 doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
298 LimitEntity actual = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
299 vendorLicenseManagerImpl.getLimit(actual, USER1);
300 Assert.assertEquals(actual.getId(), expected.getId());
301 Assert.assertEquals(actual.getName(), expected.getName());
302 Assert.assertEquals(actual.getUnit(), expected.getUnit());
303 Assert.assertEquals(actual.getValue(), expected.getValue());
304 Assert.assertEquals(actual.getAggregationFunction().name(), expected.getAggregationFunction()
306 Assert.assertEquals(actual.getMetric(), expected.getMetric());
310 static LimitEntity createLimit(String vlmId, Version version, String epLkgId, String limitId) {
311 LimitEntity limitEntity = new LimitEntity(vlmId, version, epLkgId, limitId);
312 limitEntity.setName(limitId + " name");
313 limitEntity.setDescription(limitId + " desc");
314 limitEntity.setVersion(version);
315 limitEntity.setMetric("BWTH");
316 limitEntity.setAggregationFunction(AggregationFunction.Average);
317 limitEntity.setUnit(String.valueOf(10));
318 limitEntity.setTime("Day");
319 limitEntity.setValue(String.valueOf(100));