re base code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / test / java / org / openecomp / sdc / vendorlicense / LimitTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.vendorlicense;
18
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;
38
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.Collection;
42 import java.util.List;
43
44 import static org.mockito.Matchers.anyObject;
45 import static org.mockito.Mockito.doReturn;
46 import static org.mockito.Mockito.verify;
47
48 public class LimitTest {
49
50   private final String USER1 = "limitTestUser1";
51   private final String LT1_NAME = "LT1 name";
52
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";
58
59   @Mock
60   private VendorLicenseFacade vendorLicenseFacade;
61
62   @Mock
63   private LimitDao limitDao;
64
65   @InjectMocks
66   @Spy
67   private VendorLicenseManagerImpl vendorLicenseManagerImpl;
68
69   public static LimitEntity createLimitEntity(String name, LimitType type, String description,
70                                               Version version, String metric,
71                                               AggregationFunction aggregationFunction, int unit,
72                                               String time) {
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);
82     return limitEntity;
83   }
84
85   @BeforeMethod
86   public void setUp() throws Exception {
87     MockitoAnnotations.initMocks(this);
88   }
89
90   @AfterMethod
91   public void tearDown(){
92     vendorLicenseManagerImpl = null;
93   }
94
95   @Test
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);
105
106     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
107     doReturn(true).when(limitDao).isLimitPresent(anyObject());
108     doReturn(limitEntity1).when(limitDao).get(anyObject());
109
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());
117
118     vendorLicenseManagerImpl.updateLimit(limitEntity2);
119
120     verify(vendorLicenseFacade).updateLimit(anyObject());
121   }
122
123   @Test
124   public void testUpdateLimitErrorWithSameNameType() {
125     try {
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);
134
135 /*      doReturn(info).when(vendorLicenseFacade)
136           .getVersionInfo(anyObject(), anyObject(), anyObject());*/
137       doReturn(limitEntity1).when(limitDao).get(anyObject());
138
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());
146
147       vendorLicenseManagerImpl.updateLimit(limitEntity2);
148       Assert.fail();
149     } catch (CoreException exception) {
150       Assert.assertEquals(exception.code().id(),
151           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
152     }
153   }
154
155   @Test
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);
163
164     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
165     doReturn(true).when(limitDao).isLimitPresent(anyObject());
166     doReturn(limitEntity).when(limitDao).get(anyObject());
167
168     List<LimitEntity> limitEntityList = new ArrayList<>();
169     limitEntityList.add(limitEntity);
170     limitEntity.setId("1234");
171
172     vendorLicenseManagerImpl.deleteLimit(limitEntity);
173
174     verify(vendorLicenseManagerImpl).deleteLimit(anyObject());
175   }
176
177   @Test
178   public void testUpdateLimitErrorWithInvalidId() {
179     try {
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);
188
189 /*      doReturn(info).when(vendorLicenseFacade)
190           .getVersionInfo(anyObject(), anyObject(), anyObject());*/
191       doReturn(null).when(limitDao).get(anyObject());
192
193       vendorLicenseManagerImpl.updateLimit(limitEntity2);
194       Assert.fail();
195     } catch (CoreException exception) {
196       Assert.assertEquals(exception.code().id(),
197           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
198     }
199   }
200
201   @Test
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);
207
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");
214     }
215   }
216
217   @Test
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);
223
224     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
225
226     vendorLicenseManagerImpl.createLimit(expected);
227     verify(vendorLicenseFacade).createLimit(expected);
228   }
229
230   @Test
231   public void testCreateWithDuplicateName() {
232     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
233     expected.setType(LimitType.Vendor);
234
235     LimitEntity expectedDiffName = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID);
236     expectedDiffName.setName(LIMIT1_ID + " name");
237     expectedDiffName.setType(LimitType.Vendor);
238
239     List<LimitEntity> vfcImageList = new ArrayList<LimitEntity>();
240     vfcImageList.add(expectedDiffName);
241     doReturn(vfcImageList).when(vendorLicenseFacade)
242         .listLimits(anyObject(), anyObject(), anyObject());
243
244     VersionInfo info = new VersionInfo();
245     info.getViewableVersions().add(VERSION);
246     info.setActiveVersion(VERSION);
247
248 /*    doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
249
250     try {
251       vendorLicenseManagerImpl.createLimit(expected);
252       Assert.fail();
253     } catch (CoreException ex) {
254       Assert.assertEquals(ex.code().id(),
255           VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED);
256     }
257   }
258
259   @Test
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);
265
266     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
267
268     try {
269       vendorLicenseManagerImpl.getLimit(limit);
270       Assert.fail();
271     } catch (CoreException exception) {
272       Assert.assertEquals(exception.code().id(),
273           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
274     }
275   }
276
277   @Test
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");
286
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);
292
293     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
294
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()
302         .name());
303     Assert.assertEquals(actual.getMetric(), expected.getMetric());
304
305   }
306
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));
317     return limitEntity;
318   }
319 }