b0b5953a53bb548e08508680015da7fcb2055e88
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorlicense.impl;
22
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;
60
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;
70
71 import java.util.ArrayList;
72 import java.util.Arrays;
73 import java.util.Collection;
74 import java.util.HashSet;
75 import java.util.Set;
76
77 public class LicenseAgreementTest {
78
79   private static final String USER1 = "TestUser1";
80   private static final String USER2 = "TestUser2";
81
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);
90
91   @Mock
92   private VersioningManager versioningManagerMcok;
93   @Mock
94   private VendorLicenseFacade vendorLicenseFacadeMcok;
95   @Mock
96   private VendorLicenseModelDao vendorLicenseModelDaoMcok;
97   @Mock
98   private LicenseAgreementDao licenseAgreementDaoMcok;
99   @Mock
100   private FeatureGroupDao featureGroupDaoMcok;
101   @Mock
102   private EntitlementPoolDao entitlementPoolDaoMcok;
103   @Mock
104   private LicenseKeyGroupDao licenseKeyGroupDaoMcok;
105   @Mock
106   private LimitDao limitDaoMcok;
107   @Mock
108   private ActivityLogManager activityLogManagerMcok;
109
110
111   @Spy
112   @InjectMocks
113   private VendorLicenseManagerImpl vendorLicenseManager;
114
115
116   @Captor
117   private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
118
119
120   @BeforeMethod
121   public void setUp() throws Exception {
122     MockitoAnnotations.initMocks(this);
123   }
124
125   public static LicenseAgreementEntity createLicenseAgreement(String vlmId, Version version,
126                                                               String id, String name, String desc,
127                                                               String requirementsAndConstrains,
128                                                               ChoiceOrOther<LicenseTerm> term,
129                                                               String... fgIds) {
130     LicenseAgreementEntity la = new LicenseAgreementEntity();
131     la.setVendorLicenseModelId(vlmId);
132     la.setVersion(version);
133     la.setId(id);
134     la.setName(name);
135     la.setDescription(desc);
136     la.setLicenseTerm(term);
137     la.setRequirementsAndConstrains(requirementsAndConstrains);
138     for (String fgId : fgIds) {
139       la.getFeatureGroupIds().add(fgId);
140     }
141     return la;
142   }
143
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);
156     return featureGroup;
157   }
158
159
160   @Test
161   public void listLicenseAgreementsTest() {
162
163     LicenseAgreementEntity la = new LicenseAgreementEntity(vlm1_id,VERSION01,USER1);
164     doReturn(la).when(vendorLicenseManager).createLicenseAgreementForList(vlm1_id,VERSION01, USER1);
165
166     doReturn(Arrays.asList(
167         createLicenseAgreement(vlm1_id, VERSION01, la1_id, "LA1", "LA1 " +
168                 "desc","RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
169             "fg1"),
170         createLicenseAgreement(vlm1_id, VERSION01, la2_id, "LA2", "LA2 desc",
171             "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
172             "fg2")))
173         .when(licenseAgreementDaoMcok).list(la);
174
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)));
179   }
180
181   @Test
182   public void testListLicenseAgreementsWhenNone() {
183
184     LicenseAgreementEntity la = new LicenseAgreementEntity(vlm1_id,VERSION01,USER1);
185     doReturn(la).when(vendorLicenseManager).createLicenseAgreementForList(vlm1_id,VERSION01,
186         USER1);
187
188     doReturn(new ArrayList<LicenseAgreementEntity>())
189         .when(licenseAgreementDaoMcok).list(la);
190
191     Collection<LicenseAgreementEntity> LAs =
192         vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01, USER1);
193
194     verify(licenseAgreementDaoMcok).list(la);
195     Assert.assertEquals(LAs.size(), 0);
196   }
197
198
199   @Test
200   public void testCreateLicenseAgreement() {
201
202     LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity(vlm1_id,VERSION01,
203         la2_id);
204
205     doReturn(licenseAgreementEntity).when(vendorLicenseFacadeMcok).createLicenseAgreement
206         (licenseAgreementEntity,USER1);
207
208     vendorLicenseManager.createLicenseAgreement(licenseAgreementEntity,USER1);
209
210     verify(vendorLicenseFacadeMcok).createLicenseAgreement(licenseAgreementEntity,USER1);
211   }
212
213   @Test
214   public void testUpdateLicenseAgreement() {
215
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);
222
223     doReturn(versionInfo).when(vendorLicenseManager).getVersionInfo(vlm1_id,
224         VersionableEntityAction.Write, USER1);
225     LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
226
227     existingLA.setFeatureGroupIds(new HashSet<String>());
228
229     doReturn(existingLA).when(licenseAgreementDaoMcok).get(existingLA);
230
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,
240         USER1);
241     doNothing().when(vendorLicenseManager).updateUniqueName(anyObject(), anyObject(), anyObject(),
242         anyObject(), anyObject());
243
244     vendorLicenseManager.updateLicenseAgreement(existingLA, addedFGs, removedFGs, USER1);
245
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);
251
252   }
253
254   @Test
255   public void deleteLicenseAgreementsTest() {
256
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);
263
264     doReturn(versionInfo).when(vendorLicenseManager).getVersionInfo(vlm1_id,
265         VersionableEntityAction.Write, USER1);
266
267     LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
268     existingLA.setName("LA");
269     existingLA.setFeatureGroupIds(new HashSet<>());
270
271     doReturn(existingLA).when(licenseAgreementDaoMcok).get(anyObject());
272
273     doNothing().when(vendorLicenseManager).deleteUniqueName(VendorLicenseConstants.UniqueValues
274         .LICENSE_AGREEMENT_NAME,vlm1_id,VERSION01.toString(),existingLA.getName());
275
276     vendorLicenseManager.deleteLicenseAgreement(vlm1_id, VERSION01, la1_id, USER1);
277
278     verify(licenseAgreementDaoMcok).delete(existingLA);
279     verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id,VERSION01);
280     verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(existingLA
281         .getFeatureGroupIds(),existingLA);
282   }
283
284   @Test
285   public void testGetLicenseAgreement(){
286     vendorLicenseManager.getLicenseAgreementModel(vlm1_id,VERSION01,la1_id,USER1);
287     verify(vendorLicenseFacadeMcok).getLicenseAgreementModel(vlm1_id,VERSION01,la1_id,USER1);
288   }
289
290 /*
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());
302
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"));
307   }
308
309   */
310 }
311