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