0584ac5e997fdb91812be501024380ad397c5606
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.vendorlicense.impl;
18
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;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.HashSet;
28 import java.util.Set;
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;
54
55 public class LicenseAgreementTest {
56
57   private static final String USER1 = "TestUser1";
58   private static final String USER2 = "TestUser2";
59
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);
68
69   @Mock
70   private VersioningManager versioningManagerMcok;
71   @Mock
72   private VendorLicenseFacade vendorLicenseFacadeMcok;
73   @Mock
74   private VendorLicenseModelDao vendorLicenseModelDaoMcok;
75   @Mock
76   private LicenseAgreementDao licenseAgreementDaoMcok;
77   @Mock
78   private FeatureGroupDao featureGroupDaoMcok;
79   @Mock
80   private EntitlementPoolDao entitlementPoolDaoMcok;
81   @Mock
82   private LicenseKeyGroupDao licenseKeyGroupDaoMcok;
83   @Mock
84   private LimitDao limitDaoMcok;
85
86
87   @Spy
88   @InjectMocks
89   private VendorLicenseManagerImpl vendorLicenseManager;
90
91
92   @Captor
93   private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
94
95
96   @BeforeMethod
97   public void setUp() throws Exception {
98     MockitoAnnotations.initMocks(this);
99   }
100
101   @AfterMethod
102   public void tearDown(){
103     vendorLicenseManager = null;
104   }
105
106   public static LicenseAgreementEntity createLicenseAgreement(String vlmId, Version version,
107                                                               String id, String name, String desc,
108                                                               String requirementsAndConstrains,
109                                                               ChoiceOrOther<LicenseTerm> term,
110                                                               String... fgIds) {
111     LicenseAgreementEntity la = new LicenseAgreementEntity();
112     la.setVendorLicenseModelId(vlmId);
113     la.setVersion(version);
114     la.setId(id);
115     la.setName(name);
116     la.setDescription(desc);
117     la.setLicenseTerm(term);
118     la.setRequirementsAndConstrains(requirementsAndConstrains);
119     for (String fgId : fgIds) {
120       la.getFeatureGroupIds().add(fgId);
121     }
122     return la;
123   }
124
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);
137     return featureGroup;
138   }
139
140
141   @Test
142   public void listLicenseAgreementsTest() {
143
144     LicenseAgreementEntity la =
145         new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017
146
147     doReturn(Arrays.asList(
148         createLicenseAgreement(vlm1_id, VERSION01, la1_id, "LA1", "LA1 " +
149                 "desc", "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
150             "fg1"),
151         createLicenseAgreement(vlm1_id, VERSION01, la2_id, "LA2", "LA2 desc",
152             "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
153             "fg2")))
154         .when(licenseAgreementDaoMcok).list(la);
155
156     Collection<LicenseAgreementEntity> LAs =
157         vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
158     Assert.assertEquals(LAs.size(), 2);
159     LAs.forEach(
160         licseAgreement -> Assert.assertTrue(licseAgreement.getId().matches(la1_id + "|" + la2_id)));
161   }
162
163   @Test
164   public void testListLicenseAgreementsWhenNone() {
165
166     LicenseAgreementEntity la =
167         new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017
168
169     doReturn(new ArrayList<LicenseAgreementEntity>())
170         .when(licenseAgreementDaoMcok).list(la);
171
172     Collection<LicenseAgreementEntity> LAs =
173         vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
174
175     verify(licenseAgreementDaoMcok).list(la);
176     Assert.assertEquals(LAs.size(), 0);
177   }
178
179
180   @Test
181   public void testCreateLicenseAgreement() {
182
183     LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity(vlm1_id, VERSION01,
184         la2_id);
185
186     doReturn(licenseAgreementEntity).when(vendorLicenseFacadeMcok).createLicenseAgreement
187         (licenseAgreementEntity);
188
189     vendorLicenseManager.createLicenseAgreement(licenseAgreementEntity);
190
191     verify(vendorLicenseFacadeMcok).createLicenseAgreement(licenseAgreementEntity);
192   }
193
194   @Test
195   public void testUpdateLicenseAgreement() {
196     LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
197
198     existingLA.setFeatureGroupIds(new HashSet<String>());
199
200     doReturn(existingLA).when(licenseAgreementDaoMcok).get(existingLA);
201
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());
212
213     vendorLicenseManager.updateLicenseAgreement(existingLA, addedFGs, removedFGs);
214
215     verify(licenseAgreementDaoMcok)
216         .updateColumnsAndDeltaFeatureGroupIds(existingLA, addedFGs, removedFGs);
217     verify(vendorLicenseManager).addFeatureGroupsToLicenseAgreementRef(addedFGs, existingLA);
218     verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(removedFGs, existingLA);
219
220   }
221
222   @Test
223   public void deleteLicenseAgreementsTest() {
224     LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
225     existingLA.setName("LA");
226     existingLA.setFeatureGroupIds(new HashSet<>());
227
228     doReturn(existingLA).when(licenseAgreementDaoMcok).get(anyObject());
229
230     doNothing().when(vendorLicenseManager).deleteUniqueName(VendorLicenseConstants.UniqueValues
231         .LICENSE_AGREEMENT_NAME, vlm1_id, VERSION01.toString(), existingLA.getName());
232
233     vendorLicenseManager.deleteLicenseAgreement(vlm1_id, VERSION01, la1_id);
234
235     verify(licenseAgreementDaoMcok).delete(existingLA);
236     verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(existingLA
237         .getFeatureGroupIds(), existingLA);
238   }
239
240   @Test
241   public void testGetLicenseAgreement() {
242     vendorLicenseManager.getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
243     verify(vendorLicenseFacadeMcok).getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
244   }
245
246 /*
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());
258
259     vendorLicenseManager.deleteFeatureGroup(created);
260     LicenseAgreementEntity afterDeletingFG = licenseAgreementDao.get(licenseAgreement);
261     Assert.assertEquals(afterDeletingFG.getFeatureGroupIds().size(), 1);
262     Assert.assertTrue(afterDeletingFG.getFeatureGroupIds().contains("fg2"));
263   }
264
265   */
266 }
267