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