c71b591a0415a8b7aaa37d19eb14a678c7bd0116
[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.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, 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() throws Exception {
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       Assert.assertEquals(exception.code().id(),
154           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
155     }
156   }
157
158   @Test
159   public void testDeleteLimit() {
160     Version version = new Version();
161     LimitEntity limitEntity = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
162             "Core",AggregationFunction.Average,10,"Hour");
163     VersionInfo info = new VersionInfo();
164     info.getViewableVersions().add(version);
165     info.setActiveVersion(version);
166
167     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
168     doReturn(true).when(limitDao).isLimitPresent(anyObject());
169     doReturn(limitEntity).when(limitDao).get(anyObject());
170
171     List<LimitEntity> limitEntityList = new ArrayList<>();
172     limitEntityList.add(limitEntity);
173     limitEntity.setId("1234");
174
175     vendorLicenseManagerImpl.deleteLimit(limitEntity,LT1_NAME);
176
177     verify(vendorLicenseManagerImpl).deleteLimit(anyObject(), anyObject());
178   }
179
180   @Test
181   public void testUpdateLimitErrorWithInvalidId() {
182     try {
183       Version version = new Version();
184       LimitEntity limitEntity1 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
185           "Core",AggregationFunction.Average,10,"Hour");
186       LimitEntity limitEntity2 = createLimitEntity(LT1_NAME,LimitType.Vendor,"string",version,
187           "Tokens",AggregationFunction.Peak,12,"Month");
188       VersionInfo info = new VersionInfo();
189       info.getViewableVersions().add(version);
190       info.setActiveVersion(version);
191
192       doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
193       doReturn(null).when(limitDao).get(anyObject());
194
195       vendorLicenseManagerImpl.updateLimit(limitEntity2,USER1);
196       Assert.fail();
197     } catch (CoreException exception) {
198       Assert.assertEquals(exception.code().id(),
199           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
200     }
201   }
202
203   @Test
204   public void testList() {
205     doReturn(Arrays.asList(
206         createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID),
207         createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID)))
208         .when(vendorLicenseFacade).listLimits(VLM_ID, VERSION, EPLKG_ID, USER1);
209
210     final Collection<LimitEntity> limits =
211         vendorLicenseManagerImpl.listLimits(VLM_ID, VERSION, EPLKG_ID, USER1);
212     Assert.assertEquals(limits.size(), 2);
213     for (LimitEntity limit : limits) {
214       Assert.assertEquals(limit.getName(),
215           LIMIT1_ID.equals(limit.getId()) ? LIMIT1_ID+" name" : LIMIT2_ID+" name" );
216     }
217   }
218
219   @Test
220   public void testCreateLimit() {
221     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
222     VersionInfo info = new VersionInfo();
223     info.getViewableVersions().add(VERSION);
224     info.setActiveVersion(VERSION);
225
226     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
227
228     vendorLicenseManagerImpl.createLimit(expected, USER1);
229     verify(vendorLicenseFacade).createLimit(expected,USER1);
230   }
231
232   @Test
233   public void testCreateWithDuplicateName() {
234     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
235     expected.setType(LimitType.Vendor);
236
237     LimitEntity expectedDiffName = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID);
238     expectedDiffName.setName(LIMIT1_ID + " name");
239     expectedDiffName.setType(LimitType.Vendor);
240
241     List<LimitEntity> vfcImageList = new ArrayList<LimitEntity>();
242     vfcImageList.add(expectedDiffName);
243     doReturn(vfcImageList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(),
244         anyObject());
245
246     VersionInfo info = new VersionInfo();
247     info.getViewableVersions().add(VERSION);
248     info.setActiveVersion(VERSION);
249
250     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
251
252     try {
253       vendorLicenseManagerImpl.createLimit(expected, USER1);
254       Assert.fail();
255     }
256     catch (CoreException ex) {
257       Assert.assertEquals(ex.code().id(),
258           VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED);
259     }
260   }
261
262   @Test
263   public void testGetNonExistingLimitId_negative() {
264     LimitEntity limit = createLimit(VLM_ID, VERSION, EPLKG_ID, "non existing limit id");
265     VersionInfo info = new VersionInfo();
266     info.getViewableVersions().add(VERSION);
267     info.setActiveVersion(VERSION);
268
269     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
270
271     try {
272       vendorLicenseManagerImpl.getLimit(limit , USER1);
273       Assert.fail();
274     } catch (CoreException exception) {
275       Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
276     }
277   }
278
279   @Test
280   public void testGet() {
281     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
282     expected.setType(LimitType.Vendor);
283     expected.setValue(String.valueOf(100));
284     expected.setUnit(String.valueOf(10));
285     expected.setAggregationFunction(AggregationFunction.Average);
286     expected.setMetric("BWTH");
287     expected.setTime("Day");
288
289     doReturn(true).when(limitDao).isLimitPresent(anyObject());
290     doReturn(expected).when(limitDao).get(anyObject());
291     VersionInfo info = new VersionInfo();
292     info.getViewableVersions().add(VERSION);
293     info.setActiveVersion(VERSION);
294
295     doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
296
297     LimitEntity actual = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
298     vendorLicenseManagerImpl.getLimit(actual, USER1);
299     Assert.assertEquals(actual.getId(), expected.getId());
300     Assert.assertEquals(actual.getName(), expected.getName());
301     Assert.assertEquals(actual.getUnit(), expected.getUnit());
302     Assert.assertEquals(actual.getValue(), expected.getValue());
303     Assert.assertEquals(actual.getAggregationFunction().name(), expected.getAggregationFunction()
304         .name());
305     Assert.assertEquals(actual.getMetric(), expected.getMetric());
306
307   }
308
309   static LimitEntity createLimit(String vlmId, Version version, String epLkgId, String limitId) {
310     LimitEntity limitEntity = new LimitEntity(vlmId, version, epLkgId, limitId);
311     limitEntity.setName(limitId + " name");
312     limitEntity.setDescription(limitId + " desc");
313     limitEntity.setVersion(version);
314     limitEntity.setMetric("BWTH");
315     limitEntity.setAggregationFunction(AggregationFunction.Average);
316     limitEntity.setUnit(String.valueOf(10));
317     limitEntity.setTime("Day");
318     limitEntity.setValue(String.valueOf(100));
319     return limitEntity;
320   }
321 }