push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / test / java / org / openecomp / sdc / vendorlicense / FeatureGroupTest.java
1 package org.openecomp.sdc.vendorlicense;
2
3 import org.openecomp.sdc.common.errors.CoreException;
4 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
5 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
6 import org.openecomp.sdc.versioning.dao.types.Version;
7 import org.openecomp.core.util.UniqueValueUtil;
8 import org.openecomp.core.utilities.CommonMethods;
9 import org.testng.Assert;
10 import org.testng.annotations.Test;
11
12 import java.util.*;
13
14 public class FeatureGroupTest {
15   protected static final Version VERSION01 = new Version(0, 1);
16   protected static final String USER1 = "FeatureGroupTest_User1";
17   protected static VendorLicenseManager vendorLicenseManager = new VendorLicenseManagerImpl();
18   protected static VendorLicenseFacade vendorLicenseFacade = org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory
19       .getInstance().createInterface();
20
21
22   @Test
23   public void testListFeatureGroups() throws Exception {
24     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest.createVendorLicenseModel("vlmId_" + CommonMethods.nextUuId(), "vlm2Id desc", "icon2"), USER1).getId();
25     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity
26         fg22 = LicenseAgreementTest.createFeatureGroup(vlmId, VERSION01, "fg2", "FG2", "FG2 desc", null, null);
27     String fg22Id = vendorLicenseManager.createFeatureGroup(fg22, USER1).getId();
28     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity
29         fg33 = LicenseAgreementTest.createFeatureGroup(vlmId, VERSION01, "fg3", "FG3", "FG3 desc", null, null);
30     String fg33Id = vendorLicenseManager.createFeatureGroup(fg33, USER1).getId();
31
32     Collection<org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity> featureGroupEntities = vendorLicenseManager.listFeatureGroups(vlmId, null, USER1);
33
34     Assert.assertEquals(featureGroupEntities.size(), 2);
35     Set<String> actualIds = new HashSet<>();
36     for (org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity featureGroupEntity : featureGroupEntities) {
37       actualIds.add(featureGroupEntity.getId());
38     }
39
40     Set<String> expectedIds = new HashSet<>();
41     expectedIds.add(fg22Id);
42     expectedIds.add(fg33Id);
43     for (String id : actualIds) {
44       Assert.assertTrue(expectedIds.contains(id));
45     }
46
47   }
48
49   @Test
50   public void testCreateFeatureGroup() throws Exception {
51     String testName = "testCreateFeatureGroup";
52     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest.createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1).getId();
53     Set<org.openecomp.sdc.vendorlicense.dao.types.OperationalScope> opScopeChoices = new HashSet<>();
54     opScopeChoices.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.Other);
55     opScopeChoices.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.Data_Center);
56     opScopeChoices.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.Network_Wide);
57     org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity
58         ep = EntitlementPoolTest.createEntitlementPool(vlmId, VERSION01, "EP1" + CommonMethods.nextUuId(), "EP1 dec", 80, org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit.Absolute, org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric.Core, null, "inc1", org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction.Other, "agg func1", opScopeChoices, null, org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime.Hour, null, "sku1");
59     String epId = vendorLicenseManager.createEntitlementPool(ep, USER1).getId();
60     Set<org.openecomp.sdc.vendorlicense.dao.types.OperationalScope> opScopeChoicesLKG = new HashSet<>();
61     opScopeChoicesLKG.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.CPU);
62     opScopeChoicesLKG.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.VM);
63     opScopeChoicesLKG.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.Availability_Zone);
64     opScopeChoicesLKG.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.Data_Center);
65
66     org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity
67         lkg = LicenseKeyGroupTest.createLicenseKeyGroup(vlmId, VERSION01, "LKG1", "LKG1 dec", org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyType.One_Time, new org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther<>(opScopeChoicesLKG, null));
68     String lkgId = vendorLicenseManager.createLicenseKeyGroup(lkg, USER1).getId();
69     lkg.setId(lkgId);
70     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity
71         fg1 = createFGForTest(vlmId, "created" + CommonMethods.nextUuId(), Collections.singleton(epId), Collections.singleton(lkgId));
72     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity
73         fg1FromDB = vendorLicenseManager.getFeatureGroupModel(fg1, USER1).getFeatureGroup();
74     Assert.assertTrue(fg1FromDB.equals(fg1));
75   }
76
77
78   @Test
79   public void testCreateWithExistingName_negative() {
80     String testName = "createExistingName";
81     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest.createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1).getId();
82     createFGForTest(vlmId, "created", Collections.emptySet(), Collections.emptySet());
83     try {
84       org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity
85           created = LicenseAgreementTest.createFeatureGroup(vlmId, null, "created", "created", "created desc", Collections.emptySet(), Collections.emptySet());
86       vendorLicenseManager.createFeatureGroup(created, USER1);
87       Assert.fail();
88     } catch (CoreException e) {
89       Assert.assertEquals(e.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
90     }
91   }
92
93   private org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity createFGForTest(String vlmId, String fgName, Set<String> epIds, Set<String> lkgIds) {
94     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity
95         created = LicenseAgreementTest.createFeatureGroup(vlmId, null, null, fgName, "created desc", epIds, lkgIds);
96     return vendorLicenseManager.createFeatureGroup(created, USER1);
97   }
98
99   @Test
100   public void testUpdateFeatureGroup_addEP_andGET() throws Exception {
101     String testName = "testUpdateFeatureGroup_addEP_andGET";
102     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest.createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1).getId();
103
104     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity
105         fg5 = LicenseAgreementTest.createFeatureGroup(vlmId, VERSION01, "id" + CommonMethods.nextUuId(), "created" + CommonMethods.nextUuId(), "created desc", null, null);
106     vendorLicenseManager.createFeatureGroup(fg5, USER1).getId();
107
108
109     Set<org.openecomp.sdc.vendorlicense.dao.types.OperationalScope> opScopeChoices = new HashSet<>();
110     opScopeChoices.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.Other);
111     opScopeChoices.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.Data_Center);
112
113     org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity
114         epToAdd = EntitlementPoolTest.createEntitlementPool(vlmId, VERSION01, "epToAdd", "epToAdd dec", 80, org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit.Absolute, org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric.Core, null, "inc1", org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction.Other, "agg func1", opScopeChoices, null, org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime.Hour, null, "sku1");
115     String epToAddId = vendorLicenseManager.createEntitlementPool(epToAdd, USER1).getId();
116
117     vendorLicenseManager.updateFeatureGroup(fg5, null, null, CommonMethods.toSingleElementSet(epToAddId), null, USER1);
118     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel
119         updatedFG = vendorLicenseManager.getFeatureGroupModel(fg5, USER1);
120     Set<org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity> updatedEPs = updatedFG.getEntitlementPools();
121
122     epToAdd.setReferencingFeatureGroups(CommonMethods.toSingleElementSet(fg5.getId()));
123
124     Assert.assertEquals(updatedEPs.size(), 1);
125     for (org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity updatedEP : updatedEPs) {
126       Assert.assertTrue(updatedEP.getReferencingFeatureGroups().contains(fg5.getId()));
127       Assert.assertEquals(updatedEP.getId(), epToAddId);
128     }
129   }
130
131   @Test
132   public void testUpdateFeatureGroup_removeLKG_andGET() throws Exception {
133     String testName = "testUpdateFeatureGroup_removeLKG_andGET";
134     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest.createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1).getId();
135
136     Set<org.openecomp.sdc.vendorlicense.dao.types.OperationalScope> opScopeChoicesLKG = new HashSet<>();
137     opScopeChoicesLKG.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.CPU);
138     opScopeChoicesLKG.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.VM);
139     opScopeChoicesLKG.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.Availability_Zone);
140     opScopeChoicesLKG.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.Data_Center);
141     org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity
142         lkg = LicenseKeyGroupTest.createLicenseKeyGroup(vlmId, VERSION01, "lkg" + CommonMethods.nextUuId(), "lkg desc", org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyType.Unique, new org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther<>(opScopeChoicesLKG, null));
143     String lkgId = vendorLicenseManager.createLicenseKeyGroup(lkg, USER1).getId();
144     lkg.setId(lkgId);
145
146     org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity
147         lkg_1 = LicenseKeyGroupTest.createLicenseKeyGroup(vlmId, VERSION01, "lkg" + CommonMethods.nextUuId(), "lkg_1 desc", org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyType.Unique, new org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther<>(opScopeChoicesLKG, null));
148     String lkgId_1 = vendorLicenseManager.createLicenseKeyGroup(lkg_1, USER1).getId();
149     lkg.setId(lkgId);
150
151     Set<org.openecomp.sdc.vendorlicense.dao.types.OperationalScope> opScopeChoices = new HashSet<>();
152     opScopeChoices.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.Other);
153     opScopeChoices.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.Data_Center);
154     opScopeChoices.add(org.openecomp.sdc.vendorlicense.dao.types.OperationalScope.Network_Wide);
155     org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity
156         ep = EntitlementPoolTest.createEntitlementPool(vlmId, VERSION01, "EP1" + CommonMethods.nextUuId(), "EP1 dec", 80, org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit.Absolute, org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric.Core, null, "inc1", org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction.Other, "agg func1", opScopeChoices, null, org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime.Hour, null, "sku1");
157     String epId = vendorLicenseManager.createEntitlementPool(ep, USER1).getId();
158
159     Set<String> lkgs = new HashSet<>();
160     lkgs.add(lkgId);
161     lkgs.add(lkgId_1);
162
163     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity
164         fg = LicenseAgreementTest.createFeatureGroup(vlmId, VERSION01, "fg11" + CommonMethods.nextUuId(), "FG1", "FG1 desc", CommonMethods.toSingleElementSet(epId), lkgs);
165     String fgId = vendorLicenseManager.createFeatureGroup(fg, USER1).getId();
166     vendorLicenseManager.updateFeatureGroup(fg, null, CommonMethods.toSingleElementSet(lkgId), null, null, USER1);
167
168     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel
169         featureGroup = vendorLicenseManager.getFeatureGroupModel(fg, USER1);
170     Set<org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity> licenseKeyGroups = featureGroup.getLicenseKeyGroups();
171     Assert.assertEquals(licenseKeyGroups.size(), 1);
172     List<String> lkgIds = new ArrayList<>();
173     for (org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity licenseKeyGroup : licenseKeyGroups) {
174       lkgIds.add(licenseKeyGroup.getId());
175     }
176
177     Assert.assertTrue(lkgIds.contains(lkgId_1));
178     Assert.assertFalse(lkgIds.contains(lkgId));
179
180   }
181
182
183   @Test
184   public void testDeleteFeatureGroup() throws Exception {
185     String testName = "testDeleteFeatureGroup";
186     String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest.createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1).getId();
187
188     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity
189         fg1 = createFGForTest(vlmId, "new", Collections.emptySet(), Collections.emptySet());
190     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity
191         fg2 = createFGForTest(vlmId, "newer", Collections.emptySet(), Collections.emptySet());
192     Collection<org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity> featureGroupEntities = vendorLicenseManager.listFeatureGroups(vlmId, null, USER1);
193     Assert.assertEquals(featureGroupEntities.size(), 2); //precondition
194
195     vendorLicenseManager.deleteFeatureGroup(fg1, USER1);
196     Assert.assertEquals(vendorLicenseManager.listFeatureGroups(vlmId, null, USER1).size(), 1);
197
198
199   }
200
201
202 }