2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.vendorlicense.impl;
20 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
21 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
22 import org.openecomp.sdc.vendorlicense.dao.*;
23 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
24 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
25 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
26 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
27 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
28 import org.openecomp.sdc.versioning.VersioningManager;
29 import org.openecomp.sdc.versioning.dao.types.Version;
30 import org.testng.Assert;
31 import org.testng.annotations.AfterMethod;
32 import org.testng.annotations.BeforeMethod;
33 import org.testng.annotations.Test;
37 import static org.mockito.Matchers.anyObject;
38 import static org.mockito.Mockito.*;
40 public class LicenseAgreementTest {
42 private static final String USER1 = "TestUser1";
43 private static final String USER2 = "TestUser2";
45 private static String vlm1_id = "vlm1_id";
46 private static String vlm2_id = "vlm2_id";
47 private static String la1_id = "la1_id";
48 private static String la2_id = "la2_id";
49 private static String fg1_id = "fg1_id";
50 private static String fg2_id = "fg2_id";
51 public static final Version VERSION01 = new Version(0, 1);
52 private static final Version VERSION10 = new Version(1, 0);
55 private VersioningManager versioningManagerMcok;
57 private VendorLicenseFacade vendorLicenseFacadeMcok;
59 private VendorLicenseModelDao vendorLicenseModelDaoMcok;
61 private LicenseAgreementDao licenseAgreementDaoMcok;
63 private FeatureGroupDao featureGroupDaoMcok;
65 private EntitlementPoolDao entitlementPoolDaoMcok;
67 private LicenseKeyGroupDao licenseKeyGroupDaoMcok;
69 private LimitDao limitDaoMcok;
74 private VendorLicenseManagerImpl vendorLicenseManager;
78 private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
82 public void setUp() throws Exception {
83 MockitoAnnotations.initMocks(this);
87 public void tearDown(){
88 vendorLicenseManager = null;
91 public static LicenseAgreementEntity createLicenseAgreement(String vlmId, Version version,
92 String id, String name, String desc,
93 String requirementsAndConstrains,
94 ChoiceOrOther<LicenseTerm> term,
96 LicenseAgreementEntity la = new LicenseAgreementEntity();
97 la.setVendorLicenseModelId(vlmId);
98 la.setVersion(version);
101 la.setDescription(desc);
102 la.setLicenseTerm(term);
103 la.setRequirementsAndConstrains(requirementsAndConstrains);
104 for (String fgId : fgIds) {
105 la.getFeatureGroupIds().add(fgId);
110 public static FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
111 String name, String description,
112 Set<String> entitlementPoolIds,
113 Set<String> licenseKeyGroupIds) {
114 FeatureGroupEntity featureGroup = new FeatureGroupEntity();
115 featureGroup.setVendorLicenseModelId(vendorId);
116 featureGroup.setVersion(version);
117 featureGroup.setId(id);
118 featureGroup.setName(name);
119 featureGroup.setDescription(description);
120 featureGroup.setEntitlementPoolIds(entitlementPoolIds);
121 featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
127 public void listLicenseAgreementsTest() {
129 LicenseAgreementEntity la =
130 new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017
132 doReturn(Arrays.asList(
133 createLicenseAgreement(vlm1_id, VERSION01, la1_id, "LA1", "LA1 " +
134 "desc", "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
136 createLicenseAgreement(vlm1_id, VERSION01, la2_id, "LA2", "LA2 desc",
137 "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
139 .when(licenseAgreementDaoMcok).list(la);
141 Collection<LicenseAgreementEntity> LAs =
142 vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
143 Assert.assertEquals(LAs.size(), 2);
145 licseAgreement -> Assert.assertTrue(licseAgreement.getId().matches(la1_id + "|" + la2_id)));
149 public void testListLicenseAgreementsWhenNone() {
151 LicenseAgreementEntity la =
152 new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017
154 doReturn(new ArrayList<>())
155 .when(licenseAgreementDaoMcok).list(la);
157 Collection<LicenseAgreementEntity> LAs =
158 vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
160 verify(licenseAgreementDaoMcok).list(la);
161 Assert.assertEquals(LAs.size(), 0);
166 public void testCreateLicenseAgreement() {
168 LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity(vlm1_id, VERSION01,
171 doReturn(licenseAgreementEntity).when(vendorLicenseFacadeMcok).createLicenseAgreement
172 (licenseAgreementEntity);
174 vendorLicenseManager.createLicenseAgreement(licenseAgreementEntity);
176 verify(vendorLicenseFacadeMcok).createLicenseAgreement(licenseAgreementEntity);
180 public void testUpdateLicenseAgreement() {
181 LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
183 existingLA.setFeatureGroupIds(new HashSet<>());
185 doReturn(existingLA).when(licenseAgreementDaoMcok).get(existingLA);
187 Set<String> removedFGs = new HashSet<>();
188 Set<String> addedFGs = new HashSet<>();
189 addedFGs.add(fg1_id);
190 addedFGs.add(fg2_id);
191 FeatureGroupEntity fg1 = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
192 FeatureGroupEntity fg2 = new FeatureGroupEntity(vlm1_id, VERSION01, fg2_id);
193 doReturn(fg1).when(featureGroupDaoMcok).get(fg1);
194 doReturn(fg2).when(featureGroupDaoMcok).get(fg2);
195 doNothing().when(vendorLicenseManager).updateUniqueName(anyObject(), anyObject(), anyObject(),
196 anyObject(), anyObject());
198 vendorLicenseManager.updateLicenseAgreement(existingLA, addedFGs, removedFGs);
200 verify(licenseAgreementDaoMcok)
201 .updateColumnsAndDeltaFeatureGroupIds(existingLA, addedFGs, removedFGs);
202 verify(vendorLicenseManager).addFeatureGroupsToLicenseAgreementRef(addedFGs, existingLA);
203 verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(removedFGs, existingLA);
208 public void deleteLicenseAgreementsTest() {
209 LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
210 existingLA.setName("LA");
211 existingLA.setFeatureGroupIds(new HashSet<>());
213 doReturn(existingLA).when(licenseAgreementDaoMcok).get(anyObject());
215 doNothing().when(vendorLicenseManager).deleteUniqueName(VendorLicenseConstants.UniqueValues
216 .LICENSE_AGREEMENT_NAME, vlm1_id, VERSION01.toString(), existingLA.getName());
218 vendorLicenseManager.deleteLicenseAgreement(vlm1_id, VERSION01, la1_id);
220 verify(licenseAgreementDaoMcok).delete(existingLA);
221 verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(existingLA
222 .getFeatureGroupIds(), existingLA);
226 public void testGetLicenseAgreement() {
227 vendorLicenseManager.getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
228 verify(vendorLicenseFacadeMcok).getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
232 @Test(dependsOnMethods = {"listLicenseAgreementsTest"})
233 public void featureGroupDeletedLicenseAgreementUpdated() {
234 LicenseAgreementEntity licenseAgreement =
235 createLicenseAgreement(vlm1Id, VERSION01, "laId", "LA2", "LA2 desc",
236 "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null), "fg2");
237 licenseAgreementDao.create(licenseAgreement);
238 String featureGroupId = "FeatureGroupId";
239 FeatureGroupEntity created =
240 createFeatureGroup(vlm1Id, VERSION01, "fg11", "FG1", "FG1 desc", null, null);
241 featureGroupDao.create(created);
242 featureGroupDao.addReferencingLicenseAgreement(created, licenseAgreement.getId());
244 vendorLicenseManager.deleteFeatureGroup(created);
245 LicenseAgreementEntity afterDeletingFG = licenseAgreementDao.get(licenseAgreement);
246 Assert.assertEquals(afterDeletingFG.getFeatureGroupIds().size(), 1);
247 Assert.assertTrue(afterDeletingFG.getFeatureGroupIds().contains("fg2"));