52923877cf0329b6c6fca19d6df2fa05f273600f
[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.junit.After;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.mockito.Spy;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
29 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
30 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
31 import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
32 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes;
33 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
34 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
37 import org.openecomp.sdc.versioning.types.VersionInfo;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collection;
41 import java.util.List;
42
43 import static org.mockito.Matchers.anyObject;
44 import static org.mockito.Mockito.doReturn;
45 import static org.mockito.Mockito.verify;
46
47 public class LimitTest {
48
49   private final String USER1 = "limitTestUser1";
50   private final String LT1_NAME = "LT1 name";
51
52   private static final String VLM_ID = "VLM_ID";
53   private static final Version VERSION = new Version(0, 1);
54   private static final String EPLKG_ID = "ID";
55   private static final String LIMIT1_ID = "limit1";
56   private static final String LIMIT2_ID = "limit2";
57
58   @Mock
59   private VendorLicenseFacade vendorLicenseFacade;
60
61   @Mock
62   private LimitDao limitDao;
63
64   @InjectMocks
65   @Spy
66   private VendorLicenseManagerImpl vendorLicenseManagerImpl;
67
68   public static LimitEntity createLimitEntity(String name, LimitType type, String description,
69                                               Version version, String metric,
70                                               AggregationFunction aggregationFunction, int unit,
71                                               String time) {
72     LimitEntity limitEntity = new LimitEntity();
73     limitEntity.setName(name);
74     limitEntity.setType(type);
75     limitEntity.setDescription(description);
76     limitEntity.setVersion(version);
77     limitEntity.setMetric(metric);
78     limitEntity.setAggregationFunction(aggregationFunction);
79     limitEntity.setUnit(String.valueOf(unit));
80     limitEntity.setTime(time);
81     return limitEntity;
82   }
83
84   @Before
85   public void setUp() throws Exception {
86     MockitoAnnotations.openMocks(this);
87   }
88
89   @After
90   public void tearDown(){
91     vendorLicenseManagerImpl = null;
92   }
93
94   @Test
95   public void testUpdateLimit() {
96     Version version = new Version();
97     LimitEntity limitEntity1 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
98         "Core", AggregationFunction.Average, 10, "Hour");
99     LimitEntity limitEntity2 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
100         "Tokens", AggregationFunction.Peak, 12, "Month");
101     VersionInfo info = new VersionInfo();
102     info.getViewableVersions().add(version);
103     info.setActiveVersion(version);
104
105     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
106     doReturn(true).when(limitDao).isLimitPresent(anyObject());
107     doReturn(limitEntity1).when(limitDao).get(anyObject());
108
109     List<LimitEntity> limitEntityList = new ArrayList<>();
110     limitEntityList.add(limitEntity1);
111     limitEntityList.add(limitEntity2);
112     limitEntity1.setId("1234");
113     limitEntity2.setId("1234");
114     doReturn(limitEntityList).when(vendorLicenseFacade)
115         .listLimits(anyObject(), anyObject(), anyObject());
116
117     vendorLicenseManagerImpl.updateLimit(limitEntity2);
118
119     verify(vendorLicenseFacade).updateLimit(anyObject());
120   }
121
122   @Test
123   public void testUpdateLimitErrorWithSameNameType() {
124     try {
125       Version version = new Version();
126       LimitEntity limitEntity1 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
127           "Core", AggregationFunction.Average, 10, "Hour");
128       LimitEntity limitEntity2 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
129           "Tokens", AggregationFunction.Peak, 12, "Month");
130       VersionInfo info = new VersionInfo();
131       info.getViewableVersions().add(version);
132       info.setActiveVersion(version);
133
134 /*      doReturn(info).when(vendorLicenseFacade)
135           .getVersionInfo(anyObject(), anyObject(), anyObject());*/
136       doReturn(limitEntity1).when(limitDao).get(anyObject());
137
138       List<LimitEntity> limitEntityList = new ArrayList<>();
139       limitEntityList.add(limitEntity1);
140       limitEntityList.add(limitEntity2);
141       limitEntity1.setId("1234");
142       limitEntity2.setId("9632");
143       doReturn(limitEntityList).when(vendorLicenseFacade)
144           .listLimits(anyObject(), anyObject(), anyObject());
145
146       vendorLicenseManagerImpl.updateLimit(limitEntity2);
147       Assert.fail();
148     } catch (CoreException exception) {
149       Assert.assertEquals(exception.code().id(),
150           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
151     }
152   }
153
154   @Test
155   public void testDeleteLimit() {
156     Version version = new Version();
157     LimitEntity limitEntity = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
158         "Core", AggregationFunction.Average, 10, "Hour");
159     VersionInfo info = new VersionInfo();
160     info.getViewableVersions().add(version);
161     info.setActiveVersion(version);
162
163     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
164     doReturn(true).when(limitDao).isLimitPresent(anyObject());
165     doReturn(limitEntity).when(limitDao).get(anyObject());
166
167     List<LimitEntity> limitEntityList = new ArrayList<>();
168     limitEntityList.add(limitEntity);
169     limitEntity.setId("1234");
170
171     vendorLicenseManagerImpl.deleteLimit(limitEntity);
172
173     verify(vendorLicenseManagerImpl).deleteLimit(anyObject());
174   }
175
176   @Test
177   public void testUpdateLimitErrorWithInvalidId() {
178     try {
179       Version version = new Version();
180       LimitEntity limitEntity1 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
181           "Core", AggregationFunction.Average, 10, "Hour");
182       LimitEntity limitEntity2 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
183           "Tokens", AggregationFunction.Peak, 12, "Month");
184       VersionInfo info = new VersionInfo();
185       info.getViewableVersions().add(version);
186       info.setActiveVersion(version);
187
188 /*      doReturn(info).when(vendorLicenseFacade)
189           .getVersionInfo(anyObject(), anyObject(), anyObject());*/
190       doReturn(null).when(limitDao).get(anyObject());
191
192       vendorLicenseManagerImpl.updateLimit(limitEntity2);
193       Assert.fail();
194     } catch (CoreException exception) {
195       Assert.assertEquals(exception.code().id(),
196           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
197     }
198   }
199
200   @Test
201   public void testList() {
202     doReturn(Arrays.asList(
203         createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID),
204         createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID)))
205         .when(vendorLicenseFacade).listLimits(VLM_ID, VERSION, EPLKG_ID);
206
207     final Collection<LimitEntity> limits =
208         vendorLicenseManagerImpl.listLimits(VLM_ID, VERSION, EPLKG_ID);
209     Assert.assertEquals(limits.size(), 2);
210     for (LimitEntity limit : limits) {
211       Assert.assertEquals(limit.getName(),
212           LIMIT1_ID.equals(limit.getId()) ? LIMIT1_ID + " name" : LIMIT2_ID + " name");
213     }
214   }
215
216   @Test
217   public void testCreateLimit() {
218     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
219     VersionInfo info = new VersionInfo();
220     info.getViewableVersions().add(VERSION);
221     info.setActiveVersion(VERSION);
222
223     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
224
225     vendorLicenseManagerImpl.createLimit(expected);
226     verify(vendorLicenseFacade).createLimit(expected);
227   }
228
229   @Test
230   public void testCreateWithDuplicateName() {
231     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
232     expected.setType(LimitType.Vendor);
233
234     LimitEntity expectedDiffName = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID);
235     expectedDiffName.setName(LIMIT1_ID + " name");
236     expectedDiffName.setType(LimitType.Vendor);
237
238     List<LimitEntity> vfcImageList = new ArrayList<LimitEntity>();
239     vfcImageList.add(expectedDiffName);
240     doReturn(vfcImageList).when(vendorLicenseFacade)
241         .listLimits(anyObject(), anyObject(), anyObject());
242
243     VersionInfo info = new VersionInfo();
244     info.getViewableVersions().add(VERSION);
245     info.setActiveVersion(VERSION);
246
247 /*    doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
248
249     try {
250       vendorLicenseManagerImpl.createLimit(expected);
251       Assert.fail();
252     } catch (CoreException ex) {
253       Assert.assertEquals(ex.code().id(),
254           VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED);
255     }
256   }
257
258   @Test
259   public void testGetNonExistingLimitId_negative() {
260     LimitEntity limit = createLimit(VLM_ID, VERSION, EPLKG_ID, "non existing limit id");
261     VersionInfo info = new VersionInfo();
262     info.getViewableVersions().add(VERSION);
263     info.setActiveVersion(VERSION);
264
265     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
266
267     try {
268       vendorLicenseManagerImpl.getLimit(limit);
269       Assert.fail();
270     } catch (CoreException exception) {
271       Assert.assertEquals(exception.code().id(),
272           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
273     }
274   }
275
276   @Test
277   public void testGet() {
278     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
279     expected.setType(LimitType.Vendor);
280     expected.setValue(String.valueOf(100));
281     expected.setUnit(String.valueOf(10));
282     expected.setAggregationFunction(AggregationFunction.Average);
283     expected.setMetric("BWTH");
284     expected.setTime("Day");
285
286     doReturn(true).when(limitDao).isLimitPresent(anyObject());
287     doReturn(expected).when(limitDao).get(anyObject());
288     VersionInfo info = new VersionInfo();
289     info.getViewableVersions().add(VERSION);
290     info.setActiveVersion(VERSION);
291
292     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
293
294     LimitEntity actual = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
295     vendorLicenseManagerImpl.getLimit(actual);
296     Assert.assertEquals(actual.getId(), expected.getId());
297     Assert.assertEquals(actual.getName(), expected.getName());
298     Assert.assertEquals(actual.getUnit(), expected.getUnit());
299     Assert.assertEquals(actual.getValue(), expected.getValue());
300     Assert.assertEquals(actual.getAggregationFunction().name(), expected.getAggregationFunction()
301         .name());
302     Assert.assertEquals(actual.getMetric(), expected.getMetric());
303
304   }
305
306   static LimitEntity createLimit(String vlmId, Version version, String epLkgId, String limitId) {
307     LimitEntity limitEntity = new LimitEntity(vlmId, version, epLkgId, limitId);
308     limitEntity.setName(limitId + " name");
309     limitEntity.setDescription(limitId + " desc");
310     limitEntity.setVersion(version);
311     limitEntity.setMetric("BWTH");
312     limitEntity.setAggregationFunction(AggregationFunction.Average);
313     limitEntity.setUnit(String.valueOf(10));
314     limitEntity.setTime("Day");
315     limitEntity.setValue(String.valueOf(100));
316     return limitEntity;
317   }
318 }