[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / test / java / org / openecomp / sdc / vendorlicense / FeatureGroupTest.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.vendorlicense.dao.*;
29 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
30 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
31 import org.openecomp.sdc.vendorlicense.facade.impl.VendorLicenseFacadeImpl;
32 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
33 import org.openecomp.sdc.versioning.VersioningManager;
34 import org.openecomp.sdc.versioning.dao.types.Version;
35 import org.testng.Assert;
36 import org.testng.annotations.BeforeMethod;
37 import org.testng.annotations.Test;
38
39 import java.util.HashSet;
40 import java.util.Set;
41
42 import static org.mockito.Matchers.anyObject;
43 import static org.mockito.Mockito.doReturn;
44 import static org.mockito.Mockito.never;
45 import static org.mockito.Mockito.verify;
46
47 /**
48  * Created by KATYR on 4/10/2016
49  */
50
51 public class FeatureGroupTest {
52     //JUnit Test Cases using Mockito
53     private static final Version VERSION01 = new Version(0, 1);
54     private final String FG1_NAME = "FG1 name";
55
56     @Mock
57     private VendorLicenseModelDao vendorLicenseModelDao;
58
59     @Mock
60     private LicenseAgreementDao licenseAgreementDao;
61
62     @Mock
63     private FeatureGroupDao featureGroupDao;
64
65     @Mock
66     private EntitlementPoolDao entitlementPoolDao;
67
68     @Mock
69     private LicenseKeyGroupDao licenseKeyGroupDao;
70
71     @Mock
72     private VersioningManager versioningManager;
73
74     @InjectMocks
75     @Spy
76     private VendorLicenseManagerImpl vendorLicenseManagerImpl;
77
78     public FeatureGroupEntity updateFeatureGroup(String vlmId, Version version, String id, String name, String desc,
79                                                  String partNumber, String manufacturerReferenceNumber, Set<String>
80                                                          licenseKeyGroupIds, Set<String> entitlementPoolIds, Set<String>
81                                                          referencingLicenseAgreements){
82         FeatureGroupEntity featureGroup = new FeatureGroupEntity(vlmId, version, id);
83         featureGroup.setVendorLicenseModelId(vlmId);
84         featureGroup.setVersion(version);
85         featureGroup.setId(id);
86         featureGroup.setName(name);
87         featureGroup.setDescription(desc);
88         featureGroup.setPartNumber(partNumber);
89         //featureGroup.setManufacturerReferenceNumber(manufacturerReferenceNumber);
90         featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
91         featureGroup.setEntitlementPoolIds(entitlementPoolIds);
92         featureGroup.setReferencingLicenseAgreements(referencingLicenseAgreements);
93
94         return featureGroup;
95     }
96
97     @BeforeMethod
98     public void setUp() throws Exception{
99         MockitoAnnotations.initMocks(this);
100     }
101
102     @Test
103     public void testUpdate(){
104         Set<String> licenseKeyGroupIds;
105         licenseKeyGroupIds = new HashSet<>();
106         licenseKeyGroupIds.add("lkg1");
107
108         Set<String> entitlementPoolIds;
109         entitlementPoolIds = new HashSet<>();
110         entitlementPoolIds.add("ep1");
111
112         Set<String> referencingLicenseAgreements;
113         referencingLicenseAgreements = new HashSet<>();
114         referencingLicenseAgreements.add("la1");
115
116         FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
117                 "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds,
118                 referencingLicenseAgreements);
119
120         doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
121
122         /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
123             featureGroupDao.update(featureGroupEntity);
124         verify(featureGroupDao).update(anyObject());*/
125     }
126
127     @Test
128     public void testUpdateWithoutManufacturingReferenceNumber(){
129         Set<String> licenseKeyGroupIds;
130         licenseKeyGroupIds = new HashSet<>();
131         licenseKeyGroupIds.add("lkg1");
132
133         Set<String> entitlementPoolIds;
134         entitlementPoolIds = new HashSet<>();
135         entitlementPoolIds.add("ep1");
136
137         Set<String> referencingLicenseAgreements;
138         referencingLicenseAgreements = new HashSet<>();
139         referencingLicenseAgreements.add("la1");
140
141         FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
142                 "partNumber", null, licenseKeyGroupIds, entitlementPoolIds,
143                 referencingLicenseAgreements);
144
145         doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
146
147         /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
148             featureGroupDao.update(featureGroupEntity);
149         verify(featureGroupDao, never()).update(anyObject());*/
150     }
151
152
153 }
154
155 /*
156   protected static final Version VERSION01 = new Version(0, 1);
157   protected static final String USER1 = "FeatureGroupTest_User1";
158   protected static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
159   protected static VendorLicenseFacade vendorLicenseFacade =
160       VendorLicenseFacadeFactory.getInstance().createInterface();
161
162
163   @Test
164   public void testListFeatureGroups() throws Exception {
165     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
166             .createVendorLicenseModel("vlmId_" + CommonMethods.nextUuId(), "vlm2Id desc", "icon2"),
167         USER1).getId();
168     FeatureGroupEntity
169         fg22 = LicenseAgreementTest
170         .createFeatureGroup(vlmId, VERSION01, "fg2", "FG2", "FG2 desc", null, null);
171     String fg22Id = vendorLicenseManager.createFeatureGroup(fg22, USER1).getId();
172     FeatureGroupEntity fg33 = LicenseAgreementTest
173         .createFeatureGroup(vlmId, VERSION01, "fg3", "FG3", "FG3 desc", null, null);
174     String fg33Id = vendorLicenseManager.createFeatureGroup(fg33, USER1).getId();
175
176     Collection<FeatureGroupEntity> featureGroupEntities =
177         vendorLicenseManager.listFeatureGroups(vlmId, null, USER1);
178
179     Assert.assertEquals(featureGroupEntities.size(), 2);
180     Set<String> actualIds = new HashSet<>();
181     for (FeatureGroupEntity featureGroupEntity : featureGroupEntities) {
182       actualIds.add(featureGroupEntity.getId());
183     }
184
185     Set<String> expectedIds = new HashSet<>();
186     expectedIds.add(fg22Id);
187     expectedIds.add(fg33Id);
188     for (String id : actualIds) {
189       Assert.assertTrue(expectedIds.contains(id));
190     }
191
192   }
193
194   @Test
195   public void testCreateFeatureGroup() throws Exception {
196     String testName = "testCreateFeatureGroup";
197     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
198         .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
199         .getId();
200     Set<OperationalScope> opScopeChoices = new HashSet<>();
201     opScopeChoices.add(OperationalScope.Other);
202     opScopeChoices.add(OperationalScope.Data_Center);
203     opScopeChoices.add(OperationalScope.Network_Wide);
204     EntitlementPoolEntity
205         ep = EntitlementPoolTest
206         .createEntitlementPool(vlmId, VERSION01, "EP1" + CommonMethods.nextUuId(), "EP1 dec", 80,
207             ThresholdUnit.Absolute, EntitlementMetric.Core, null, "inc1", AggregationFunction.Other,
208             "agg func1", opScopeChoices, null, EntitlementTime.Hour, null, "sku1");
209     String epId = vendorLicenseManager.createEntitlementPool(ep, USER1).getId();
210     Set<OperationalScope> opScopeChoicesLKG = new HashSet<>();
211     opScopeChoicesLKG.add(OperationalScope.CPU);
212     opScopeChoicesLKG.add(OperationalScope.VM);
213     opScopeChoicesLKG.add(OperationalScope.Availability_Zone);
214     opScopeChoicesLKG.add(OperationalScope.Data_Center);
215
216     LicenseKeyGroupEntity
217         lkg = LicenseKeyGroupTest
218         .createLicenseKeyGroup(vlmId, VERSION01, "LKG1", "LKG1 dec", LicenseKeyType.One_Time,
219             new MultiChoiceOrOther<>(opScopeChoicesLKG, null));
220     String lkgId = vendorLicenseManager.createLicenseKeyGroup(lkg, USER1).getId();
221     lkg.setId(lkgId);
222     FeatureGroupEntity fg1 =
223         createFGForTest(vlmId, "created" + CommonMethods.nextUuId(), Collections.singleton(epId),
224             Collections.singleton(lkgId));
225     FeatureGroupEntity fg1FromDB =
226         vendorLicenseManager.getFeatureGroupModel(fg1, USER1).getFeatureGroup();
227     Assert.assertTrue(fg1FromDB.equals(fg1));
228   }
229
230
231   @Test
232   public void testCreateWithExistingName_negative() {
233     String testName = "createExistingName";
234     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
235         .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
236         .getId();
237     createFGForTest(vlmId, "created", Collections.emptySet(), Collections.emptySet());
238     try {
239       FeatureGroupEntity created = LicenseAgreementTest
240           .createFeatureGroup(vlmId, null, "created", "created", "created desc",
241               Collections.emptySet(), Collections.emptySet());
242       vendorLicenseManager.createFeatureGroup(created, USER1);
243       Assert.fail();
244     } catch (CoreException exception) {
245       Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
246     }
247   }
248
249   private FeatureGroupEntity createFGForTest(String vlmId, String fgName, Set<String> epIds,
250                                              Set<String> lkgIds) {
251     FeatureGroupEntity created = LicenseAgreementTest
252         .createFeatureGroup(vlmId, null, null, fgName, "created desc", epIds, lkgIds);
253     return vendorLicenseManager.createFeatureGroup(created, USER1);
254   }
255
256   @Test
257   public void testUpdateFeatureGroup_addEP_andGET() throws Exception {
258     String testName = "testUpdateFeatureGroup_addEP_andGET";
259     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
260         .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
261         .getId();
262
263     FeatureGroupEntity fg5 = LicenseAgreementTest
264         .createFeatureGroup(vlmId, VERSION01, "id" + CommonMethods.nextUuId(),
265             "created" + CommonMethods.nextUuId(), "created desc", null, null);
266     vendorLicenseManager.createFeatureGroup(fg5, USER1).getId();
267
268
269     Set<OperationalScope> opScopeChoices = new HashSet<>();
270     opScopeChoices.add(OperationalScope.Other);
271     opScopeChoices.add(OperationalScope.Data_Center);
272
273     EntitlementPoolEntity epToAdd = EntitlementPoolTest
274         .createEntitlementPool(vlmId, VERSION01, "epToAdd", "epToAdd dec", 80,
275             ThresholdUnit.Absolute, EntitlementMetric.Core, null, "inc1", AggregationFunction.Other,
276             "agg func1", opScopeChoices, null, EntitlementTime.Hour, null, "sku1");
277     String epToAddId = vendorLicenseManager.createEntitlementPool(epToAdd, USER1).getId();
278
279     vendorLicenseManager
280         .updateFeatureGroup(fg5, null, null, CommonMethods.toSingleElementSet(epToAddId), null,
281             USER1);
282     FeatureGroupModel updatedFG = vendorLicenseManager.getFeatureGroupModel(fg5, USER1);
283     Set<EntitlementPoolEntity> updatedEPs = updatedFG.getEntitlementPools();
284
285     epToAdd.setReferencingFeatureGroups(CommonMethods.toSingleElementSet(fg5.getId()));
286
287     Assert.assertEquals(updatedEPs.size(), 1);
288     for (EntitlementPoolEntity updatedEP : updatedEPs) {
289       Assert.assertTrue(updatedEP.getReferencingFeatureGroups().contains(fg5.getId()));
290       Assert.assertEquals(updatedEP.getId(), epToAddId);
291     }
292   }
293
294   @Test
295   public void testUpdateFeatureGroup_removeLKG_andGET() throws Exception {
296     String testName = "testUpdateFeatureGroup_removeLKG_andGET";
297     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
298         .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
299         .getId();
300
301     Set<OperationalScope> opScopeChoicesLKG = new HashSet<>();
302     opScopeChoicesLKG.add(OperationalScope.CPU);
303     opScopeChoicesLKG.add(OperationalScope.VM);
304     opScopeChoicesLKG.add(OperationalScope.Availability_Zone);
305     opScopeChoicesLKG.add(OperationalScope.Data_Center);
306     LicenseKeyGroupEntity lkg = LicenseKeyGroupTest
307         .createLicenseKeyGroup(vlmId, VERSION01, "lkg" + CommonMethods.nextUuId(), "lkg desc",
308             LicenseKeyType.Unique, new MultiChoiceOrOther<>(opScopeChoicesLKG, null));
309     String lkgId = vendorLicenseManager.createLicenseKeyGroup(lkg, USER1).getId();
310     lkg.setId(lkgId);
311
312     LicenseKeyGroupEntity lkg_1 = LicenseKeyGroupTest
313         .createLicenseKeyGroup(vlmId, VERSION01, "lkg" + CommonMethods.nextUuId(), "lkg_1 desc",
314             LicenseKeyType.Unique, new MultiChoiceOrOther<>(opScopeChoicesLKG, null));
315     String lkgId_1 = vendorLicenseManager.createLicenseKeyGroup(lkg_1, USER1).getId();
316     lkg.setId(lkgId);
317
318     Set<OperationalScope> opScopeChoices = new HashSet<>();
319     opScopeChoices.add(OperationalScope.Other);
320     opScopeChoices.add(OperationalScope.Data_Center);
321     opScopeChoices.add(OperationalScope.Network_Wide);
322     EntitlementPoolEntity ep = EntitlementPoolTest
323         .createEntitlementPool(vlmId, VERSION01, "EP1" + CommonMethods.nextUuId(), "EP1 dec", 80,
324             ThresholdUnit.Absolute, EntitlementMetric.Core, null, "inc1", AggregationFunction.Other,
325             "agg func1", opScopeChoices, null, EntitlementTime.Hour, null, "sku1");
326     String epId = vendorLicenseManager.createEntitlementPool(ep, USER1).getId();
327
328     Set<String> lkgs = new HashSet<>();
329     lkgs.add(lkgId);
330     lkgs.add(lkgId_1);
331
332     FeatureGroupEntity fg = LicenseAgreementTest
333         .createFeatureGroup(vlmId, VERSION01, "fg11" + CommonMethods.nextUuId(), "FG1", "FG1 desc",
334             CommonMethods.toSingleElementSet(epId), lkgs);
335     String fgId = vendorLicenseManager.createFeatureGroup(fg, USER1).getId();
336     vendorLicenseManager
337         .updateFeatureGroup(fg, null, CommonMethods.toSingleElementSet(lkgId), null, null, USER1);
338
339     FeatureGroupModel featureGroup = vendorLicenseManager.getFeatureGroupModel(fg, USER1);
340     Set<LicenseKeyGroupEntity> licenseKeyGroups = featureGroup.getLicenseKeyGroups();
341     Assert.assertEquals(licenseKeyGroups.size(), 1);
342     List<String> lkgIds = new ArrayList<>();
343     for (LicenseKeyGroupEntity licenseKeyGroup : licenseKeyGroups) {
344       lkgIds.add(licenseKeyGroup.getId());
345     }
346
347     Assert.assertTrue(lkgIds.contains(lkgId_1));
348     Assert.assertFalse(lkgIds.contains(lkgId));
349
350   }
351
352
353   @Test
354   public void testDeleteFeatureGroup() throws Exception {
355     String testName = "testDeleteFeatureGroup";
356     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
357         .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
358         .getId();
359
360     FeatureGroupEntity fg1 =
361         createFGForTest(vlmId, "new", Collections.emptySet(), Collections.emptySet());
362     FeatureGroupEntity fg2 =
363         createFGForTest(vlmId, "newer", Collections.emptySet(), Collections.emptySet());
364     Collection<FeatureGroupEntity> featureGroupEntities =
365         vendorLicenseManager.listFeatureGroups(vlmId, null, USER1);
366     Assert.assertEquals(featureGroupEntities.size(), 2); //precondition
367
368     vendorLicenseManager.deleteFeatureGroup(fg1, USER1);
369     Assert.assertEquals(vendorLicenseManager.listFeatureGroups(vlmId, null, USER1).size(), 1);
370
371
372   }
373
374
375 }
376 */