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 static org.mockito.Matchers.anyObject;
20 import static org.mockito.Mockito.doNothing;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.verify;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.HashSet;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.Captor;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.mockito.Spy;
35 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
36 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
37 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
38 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
39 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
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.facade.VendorLicenseFacade;
48 import org.openecomp.sdc.versioning.VersioningManager;
49 import org.openecomp.sdc.versioning.dao.types.Version;
50 import org.testng.Assert;
51 import org.testng.annotations.AfterMethod;
52 import org.testng.annotations.BeforeMethod;
53 import org.testng.annotations.Test;
55 public class LicenseAgreementTest {
57 private static final String USER1 = "TestUser1";
58 private static final String USER2 = "TestUser2";
60 private static String vlm1_id = "vlm1_id";
61 private static String vlm2_id = "vlm2_id";
62 private static String la1_id = "la1_id";
63 private static String la2_id = "la2_id";
64 private static String fg1_id = "fg1_id";
65 private static String fg2_id = "fg2_id";
66 public static final Version VERSION01 = new Version(0, 1);
67 private static final Version VERSION10 = new Version(1, 0);
70 private VersioningManager versioningManagerMcok;
72 private VendorLicenseFacade vendorLicenseFacadeMcok;
74 private VendorLicenseModelDao vendorLicenseModelDaoMcok;
76 private LicenseAgreementDao licenseAgreementDaoMcok;
78 private FeatureGroupDao featureGroupDaoMcok;
80 private EntitlementPoolDao entitlementPoolDaoMcok;
82 private LicenseKeyGroupDao licenseKeyGroupDaoMcok;
84 private LimitDao limitDaoMcok;
89 private VendorLicenseManagerImpl vendorLicenseManager;
93 private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
97 public void setUp() throws Exception {
98 MockitoAnnotations.initMocks(this);
102 public void tearDown(){
103 vendorLicenseManager = null;
106 public static LicenseAgreementEntity createLicenseAgreement(String vlmId, Version version,
107 String id, String name, String desc,
108 String requirementsAndConstrains,
109 ChoiceOrOther<LicenseTerm> term,
111 LicenseAgreementEntity la = new LicenseAgreementEntity();
112 la.setVendorLicenseModelId(vlmId);
113 la.setVersion(version);
116 la.setDescription(desc);
117 la.setLicenseTerm(term);
118 la.setRequirementsAndConstrains(requirementsAndConstrains);
119 for (String fgId : fgIds) {
120 la.getFeatureGroupIds().add(fgId);
125 public static FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
126 String name, String description,
127 Set<String> entitlementPoolIds,
128 Set<String> licenseKeyGroupIds) {
129 FeatureGroupEntity featureGroup = new FeatureGroupEntity();
130 featureGroup.setVendorLicenseModelId(vendorId);
131 featureGroup.setVersion(version);
132 featureGroup.setId(id);
133 featureGroup.setName(name);
134 featureGroup.setDescription(description);
135 featureGroup.setEntitlementPoolIds(entitlementPoolIds);
136 featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
142 public void listLicenseAgreementsTest() {
144 LicenseAgreementEntity la =
145 new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017
147 doReturn(Arrays.asList(
148 createLicenseAgreement(vlm1_id, VERSION01, la1_id, "LA1", "LA1 " +
149 "desc", "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
151 createLicenseAgreement(vlm1_id, VERSION01, la2_id, "LA2", "LA2 desc",
152 "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
154 .when(licenseAgreementDaoMcok).list(la);
156 Collection<LicenseAgreementEntity> LAs =
157 vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
158 Assert.assertEquals(LAs.size(), 2);
160 licseAgreement -> Assert.assertTrue(licseAgreement.getId().matches(la1_id + "|" + la2_id)));
164 public void testListLicenseAgreementsWhenNone() {
166 LicenseAgreementEntity la =
167 new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017
169 doReturn(new ArrayList<LicenseAgreementEntity>())
170 .when(licenseAgreementDaoMcok).list(la);
172 Collection<LicenseAgreementEntity> LAs =
173 vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
175 verify(licenseAgreementDaoMcok).list(la);
176 Assert.assertEquals(LAs.size(), 0);
181 public void testCreateLicenseAgreement() {
183 LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity(vlm1_id, VERSION01,
186 doReturn(licenseAgreementEntity).when(vendorLicenseFacadeMcok).createLicenseAgreement
187 (licenseAgreementEntity);
189 vendorLicenseManager.createLicenseAgreement(licenseAgreementEntity);
191 verify(vendorLicenseFacadeMcok).createLicenseAgreement(licenseAgreementEntity);
195 public void testUpdateLicenseAgreement() {
196 LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
198 existingLA.setFeatureGroupIds(new HashSet<String>());
200 doReturn(existingLA).when(licenseAgreementDaoMcok).get(existingLA);
202 Set<String> removedFGs = new HashSet<>();
203 Set<String> addedFGs = new HashSet<>();
204 addedFGs.add(fg1_id);
205 addedFGs.add(fg2_id);
206 FeatureGroupEntity fg1 = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
207 FeatureGroupEntity fg2 = new FeatureGroupEntity(vlm1_id, VERSION01, fg2_id);
208 doReturn(fg1).when(featureGroupDaoMcok).get(fg1);
209 doReturn(fg2).when(featureGroupDaoMcok).get(fg2);
210 doNothing().when(vendorLicenseManager).updateUniqueName(anyObject(), anyObject(), anyObject(),
211 anyObject(), anyObject());
213 vendorLicenseManager.updateLicenseAgreement(existingLA, addedFGs, removedFGs);
215 verify(licenseAgreementDaoMcok)
216 .updateColumnsAndDeltaFeatureGroupIds(existingLA, addedFGs, removedFGs);
217 verify(vendorLicenseManager).addFeatureGroupsToLicenseAgreementRef(addedFGs, existingLA);
218 verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(removedFGs, existingLA);
223 public void deleteLicenseAgreementsTest() {
224 LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
225 existingLA.setName("LA");
226 existingLA.setFeatureGroupIds(new HashSet<>());
228 doReturn(existingLA).when(licenseAgreementDaoMcok).get(anyObject());
230 doNothing().when(vendorLicenseManager).deleteUniqueName(VendorLicenseConstants.UniqueValues
231 .LICENSE_AGREEMENT_NAME, vlm1_id, VERSION01.toString(), existingLA.getName());
233 vendorLicenseManager.deleteLicenseAgreement(vlm1_id, VERSION01, la1_id);
235 verify(licenseAgreementDaoMcok).delete(existingLA);
236 verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(existingLA
237 .getFeatureGroupIds(), existingLA);
241 public void testGetLicenseAgreement() {
242 vendorLicenseManager.getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
243 verify(vendorLicenseFacadeMcok).getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
247 @Test(dependsOnMethods = {"listLicenseAgreementsTest"})
248 public void featureGroupDeletedLicenseAgreementUpdated() {
249 LicenseAgreementEntity licenseAgreement =
250 createLicenseAgreement(vlm1Id, VERSION01, "laId", "LA2", "LA2 desc",
251 "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null), "fg2");
252 licenseAgreementDao.create(licenseAgreement);
253 String featureGroupId = "FeatureGroupId";
254 FeatureGroupEntity created =
255 createFeatureGroup(vlm1Id, VERSION01, "fg11", "FG1", "FG1 desc", null, null);
256 featureGroupDao.create(created);
257 featureGroupDao.addReferencingLicenseAgreement(created, licenseAgreement.getId());
259 vendorLicenseManager.deleteFeatureGroup(created);
260 LicenseAgreementEntity afterDeletingFG = licenseAgreementDao.get(licenseAgreement);
261 Assert.assertEquals(afterDeletingFG.getFeatureGroupIds().size(), 1);
262 Assert.assertTrue(afterDeletingFG.getFeatureGroupIds().contains("fg2"));