[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / test / java / org / openecomp / sdc / vendorlicense / LimitTest.java
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.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;
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 import static org.mockito.Mockito.when;
56
57 public class LimitTest {
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, EntitlementMetric metric,
80                                        AggregationFunction aggregationFunction, int unit,
81                                        EntitlementTime 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(unit);
90     limitEntity.setTime(time);
91     return limitEntity;
92   }
93
94   @BeforeMethod
95   public void setUp() throws Exception {
96     MockitoAnnotations.initMocks(this);
97     try {
98       Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
99       limitField.setAccessible(true);
100       Field modifiersField = Field.class.getDeclaredField("modifiers");
101       modifiersField.setAccessible(true);
102       modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
103       limitField.set(null, limitDao);
104     } catch(NoSuchFieldException | IllegalAccessException e)
105     {
106       Assert.fail();
107     }
108   }
109
110   @Test
111   public void testUpdateLimit() {
112     Version version = new Version();
113     LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
114         EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
115     LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
116         EntitlementMetric.Tokens,AggregationFunction.Peak,12,EntitlementTime.Month);
117     VersionInfo info = new VersionInfo();
118     info.getViewableVersions().add(version);
119     info.setActiveVersion(version);
120
121     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
122     doReturn(true).when(limitDao).isLimitPresent(anyObject());
123     doReturn(limitEntity1).when(limitDao).get(anyObject());
124
125     List<LimitEntity> limitEntityList = new ArrayList<>();
126     limitEntityList.add(limitEntity1);
127     limitEntityList.add(limitEntity2);
128     limitEntity1.setId("1234");
129     limitEntity2.setId("1234");
130     doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(),anyObject(),
131         anyObject(),anyObject());
132
133     vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
134
135     verify(vendorLicenseFacade).updateLimit(anyObject(), anyObject());
136   }
137
138   @Test
139   public void testUpdateLimitErrorWithSameNameType() {
140     try {
141       Version version = new Version();
142       LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
143           EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
144       LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
145           EntitlementMetric.Tokens,AggregationFunction.Peak,12,EntitlementTime.Month);
146       VersionInfo info = new VersionInfo();
147       info.getViewableVersions().add(version);
148       info.setActiveVersion(version);
149
150       doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
151       doReturn(limitEntity1).when(limitDao).get(anyObject());
152
153       List<LimitEntity> limitEntityList = new ArrayList<>();
154       limitEntityList.add(limitEntity1);
155       limitEntityList.add(limitEntity2);
156       limitEntity1.setId("1234");
157       limitEntity2.setId("9632");
158       doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(),anyObject(),
159           anyObject(),anyObject());
160
161       vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
162       Assert.fail();
163     } catch (CoreException exception) {
164       Assert.assertEquals(exception.code().id(),
165           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
166     }
167   }
168
169   @Test
170   public void testDeleteLimit() {
171     Version version = new Version();
172     LimitEntity limitEntity = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
173             EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
174     VersionInfo info = new VersionInfo();
175     info.getViewableVersions().add(version);
176     info.setActiveVersion(version);
177
178     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
179     doReturn(true).when(limitDao).isLimitPresent(anyObject());
180     doReturn(limitEntity).when(limitDao).get(anyObject());
181
182     List<LimitEntity> limitEntityList = new ArrayList<>();
183     limitEntityList.add(limitEntity);
184     limitEntity.setId("1234");
185
186     vendorLicenseManagerImpl.deleteLimit(limitEntity,LT1_NAME);
187
188     verify(vendorLicenseManagerImpl).deleteLimit(anyObject(), anyObject());
189   }
190
191   @Test
192   public void testUpdateLimitErrorWithInvalidId() {
193     try {
194       Version version = new Version();
195       LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
196           EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
197       LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
198           EntitlementMetric.Tokens,AggregationFunction.Peak,12,EntitlementTime.Month);
199       VersionInfo info = new VersionInfo();
200       info.getViewableVersions().add(version);
201       info.setActiveVersion(version);
202
203       doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
204       doReturn(null).when(limitDao).get(anyObject());
205
206       vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
207       Assert.fail();
208     } catch (CoreException exception) {
209       Assert.assertEquals(exception.code().id(),
210           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
211     }
212   }
213
214   @Test
215   public void testList() {
216     doReturn(Arrays.asList(
217         createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID),
218         createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID)))
219         .when(vendorLicenseFacade).listLimits(VLM_ID, VERSION, EPLKG_ID, USER1);
220
221     final Collection<LimitEntity> limits =
222         vendorLicenseManagerImpl.listLimits(VLM_ID, VERSION, EPLKG_ID, USER1);
223     Assert.assertEquals(limits.size(), 2);
224     for (LimitEntity limit : limits) {
225       Assert.assertEquals(limit.getName(),
226           LIMIT1_ID.equals(limit.getId()) ? LIMIT1_ID+" name" : LIMIT2_ID+" name" );
227     }
228   }
229
230   @Test
231   public void testCreateLimit() {
232     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
233     VersionInfo info = new VersionInfo();
234     info.getViewableVersions().add(VERSION);
235     info.setActiveVersion(VERSION);
236
237     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
238
239     vendorLicenseManagerImpl.createLimit(expected, USER1);
240     verify(vendorLicenseFacade).createLimit(expected,USER1);
241   }
242
243   @Test
244   public void testCreateWithDuplicateName() {
245     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
246     expected.setType(LimitType.Vendor);
247
248     LimitEntity expectedDiffName = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID);
249     expectedDiffName.setName(LIMIT1_ID + " name");
250     expectedDiffName.setType(LimitType.Vendor);
251
252     List<LimitEntity> vfcImageList = new ArrayList<LimitEntity>();
253     vfcImageList.add(expectedDiffName);
254     doReturn(vfcImageList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(),
255         anyObject());
256
257     VersionInfo info = new VersionInfo();
258     info.getViewableVersions().add(VERSION);
259     info.setActiveVersion(VERSION);
260
261     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
262
263     try {
264       vendorLicenseManagerImpl.createLimit(expected, USER1);
265       Assert.fail();
266     }
267     catch (CoreException ex) {
268       Assert.assertEquals(ex.code().id(),
269           VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED);
270     }
271   }
272
273   @Test
274   public void testGetNonExistingLimitId_negative() {
275     LimitEntity limit = createLimit(VLM_ID, VERSION, EPLKG_ID, "non existing limit id");
276     VersionInfo info = new VersionInfo();
277     info.getViewableVersions().add(VERSION);
278     info.setActiveVersion(VERSION);
279
280     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
281
282     try {
283       vendorLicenseManagerImpl.getLimit(limit , USER1);
284       Assert.fail();
285     } catch (CoreException exception) {
286       Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
287     }
288   }
289
290   @Test
291   public void testGet() {
292     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
293     expected.setType(LimitType.Vendor);
294     expected.setValue(100);
295     expected.setUnit(10);
296     expected.setAggregationFunction(AggregationFunction.Average);
297     expected.setMetric(EntitlementMetric.CPU);
298     expected.setTime(EntitlementTime.Day);
299
300     doReturn(true).when(limitDao).isLimitPresent(anyObject());
301     doReturn(expected).when(limitDao).get(anyObject());
302     VersionInfo info = new VersionInfo();
303     info.getViewableVersions().add(VERSION);
304     info.setActiveVersion(VERSION);
305
306     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
307
308     LimitEntity actual = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
309     vendorLicenseManagerImpl.getLimit(actual, USER1);
310     Assert.assertEquals(actual.getId(), expected.getId());
311     Assert.assertEquals(actual.getName(), expected.getName());
312     Assert.assertEquals(actual.getUnit(), expected.getUnit());
313     Assert.assertEquals(actual.getValue(), expected.getValue());
314     Assert.assertEquals(actual.getAggregationFunction().name(), expected.getAggregationFunction()
315         .name());
316     Assert.assertEquals(actual.getMetric().name(), expected.getMetric().name());
317
318   }
319
320   static LimitEntity createLimit(String vlmId, Version version, String epLkgId, String limitId) {
321     LimitEntity limitEntity = new LimitEntity(vlmId, version, epLkgId, limitId);
322     limitEntity.setName(limitId + " name");
323     limitEntity.setDescription(limitId + " desc");
324     limitEntity.setVersion(version);
325     limitEntity.setMetric(EntitlementMetric.CPU);
326     limitEntity.setAggregationFunction(AggregationFunction.Average);
327     limitEntity.setUnit(10);
328     limitEntity.setTime(EntitlementTime.Day);
329     limitEntity.setValue(100);
330     return limitEntity;
331   }
332 }