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.sdc.activitylog.dao.type.ActivityLogEntity;
30 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
31 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
32 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
33 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
34 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
35 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
36 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
37 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
38 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
39 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
40 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
41 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
42 import org.openecomp.sdc.versioning.VersioningManager;
43 import org.openecomp.sdc.versioning.dao.types.Version;
44 import org.testng.Assert;
45 import org.testng.annotations.BeforeMethod;
46 import org.testng.annotations.Test;
48 import java.util.ArrayList;
49 import java.util.Arrays;
50 import java.util.Collection;
51 import java.util.HashSet;
54 import static org.mockito.Matchers.anyObject;
55 import static org.mockito.Mockito.doNothing;
56 import static org.mockito.Mockito.doReturn;
57 import static org.mockito.Mockito.verify;
59 public class LicenseAgreementTest {
61 private static final String USER1 = "TestUser1";
62 private static final String USER2 = "TestUser2";
64 private static String vlm1_id = "vlm1_id";
65 private static String vlm2_id = "vlm2_id";
66 private static String la1_id = "la1_id";
67 private static String la2_id = "la2_id";
68 private static String fg1_id = "fg1_id";
69 private static String fg2_id = "fg2_id";
70 public static final Version VERSION01 = new Version(0, 1);
71 private static final Version VERSION10 = new Version(1, 0);
74 private VersioningManager versioningManagerMcok;
76 private VendorLicenseFacade vendorLicenseFacadeMcok;
78 private VendorLicenseModelDao vendorLicenseModelDaoMcok;
80 private LicenseAgreementDao licenseAgreementDaoMcok;
82 private FeatureGroupDao featureGroupDaoMcok;
84 private EntitlementPoolDao entitlementPoolDaoMcok;
86 private LicenseKeyGroupDao licenseKeyGroupDaoMcok;
88 private LimitDao limitDaoMcok;
93 private VendorLicenseManagerImpl vendorLicenseManager;
97 private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
101 public void setUp() throws Exception {
102 MockitoAnnotations.initMocks(this);
105 public static LicenseAgreementEntity createLicenseAgreement(String vlmId, Version version,
106 String id, String name, String desc,
107 String requirementsAndConstrains,
108 ChoiceOrOther<LicenseTerm> term,
110 LicenseAgreementEntity la = new LicenseAgreementEntity();
111 la.setVendorLicenseModelId(vlmId);
112 la.setVersion(version);
115 la.setDescription(desc);
116 la.setLicenseTerm(term);
117 la.setRequirementsAndConstrains(requirementsAndConstrains);
118 for (String fgId : fgIds) {
119 la.getFeatureGroupIds().add(fgId);
124 public static FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
125 String name, String description,
126 Set<String> entitlementPoolIds,
127 Set<String> licenseKeyGroupIds) {
128 FeatureGroupEntity featureGroup = new FeatureGroupEntity();
129 featureGroup.setVendorLicenseModelId(vendorId);
130 featureGroup.setVersion(version);
131 featureGroup.setId(id);
132 featureGroup.setName(name);
133 featureGroup.setDescription(description);
134 featureGroup.setEntitlementPoolIds(entitlementPoolIds);
135 featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
141 public void listLicenseAgreementsTest() {
143 LicenseAgreementEntity la =
144 new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017
146 doReturn(Arrays.asList(
147 createLicenseAgreement(vlm1_id, VERSION01, la1_id, "LA1", "LA1 " +
148 "desc", "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
150 createLicenseAgreement(vlm1_id, VERSION01, la2_id, "LA2", "LA2 desc",
151 "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
153 .when(licenseAgreementDaoMcok).list(la);
155 Collection<LicenseAgreementEntity> LAs =
156 vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
157 Assert.assertEquals(LAs.size(), 2);
159 licseAgreement -> Assert.assertTrue(licseAgreement.getId().matches(la1_id + "|" + la2_id)));
163 public void testListLicenseAgreementsWhenNone() {
165 LicenseAgreementEntity la =
166 new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017
168 doReturn(new ArrayList<LicenseAgreementEntity>())
169 .when(licenseAgreementDaoMcok).list(la);
171 Collection<LicenseAgreementEntity> LAs =
172 vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
174 verify(licenseAgreementDaoMcok).list(la);
175 Assert.assertEquals(LAs.size(), 0);
180 public void testCreateLicenseAgreement() {
182 LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity(vlm1_id, VERSION01,
185 doReturn(licenseAgreementEntity).when(vendorLicenseFacadeMcok).createLicenseAgreement
186 (licenseAgreementEntity);
188 vendorLicenseManager.createLicenseAgreement(licenseAgreementEntity);
190 verify(vendorLicenseFacadeMcok).createLicenseAgreement(licenseAgreementEntity);
194 public void testUpdateLicenseAgreement() {
195 LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
197 existingLA.setFeatureGroupIds(new HashSet<String>());
199 doReturn(existingLA).when(licenseAgreementDaoMcok).get(existingLA);
201 Set<String> removedFGs = new HashSet<>();
202 Set<String> addedFGs = new HashSet<>();
203 addedFGs.add(fg1_id);
204 addedFGs.add(fg2_id);
205 FeatureGroupEntity fg1 = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
206 FeatureGroupEntity fg2 = new FeatureGroupEntity(vlm1_id, VERSION01, fg2_id);
207 doReturn(fg1).when(featureGroupDaoMcok).get(fg1);
208 doReturn(fg2).when(featureGroupDaoMcok).get(fg2);
209 doNothing().when(vendorLicenseManager).updateUniqueName(anyObject(), anyObject(), anyObject(),
210 anyObject(), anyObject());
212 vendorLicenseManager.updateLicenseAgreement(existingLA, addedFGs, removedFGs);
214 verify(licenseAgreementDaoMcok)
215 .updateColumnsAndDeltaFeatureGroupIds(existingLA, addedFGs, removedFGs);
216 verify(vendorLicenseManager).addFeatureGroupsToLicenseAgreementRef(addedFGs, existingLA);
217 verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(removedFGs, existingLA);
222 public void deleteLicenseAgreementsTest() {
223 LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
224 existingLA.setName("LA");
225 existingLA.setFeatureGroupIds(new HashSet<>());
227 doReturn(existingLA).when(licenseAgreementDaoMcok).get(anyObject());
229 doNothing().when(vendorLicenseManager).deleteUniqueName(VendorLicenseConstants.UniqueValues
230 .LICENSE_AGREEMENT_NAME, vlm1_id, VERSION01.toString(), existingLA.getName());
232 vendorLicenseManager.deleteLicenseAgreement(vlm1_id, VERSION01, la1_id);
234 verify(licenseAgreementDaoMcok).delete(existingLA);
235 verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(existingLA
236 .getFeatureGroupIds(), existingLA);
240 public void testGetLicenseAgreement() {
241 vendorLicenseManager.getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
242 verify(vendorLicenseFacadeMcok).getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
246 @Test(dependsOnMethods = {"listLicenseAgreementsTest"})
247 public void featureGroupDeletedLicenseAgreementUpdated() {
248 LicenseAgreementEntity licenseAgreement =
249 createLicenseAgreement(vlm1Id, VERSION01, "laId", "LA2", "LA2 desc",
250 "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null), "fg2");
251 licenseAgreementDao.create(licenseAgreement);
252 String featureGroupId = "FeatureGroupId";
253 FeatureGroupEntity created =
254 createFeatureGroup(vlm1Id, VERSION01, "fg11", "FG1", "FG1 desc", null, null);
255 featureGroupDao.create(created);
256 featureGroupDao.addReferencingLicenseAgreement(created, licenseAgreement.getId());
258 vendorLicenseManager.deleteFeatureGroup(created);
259 LicenseAgreementEntity afterDeletingFG = licenseAgreementDao.get(licenseAgreement);
260 Assert.assertEquals(afterDeletingFG.getFeatureGroupIds().size(), 1);
261 Assert.assertTrue(afterDeletingFG.getFeatureGroupIds().contains("fg2"));