e41cfa95323852c7f34bc38fe0f95a2dbc116eef
[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.openecomp.core.util.UniqueValueUtil;
25 import org.openecomp.core.utilities.CommonMethods;
26 import org.openecomp.sdc.common.errors.CoreException;
27 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
28 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric;
29 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
30 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime;
31 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
32 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel;
33 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
34 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyType;
35 import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther;
36 import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
37 import org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit;
38 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
39 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
40 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
41 import org.openecomp.sdc.versioning.dao.types.Version;
42 import org.testng.Assert;
43 import org.testng.annotations.Test;
44
45 import java.util.ArrayList;
46 import java.util.Collection;
47 import java.util.Collections;
48 import java.util.HashSet;
49 import java.util.List;
50 import java.util.Set;
51
52 */
53 /**
54  * Created by KATYR on 4/10/2016
55  *//*
56
57 public class FeatureGroupTest {
58   protected static final Version VERSION01 = new Version(0, 1);
59   protected static final String USER1 = "FeatureGroupTest_User1";
60   protected static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
61   protected static VendorLicenseFacade vendorLicenseFacade =
62       VendorLicenseFacadeFactory.getInstance().createInterface();
63
64
65   @Test
66   public void testListFeatureGroups() throws Exception {
67     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
68             .createVendorLicenseModel("vlmId_" + CommonMethods.nextUuId(), "vlm2Id desc", "icon2"),
69         USER1).getId();
70     FeatureGroupEntity
71         fg22 = LicenseAgreementTest
72         .createFeatureGroup(vlmId, VERSION01, "fg2", "FG2", "FG2 desc", null, null);
73     String fg22Id = vendorLicenseManager.createFeatureGroup(fg22, USER1).getId();
74     FeatureGroupEntity fg33 = LicenseAgreementTest
75         .createFeatureGroup(vlmId, VERSION01, "fg3", "FG3", "FG3 desc", null, null);
76     String fg33Id = vendorLicenseManager.createFeatureGroup(fg33, USER1).getId();
77
78     Collection<FeatureGroupEntity> featureGroupEntities =
79         vendorLicenseManager.listFeatureGroups(vlmId, null, USER1);
80
81     Assert.assertEquals(featureGroupEntities.size(), 2);
82     Set<String> actualIds = new HashSet<>();
83     for (FeatureGroupEntity featureGroupEntity : featureGroupEntities) {
84       actualIds.add(featureGroupEntity.getId());
85     }
86
87     Set<String> expectedIds = new HashSet<>();
88     expectedIds.add(fg22Id);
89     expectedIds.add(fg33Id);
90     for (String id : actualIds) {
91       Assert.assertTrue(expectedIds.contains(id));
92     }
93
94   }
95
96   @Test
97   public void testCreateFeatureGroup() throws Exception {
98     String testName = "testCreateFeatureGroup";
99     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
100         .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
101         .getId();
102     Set<OperationalScope> opScopeChoices = new HashSet<>();
103     opScopeChoices.add(OperationalScope.Other);
104     opScopeChoices.add(OperationalScope.Data_Center);
105     opScopeChoices.add(OperationalScope.Network_Wide);
106     EntitlementPoolEntity
107         ep = EntitlementPoolTest
108         .createEntitlementPool(vlmId, VERSION01, "EP1" + CommonMethods.nextUuId(), "EP1 dec", 80,
109             ThresholdUnit.Absolute, EntitlementMetric.Core, null, "inc1", AggregationFunction.Other,
110             "agg func1", opScopeChoices, null, EntitlementTime.Hour, null, "sku1");
111     String epId = vendorLicenseManager.createEntitlementPool(ep, USER1).getId();
112     Set<OperationalScope> opScopeChoicesLKG = new HashSet<>();
113     opScopeChoicesLKG.add(OperationalScope.CPU);
114     opScopeChoicesLKG.add(OperationalScope.VM);
115     opScopeChoicesLKG.add(OperationalScope.Availability_Zone);
116     opScopeChoicesLKG.add(OperationalScope.Data_Center);
117
118     LicenseKeyGroupEntity
119         lkg = LicenseKeyGroupTest
120         .createLicenseKeyGroup(vlmId, VERSION01, "LKG1", "LKG1 dec", LicenseKeyType.One_Time,
121             new MultiChoiceOrOther<>(opScopeChoicesLKG, null));
122     String lkgId = vendorLicenseManager.createLicenseKeyGroup(lkg, USER1).getId();
123     lkg.setId(lkgId);
124     FeatureGroupEntity fg1 =
125         createFGForTest(vlmId, "created" + CommonMethods.nextUuId(), Collections.singleton(epId),
126             Collections.singleton(lkgId));
127     FeatureGroupEntity fg1FromDB =
128         vendorLicenseManager.getFeatureGroupModel(fg1, USER1).getFeatureGroup();
129     Assert.assertTrue(fg1FromDB.equals(fg1));
130   }
131
132
133   @Test
134   public void testCreateWithExistingName_negative() {
135     String testName = "createExistingName";
136     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
137         .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
138         .getId();
139     createFGForTest(vlmId, "created", Collections.emptySet(), Collections.emptySet());
140     try {
141       FeatureGroupEntity created = LicenseAgreementTest
142           .createFeatureGroup(vlmId, null, "created", "created", "created desc",
143               Collections.emptySet(), Collections.emptySet());
144       vendorLicenseManager.createFeatureGroup(created, USER1);
145       Assert.fail();
146     } catch (CoreException exception) {
147       Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
148     }
149   }
150
151   private FeatureGroupEntity createFGForTest(String vlmId, String fgName, Set<String> epIds,
152                                              Set<String> lkgIds) {
153     FeatureGroupEntity created = LicenseAgreementTest
154         .createFeatureGroup(vlmId, null, null, fgName, "created desc", epIds, lkgIds);
155     return vendorLicenseManager.createFeatureGroup(created, USER1);
156   }
157
158   @Test
159   public void testUpdateFeatureGroup_addEP_andGET() throws Exception {
160     String testName = "testUpdateFeatureGroup_addEP_andGET";
161     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
162         .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
163         .getId();
164
165     FeatureGroupEntity fg5 = LicenseAgreementTest
166         .createFeatureGroup(vlmId, VERSION01, "id" + CommonMethods.nextUuId(),
167             "created" + CommonMethods.nextUuId(), "created desc", null, null);
168     vendorLicenseManager.createFeatureGroup(fg5, USER1).getId();
169
170
171     Set<OperationalScope> opScopeChoices = new HashSet<>();
172     opScopeChoices.add(OperationalScope.Other);
173     opScopeChoices.add(OperationalScope.Data_Center);
174
175     EntitlementPoolEntity epToAdd = EntitlementPoolTest
176         .createEntitlementPool(vlmId, VERSION01, "epToAdd", "epToAdd dec", 80,
177             ThresholdUnit.Absolute, EntitlementMetric.Core, null, "inc1", AggregationFunction.Other,
178             "agg func1", opScopeChoices, null, EntitlementTime.Hour, null, "sku1");
179     String epToAddId = vendorLicenseManager.createEntitlementPool(epToAdd, USER1).getId();
180
181     vendorLicenseManager
182         .updateFeatureGroup(fg5, null, null, CommonMethods.toSingleElementSet(epToAddId), null,
183             USER1);
184     FeatureGroupModel updatedFG = vendorLicenseManager.getFeatureGroupModel(fg5, USER1);
185     Set<EntitlementPoolEntity> updatedEPs = updatedFG.getEntitlementPools();
186
187     epToAdd.setReferencingFeatureGroups(CommonMethods.toSingleElementSet(fg5.getId()));
188
189     Assert.assertEquals(updatedEPs.size(), 1);
190     for (EntitlementPoolEntity updatedEP : updatedEPs) {
191       Assert.assertTrue(updatedEP.getReferencingFeatureGroups().contains(fg5.getId()));
192       Assert.assertEquals(updatedEP.getId(), epToAddId);
193     }
194   }
195
196   @Test
197   public void testUpdateFeatureGroup_removeLKG_andGET() throws Exception {
198     String testName = "testUpdateFeatureGroup_removeLKG_andGET";
199     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
200         .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
201         .getId();
202
203     Set<OperationalScope> opScopeChoicesLKG = new HashSet<>();
204     opScopeChoicesLKG.add(OperationalScope.CPU);
205     opScopeChoicesLKG.add(OperationalScope.VM);
206     opScopeChoicesLKG.add(OperationalScope.Availability_Zone);
207     opScopeChoicesLKG.add(OperationalScope.Data_Center);
208     LicenseKeyGroupEntity lkg = LicenseKeyGroupTest
209         .createLicenseKeyGroup(vlmId, VERSION01, "lkg" + CommonMethods.nextUuId(), "lkg desc",
210             LicenseKeyType.Unique, new MultiChoiceOrOther<>(opScopeChoicesLKG, null));
211     String lkgId = vendorLicenseManager.createLicenseKeyGroup(lkg, USER1).getId();
212     lkg.setId(lkgId);
213
214     LicenseKeyGroupEntity lkg_1 = LicenseKeyGroupTest
215         .createLicenseKeyGroup(vlmId, VERSION01, "lkg" + CommonMethods.nextUuId(), "lkg_1 desc",
216             LicenseKeyType.Unique, new MultiChoiceOrOther<>(opScopeChoicesLKG, null));
217     String lkgId_1 = vendorLicenseManager.createLicenseKeyGroup(lkg_1, USER1).getId();
218     lkg.setId(lkgId);
219
220     Set<OperationalScope> opScopeChoices = new HashSet<>();
221     opScopeChoices.add(OperationalScope.Other);
222     opScopeChoices.add(OperationalScope.Data_Center);
223     opScopeChoices.add(OperationalScope.Network_Wide);
224     EntitlementPoolEntity ep = EntitlementPoolTest
225         .createEntitlementPool(vlmId, VERSION01, "EP1" + CommonMethods.nextUuId(), "EP1 dec", 80,
226             ThresholdUnit.Absolute, EntitlementMetric.Core, null, "inc1", AggregationFunction.Other,
227             "agg func1", opScopeChoices, null, EntitlementTime.Hour, null, "sku1");
228     String epId = vendorLicenseManager.createEntitlementPool(ep, USER1).getId();
229
230     Set<String> lkgs = new HashSet<>();
231     lkgs.add(lkgId);
232     lkgs.add(lkgId_1);
233
234     FeatureGroupEntity fg = LicenseAgreementTest
235         .createFeatureGroup(vlmId, VERSION01, "fg11" + CommonMethods.nextUuId(), "FG1", "FG1 desc",
236             CommonMethods.toSingleElementSet(epId), lkgs);
237     String fgId = vendorLicenseManager.createFeatureGroup(fg, USER1).getId();
238     vendorLicenseManager
239         .updateFeatureGroup(fg, null, CommonMethods.toSingleElementSet(lkgId), null, null, USER1);
240
241     FeatureGroupModel featureGroup = vendorLicenseManager.getFeatureGroupModel(fg, USER1);
242     Set<LicenseKeyGroupEntity> licenseKeyGroups = featureGroup.getLicenseKeyGroups();
243     Assert.assertEquals(licenseKeyGroups.size(), 1);
244     List<String> lkgIds = new ArrayList<>();
245     for (LicenseKeyGroupEntity licenseKeyGroup : licenseKeyGroups) {
246       lkgIds.add(licenseKeyGroup.getId());
247     }
248
249     Assert.assertTrue(lkgIds.contains(lkgId_1));
250     Assert.assertFalse(lkgIds.contains(lkgId));
251
252   }
253
254
255   @Test
256   public void testDeleteFeatureGroup() throws Exception {
257     String testName = "testDeleteFeatureGroup";
258     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
259         .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
260         .getId();
261
262     FeatureGroupEntity fg1 =
263         createFGForTest(vlmId, "new", Collections.emptySet(), Collections.emptySet());
264     FeatureGroupEntity fg2 =
265         createFGForTest(vlmId, "newer", Collections.emptySet(), Collections.emptySet());
266     Collection<FeatureGroupEntity> featureGroupEntities =
267         vendorLicenseManager.listFeatureGroups(vlmId, null, USER1);
268     Assert.assertEquals(featureGroupEntities.size(), 2); //precondition
269
270     vendorLicenseManager.deleteFeatureGroup(fg1, USER1);
271     Assert.assertEquals(vendorLicenseManager.listFeatureGroups(vlmId, null, USER1).size(), 1);
272
273
274   }
275
276
277 }
278 */