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