[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / test / java / org / openecomp / sdc / vendorlicense / LicenseKeyGroupTest.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
22 package org.openecomp.sdc.vendorlicense;
23
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.mockito.Spy;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
30 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
31 import org.openecomp.sdc.vendorlicense.dao.types.*;
32 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
33 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
34 import org.openecomp.sdc.versioning.dao.types.Version;
35 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
36 import org.openecomp.sdc.versioning.types.VersionInfo;
37 import org.testng.Assert;
38 import org.testng.annotations.BeforeMethod;
39 import org.testng.annotations.Test;
40
41 import java.lang.reflect.Field;
42 import java.lang.reflect.Modifier;
43 import java.util.ArrayList;
44 import java.util.HashSet;
45 import java.util.Set;
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 LicenseKeyGroupTest {
52
53     //JUnit Test Cases using Mockito
54     private  final String USER = "lkgTestUser";
55     private  final String LKG_NAME = "LKG name";
56     private  final String LT_NAME = "LT name";
57
58     @Mock
59     private VendorLicenseFacade vendorLicenseFacade;
60
61     @Mock
62     private LicenseKeyGroupDao licenseKeyGroupDao;
63     @Mock
64     private LimitDao limitDao;
65
66     @InjectMocks
67     @Spy
68     private VendorLicenseManagerImpl vendorLicenseManagerImpl;
69
70     @BeforeMethod
71     public void setUp() throws Exception {
72         MockitoAnnotations.initMocks(this);
73     }
74
75     private LicenseKeyGroupEntity createLicenseKeyGroup(LicenseKeyType type, Set<OperationalScope> operationalScopeChoices,
76                                                             String operationalScopeOther)
77     {
78         LicenseKeyGroupEntity licenseKeyGroupEntity = new LicenseKeyGroupEntity();
79         licenseKeyGroupEntity.setType(type);
80         licenseKeyGroupEntity.setOperationalScope(
81                 new MultiChoiceOrOther<>(operationalScopeChoices, operationalScopeOther));
82         return licenseKeyGroupEntity;
83     }
84
85     @Test
86     public void deleteLicenseKeyGroupTest() {
87         Set<OperationalScope> opScopeChoices;
88         opScopeChoices = new HashSet<>();
89         opScopeChoices.add(OperationalScope.Core);
90         opScopeChoices.add(OperationalScope.CPU);
91         opScopeChoices.add(OperationalScope.Network_Wide);
92
93         LicenseKeyGroupEntity licenseKeyGroup =
94                 createLicenseKeyGroup(LicenseKeyType.Unique, opScopeChoices, null);
95
96         VersionInfo info = new VersionInfo();
97         Version version = new Version();
98         info.getViewableVersions().add(version);
99         info.setActiveVersion(version);
100         doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
101
102         LimitEntity limitEntity = LimitTest.createLimitEntity(LT_NAME,LimitType.Vendor,"string",version,
103                 EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
104
105         ArrayList<LimitEntity> limitEntityList = new ArrayList();
106         limitEntityList.add(limitEntity);
107
108         doReturn(licenseKeyGroup).when(licenseKeyGroupDao).get(anyObject());
109         doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
110         doReturn(true).when(limitDao).isLimitPresent(anyObject());
111         doReturn(limitEntity).when(limitDao).get(anyObject());
112         try {
113             Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
114             limitField.setAccessible(true);
115             Field modifiersField = Field.class.getDeclaredField("modifiers");
116             modifiersField.setAccessible(true);
117             modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
118             limitField.set(null, limitDao);
119
120             Field lkgField = VendorLicenseManagerImpl.class.getDeclaredField("licenseKeyGroupDao");
121             lkgField.setAccessible(true);
122             modifiersField = Field.class.getDeclaredField("modifiers");
123             modifiersField.setAccessible(true);
124             modifiersField.setInt(lkgField, lkgField.getModifiers() & ~Modifier.FINAL);
125             lkgField.set(null, licenseKeyGroupDao);
126         } catch(NoSuchFieldException | IllegalAccessException e)
127         {
128             Assert.fail();
129         }
130
131         vendorLicenseManagerImpl.deleteLicenseKeyGroup(licenseKeyGroup, USER);
132
133         verify(limitDao).delete(anyObject());
134     }
135
136     @Test
137     public void deleteLicenseKeyGroupInvalidTest() {
138         try {
139             Set<OperationalScope> opScopeChoices;
140             opScopeChoices = new HashSet<>();
141             opScopeChoices.add(OperationalScope.Core);
142             opScopeChoices.add(OperationalScope.CPU);
143             opScopeChoices.add(OperationalScope.Network_Wide);
144
145             LicenseKeyGroupEntity licenseKeyGroup =
146                 createLicenseKeyGroup(LicenseKeyType.Unique, opScopeChoices, null);
147
148             VersionInfo info = new VersionInfo();
149             Version version = new Version();
150             info.getViewableVersions().add(version);
151             info.setActiveVersion(version);
152             doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(),anyObject(),anyObject());
153
154             LimitEntity limitEntity = LimitTest.createLimitEntity(LT_NAME,LimitType.Vendor,"string",version,
155                 EntitlementMetric.Core,AggregationFunction.Average,10,EntitlementTime.Hour);
156
157             ArrayList<LimitEntity> limitEntityList = new ArrayList();
158             limitEntityList.add(limitEntity);
159
160             doReturn(licenseKeyGroup).when(licenseKeyGroupDao).get(anyObject());
161             doReturn(limitEntityList).when(vendorLicenseFacade).listLimits(anyObject(), anyObject(), anyObject(), anyObject());
162             doReturn(false).when(limitDao).isLimitPresent(anyObject());
163
164             try {
165                 Field limitField = VendorLicenseManagerImpl.class.getDeclaredField("limitDao");
166                 limitField.setAccessible(true);
167                 Field modifiersField = Field.class.getDeclaredField("modifiers");
168                 modifiersField.setAccessible(true);
169                 modifiersField.setInt(limitField, limitField.getModifiers() & ~Modifier.FINAL);
170                 limitField.set(null, limitDao);
171
172                 Field lkgField = VendorLicenseManagerImpl.class.getDeclaredField("licenseKeyGroupDao");
173                 lkgField.setAccessible(true);
174                 modifiersField = Field.class.getDeclaredField("modifiers");
175                 modifiersField.setAccessible(true);
176                 modifiersField.setInt(lkgField, lkgField.getModifiers() & ~Modifier.FINAL);
177                 lkgField.set(null, licenseKeyGroupDao);
178             } catch(NoSuchFieldException | IllegalAccessException e)
179             {
180                 Assert.fail();
181             }
182
183             vendorLicenseManagerImpl.deleteLicenseKeyGroup(licenseKeyGroup, USER);
184         } catch (CoreException exception) {
185             Assert.assertEquals(exception.code().id(), VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
186         }
187     }
188
189   /*public static final String LKG1_NAME = "LKG1 name";
190   private static final Version VERSION01 = new Version(0, 1);
191   private static final String USER1 = "user1";
192   public static String vlm1Id;
193   public static String vlm2Id;
194   private static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
195   private static LicenseKeyGroupDao licenseKeyGroupDao;
196   private static NoSqlDb noSqlDb;
197   private static String lkg1Id;
198   private static String lkg2Id;
199
200   public static LicenseKeyGroupEntity createLicenseKeyGroup(String vlmId, Version version,
201                                                             String name, String desc,
202                                                             LicenseKeyType type,
203                                                             MultiChoiceOrOther<OperationalScope> operationalScope) {
204     LicenseKeyGroupEntity licenseKeyGroup = new LicenseKeyGroupEntity();
205     licenseKeyGroup.setVendorLicenseModelId(vlmId);
206     licenseKeyGroup.setVersion(version);
207     licenseKeyGroup.setName(name);
208     licenseKeyGroup.setDescription(desc);
209     licenseKeyGroup.setType(type);
210     licenseKeyGroup.setOperationalScope(operationalScope);
211     return licenseKeyGroup;
212   }
213
214   @BeforeClass
215   private void init() {
216     licenseKeyGroupDao = LicenseKeyGroupDaoFactory.getInstance().createInterface();
217     noSqlDb = NoSqlDbFactory.getInstance().createInterface();
218
219     vlm1Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
220         .createVendorLicenseModel("vendor1 name " + CommonMethods.nextUuId(), "vlm1Id dec",
221             "icon1"), USER1).getId();
222     vlm2Id = vendorLicenseManager.createVendorLicenseModel(VendorLicenseModelTest
223             .createVendorLicenseModel("vendor2 name " + CommonMethods.nextUuId(), "vlm2 dec", "icon2"),
224         USER1).getId();
225   }
226
227   @Test
228   public void createTest() {
229     lkg1Id = testCreate(vlm1Id, LKG1_NAME);
230   }
231
232   private String testCreate(String vlmId, String name) {
233     Set<OperationalScope> opScopeChoices = new HashSet<>();
234     opScopeChoices.add(OperationalScope.CPU);
235     opScopeChoices.add(OperationalScope.VM);
236     opScopeChoices.add(OperationalScope.Tenant);
237     opScopeChoices.add(OperationalScope.Data_Center);
238     LicenseKeyGroupEntity
239         lkg1 = createLicenseKeyGroup(vlmId, VERSION01, name, "LKG1 dec", LicenseKeyType.One_Time,
240         new MultiChoiceOrOther<>(opScopeChoices, null));
241     String lkg1Id = vendorLicenseManager.createLicenseKeyGroup(lkg1, USER1).getId();
242     lkg1.setId(lkg1Id);
243
244     LicenseKeyGroupEntity loadedLkg1 = licenseKeyGroupDao.get(lkg1);
245     Assert.assertTrue(loadedLkg1.equals(lkg1));
246     return lkg1Id;
247   }
248
249   @Test(dependsOnMethods = {"createTest"})
250   public void testCreateWithExistingName_negative() {
251     try {
252       LicenseKeyGroupEntity lkg1 =
253           createLicenseKeyGroup(vlm1Id, VERSION01, LKG1_NAME, "LKG1 dec", LicenseKeyType.One_Time,
254               new MultiChoiceOrOther<>(Collections.singleton(OperationalScope.Other),
255                   "other op scope"));
256       vendorLicenseManager.createLicenseKeyGroup(lkg1, USER1).getId();
257       Assert.fail();
258     } catch (CoreException exception) {
259       Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
260     }
261   }
262
263   @Test(dependsOnMethods = {"createTest"})
264   public void testCreateWithExistingNameUnderOtherVlm() {
265     testCreate(vlm2Id, LKG1_NAME);
266   }
267
268   @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
269   public void updateAndGetTest() {
270     LicenseKeyGroupEntity lkg1 =
271         licenseKeyGroupDao.get(new LicenseKeyGroupEntity(vlm1Id, VERSION01, lkg1Id));
272     Set<OperationalScope> opScopeChoices = new HashSet<>();
273     opScopeChoices.add(OperationalScope.Other);
274     lkg1.setOperationalScope(new MultiChoiceOrOther<>(opScopeChoices, "op scope1 updated"));
275     lkg1.setDescription("LKG1 dec updated");
276
277     vendorLicenseManager.updateLicenseKeyGroup(lkg1, USER1);
278
279     LicenseKeyGroupEntity loadedLkg1 = vendorLicenseManager.getLicenseKeyGroup(lkg1, USER1);
280     Assert.assertTrue(loadedLkg1.equals(lkg1));
281
282   }
283
284   @Test(dependsOnMethods = {"updateAndGetTest"})
285   public void listTest() {
286     Set<OperationalScope> opScopeChoices = new HashSet<>();
287     opScopeChoices.add(OperationalScope.Network_Wide);
288     LicenseKeyGroupEntity lkg2 =
289         createLicenseKeyGroup(vlm1Id, VERSION01, "LKG2", "LKG2 dec", LicenseKeyType.Universal,
290             new MultiChoiceOrOther<>(opScopeChoices, null));
291     lkg2Id = vendorLicenseManager.createLicenseKeyGroup(lkg2, USER1).getId();
292     lkg2.setId(lkg2Id);
293
294     Collection<LicenseKeyGroupEntity> loadedLkgs =
295         vendorLicenseManager.listLicenseKeyGroups(vlm1Id, null, USER1);
296     Assert.assertEquals(loadedLkgs.size(), 2);
297     for (LicenseKeyGroupEntity loadedLkg : loadedLkgs) {
298       if (lkg2Id.equals(loadedLkg.getId())) {
299         Assert.assertTrue(loadedLkg.equals(lkg2));
300       }
301     }
302   }
303
304   @Test(dependsOnMethods = {"listTest"})
305   public void deleteTest() {
306     vendorLicenseManager
307         .deleteLicenseKeyGroup(new LicenseKeyGroupEntity(vlm1Id, VERSION01, lkg1Id), USER1);
308
309     LicenseKeyGroupEntity loadedLkg1 =
310         licenseKeyGroupDao.get(new LicenseKeyGroupEntity(vlm1Id, VERSION01, lkg1Id));
311     Assert.assertEquals(loadedLkg1, null);
312
313     Collection<LicenseKeyGroupEntity> loadedLkgs =
314         licenseKeyGroupDao.list(new LicenseKeyGroupEntity(vlm1Id, VERSION01, null));
315     Assert.assertEquals(loadedLkgs.size(), 1);
316     Assert.assertEquals(loadedLkgs.iterator().next().getId(), lkg2Id);
317   }
318
319   @Test(dependsOnMethods = "deleteTest")
320   public void testCreateWithRemovedName() {
321     testCreate(vlm1Id, LKG1_NAME);
322   }
323   */
324 }