2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.vendorlicense;
19 import org.mockito.InjectMocks;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.mockito.Spy;
23 import org.openecomp.sdc.common.errors.CoreException;
24 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
25 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
26 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
27 import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
28 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes;
29 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
30 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
31 import org.openecomp.sdc.versioning.dao.types.Version;
32 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
33 import org.openecomp.sdc.versioning.types.VersionInfo;
34 import org.testng.Assert;
35 import org.testng.annotations.AfterMethod;
36 import org.testng.annotations.BeforeMethod;
37 import org.testng.annotations.Test;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.Collection;
42 import java.util.List;
44 import static org.mockito.Matchers.anyObject;
45 import static org.mockito.Mockito.doReturn;
46 import static org.mockito.Mockito.verify;
48 public class LimitTest {
50 private final String USER1 = "limitTestUser1";
51 private final String LT1_NAME = "LT1 name";
53 private static final String VLM_ID = "VLM_ID";
54 private static final Version VERSION = new Version(0, 1);
55 private static final String EPLKG_ID = "ID";
56 private static final String LIMIT1_ID = "limit1";
57 private static final String LIMIT2_ID = "limit2";
60 private VendorLicenseFacade vendorLicenseFacade;
63 private LimitDao limitDao;
67 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
69 public static LimitEntity createLimitEntity(String name, LimitType type, String description,
70 Version version, String metric,
71 AggregationFunction aggregationFunction, int unit,
73 LimitEntity limitEntity = new LimitEntity();
74 limitEntity.setName(name);
75 limitEntity.setType(type);
76 limitEntity.setDescription(description);
77 limitEntity.setVersion(version);
78 limitEntity.setMetric(metric);
79 limitEntity.setAggregationFunction(aggregationFunction);
80 limitEntity.setUnit(String.valueOf(unit));
81 limitEntity.setTime(time);
86 public void setUp() throws Exception {
87 MockitoAnnotations.initMocks(this);
91 public void tearDown(){
92 vendorLicenseManagerImpl = null;
96 public void testUpdateLimit() {
97 Version version = new Version();
98 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
99 "Core", AggregationFunction.Average, 10, "Hour");
100 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
101 "Tokens", AggregationFunction.Peak, 12, "Month");
102 VersionInfo info = new VersionInfo();
103 info.getViewableVersions().add(version);
104 info.setActiveVersion(version);
106 /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
107 doReturn(true).when(limitDao).isLimitPresent(anyObject());
108 doReturn(limitEntity1).when(limitDao).get(anyObject());
110 List<LimitEntity> limitEntityList = new ArrayList<>();
111 limitEntityList.add(limitEntity1);
112 limitEntityList.add(limitEntity2);
113 limitEntity1.setId("1234");
114 limitEntity2.setId("1234");
115 doReturn(limitEntityList).when(vendorLicenseFacade)
116 .listLimits(anyObject(), anyObject(), anyObject());
118 vendorLicenseManagerImpl.updateLimit(limitEntity2);
120 verify(vendorLicenseFacade).updateLimit(anyObject());
124 public void testUpdateLimitErrorWithSameNameType() {
126 Version version = new Version();
127 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
128 "Core", AggregationFunction.Average, 10, "Hour");
129 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
130 "Tokens", AggregationFunction.Peak, 12, "Month");
131 VersionInfo info = new VersionInfo();
132 info.getViewableVersions().add(version);
133 info.setActiveVersion(version);
135 /* doReturn(info).when(vendorLicenseFacade)
136 .getVersionInfo(anyObject(), anyObject(), anyObject());*/
137 doReturn(limitEntity1).when(limitDao).get(anyObject());
139 List<LimitEntity> limitEntityList = new ArrayList<>();
140 limitEntityList.add(limitEntity1);
141 limitEntityList.add(limitEntity2);
142 limitEntity1.setId("1234");
143 limitEntity2.setId("9632");
144 doReturn(limitEntityList).when(vendorLicenseFacade)
145 .listLimits(anyObject(), anyObject(), anyObject());
147 vendorLicenseManagerImpl.updateLimit(limitEntity2);
149 } catch (CoreException exception) {
150 Assert.assertEquals(exception.code().id(),
151 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
156 public void testDeleteLimit() {
157 Version version = new Version();
158 LimitEntity limitEntity = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
159 "Core", AggregationFunction.Average, 10, "Hour");
160 VersionInfo info = new VersionInfo();
161 info.getViewableVersions().add(version);
162 info.setActiveVersion(version);
164 /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
165 doReturn(true).when(limitDao).isLimitPresent(anyObject());
166 doReturn(limitEntity).when(limitDao).get(anyObject());
168 List<LimitEntity> limitEntityList = new ArrayList<>();
169 limitEntityList.add(limitEntity);
170 limitEntity.setId("1234");
172 vendorLicenseManagerImpl.deleteLimit(limitEntity);
174 verify(vendorLicenseManagerImpl).deleteLimit(anyObject());
178 public void testUpdateLimitErrorWithInvalidId() {
180 Version version = new Version();
181 LimitEntity limitEntity1 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
182 "Core", AggregationFunction.Average, 10, "Hour");
183 LimitEntity limitEntity2 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
184 "Tokens", AggregationFunction.Peak, 12, "Month");
185 VersionInfo info = new VersionInfo();
186 info.getViewableVersions().add(version);
187 info.setActiveVersion(version);
189 /* doReturn(info).when(vendorLicenseFacade)
190 .getVersionInfo(anyObject(), anyObject(), anyObject());*/
191 doReturn(null).when(limitDao).get(anyObject());
193 vendorLicenseManagerImpl.updateLimit(limitEntity2);
195 } catch (CoreException exception) {
196 Assert.assertEquals(exception.code().id(),
197 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
202 public void testList() {
203 doReturn(Arrays.asList(
204 createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID),
205 createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID)))
206 .when(vendorLicenseFacade).listLimits(VLM_ID, VERSION, EPLKG_ID);
208 final Collection<LimitEntity> limits =
209 vendorLicenseManagerImpl.listLimits(VLM_ID, VERSION, EPLKG_ID);
210 Assert.assertEquals(limits.size(), 2);
211 for (LimitEntity limit : limits) {
212 Assert.assertEquals(limit.getName(),
213 LIMIT1_ID.equals(limit.getId()) ? LIMIT1_ID + " name" : LIMIT2_ID + " name");
218 public void testCreateLimit() {
219 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
220 VersionInfo info = new VersionInfo();
221 info.getViewableVersions().add(VERSION);
222 info.setActiveVersion(VERSION);
224 /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
226 vendorLicenseManagerImpl.createLimit(expected);
227 verify(vendorLicenseFacade).createLimit(expected);
231 public void testCreateWithDuplicateName() {
232 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
233 expected.setType(LimitType.Vendor);
235 LimitEntity expectedDiffName = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID);
236 expectedDiffName.setName(LIMIT1_ID + " name");
237 expectedDiffName.setType(LimitType.Vendor);
239 List<LimitEntity> vfcImageList = new ArrayList<LimitEntity>();
240 vfcImageList.add(expectedDiffName);
241 doReturn(vfcImageList).when(vendorLicenseFacade)
242 .listLimits(anyObject(), anyObject(), anyObject());
244 VersionInfo info = new VersionInfo();
245 info.getViewableVersions().add(VERSION);
246 info.setActiveVersion(VERSION);
248 /* doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
251 vendorLicenseManagerImpl.createLimit(expected);
253 } catch (CoreException ex) {
254 Assert.assertEquals(ex.code().id(),
255 VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED);
260 public void testGetNonExistingLimitId_negative() {
261 LimitEntity limit = createLimit(VLM_ID, VERSION, EPLKG_ID, "non existing limit id");
262 VersionInfo info = new VersionInfo();
263 info.getViewableVersions().add(VERSION);
264 info.setActiveVersion(VERSION);
266 /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
269 vendorLicenseManagerImpl.getLimit(limit);
271 } catch (CoreException exception) {
272 Assert.assertEquals(exception.code().id(),
273 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
278 public void testGet() {
279 LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
280 expected.setType(LimitType.Vendor);
281 expected.setValue(String.valueOf(100));
282 expected.setUnit(String.valueOf(10));
283 expected.setAggregationFunction(AggregationFunction.Average);
284 expected.setMetric("BWTH");
285 expected.setTime("Day");
287 doReturn(true).when(limitDao).isLimitPresent(anyObject());
288 doReturn(expected).when(limitDao).get(anyObject());
289 VersionInfo info = new VersionInfo();
290 info.getViewableVersions().add(VERSION);
291 info.setActiveVersion(VERSION);
293 /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
295 LimitEntity actual = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
296 vendorLicenseManagerImpl.getLimit(actual);
297 Assert.assertEquals(actual.getId(), expected.getId());
298 Assert.assertEquals(actual.getName(), expected.getName());
299 Assert.assertEquals(actual.getUnit(), expected.getUnit());
300 Assert.assertEquals(actual.getValue(), expected.getValue());
301 Assert.assertEquals(actual.getAggregationFunction().name(), expected.getAggregationFunction()
303 Assert.assertEquals(actual.getMetric(), expected.getMetric());
307 static LimitEntity createLimit(String vlmId, Version version, String epLkgId, String limitId) {
308 LimitEntity limitEntity = new LimitEntity(vlmId, version, epLkgId, limitId);
309 limitEntity.setName(limitId + " name");
310 limitEntity.setDescription(limitId + " desc");
311 limitEntity.setVersion(version);
312 limitEntity.setMetric("BWTH");
313 limitEntity.setAggregationFunction(AggregationFunction.Average);
314 limitEntity.setUnit(String.valueOf(10));
315 limitEntity.setTime("Day");
316 limitEntity.setValue(String.valueOf(100));