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 org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.mockito.Spy;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
29 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
30 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
31 import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
32 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes;
33 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
34 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
37 import org.openecomp.sdc.versioning.types.VersionInfo;
38 import org.testng.Assert;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Collection;
45 import java.util.List;
47 import static org.mockito.Matchers.anyObject;
48 import static org.mockito.Mockito.doReturn;
49 import static org.mockito.Mockito.verify;
51 public class LimitTest {
53 private final String USER1 = "limitTestUser1";
54 private final String LT1_NAME = "LT1 name";
56 private static final String VLM_ID = "VLM_ID";
57 private static final Version VERSION = new Version(0, 1);
58 private static final String EPLKG_ID = "ID";
59 private static final String LIMIT1_ID = "limit1";
60 private static final String LIMIT2_ID = "limit2";
63 private VendorLicenseFacade vendorLicenseFacade;
66 private LimitDao limitDao;
70 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
72 public static LimitEntity createLimitEntity(String name, LimitType type, String description,
73 Version version, String metric,
74 AggregationFunction aggregationFunction, int unit,
76 LimitEntity limitEntity = new LimitEntity();
77 limitEntity.setName(name);
78 limitEntity.setType(type);
79 limitEntity.setDescription(description);
80 limitEntity.setVersion(version);
81 limitEntity.setMetric(metric);
82 limitEntity.setAggregationFunction(aggregationFunction);
83 limitEntity.setUnit(String.valueOf(unit));
84 limitEntity.setTime(time);
89 public void setUp() throws Exception {
90 MockitoAnnotations.initMocks(this);
94 public void testUpdateLimit() {
95 Version version = new Version();
96 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
97 "Core", AggregationFunction.Average, 10, "Hour");
98 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
99 "Tokens", AggregationFunction.Peak, 12, "Month");
100 VersionInfo info = new VersionInfo();
101 info.getViewableVersions().add(version);
102 info.setActiveVersion(version);
104 /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
105 doReturn(true).when(limitDao).isLimitPresent(anyObject());
106 doReturn(limitEntity1).when(limitDao).get(anyObject());
108 List<LimitEntity> limitEntityList = new ArrayList<>();
109 limitEntityList.add(limitEntity1);
110 limitEntityList.add(limitEntity2);
111 limitEntity1.setId("1234");
112 limitEntity2.setId("1234");
113 doReturn(limitEntityList).when(vendorLicenseFacade)
114 .listLimits(anyObject(), anyObject(), anyObject());
116 vendorLicenseManagerImpl.updateLimit(limitEntity2);
118 verify(vendorLicenseFacade).updateLimit(anyObject());
122 public void testUpdateLimitErrorWithSameNameType() {
124 Version version = new Version();
125 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
126 "Core", AggregationFunction.Average, 10, "Hour");
127 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
128 "Tokens", AggregationFunction.Peak, 12, "Month");
129 VersionInfo info = new VersionInfo();
130 info.getViewableVersions().add(version);
131 info.setActiveVersion(version);
133 /* doReturn(info).when(vendorLicenseFacade)
134 .getVersionInfo(anyObject(), anyObject(), anyObject());*/
135 doReturn(limitEntity1).when(limitDao).get(anyObject());
137 List<LimitEntity> limitEntityList = new ArrayList<>();
138 limitEntityList.add(limitEntity1);
139 limitEntityList.add(limitEntity2);
140 limitEntity1.setId("1234");
141 limitEntity2.setId("9632");
142 doReturn(limitEntityList).when(vendorLicenseFacade)
143 .listLimits(anyObject(), anyObject(), anyObject());
145 vendorLicenseManagerImpl.updateLimit(limitEntity2);
147 } catch (CoreException exception) {
148 Assert.assertEquals(exception.code().id(),
149 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
154 public void testDeleteLimit() {
155 Version version = new Version();
156 LimitEntity limitEntity = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
157 "Core", AggregationFunction.Average, 10, "Hour");
158 VersionInfo info = new VersionInfo();
159 info.getViewableVersions().add(version);
160 info.setActiveVersion(version);
162 /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
163 doReturn(true).when(limitDao).isLimitPresent(anyObject());
164 doReturn(limitEntity).when(limitDao).get(anyObject());
166 List<LimitEntity> limitEntityList = new ArrayList<>();
167 limitEntityList.add(limitEntity);
168 limitEntity.setId("1234");
170 vendorLicenseManagerImpl.deleteLimit(limitEntity);
172 verify(vendorLicenseManagerImpl).deleteLimit(anyObject());
176 public void testUpdateLimitErrorWithInvalidId() {
178 Version version = new Version();
179 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
180 "Core", AggregationFunction.Average, 10, "Hour");
181 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
182 "Tokens", AggregationFunction.Peak, 12, "Month");
183 VersionInfo info = new VersionInfo();
184 info.getViewableVersions().add(version);
185 info.setActiveVersion(version);
187 /* doReturn(info).when(vendorLicenseFacade)
188 .getVersionInfo(anyObject(), anyObject(), anyObject());*/
189 doReturn(null).when(limitDao).get(anyObject());
191 vendorLicenseManagerImpl.updateLimit(limitEntity2);
193 } catch (CoreException exception) {
194 Assert.assertEquals(exception.code().id(),
195 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
200 public void testList() {
201 doReturn(Arrays.asList(
202 createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID),
203 createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID)))
204 .when(vendorLicenseFacade).listLimits(VLM_ID, VERSION, EPLKG_ID);
206 final Collection<LimitEntity> limits =
207 vendorLicenseManagerImpl.listLimits(VLM_ID, VERSION, EPLKG_ID);
208 Assert.assertEquals(limits.size(), 2);
209 for (LimitEntity limit : limits) {
210 Assert.assertEquals(limit.getName(),
211 LIMIT1_ID.equals(limit.getId()) ? LIMIT1_ID + " name" : LIMIT2_ID + " name");
216 public void testCreateLimit() {
217 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
218 VersionInfo info = new VersionInfo();
219 info.getViewableVersions().add(VERSION);
220 info.setActiveVersion(VERSION);
222 /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
224 vendorLicenseManagerImpl.createLimit(expected);
225 verify(vendorLicenseFacade).createLimit(expected);
229 public void testCreateWithDuplicateName() {
230 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
231 expected.setType(LimitType.Vendor);
233 LimitEntity expectedDiffName = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID);
234 expectedDiffName.setName(LIMIT1_ID + " name");
235 expectedDiffName.setType(LimitType.Vendor);
237 List<LimitEntity> vfcImageList = new ArrayList<LimitEntity>();
238 vfcImageList.add(expectedDiffName);
239 doReturn(vfcImageList).when(vendorLicenseFacade)
240 .listLimits(anyObject(), anyObject(), anyObject());
242 VersionInfo info = new VersionInfo();
243 info.getViewableVersions().add(VERSION);
244 info.setActiveVersion(VERSION);
246 /* doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
249 vendorLicenseManagerImpl.createLimit(expected);
251 } catch (CoreException ex) {
252 Assert.assertEquals(ex.code().id(),
253 VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED);
258 public void testGetNonExistingLimitId_negative() {
259 LimitEntity limit = createLimit(VLM_ID, VERSION, EPLKG_ID, "non existing limit id");
260 VersionInfo info = new VersionInfo();
261 info.getViewableVersions().add(VERSION);
262 info.setActiveVersion(VERSION);
264 /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
267 vendorLicenseManagerImpl.getLimit(limit);
269 } catch (CoreException exception) {
270 Assert.assertEquals(exception.code().id(),
271 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
276 public void testGet() {
277 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
278 expected.setType(LimitType.Vendor);
279 expected.setValue(String.valueOf(100));
280 expected.setUnit(String.valueOf(10));
281 expected.setAggregationFunction(AggregationFunction.Average);
282 expected.setMetric("BWTH");
283 expected.setTime("Day");
285 doReturn(true).when(limitDao).isLimitPresent(anyObject());
286 doReturn(expected).when(limitDao).get(anyObject());
287 VersionInfo info = new VersionInfo();
288 info.getViewableVersions().add(VERSION);
289 info.setActiveVersion(VERSION);
291 /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
293 LimitEntity actual = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
294 vendorLicenseManagerImpl.getLimit(actual);
295 Assert.assertEquals(actual.getId(), expected.getId());
296 Assert.assertEquals(actual.getName(), expected.getName());
297 Assert.assertEquals(actual.getUnit(), expected.getUnit());
298 Assert.assertEquals(actual.getValue(), expected.getValue());
299 Assert.assertEquals(actual.getAggregationFunction().name(), expected.getAggregationFunction()
301 Assert.assertEquals(actual.getMetric(), expected.getMetric());
305 static LimitEntity createLimit(String vlmId, Version version, String epLkgId, String limitId) {
306 LimitEntity limitEntity = new LimitEntity(vlmId, version, epLkgId, limitId);
307 limitEntity.setName(limitId + " name");
308 limitEntity.setDescription(limitId + " desc");
309 limitEntity.setVersion(version);
310 limitEntity.setMetric("BWTH");
311 limitEntity.setAggregationFunction(AggregationFunction.Average);
312 limitEntity.setUnit(String.valueOf(10));
313 limitEntity.setTime("Day");
314 limitEntity.setValue(String.valueOf(100));