re base code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / test / java / org / openecomp / sdc / vendorlicense / impl / LicenseAgreementTest.java
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 org.mockito.*;
20 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
21 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
22 import org.openecomp.sdc.vendorlicense.dao.*;
23 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
24 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
25 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
26 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
27 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
28 import org.openecomp.sdc.versioning.VersioningManager;
29 import org.openecomp.sdc.versioning.dao.types.Version;
30 import org.testng.Assert;
31 import org.testng.annotations.AfterMethod;
32 import org.testng.annotations.BeforeMethod;
33 import org.testng.annotations.Test;
34
35 import java.util.*;
36
37 import static org.mockito.Matchers.anyObject;
38 import static org.mockito.Mockito.*;
39
40 public class LicenseAgreementTest {
41
42   private static final String USER1 = "TestUser1";
43   private static final String USER2 = "TestUser2";
44
45   private static String vlm1_id = "vlm1_id";
46   private static String vlm2_id = "vlm2_id";
47   private static String la1_id = "la1_id";
48   private static String la2_id = "la2_id";
49   private static String fg1_id = "fg1_id";
50   private static String fg2_id = "fg2_id";
51   public static final Version VERSION01 = new Version(0, 1);
52   private static final Version VERSION10 = new Version(1, 0);
53
54   @Mock
55   private VersioningManager versioningManagerMcok;
56   @Mock
57   private VendorLicenseFacade vendorLicenseFacadeMcok;
58   @Mock
59   private VendorLicenseModelDao vendorLicenseModelDaoMcok;
60   @Mock
61   private LicenseAgreementDao licenseAgreementDaoMcok;
62   @Mock
63   private FeatureGroupDao featureGroupDaoMcok;
64   @Mock
65   private EntitlementPoolDao entitlementPoolDaoMcok;
66   @Mock
67   private LicenseKeyGroupDao licenseKeyGroupDaoMcok;
68   @Mock
69   private LimitDao limitDaoMcok;
70
71
72   @Spy
73   @InjectMocks
74   private VendorLicenseManagerImpl vendorLicenseManager;
75
76
77   @Captor
78   private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
79
80
81   @BeforeMethod
82   public void setUp() throws Exception {
83     MockitoAnnotations.initMocks(this);
84   }
85
86   @AfterMethod
87   public void tearDown(){
88     vendorLicenseManager = null;
89   }
90
91   public static LicenseAgreementEntity createLicenseAgreement(String vlmId, Version version,
92                                                               String id, String name, String desc,
93                                                               String requirementsAndConstrains,
94                                                               ChoiceOrOther<LicenseTerm> term,
95                                                               String... fgIds) {
96     LicenseAgreementEntity la = new LicenseAgreementEntity();
97     la.setVendorLicenseModelId(vlmId);
98     la.setVersion(version);
99     la.setId(id);
100     la.setName(name);
101     la.setDescription(desc);
102     la.setLicenseTerm(term);
103     la.setRequirementsAndConstrains(requirementsAndConstrains);
104     for (String fgId : fgIds) {
105       la.getFeatureGroupIds().add(fgId);
106     }
107     return la;
108   }
109
110   public static FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
111                                                       String name, String description,
112                                                       Set<String> entitlementPoolIds,
113                                                       Set<String> licenseKeyGroupIds) {
114     FeatureGroupEntity featureGroup = new FeatureGroupEntity();
115     featureGroup.setVendorLicenseModelId(vendorId);
116     featureGroup.setVersion(version);
117     featureGroup.setId(id);
118     featureGroup.setName(name);
119     featureGroup.setDescription(description);
120     featureGroup.setEntitlementPoolIds(entitlementPoolIds);
121     featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
122     return featureGroup;
123   }
124
125
126   @Test
127   public void listLicenseAgreementsTest() {
128
129     LicenseAgreementEntity la =
130         new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017
131
132     doReturn(Arrays.asList(
133         createLicenseAgreement(vlm1_id, VERSION01, la1_id, "LA1", "LA1 " +
134                 "desc", "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
135             "fg1"),
136         createLicenseAgreement(vlm1_id, VERSION01, la2_id, "LA2", "LA2 desc",
137             "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null),
138             "fg2")))
139         .when(licenseAgreementDaoMcok).list(la);
140
141     Collection<LicenseAgreementEntity> LAs =
142         vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
143     Assert.assertEquals(LAs.size(), 2);
144     LAs.forEach(
145         licseAgreement -> Assert.assertTrue(licseAgreement.getId().matches(la1_id + "|" + la2_id)));
146   }
147
148   @Test
149   public void testListLicenseAgreementsWhenNone() {
150
151     LicenseAgreementEntity la =
152         new LicenseAgreementEntity(vlm1_id, VERSION01, null); // TODO: 8/13/2017
153
154     doReturn(new ArrayList<>())
155         .when(licenseAgreementDaoMcok).list(la);
156
157     Collection<LicenseAgreementEntity> LAs =
158         vendorLicenseManager.listLicenseAgreements(vlm1_id, VERSION01);
159
160     verify(licenseAgreementDaoMcok).list(la);
161     Assert.assertEquals(LAs.size(), 0);
162   }
163
164
165   @Test
166   public void testCreateLicenseAgreement() {
167
168     LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity(vlm1_id, VERSION01,
169         la2_id);
170
171     doReturn(licenseAgreementEntity).when(vendorLicenseFacadeMcok).createLicenseAgreement
172         (licenseAgreementEntity);
173
174     vendorLicenseManager.createLicenseAgreement(licenseAgreementEntity);
175
176     verify(vendorLicenseFacadeMcok).createLicenseAgreement(licenseAgreementEntity);
177   }
178
179   @Test
180   public void testUpdateLicenseAgreement() {
181     LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
182
183     existingLA.setFeatureGroupIds(new HashSet<>());
184
185     doReturn(existingLA).when(licenseAgreementDaoMcok).get(existingLA);
186
187     Set<String> removedFGs = new HashSet<>();
188     Set<String> addedFGs = new HashSet<>();
189     addedFGs.add(fg1_id);
190     addedFGs.add(fg2_id);
191     FeatureGroupEntity fg1 = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
192     FeatureGroupEntity fg2 = new FeatureGroupEntity(vlm1_id, VERSION01, fg2_id);
193     doReturn(fg1).when(featureGroupDaoMcok).get(fg1);
194     doReturn(fg2).when(featureGroupDaoMcok).get(fg2);
195     doNothing().when(vendorLicenseManager).updateUniqueName(anyObject(), anyObject(), anyObject(),
196         anyObject(), anyObject());
197
198     vendorLicenseManager.updateLicenseAgreement(existingLA, addedFGs, removedFGs);
199
200     verify(licenseAgreementDaoMcok)
201         .updateColumnsAndDeltaFeatureGroupIds(existingLA, addedFGs, removedFGs);
202     verify(vendorLicenseManager).addFeatureGroupsToLicenseAgreementRef(addedFGs, existingLA);
203     verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(removedFGs, existingLA);
204
205   }
206
207   @Test
208   public void deleteLicenseAgreementsTest() {
209     LicenseAgreementEntity existingLA = new LicenseAgreementEntity(vlm1_id, VERSION01, la1_id);
210     existingLA.setName("LA");
211     existingLA.setFeatureGroupIds(new HashSet<>());
212
213     doReturn(existingLA).when(licenseAgreementDaoMcok).get(anyObject());
214
215     doNothing().when(vendorLicenseManager).deleteUniqueName(VendorLicenseConstants.UniqueValues
216         .LICENSE_AGREEMENT_NAME, vlm1_id, VERSION01.toString(), existingLA.getName());
217
218     vendorLicenseManager.deleteLicenseAgreement(vlm1_id, VERSION01, la1_id);
219
220     verify(licenseAgreementDaoMcok).delete(existingLA);
221     verify(vendorLicenseManager).removeFeatureGroupsToLicenseAgreementRef(existingLA
222         .getFeatureGroupIds(), existingLA);
223   }
224
225   @Test
226   public void testGetLicenseAgreement() {
227     vendorLicenseManager.getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
228     verify(vendorLicenseFacadeMcok).getLicenseAgreementModel(vlm1_id, VERSION01, la1_id);
229   }
230
231 /*
232   @Test(dependsOnMethods = {"listLicenseAgreementsTest"})
233   public void featureGroupDeletedLicenseAgreementUpdated() {
234     LicenseAgreementEntity licenseAgreement =
235         createLicenseAgreement(vlm1Id, VERSION01, "laId", "LA2", "LA2 desc",
236             "RequirementsAndConstrains2", new ChoiceOrOther<>(LicenseTerm.Unlimited, null), "fg2");
237     licenseAgreementDao.create(licenseAgreement);
238     String featureGroupId = "FeatureGroupId";
239     FeatureGroupEntity created =
240         createFeatureGroup(vlm1Id, VERSION01, "fg11", "FG1", "FG1 desc", null, null);
241     featureGroupDao.create(created);
242     featureGroupDao.addReferencingLicenseAgreement(created, licenseAgreement.getId());
243
244     vendorLicenseManager.deleteFeatureGroup(created);
245     LicenseAgreementEntity afterDeletingFG = licenseAgreementDao.get(licenseAgreement);
246     Assert.assertEquals(afterDeletingFG.getFeatureGroupIds().size(), 1);
247     Assert.assertTrue(afterDeletingFG.getFeatureGroupIds().contains("fg2"));
248   }
249
250   */
251 }
252