2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.sdc.vendorlicense;
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;
39 import java.util.HashSet;
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;
48 * Created by KATYR on 4/10/2016
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";
57 private VendorLicenseModelDao vendorLicenseModelDao;
60 private LicenseAgreementDao licenseAgreementDao;
63 private FeatureGroupDao featureGroupDao;
66 private EntitlementPoolDao entitlementPoolDao;
69 private LicenseKeyGroupDao licenseKeyGroupDao;
72 private VersioningManager versioningManager;
76 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
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);
98 public void setUp() throws Exception{
99 MockitoAnnotations.initMocks(this);
103 public void testUpdate(){
104 Set<String> licenseKeyGroupIds;
105 licenseKeyGroupIds = new HashSet<>();
106 licenseKeyGroupIds.add("lkg1");
108 Set<String> entitlementPoolIds;
109 entitlementPoolIds = new HashSet<>();
110 entitlementPoolIds.add("ep1");
112 Set<String> referencingLicenseAgreements;
113 referencingLicenseAgreements = new HashSet<>();
114 referencingLicenseAgreements.add("la1");
116 FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
117 "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds,
118 referencingLicenseAgreements);
120 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
122 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
123 featureGroupDao.update(featureGroupEntity);
124 verify(featureGroupDao).update(anyObject());*/
128 public void testUpdateWithoutManufacturingReferenceNumber(){
129 Set<String> licenseKeyGroupIds;
130 licenseKeyGroupIds = new HashSet<>();
131 licenseKeyGroupIds.add("lkg1");
133 Set<String> entitlementPoolIds;
134 entitlementPoolIds = new HashSet<>();
135 entitlementPoolIds.add("ep1");
137 Set<String> referencingLicenseAgreements;
138 referencingLicenseAgreements = new HashSet<>();
139 referencingLicenseAgreements.add("la1");
141 FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
142 "partNumber", null, licenseKeyGroupIds, entitlementPoolIds,
143 referencingLicenseAgreements);
145 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
147 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
148 featureGroupDao.update(featureGroupEntity);
149 verify(featureGroupDao, never()).update(anyObject());*/
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();
164 public void testListFeatureGroups() throws Exception {
165 String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
166 .createVendorLicenseModel("vlmId_" + CommonMethods.nextUuId(), "vlm2Id desc", "icon2"),
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();
176 Collection<FeatureGroupEntity> featureGroupEntities =
177 vendorLicenseManager.listFeatureGroups(vlmId, null, USER1);
179 Assert.assertEquals(featureGroupEntities.size(), 2);
180 Set<String> actualIds = new HashSet<>();
181 for (FeatureGroupEntity featureGroupEntity : featureGroupEntities) {
182 actualIds.add(featureGroupEntity.getId());
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));
195 public void testCreateFeatureGroup() throws Exception {
196 String testName = "testCreateFeatureGroup";
197 String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
198 .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
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);
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();
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));
232 public void testCreateWithExistingName_negative() {
233 String testName = "createExistingName";
234 String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
235 .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
237 createFGForTest(vlmId, "created", Collections.emptySet(), Collections.emptySet());
239 FeatureGroupEntity created = LicenseAgreementTest
240 .createFeatureGroup(vlmId, null, "created", "created", "created desc",
241 Collections.emptySet(), Collections.emptySet());
242 vendorLicenseManager.createFeatureGroup(created, USER1);
244 } catch (CoreException exception) {
245 Assert.assertEquals(exception.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
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);
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)
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();
269 Set<OperationalScope> opScopeChoices = new HashSet<>();
270 opScopeChoices.add(OperationalScope.Other);
271 opScopeChoices.add(OperationalScope.Data_Center);
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();
280 .updateFeatureGroup(fg5, null, null, CommonMethods.toSingleElementSet(epToAddId), null,
282 FeatureGroupModel updatedFG = vendorLicenseManager.getFeatureGroupModel(fg5, USER1);
283 Set<EntitlementPoolEntity> updatedEPs = updatedFG.getEntitlementPools();
285 epToAdd.setReferencingFeatureGroups(CommonMethods.toSingleElementSet(fg5.getId()));
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);
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)
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();
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();
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();
328 Set<String> lkgs = new HashSet<>();
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();
337 .updateFeatureGroup(fg, null, CommonMethods.toSingleElementSet(lkgId), null, null, USER1);
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());
347 Assert.assertTrue(lkgIds.contains(lkgId_1));
348 Assert.assertFalse(lkgIds.contains(lkgId));
354 public void testDeleteFeatureGroup() throws Exception {
355 String testName = "testDeleteFeatureGroup";
356 String vlmId = vendorLicenseFacade.createVendorLicenseModel(VendorLicenseModelTest
357 .createVendorLicenseModel(testName + CommonMethods.nextUuId(), testName, "icon1"), USER1)
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
368 vendorLicenseManager.deleteFeatureGroup(fg1, USER1);
369 Assert.assertEquals(vendorLicenseManager.listFeatureGroups(vlmId, null, USER1).size(), 1);