07fdeeeee80ea6eacb1b54d338ee7c756c8647ea
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorlicense;
22
23 import static org.mockito.Matchers.anyObject;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.verify;
26
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;
47
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;
54
55 public class LimitTest {
56
57   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
58
59   private  final String USER1 = "limitTestUser1";
60   private  final String LT1_NAME = "LT1 name";
61
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";
67
68   @Mock
69   private VendorLicenseFacade vendorLicenseFacade;
70
71   @Mock
72   private LimitDao limitDao;
73
74   @InjectMocks
75   @Spy
76   private VendorLicenseManagerImpl vendorLicenseManagerImpl;
77
78   public static LimitEntity createLimitEntity(String name, LimitType type, String description,
79                                        Version version, String metric,
80                                        AggregationFunction aggregationFunction, int unit,
81                                               String 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(String.valueOf(unit));
90     limitEntity.setTime(time);
91     return limitEntity;
92   }
93
94   @BeforeMethod
95   public void setUp() {
96     MockitoAnnotations.initMocks(this);
97   }
98
99   @Test
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);
109
110     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
111     doReturn(true).when(limitDao).isLimitPresent(anyObject());
112     doReturn(limitEntity1).when(limitDao).get(anyObject());
113
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());
121
122     vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
123
124     verify(vendorLicenseFacade).updateLimit(anyObject(), anyObject());
125   }
126
127   @Test
128   public void testUpdateLimitErrorWithSameNameType() {
129     try {
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);
138
139       doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
140       doReturn(limitEntity1).when(limitDao).get(anyObject());
141
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());
149
150       vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
151       Assert.fail();
152     } catch (CoreException exception) {
153       log.debug("",exception);
154       Assert.assertEquals(exception.code().id(),
155           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
156     }
157   }
158
159   @Test
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);
167
168     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
169     doReturn(true).when(limitDao).isLimitPresent(anyObject());
170     doReturn(limitEntity).when(limitDao).get(anyObject());
171
172     List<LimitEntity> limitEntityList = new ArrayList<>();
173     limitEntityList.add(limitEntity);
174     limitEntity.setId("1234");
175
176     vendorLicenseManagerImpl.deleteLimit(limitEntity,LT1_NAME);
177
178     verify(vendorLicenseManagerImpl).deleteLimit(anyObject(), anyObject());
179   }
180
181   @Test
182   public void testUpdateLimitErrorWithInvalidId() {
183     try {
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);
192
193       doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
194       doReturn(null).when(limitDao).get(anyObject());
195
196       vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
197       Assert.fail();
198     } catch (CoreException exception) {
199       Assert.assertEquals(exception.code().id(),
200           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
201     }
202   }
203
204   @Test
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);
210
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" );
217     }
218   }
219
220   @Test
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);
226
227     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
228
229     vendorLicenseManagerImpl.createLimit(expected, USER1);
230     verify(vendorLicenseFacade).createLimit(expected,USER1);
231   }
232
233   @Test
234   public void testCreateWithDuplicateName() {
235     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
236     expected.setType(LimitType.Vendor);
237
238     LimitEntity expectedDiffName = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID);
239     expectedDiffName.setName(LIMIT1_ID + " name");
240     expectedDiffName.setType(LimitType.Vendor);
241
242     List<LimitEntity> vfcImageList = new ArrayList<LimitEntity>();
243     vfcImageList.add(expectedDiffName);
244     doReturn(vfcImageList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(),
245         anyObject());
246
247     VersionInfo info = new VersionInfo();
248     info.getViewableVersions().add(VERSION);
249     info.setActiveVersion(VERSION);
250
251     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
252
253     try {
254       vendorLicenseManagerImpl.createLimit(expected, USER1);
255       Assert.fail();
256     }
257     catch (CoreException ex) {
258       Assert.assertEquals(ex.code().id(),
259           VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED);
260     }
261   }
262
263   @Test
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);
269
270     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
271
272     try {
273       vendorLicenseManagerImpl.getLimit(limit , USER1);
274       Assert.fail();
275     } catch (CoreException exception) {
276       Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
277     }
278   }
279
280   @Test
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");
289
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);
295
296     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
297
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()
305         .name());
306     Assert.assertEquals(actual.getMetric(), expected.getMetric());
307
308   }
309
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));
320     return limitEntity;
321   }
322 }