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.impl;
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.activityLog.ActivityLogManager;
29 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
30 import org.openecomp.sdc.vendorlicense.dao.*;
31 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
32 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
33 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
34 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
35 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
36 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
37 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
38 import org.openecomp.sdc.vendorlicense.facade.impl.VendorLicenseFacadeImpl;
39 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
40 import org.openecomp.sdc.versioning.VersioningManager;
41 import org.openecomp.sdc.versioning.dao.types.Version;
42 import org.openecomp.sdc.versioning.types.VersionInfo;
43 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
44 import org.testng.Assert;
45 import org.testng.AssertJUnit.*;
46 import org.testng.annotations.BeforeMethod;
47 import org.testng.annotations.Test;
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.Collection;
52 import java.util.HashSet;
55 import static org.mockito.Matchers.anyObject;
56 import static org.mockito.Mockito.doNothing;
57 import static org.mockito.Mockito.doReturn;
58 import static org.mockito.Mockito.never;
59 import static org.mockito.Mockito.verify;
62 * Created by KATYR on 4/10/2016
65 public class FeatureGroupTest {
66 //JUnit Test Cases using Mockito
67 private static final Version VERSION01 = new Version(0, 1);
68 private static final Version VERSION10 = new Version(1, 0);
69 private final String FG1_NAME = "FG1 name";
70 private static final String USER1 = "TestUser1";
71 private static final String USER2 = "TestUser2";
73 private static String vlm1_id = "vlm1_id";
74 private static String vlm2_id = "vlm2_id";
75 private static String lkg1_id = "lkg1_id";
76 private static String lkg2_id = "lkg2_id";
77 private static String fg1_id = "fg1_id";
78 private static String fg2_id = "fg2_id";
79 private static String ep1_id = "ep1_id";
80 private static String ep2_id = "ep2_id";
83 private VendorLicenseFacade vendorLicenseFacadeMcok;
86 private LimitDao limitDaoMcok;
88 private ActivityLogManager activityLogManagerMcok;
91 private VendorLicenseModelDao vendorLicenseModelDao;
94 private LicenseAgreementDao licenseAgreementDao;
97 private FeatureGroupDao featureGroupDao;
100 private EntitlementPoolDao entitlementPoolDao;
103 private LicenseKeyGroupDao licenseKeyGroupDao;
106 private VersioningManager versioningManager;
110 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
112 public FeatureGroupEntity updateFeatureGroup(String vlmId, Version version, String id, String name, String desc,
113 String partNumber, String manufacturerReferenceNumber, Set<String>
114 licenseKeyGroupIds, Set<String> entitlementPoolIds, Set<String>
115 referencingLicenseAgreements){
116 FeatureGroupEntity featureGroup = new FeatureGroupEntity(vlmId, version, id);
117 featureGroup.setVendorLicenseModelId(vlmId);
118 featureGroup.setVersion(version);
119 featureGroup.setId(id);
120 featureGroup.setName(name);
121 featureGroup.setDescription(desc);
122 featureGroup.setPartNumber(partNumber);
123 //featureGroup.setManufacturerReferenceNumber(manufacturerReferenceNumber);
124 featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
125 featureGroup.setEntitlementPoolIds(entitlementPoolIds);
126 featureGroup.setReferencingLicenseAgreements(referencingLicenseAgreements);
132 public void setUp() throws Exception{
133 MockitoAnnotations.initMocks(this);
137 public void testUpdate(){
138 Set<String> licenseKeyGroupIds;
139 licenseKeyGroupIds = new HashSet<>();
140 licenseKeyGroupIds.add("lkg1");
142 Set<String> entitlementPoolIds;
143 entitlementPoolIds = new HashSet<>();
144 entitlementPoolIds.add("ep1");
146 Set<String> referencingLicenseAgreements;
147 referencingLicenseAgreements = new HashSet<>();
148 referencingLicenseAgreements.add("la1");
150 FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
151 "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds,
152 referencingLicenseAgreements);
154 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
156 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
157 featureGroupDao.update(featureGroupEntity);
158 verify(featureGroupDao).update(anyObject());*/
162 public void testUpdateWithoutManufacturingReferenceNumber(){
163 Set<String> licenseKeyGroupIds;
164 licenseKeyGroupIds = new HashSet<>();
165 licenseKeyGroupIds.add("lkg1");
167 Set<String> entitlementPoolIds;
168 entitlementPoolIds = new HashSet<>();
169 entitlementPoolIds.add("ep1");
171 Set<String> referencingLicenseAgreements;
172 referencingLicenseAgreements = new HashSet<>();
173 referencingLicenseAgreements.add("la1");
175 FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
176 "partNumber", null, licenseKeyGroupIds, entitlementPoolIds,
177 referencingLicenseAgreements);
179 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
181 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
182 featureGroupDao.update(featureGroupEntity);
183 verify(featureGroupDao, never()).update(anyObject());*/
187 public void testListFeatureGroups() {
188 doReturn(Arrays.asList(
189 createFeatureGroup(vlm1_id, VERSION01, fg1_id, "FG1", "FG1 desc", new HashSet<String>(),
190 new HashSet<String>()),
191 createFeatureGroup(vlm1_id, VERSION01, fg2_id, "FG2", "FG2 desc", new HashSet<String>(),
192 new HashSet<String>())))
193 .when(vendorLicenseFacadeMcok).listFeatureGroups(vlm1_id, VERSION01, USER1);
195 Collection<FeatureGroupEntity> FGs =
196 vendorLicenseManagerImpl.listFeatureGroups(vlm1_id, VERSION01, USER1);
198 verify(vendorLicenseFacadeMcok).listFeatureGroups(vlm1_id, VERSION01, USER1);
199 Assert.assertEquals(FGs.size(), 2);
200 FGs.forEach(fg -> Assert.assertTrue(fg.getId().matches(fg1_id + "|" + fg2_id)));
204 public void testCreateFeatureGroup() {
205 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id,VERSION01,
208 doReturn(featureGroupEntity).when(vendorLicenseFacadeMcok).createFeatureGroup
209 (featureGroupEntity,USER1);
211 vendorLicenseManagerImpl.createFeatureGroup(featureGroupEntity,USER1);
213 verify(vendorLicenseFacadeMcok).createFeatureGroup(featureGroupEntity,USER1);
218 public void testUpdateFeatureGroup(){
219 VersionInfo versionInfo = new VersionInfo();
220 versionInfo.setActiveVersion(VERSION01);
221 versionInfo.setLockingUser(USER1);
222 ArrayList<Version> viewable = new ArrayList<Version>();
223 viewable.add(VERSION01);
224 versionInfo.setViewableVersions(viewable);
226 doReturn(versionInfo).when(vendorLicenseManagerImpl).getVersionInfo(vlm1_id,
227 VersionableEntityAction.Write, USER1);
228 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
230 existingFG.setEntitlementPoolIds(new HashSet<String>());
231 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
233 doReturn(existingFG).when(featureGroupDao).get(existingFG);
235 Set<String> removedEPs = new HashSet<>();
236 Set<String> addedEPs = new HashSet<>();
238 addedEPs.add(ep1_id);
239 addedEPs.add(ep2_id);
240 EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
241 EntitlementPoolEntity ep2 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep2_id);
242 doReturn(ep1).when(entitlementPoolDao).get(ep1);
243 doReturn(ep2).when(entitlementPoolDao).get(ep2);
245 Set<String> removedLKGs = new HashSet<>();
246 Set<String> addedLKGs = new HashSet<>();
248 addedLKGs.add(lkg1_id);
249 addedLKGs.add(lkg2_id);
250 LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
251 LicenseKeyGroupEntity lkg2 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg2_id);
252 doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
253 doReturn(lkg2).when(licenseKeyGroupDao).get(lkg2);
255 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
256 anyObject(),anyObject(), anyObject());
258 vendorLicenseManagerImpl.updateFeatureGroup(existingFG,addedLKGs,removedLKGs, addedEPs,
261 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
263 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
265 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,existingFG);
266 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
269 verify(featureGroupDao)
270 .updateFeatureGroup(existingFG,addedEPs,removedEPs, addedLKGs, removedLKGs);
271 verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id,VERSION01);
276 public void testGetFeatureGroup(){
277 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id,VERSION01,fg1_id);
278 vendorLicenseManagerImpl.getFeatureGroupModel(featureGroupEntity,USER1);
279 verify(vendorLicenseFacadeMcok).getFeatureGroupModel(featureGroupEntity,USER1);
283 public void deleteFeatureGroupTest() {
285 VersionInfo versionInfo = new VersionInfo();
286 versionInfo.setActiveVersion(VERSION01);
287 versionInfo.setLockingUser(USER1);
288 ArrayList<Version> viewable = new ArrayList<Version>();
289 viewable.add(VERSION01);
290 versionInfo.setViewableVersions(viewable);
292 doReturn(versionInfo).when(vendorLicenseManagerImpl).getVersionInfo(vlm1_id,
293 VersionableEntityAction.Write, USER1);
295 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
296 existingFG.setName("FG_name");
297 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
298 existingFG.setEntitlementPoolIds(new HashSet<String>());
299 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
301 doReturn(existingFG).when(featureGroupDao).get(anyObject());
303 doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
304 .UniqueValues.FEATURE_GROUP_NAME,vlm1_id,VERSION01.toString(),existingFG.getName());
306 vendorLicenseManagerImpl.deleteFeatureGroup(existingFG, USER1);
308 verify(featureGroupDao).delete(existingFG);
309 verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id,VERSION01);
311 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(existingFG
312 .getLicenseKeyGroupIds(),existingFG);
313 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(existingFG
314 .getEntitlementPoolIds(),existingFG);
315 verify(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
316 .UniqueValues.FEATURE_GROUP_NAME,vlm1_id,VERSION01.toString(),existingFG.getName());
319 private FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
320 String name, String description,
321 Set<String> entitlementPoolIds,
322 Set<String> licenseKeyGroupIds) {
323 FeatureGroupEntity featureGroup = new FeatureGroupEntity();
324 featureGroup.setVendorLicenseModelId(vendorId);
325 featureGroup.setVersion(version);
326 featureGroup.setId(id);
327 featureGroup.setName(name);
328 featureGroup.setDescription(description);
329 featureGroup.setEntitlementPoolIds(entitlementPoolIds);
330 featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);