aa606913e133fddc1165b3100bb5e6a1c590c95a
[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
22 package org.openecomp.sdc.vendorlicense.impl;
23
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.mockito.Spy;
28 import org.openecomp.sdc.activityLog.ActivityLogManager;
29 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
30 import org.openecomp.sdc.vendorlicense.dao.*;
31 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
32 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
33 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
34 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
35 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
36 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
37 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
38 import org.openecomp.sdc.vendorlicense.facade.impl.VendorLicenseFacadeImpl;
39 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
40 import org.openecomp.sdc.versioning.VersioningManager;
41 import org.openecomp.sdc.versioning.dao.types.Version;
42 import org.openecomp.sdc.versioning.types.VersionInfo;
43 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
44 import org.testng.Assert;
45 import org.testng.AssertJUnit.*;
46 import org.testng.annotations.BeforeMethod;
47 import org.testng.annotations.Test;
48
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.Collection;
52 import java.util.HashSet;
53 import java.util.Set;
54
55 import static org.mockito.Matchers.anyObject;
56 import static org.mockito.Mockito.doNothing;
57 import static org.mockito.Mockito.doReturn;
58 import static org.mockito.Mockito.never;
59 import static org.mockito.Mockito.verify;
60
61 /**
62  * Created by KATYR on 4/10/2016
63  */
64
65 public class FeatureGroupTest {
66     //JUnit Test Cases using Mockito
67     private static final Version VERSION01 = new Version(0, 1);
68     private static final Version VERSION10 = new Version(1, 0);
69     private final String FG1_NAME = "FG1 name";
70     private static final String USER1 = "TestUser1";
71     private static final String USER2 = "TestUser2";
72
73     private static String vlm1_id = "vlm1_id";
74     private static String vlm2_id = "vlm2_id";
75     private static String lkg1_id = "lkg1_id";
76     private static String lkg2_id = "lkg2_id";
77     private static String fg1_id = "fg1_id";
78     private static String fg2_id = "fg2_id";
79     private static String ep1_id = "ep1_id";
80     private static String ep2_id = "ep2_id";
81
82     @Mock
83     private VendorLicenseFacade vendorLicenseFacadeMcok;
84
85     @Mock
86     private LimitDao limitDaoMcok;
87     @Mock
88     private ActivityLogManager activityLogManagerMcok;
89
90     @Mock
91     private VendorLicenseModelDao vendorLicenseModelDao;
92
93     @Mock
94     private LicenseAgreementDao licenseAgreementDao;
95
96     @Mock
97     private FeatureGroupDao featureGroupDao;
98
99     @Mock
100     private EntitlementPoolDao entitlementPoolDao;
101
102     @Mock
103     private LicenseKeyGroupDao licenseKeyGroupDao;
104
105     @Mock
106     private VersioningManager versioningManager;
107
108     @InjectMocks
109     @Spy
110     private VendorLicenseManagerImpl vendorLicenseManagerImpl;
111
112     public FeatureGroupEntity updateFeatureGroup(String vlmId, Version version, String id, String name, String desc,
113                                                  String partNumber, String manufacturerReferenceNumber, Set<String>
114                                                      licenseKeyGroupIds, Set<String> entitlementPoolIds, Set<String>
115                                                      referencingLicenseAgreements){
116         FeatureGroupEntity featureGroup = new FeatureGroupEntity(vlmId, version, id);
117         featureGroup.setVendorLicenseModelId(vlmId);
118         featureGroup.setVersion(version);
119         featureGroup.setId(id);
120         featureGroup.setName(name);
121         featureGroup.setDescription(desc);
122         featureGroup.setPartNumber(partNumber);
123         //featureGroup.setManufacturerReferenceNumber(manufacturerReferenceNumber);
124         featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
125         featureGroup.setEntitlementPoolIds(entitlementPoolIds);
126         featureGroup.setReferencingLicenseAgreements(referencingLicenseAgreements);
127
128         return featureGroup;
129     }
130
131     @BeforeMethod
132     public void setUp() throws Exception{
133         MockitoAnnotations.initMocks(this);
134     }
135
136     @Test
137     public void testUpdate(){
138         Set<String> licenseKeyGroupIds;
139         licenseKeyGroupIds = new HashSet<>();
140         licenseKeyGroupIds.add("lkg1");
141
142         Set<String> entitlementPoolIds;
143         entitlementPoolIds = new HashSet<>();
144         entitlementPoolIds.add("ep1");
145
146         Set<String> referencingLicenseAgreements;
147         referencingLicenseAgreements = new HashSet<>();
148         referencingLicenseAgreements.add("la1");
149
150         FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
151             "partNumber", "MRN", licenseKeyGroupIds, entitlementPoolIds,
152             referencingLicenseAgreements);
153
154         doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
155
156         /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
157             featureGroupDao.update(featureGroupEntity);
158         verify(featureGroupDao).update(anyObject());*/
159     }
160
161     @Test
162     public void testUpdateWithoutManufacturingReferenceNumber(){
163         Set<String> licenseKeyGroupIds;
164         licenseKeyGroupIds = new HashSet<>();
165         licenseKeyGroupIds.add("lkg1");
166
167         Set<String> entitlementPoolIds;
168         entitlementPoolIds = new HashSet<>();
169         entitlementPoolIds.add("ep1");
170
171         Set<String> referencingLicenseAgreements;
172         referencingLicenseAgreements = new HashSet<>();
173         referencingLicenseAgreements.add("la1");
174
175         FeatureGroupEntity featureGroupEntity = updateFeatureGroup("vlmId", VERSION01, "fgId", FG1_NAME, "fg1 desc",
176             "partNumber", null, licenseKeyGroupIds, entitlementPoolIds,
177             referencingLicenseAgreements);
178
179         doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
180
181         /*if(featureGroupEntity.getManufacturerReferenceNumber() != null)
182             featureGroupDao.update(featureGroupEntity);
183         verify(featureGroupDao, never()).update(anyObject());*/
184     }
185
186     @Test
187     public void testListFeatureGroups() {
188         doReturn(Arrays.asList(
189             createFeatureGroup(vlm1_id, VERSION01, fg1_id, "FG1", "FG1 desc", new HashSet<String>(),
190                 new HashSet<String>()),
191             createFeatureGroup(vlm1_id, VERSION01, fg2_id, "FG2", "FG2 desc", new HashSet<String>(),
192                 new HashSet<String>())))
193             .when(vendorLicenseFacadeMcok).listFeatureGroups(vlm1_id, VERSION01, USER1);
194
195         Collection<FeatureGroupEntity> FGs =
196             vendorLicenseManagerImpl.listFeatureGroups(vlm1_id, VERSION01, USER1);
197
198         verify(vendorLicenseFacadeMcok).listFeatureGroups(vlm1_id, VERSION01, USER1);
199         Assert.assertEquals(FGs.size(), 2);
200         FGs.forEach(fg -> Assert.assertTrue(fg.getId().matches(fg1_id + "|" + fg2_id)));
201     }
202
203     @Test
204     public void testCreateFeatureGroup() {
205         FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id,VERSION01,
206             fg1_id);
207
208         doReturn(featureGroupEntity).when(vendorLicenseFacadeMcok).createFeatureGroup
209             (featureGroupEntity,USER1);
210
211         vendorLicenseManagerImpl.createFeatureGroup(featureGroupEntity,USER1);
212
213         verify(vendorLicenseFacadeMcok).createFeatureGroup(featureGroupEntity,USER1);
214     }
215
216
217     @Test
218     public void testUpdateFeatureGroup(){
219         VersionInfo versionInfo = new VersionInfo();
220         versionInfo.setActiveVersion(VERSION01);
221         versionInfo.setLockingUser(USER1);
222         ArrayList<Version> viewable = new ArrayList<Version>();
223         viewable.add(VERSION01);
224         versionInfo.setViewableVersions(viewable);
225
226         doReturn(versionInfo).when(vendorLicenseManagerImpl).getVersionInfo(vlm1_id,
227             VersionableEntityAction.Write, USER1);
228         FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
229
230         existingFG.setEntitlementPoolIds(new HashSet<String>());
231         existingFG.setLicenseKeyGroupIds(new HashSet<String>());
232
233         doReturn(existingFG).when(featureGroupDao).get(existingFG);
234
235         Set<String> removedEPs = new HashSet<>();
236         Set<String> addedEPs = new HashSet<>();
237
238         addedEPs.add(ep1_id);
239         addedEPs.add(ep2_id);
240         EntitlementPoolEntity ep1 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep1_id);
241         EntitlementPoolEntity ep2 = new EntitlementPoolEntity(vlm1_id, VERSION01, ep2_id);
242         doReturn(ep1).when(entitlementPoolDao).get(ep1);
243         doReturn(ep2).when(entitlementPoolDao).get(ep2);
244
245         Set<String> removedLKGs = new HashSet<>();
246         Set<String> addedLKGs = new HashSet<>();
247
248         addedLKGs.add(lkg1_id);
249         addedLKGs.add(lkg2_id);
250         LicenseKeyGroupEntity lkg1 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg1_id);
251         LicenseKeyGroupEntity lkg2 = new LicenseKeyGroupEntity(vlm1_id, VERSION01, lkg2_id);
252         doReturn(lkg1).when(licenseKeyGroupDao).get(lkg1);
253         doReturn(lkg2).when(licenseKeyGroupDao).get(lkg2);
254
255         doNothing().when(vendorLicenseManagerImpl).updateUniqueName(anyObject(), anyObject(),
256             anyObject(),anyObject(), anyObject());
257
258         vendorLicenseManagerImpl.updateFeatureGroup(existingFG,addedLKGs,removedLKGs, addedEPs,
259             removedEPs, USER1);
260
261         verify(vendorLicenseManagerImpl).addLicenseKeyGroupsToFeatureGroupsRef(addedLKGs,
262             existingFG);
263         verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(removedLKGs,
264             existingFG);
265         verify(vendorLicenseManagerImpl).addEntitlementPoolsToFeatureGroupsRef(addedEPs,existingFG);
266         verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(removedEPs,
267             existingFG);
268
269         verify(featureGroupDao)
270             .updateFeatureGroup(existingFG,addedEPs,removedEPs, addedLKGs, removedLKGs);
271         verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id,VERSION01);
272     }
273
274
275     @Test
276     public void testGetFeatureGroup(){
277         FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity(vlm1_id,VERSION01,fg1_id);
278         vendorLicenseManagerImpl.getFeatureGroupModel(featureGroupEntity,USER1);
279         verify(vendorLicenseFacadeMcok).getFeatureGroupModel(featureGroupEntity,USER1);
280     }
281
282     @Test
283     public void deleteFeatureGroupTest() {
284
285         VersionInfo versionInfo = new VersionInfo();
286         versionInfo.setActiveVersion(VERSION01);
287         versionInfo.setLockingUser(USER1);
288         ArrayList<Version> viewable = new ArrayList<Version>();
289         viewable.add(VERSION01);
290         versionInfo.setViewableVersions(viewable);
291
292         doReturn(versionInfo).when(vendorLicenseManagerImpl).getVersionInfo(vlm1_id,
293             VersionableEntityAction.Write, USER1);
294
295         FeatureGroupEntity existingFG = new FeatureGroupEntity(vlm1_id, VERSION01, fg1_id);
296         existingFG.setName("FG_name");
297         existingFG.setLicenseKeyGroupIds(new HashSet<String>());
298         existingFG.setEntitlementPoolIds(new HashSet<String>());
299         existingFG.setLicenseKeyGroupIds(new HashSet<String>());
300
301         doReturn(existingFG).when(featureGroupDao).get(anyObject());
302
303         doNothing().when(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
304             .UniqueValues.FEATURE_GROUP_NAME,vlm1_id,VERSION01.toString(),existingFG.getName());
305
306         vendorLicenseManagerImpl.deleteFeatureGroup(existingFG, USER1);
307
308         verify(featureGroupDao).delete(existingFG);
309         verify(vendorLicenseFacadeMcok).updateVlmLastModificationTime(vlm1_id,VERSION01);
310
311         verify(vendorLicenseManagerImpl).removeLicenseKeyGroupsToFeatureGroupsRef(existingFG
312             .getLicenseKeyGroupIds(),existingFG);
313         verify(vendorLicenseManagerImpl).removeEntitlementPoolsToFeatureGroupsRef(existingFG
314             .getEntitlementPoolIds(),existingFG);
315         verify(vendorLicenseManagerImpl).deleteUniqueName(VendorLicenseConstants
316             .UniqueValues.FEATURE_GROUP_NAME,vlm1_id,VERSION01.toString(),existingFG.getName());
317     }
318
319     private FeatureGroupEntity createFeatureGroup(String vendorId, Version version, String id,
320                                                   String name, String description,
321                                                   Set<String> entitlementPoolIds,
322                                                   Set<String> licenseKeyGroupIds) {
323         FeatureGroupEntity featureGroup = new FeatureGroupEntity();
324         featureGroup.setVendorLicenseModelId(vendorId);
325         featureGroup.setVersion(version);
326         featureGroup.setId(id);
327         featureGroup.setName(name);
328         featureGroup.setDescription(description);
329         featureGroup.setEntitlementPoolIds(entitlementPoolIds);
330         featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
331         return featureGroup;
332     }
333 }
334