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=========================================================
21 package org.openecomp.sdc.vendorlicense.impl;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Captor;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.mockito.Spy;
29 import org.openecomp.core.util.UniqueValueUtil;
30 import org.openecomp.core.utilities.CommonMethods;
31 import org.openecomp.sdc.activityLog.ActivityLogManager;
32 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
33 import org.openecomp.sdc.common.errors.CoreException;
34 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
35 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
36 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
37 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
38 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
39 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDaoFactory;
40 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
41 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
42 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
43 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
44 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
45 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
46 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
47 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
48 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
49 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
50 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
51 import org.openecomp.sdc.versioning.VersioningManager;
52 import org.openecomp.sdc.versioning.VersioningUtil;
53 import org.openecomp.sdc.versioning.dao.types.Version;
54 import org.openecomp.sdc.versioning.types.VersionInfo;
55 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
56 import org.testng.Assert;
57 import org.testng.annotations.BeforeClass;
58 import org.testng.annotations.BeforeMethod;
59 import org.testng.annotations.Test;
61 import static org.mockito.Matchers.any;
62 import static org.mockito.Matchers.anyObject;
63 import static org.mockito.Matchers.eq;
64 import static org.mockito.Mockito.doNothing;
65 import static org.mockito.Mockito.doReturn;
66 import static org.mockito.Mockito.doThrow;
67 import static org.mockito.Mockito.verify;
68 import static org.openecomp.sdc.vendorlicense.VendorLicenseConstants.VENDOR_LICENSE_MODEL_VERSIONABLE_TYPE;
69 import static org.testng.AssertJUnit.assertEquals;
71 import java.util.ArrayList;
72 import java.util.Arrays;
73 import java.util.Collection;
74 import java.util.HashSet;
77 public class LicenseAgreementTest {
79 private static final String USER1 = "TestUser1";
80 private static final String USER2 = "TestUser2";
82 private static String vlm1_id = "vlm1_id";
83 private static String vlm2_id = "vlm2_id";
84 private static String la1_id = "la1_id";
85 private static String la2_id = "la2_id";
86 private static String fg1_id = "fg1_id";
87 private static String fg2_id = "fg2_id";
88 public static final Version VERSION01 = new Version(0, 1);
89 private static final Version VERSION10 = new Version(1, 0);
92 private VersioningManager versioningManagerMcok;
94 private VendorLicenseFacade vendorLicenseFacadeMcok;
96 private VendorLicenseModelDao vendorLicenseModelDaoMcok;
98 private LicenseAgreementDao licenseAgreementDaoMcok;
100 private FeatureGroupDao featureGroupDaoMcok;
102 private EntitlementPoolDao entitlementPoolDaoMcok;
104 private LicenseKeyGroupDao licenseKeyGroupDaoMcok;
106 private LimitDao limitDaoMcok;
108 private ActivityLogManager activityLogManagerMcok;
113 private VendorLicenseManagerImpl vendorLicenseManager;
117 private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
121 public void setUp() throws Exception {
122 MockitoAnnotations.initMocks(this);
125 public static LicenseAgreementEntity createLicenseAgreement(String vlmId, Version version,
126 String id, String name, String desc,
127 String requirementsAndConstrains,
128 ChoiceOrOther<LicenseTerm> term,
130 LicenseAgreementEntity la = new LicenseAgreementEntity();
131 la.setVendorLicenseModelId(vlmId);
132 la.setVersion(version);
135 la.setDescription(desc);
136 la.setLicenseTerm(term);
137 la.setRequirementsAndConstrains(requirementsAndConstrains);
138 for (String fgId : fgIds) {
139 la.getFeatureGroupIds().add(fgId);
144 public static FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
145 String name, String description,
146 Set<String> entitlementPoolIds,
147 Set<String> licenseKeyGroupIds) {
148 FeatureGroupEntity featureGroup = new FeatureGroupEntity();
149 featureGroup.setVendorLicenseModelId(vendorId);
150 featureGroup.setVersion(version);
151 featureGroup.setId(id);
152 featureGroup.setName(name);
153 featureGroup.setDescription(description);
154 featureGroup.setEntitlementPoolIds(entitlementPoolIds);
155 featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
161 public void listLicenseAgreementsTest() {
163 LicenseAgreementEntity la = new LicenseAgreementEntity(vlm1_id,VERSION01,USER1);
164 doReturn(la).when(vendorLicenseManager).createLicenseAgreementForList(vlm1_id,VERSION01, USER1);
166 doReturn(Arrays.asList(
167 createLicenseAgreement(vlm1_id, VERSION01, la1_id, "LA1", "LA1 " +
168 "desc","RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
170 createLicenseAgreement(vlm1_id, VERSION01, la2_id, "LA2", "LA2 desc",
171 "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
173 .when(licenseAgreementDaoMcok).list(la);
175 Collection<LicenseAgreementEntity> LAs =
176 vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01, USER1);
177 Assert.assertEquals(LAs.size(), 2);
178 LAs.forEach(licseAgreement-> Assert.assertTrue(licseAgreement.getId().matches(la1_id + "|" + la2_id)));
182 public void testListLicenseAgreementsWhenNone() {
184 LicenseAgreementEntity la = new LicenseAgreementEntity(vlm1_id,VERSION01,USER1);
185 doReturn(la).when(vendorLicenseManager).createLicenseAgreementForList(vlm1_id,VERSION01,
188 doReturn(new ArrayList<LicenseAgreementEntity>())
189 .when(licenseAgreementDaoMcok).list(la);
191 Collection<LicenseAgreementEntity> LAs =
192 vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01, USER1);
194 verify(licenseAgreementDaoMcok).list(la);
195 Assert.assertEquals(LAs.size(), 0);
200 public void testCreateLicenseAgreement() {
202 LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity(vlm1_id,VERSION01,
205 doReturn(licenseAgreementEntity).when(vendorLicenseFacadeMcok).createLicenseAgreement
206 (licenseAgreementEntity,USER1);
208 vendorLicenseManager.createLicenseAgreement(licenseAgreementEntity,USER1);
210 verify(vendorLicenseFacadeMcok).createLicenseAgreement(licenseAgreementEntity,USER1);
214 public void testUpdateLicenseAgreement() {
216 VersionInfo versionInfo = new VersionInfo();
217 versionInfo.setActiveVersion(VERSION01);
218 versionInfo.setLockingUser(USER1);
219 ArrayList<Version> viewable = new ArrayList<Version>();
220 viewable.add(VERSION01);
221 versionInfo.setViewableVersions(viewable);
223 doReturn(versionInfo).when(vendorLicenseManager).getVersionInfo(vlm1_id,
224 VersionableEntityAction.Write, USER1);
225 LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
227 existingLA.setFeatureGroupIds(new HashSet<String>());
229 doReturn(existingLA).when(licenseAgreementDaoMcok).get(existingLA);
231 Set<String> removedFGs = new HashSet<>();
232 Set<String> addedFGs = new HashSet<>();
233 addedFGs.add(fg1_id);
234 addedFGs.add(fg2_id);
235 FeatureGroupEntity fg1 = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
236 FeatureGroupEntity fg2 = new FeatureGroupEntity(vlm1_id, VERSION01, fg2_id);
237 doReturn(fg1).when(featureGroupDaoMcok).get(fg1);
238 doReturn(fg2).when(featureGroupDaoMcok).get(fg2);
239 doReturn(existingLA).when(vendorLicenseManager).createLicenseAgreementForList(vlm1_id,VERSION01,
241 doNothing().when(vendorLicenseManager).updateUniqueName(anyObject(), anyObject(), anyObject(),
242 anyObject(), anyObject());
244 vendorLicenseManager.updateLicenseAgreement(existingLA, addedFGs, removedFGs, USER1);
246 verify(licenseAgreementDaoMcok)
247 .updateColumnsAndDeltaFeatureGroupIds(existingLA, addedFGs, removedFGs);
248 verify(vendorLicenseManager).addFeatureGroupsToLicenseAgreementRef(addedFGs,existingLA);
249 verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(removedFGs,existingLA);
250 verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id,VERSION01);
255 public void deleteLicenseAgreementsTest() {
257 VersionInfo versionInfo = new VersionInfo();
258 versionInfo.setActiveVersion(VERSION01);
259 versionInfo.setLockingUser(USER1);
260 ArrayList<Version> viewable = new ArrayList<Version>();
261 viewable.add(VERSION01);
262 versionInfo.setViewableVersions(viewable);
264 doReturn(versionInfo).when(vendorLicenseManager).getVersionInfo(vlm1_id,
265 VersionableEntityAction.Write, USER1);
267 LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
268 existingLA.setName("LA");
269 existingLA.setFeatureGroupIds(new HashSet<>());
271 doReturn(existingLA).when(licenseAgreementDaoMcok).get(anyObject());
273 doNothing().when(vendorLicenseManager).deleteUniqueName(VendorLicenseConstants.UniqueValues
274 .LICENSE_AGREEMENT_NAME,vlm1_id,VERSION01.toString(),existingLA.getName());
276 vendorLicenseManager.deleteLicenseAgreement(vlm1_id, VERSION01, la1_id, USER1);
278 verify(licenseAgreementDaoMcok).delete(existingLA);
279 verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id,VERSION01);
280 verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(existingLA
281 .getFeatureGroupIds(),existingLA);
285 public void testGetLicenseAgreement(){
286 vendorLicenseManager.getLicenseAgreementModel(vlm1_id,VERSION01,la1_id,USER1);
287 verify(vendorLicenseFacadeMcok).getLicenseAgreementModel(vlm1_id,VERSION01,la1_id,USER1);
291 @Test(dependsOnMethods = {"listLicenseAgreementsTest"})
292 public void featureGroupDeletedLicenseAgreementUpdated() {
293 LicenseAgreementEntity licenseAgreement =
294 createLicenseAgreement(vlm1Id, VERSION01, "laId", "LA2", "LA2 desc",
295 "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null), "fg2");
296 licenseAgreementDao.create(licenseAgreement);
297 String featureGroupId = "FeatureGroupId";
298 FeatureGroupEntity created =
299 createFeatureGroup(vlm1Id, VERSION01, "fg11", "FG1", "FG1 desc", null, null);
300 featureGroupDao.create(created);
301 featureGroupDao.addReferencingLicenseAgreement(created, licenseAgreement.getId());
303 vendorLicenseManager.deleteFeatureGroup(created, USER1);
304 LicenseAgreementEntity afterDeletingFG = licenseAgreementDao.get(licenseAgreement);
305 Assert.assertEquals(afterDeletingFG.getFeatureGroupIds().size(), 1);
306 Assert.assertTrue(afterDeletingFG.getFeatureGroupIds().contains("fg2"));