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 static org.mockito.Matchers.anyObject;
25 import static org.mockito.Mockito.doReturn;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.mockito.Spy;
31 import org.openecomp.sdc.activityLog.ActivityLogManager;
32 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
33 import org.openecomp.sdc.vendorlicense.dao.*;
34 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
35 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
36 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
37 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
38 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
39 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
40 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
41 import org.openecomp.sdc.vendorlicense.facade.impl.VendorLicenseFacadeImpl;
42 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
43 import org.openecomp.sdc.versioning.VersioningManager;
44 import org.openecomp.sdc.versioning.dao.types.Version;
45 import org.openecomp.sdc.versioning.types.VersionInfo;
46 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
47 import org.testng.Assert;
48 import org.testng.AssertJUnit.*;
49 import org.testng.annotations.BeforeMethod;
50 import org.testng.annotations.Test;
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.Collection;
55 import java.util.HashSet;
58 import static org.mockito.Matchers.anyObject;
59 import static org.mockito.Mockito.doNothing;
60 import static org.mockito.Mockito.doReturn;
61 import static org.mockito.Mockito.never;
62 import static org.mockito.Mockito.verify;
65 * Created by KATYR on 4/10/2016
68 public class FeatureGroupTest {
69 //JUnit Test Cases using Mockito
70 private static final Version VERSION01 = new Version(0, 1);
71 private static final Version VERSION10 = new Version(1, 0);
72 private final String FG1_NAME = "FG1 name";
73 private static final String USER1 = "TestUser1";
74 private static final String USER2 = "TestUser2";
76 private static String vlm1_id = "vlm1_id";
77 private static String vlm2_id = "vlm2_id";
78 private static String lkg1_id = "lkg1_id";
79 private static String lkg2_id = "lkg2_id";
80 private static String fg1_id = "fg1_id";
81 private static String fg2_id = "fg2_id";
82 private static String ep1_id = "ep1_id";
83 private static String ep2_id = "ep2_id";
86 private VendorLicenseFacade vendorLicenseFacadeMcok;
89 private LimitDao limitDaoMcok;
91 private ActivityLogManager activityLogManagerMcok;
94 private VendorLicenseModelDao vendorLicenseModelDao;
97 private LicenseAgreementDao licenseAgreementDao;
100 private FeatureGroupDao featureGroupDao;
103 private EntitlementPoolDao entitlementPoolDao;
106 private LicenseKeyGroupDao licenseKeyGroupDao;
109 private VersioningManager versioningManager;
113 private VendorLicenseManagerImpl vendorLicenseManagerImpl;
115 public FeatureGroupEntity updateFeatureGroup(String vlmId, Version version, String id, String name, String desc,
116 String partNumber, String manufacturerReferenceNumber, Set<String>
117 licenseKeyGroupIds, Set<String> entitlementPoolIds, Set<String>
118 referencingLicenseAgreements){
119 FeatureGroupEntity featureGroup = new FeatureGroupEntity(vlmId, version, id);
120 featureGroup.setVendorLicenseModelId(vlmId);
121 featureGroup.setVersion(version);
122 featureGroup.setId(id);
123 featureGroup.setName(name);
124 featureGroup.setDescription(desc);
125 featureGroup.setPartNumber(partNumber);
126 //featureGroup.setManufacturerReferenceNumber(manufacturerReferenceNumber);
127 featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
128 featureGroup.setEntitlementPoolIds(entitlementPoolIds);
129 featureGroup.setReferencingLicenseAgreements(referencingLicenseAgreements);
135 public void setUp() {
136 MockitoAnnotations.initMocks(this);
140 public void testUpdate(){
141 Set<String> licenseKeyGroupIds;
142 licenseKeyGroupIds = new HashSet<>();
143 licenseKeyGroupIds.add("lkg1");
145 Set<String> entitlementPoolIds;
146 entitlementPoolIds = new HashSet<>();
147 entitlementPoolIds.add("ep1");
149 Set<String> referencingLicenseAgreements;
150 referencingLicenseAgreements = new HashSet<>();
151 referencingLicenseAgreements.add("la1");
153 FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
154 "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds,
155 referencingLicenseAgreements);
157 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
159 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
160 featureGroupDao.update(featureGroupEntity);
161 verify(featureGroupDao).update(anyObject());*/
165 public void testUpdateWithoutManufacturingReferenceNumber(){
166 Set<String> licenseKeyGroupIds;
167 licenseKeyGroupIds = new HashSet<>();
168 licenseKeyGroupIds.add("lkg1");
170 Set<String> entitlementPoolIds;
171 entitlementPoolIds = new HashSet<>();
172 entitlementPoolIds.add("ep1");
174 Set<String> referencingLicenseAgreements;
175 referencingLicenseAgreements = new HashSet<>();
176 referencingLicenseAgreements.add("la1");
178 FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
179 "partNumber", null, licenseKeyGroupIds, entitlementPoolIds,
180 referencingLicenseAgreements);
182 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
184 /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
185 featureGroupDao.update(featureGroupEntity);
186 verify(featureGroupDao, never()).update(anyObject());*/
190 public void testListFeatureGroups() {
191 doReturn(Arrays.asList(
192 createFeatureGroup(vlm1_id, VERSION01, fg1_id, "FG1", "FG1 desc", new HashSet<String>(),
193 new HashSet<String>()),
194 createFeatureGroup(vlm1_id, VERSION01, fg2_id, "FG2", "FG2 desc", new HashSet<String>(),
195 new HashSet<String>())))
196 .when(vendorLicenseFacadeMcok).listFeatureGroups(vlm1_id, VERSION01, USER1);
198 Collection<FeatureGroupEntity> FGs =
199 vendorLicenseManagerImpl.listFeatureGroups(vlm1_id, VERSION01, USER1);
201 verify(vendorLicenseFacadeMcok).listFeatureGroups(vlm1_id, VERSION01, USER1);
202 Assert.assertEquals(FGs.size(), 2);
203 FGs.forEach(fg -> Assert.assertTrue(fg.getId().matches(fg1_id + "|" + fg2_id)));
207 public void testCreateFeatureGroup() {
208 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id,VERSION01,
211 doReturn(featureGroupEntity).when(vendorLicenseFacadeMcok).createFeatureGroup
212 (featureGroupEntity,USER1);
214 vendorLicenseManagerImpl.createFeatureGroup(featureGroupEntity,USER1);
216 verify(vendorLicenseFacadeMcok).createFeatureGroup(featureGroupEntity,USER1);
221 public void testUpdateFeatureGroup(){
222 VersionInfo versionInfo = new VersionInfo();
223 versionInfo.setActiveVersion(VERSION01);
224 versionInfo.setLockingUser(USER1);
225 ArrayList<Version> viewable = new ArrayList<Version>();
226 viewable.add(VERSION01);
227 versionInfo.setViewableVersions(viewable);
229 doReturn(versionInfo).when(vendorLicenseManagerImpl).getVersionInfo(vlm1_id,
230 VersionableEntityAction.Write, USER1);
231 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
233 existingFG.setEntitlementPoolIds(new HashSet<String>());
234 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
236 doReturn(existingFG).when(featureGroupDao).get(existingFG);
238 Set<String> removedEPs = new HashSet<>();
239 Set<String> addedEPs = new HashSet<>();
241 addedEPs.add(ep1_id);
242 addedEPs.add(ep2_id);
243 EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
244 EntitlementPoolEntity ep2 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep2_id);
245 doReturn(ep1).when(entitlementPoolDao).get(ep1);
246 doReturn(ep2).when(entitlementPoolDao).get(ep2);
248 Set<String> removedLKGs = new HashSet<>();
249 Set<String> addedLKGs = new HashSet<>();
251 addedLKGs.add(lkg1_id);
252 addedLKGs.add(lkg2_id);
253 LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
254 LicenseKeyGroupEntity lkg2 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg2_id);
255 doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
256 doReturn(lkg2).when(licenseKeyGroupDao).get(lkg2);
258 doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
259 anyObject(),anyObject(), anyObject());
261 vendorLicenseManagerImpl.updateFeatureGroup(existingFG,addedLKGs,removedLKGs, addedEPs,
264 verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
266 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
268 verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,existingFG);
269 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
272 verify(featureGroupDao)
273 .updateFeatureGroup(existingFG,addedEPs,removedEPs, addedLKGs, removedLKGs);
274 verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id,VERSION01);
279 public void testGetFeatureGroup(){
280 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id,VERSION01,fg1_id);
281 vendorLicenseManagerImpl.getFeatureGroupModel(featureGroupEntity,USER1);
282 verify(vendorLicenseFacadeMcok).getFeatureGroupModel(featureGroupEntity,USER1);
286 public void deleteFeatureGroupTest() {
288 VersionInfo versionInfo = new VersionInfo();
289 versionInfo.setActiveVersion(VERSION01);
290 versionInfo.setLockingUser(USER1);
291 ArrayList<Version> viewable = new ArrayList<Version>();
292 viewable.add(VERSION01);
293 versionInfo.setViewableVersions(viewable);
295 doReturn(versionInfo).when(vendorLicenseManagerImpl).getVersionInfo(vlm1_id,
296 VersionableEntityAction.Write, USER1);
298 FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
299 existingFG.setName("FG_name");
300 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
301 existingFG.setEntitlementPoolIds(new HashSet<String>());
302 existingFG.setLicenseKeyGroupIds(new HashSet<String>());
304 doReturn(existingFG).when(featureGroupDao).get(anyObject());
306 doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
307 .UniqueValues.FEATURE_GROUP_NAME,vlm1_id,VERSION01.toString(),existingFG.getName());
309 vendorLicenseManagerImpl.deleteFeatureGroup(existingFG, USER1);
311 verify(featureGroupDao).delete(existingFG);
312 verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id,VERSION01);
314 verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(existingFG
315 .getLicenseKeyGroupIds(),existingFG);
316 verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(existingFG
317 .getEntitlementPoolIds(),existingFG);
318 verify(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
319 .UniqueValues.FEATURE_GROUP_NAME,vlm1_id,VERSION01.toString(),existingFG.getName());
322 private FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
323 String name, String description,
324 Set<String> entitlementPoolIds,
325 Set<String> licenseKeyGroupIds) {
326 FeatureGroupEntity featureGroup = new FeatureGroupEntity();
327 featureGroup.setVendorLicenseModelId(vendorId);
328 featureGroup.setVersion(version);
329 featureGroup.setId(id);
330 featureGroup.setName(name);
331 featureGroup.setDescription(description);
332 featureGroup.setEntitlementPoolIds(entitlementPoolIds);
333 featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);