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;
19 import org.junit.After;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
24 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
25 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
26 import org.openecomp.sdc.vendorlicense.dao.*;
27 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
28 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
29 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
30 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
31 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
32 import org.openecomp.sdc.versioning.VersioningManager;
33 import org.openecomp.sdc.versioning.dao.types.Version;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.HashSet;
40 import static org.mockito.Matchers.anyObject;
41 import static org.mockito.Mockito.*;
43 public class LicenseAgreementTest {
45 private static final String USER1 = "TestUser1";
46 private static final String USER2 = "TestUser2";
48 private static String vlm1_id = "vlm1_id";
49 private static String vlm2_id = "vlm2_id";
50 private static String la1_id = "la1_id";
51 private static String la2_id = "la2_id";
52 private static String fg1_id = "fg1_id";
53 private static String fg2_id = "fg2_id";
54 public static final Version VERSION01 = new Version(0, 1);
55 private static final Version VERSION10 = new Version(1, 0);
58 private VersioningManager versioningManagerMcok;
60 private VendorLicenseFacade vendorLicenseFacadeMcok;
62 private VendorLicenseModelDao vendorLicenseModelDaoMcok;
64 private LicenseAgreementDao licenseAgreementDaoMcok;
66 private FeatureGroupDao featureGroupDaoMcok;
68 private EntitlementPoolDao entitlementPoolDaoMcok;
70 private LicenseKeyGroupDao licenseKeyGroupDaoMcok;
72 private LimitDao limitDaoMcok;
77 private VendorLicenseManagerImpl vendorLicenseManager;
81 private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
85 public void setUp() throws Exception {
86 MockitoAnnotations.openMocks(this);
90 public void tearDown(){
91 vendorLicenseManager = null;
94 public static LicenseAgreementEntity createLicenseAgreement(String vlmId, Version version,
95 String id, String name, String desc,
96 String requirementsAndConstrains,
97 ChoiceOrOther<LicenseTerm> term,
99 LicenseAgreementEntity la = new LicenseAgreementEntity();
100 la.setVendorLicenseModelId(vlmId);
101 la.setVersion(version);
104 la.setDescription(desc);
105 la.setLicenseTerm(term);
106 la.setRequirementsAndConstrains(requirementsAndConstrains);
107 for (String fgId : fgIds) {
108 la.getFeatureGroupIds().add(fgId);
113 public static FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
114 String name, String description,
115 Set<String> entitlementPoolIds,
116 Set<String> licenseKeyGroupIds) {
117 FeatureGroupEntity featureGroup = new FeatureGroupEntity();
118 featureGroup.setVendorLicenseModelId(vendorId);
119 featureGroup.setVersion(version);
120 featureGroup.setId(id);
121 featureGroup.setName(name);
122 featureGroup.setDescription(description);
123 featureGroup.setEntitlementPoolIds(entitlementPoolIds);
124 featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
130 public void listLicenseAgreementsTest() {
132 LicenseAgreementEntity la =
133 new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017
135 doReturn(Arrays.asList(
136 createLicenseAgreement(vlm1_id, VERSION01, la1_id, "LA1", "LA1 " +
137 "desc", "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
139 createLicenseAgreement(vlm1_id, VERSION01, la2_id, "LA2", "LA2 desc",
140 "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
142 .when(licenseAgreementDaoMcok).list(la);
144 Collection<LicenseAgreementEntity> LAs =
145 vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
146 Assert.assertEquals(LAs.size(), 2);
148 licseAgreement -> Assert.assertTrue(licseAgreement.getId().matches(la1_id + "|" + la2_id)));
152 public void testListLicenseAgreementsWhenNone() {
154 LicenseAgreementEntity la =
155 new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017
157 doReturn(new ArrayList<>())
158 .when(licenseAgreementDaoMcok).list(la);
160 Collection<LicenseAgreementEntity> LAs =
161 vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
163 verify(licenseAgreementDaoMcok).list(la);
164 Assert.assertEquals(LAs.size(), 0);
169 public void testCreateLicenseAgreement() {
171 LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity(vlm1_id, VERSION01,
174 doReturn(licenseAgreementEntity).when(vendorLicenseFacadeMcok).createLicenseAgreement
175 (licenseAgreementEntity);
177 vendorLicenseManager.createLicenseAgreement(licenseAgreementEntity);
179 verify(vendorLicenseFacadeMcok).createLicenseAgreement(licenseAgreementEntity);
183 public void testUpdateLicenseAgreement() {
184 LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
186 existingLA.setFeatureGroupIds(new HashSet<>());
188 doReturn(existingLA).when(licenseAgreementDaoMcok).get(existingLA);
190 Set<String> removedFGs = new HashSet<>();
191 Set<String> addedFGs = new HashSet<>();
192 addedFGs.add(fg1_id);
193 addedFGs.add(fg2_id);
194 FeatureGroupEntity fg1 = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
195 FeatureGroupEntity fg2 = new FeatureGroupEntity(vlm1_id, VERSION01, fg2_id);
196 doReturn(fg1).when(featureGroupDaoMcok).get(fg1);
197 doReturn(fg2).when(featureGroupDaoMcok).get(fg2);
198 doNothing().when(vendorLicenseManager).updateUniqueName(anyObject(), anyObject(), anyObject(),
199 anyObject(), anyObject());
201 vendorLicenseManager.updateLicenseAgreement(existingLA, addedFGs, removedFGs);
203 verify(licenseAgreementDaoMcok)
204 .updateColumnsAndDeltaFeatureGroupIds(existingLA, addedFGs, removedFGs);
205 verify(vendorLicenseManager).addFeatureGroupsToLicenseAgreementRef(addedFGs, existingLA);
206 verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(removedFGs, existingLA);
211 public void deleteLicenseAgreementsTest() {
212 LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
213 existingLA.setName("LA");
214 existingLA.setFeatureGroupIds(new HashSet<>());
216 doReturn(existingLA).when(licenseAgreementDaoMcok).get(anyObject());
218 doNothing().when(vendorLicenseManager).deleteUniqueName(VendorLicenseConstants.UniqueValues
219 .LICENSE_AGREEMENT_NAME, vlm1_id, VERSION01.toString(), existingLA.getName());
221 vendorLicenseManager.deleteLicenseAgreement(vlm1_id, VERSION01, la1_id);
223 verify(licenseAgreementDaoMcok).delete(existingLA);
224 verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(existingLA
225 .getFeatureGroupIds(), existingLA);
229 public void testGetLicenseAgreement() {
230 vendorLicenseManager.getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
231 verify(vendorLicenseFacadeMcok).getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
235 @Test(dependsOnMethods = {"listLicenseAgreementsTest"})
236 public void featureGroupDeletedLicenseAgreementUpdated() {
237 LicenseAgreementEntity licenseAgreement =
238 createLicenseAgreement(vlm1Id, VERSION01, "laId", "LA2", "LA2 desc",
239 "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null), "fg2");
240 licenseAgreementDao.create(licenseAgreement);
241 String featureGroupId = "FeatureGroupId";
242 FeatureGroupEntity created =
243 createFeatureGroup(vlm1Id, VERSION01, "fg11", "FG1", "FG1 desc", null, null);
244 featureGroupDao.create(created);
245 featureGroupDao.addReferencingLicenseAgreement(created, licenseAgreement.getId());
247 vendorLicenseManager.deleteFeatureGroup(created);
248 LicenseAgreementEntity afterDeletingFG = licenseAgreementDao.get(licenseAgreement);
249 Assert.assertEquals(afterDeletingFG.getFeatureGroupIds().size(), 1);
250 Assert.assertTrue(afterDeletingFG.getFeatureGroupIds().contains("fg2"));