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