1 package org.openecomp.sdc.vendorlicense;
3 import org.mockito.InjectMocks;
4 import org.mockito.Mock;
5 import org.mockito.MockitoAnnotations;
6 import org.mockito.Spy;
7 import org.openecomp.sdc.common.errors.CoreException;
8 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
9 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
10 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
11 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
12 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
13 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
14 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
15 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
16 import org.openecomp.sdc.vendorlicense.facade.impl.VendorLicenseFacadeImpl;
17 import org.openecomp.sdc.versioning.VersioningManager;
18 import org.openecomp.sdc.versioning.dao.types.Version;
19 import org.openecomp.sdc.versioning.types.VersionInfo;
20 import org.testng.Assert;
21 import org.testng.annotations.BeforeMethod;
22 import org.testng.annotations.Test;
24 import java.lang.reflect.Field;
25 import java.lang.reflect.Modifier;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashSet;
29 import java.util.List;
32 import static org.mockito.Matchers.anyObject;
33 import static org.mockito.Mockito.doReturn;
34 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP;
35 import static org.openecomp.sdc.vendorlicense.errors.UncompletedVendorLicenseModelErrorType.SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG;
38 * This test just verifies Feature Group Get and List APIs.
40 public class VendorLicenseFacadeImplTest {
43 //JUnit Test Cases using Mockito
44 private static final Version VERSION01 = new Version(0, 1);
45 public static final String EP1 = "ep1";
46 public static final String MRN = "mrn";
47 public static final String VLM_ID = "VLM_ID";
48 public static final String USER = "USER1";
52 private VendorLicenseModelDao vendorLicenseModelDao;
55 private LicenseAgreementDao licenseAgreementDao;
58 private FeatureGroupDao featureGroupDao;
61 private EntitlementPoolDao entitlementPoolDao;
64 private LicenseKeyGroupDao licenseKeyGroupDao;
67 private VersioningManager versioningManager;
71 private VendorLicenseFacadeImpl vendorLicenseFacadeImpl;
74 public void setUp() throws Exception{
75 MockitoAnnotations.initMocks(this);
79 public void testGetFeatureGroupWhenMRNNull () {
80 resetFieldModifiers();
82 FeatureGroupEntity featureGroup = createFeatureGroup();
84 VersionInfo info = new VersionInfo();
85 info.getViewableVersions().add(VERSION01);
86 info.setActiveVersion(VERSION01);
88 Set<String> entitlementPoolIds;
89 entitlementPoolIds = new HashSet<>();
90 entitlementPoolIds.add(EP1);
92 EntitlementPoolEntity ep = createEP();
94 featureGroup.setEntitlementPoolIds(entitlementPoolIds);
96 doReturn(info).when(vendorLicenseFacadeImpl).getVersionInfo(anyObject(),anyObject(),anyObject());
97 doReturn(featureGroup).when(featureGroupDao).get(featureGroup);
98 doReturn(ep).when(entitlementPoolDao).get(anyObject());
99 doReturn(MRN).when(entitlementPoolDao).getManufacturerReferenceNumber(anyObject());
100 FeatureGroupEntity retrieved = vendorLicenseFacadeImpl.getFeatureGroup(featureGroup, USER);
101 Assert.assertEquals(MRN, retrieved.getManufacturerReferenceNumber());
105 public void testListFeatureGroups () {
106 resetFieldModifiers();
108 FeatureGroupEntity featureGroup = createFeatureGroup();
110 Collection<FeatureGroupEntity> featureGroups = new ArrayList<FeatureGroupEntity>();
111 featureGroups.add(featureGroup);
113 VersionInfo info = new VersionInfo();
114 info.getViewableVersions().add(VERSION01);
115 info.setActiveVersion(VERSION01);
117 EntitlementPoolEntity ep = createEP();
119 doReturn(info).when(vendorLicenseFacadeImpl).getVersionInfo(anyObject(),anyObject(),anyObject());
120 doReturn(featureGroup).when(featureGroupDao).get(featureGroup);
121 doReturn(ep).when(entitlementPoolDao).get(anyObject());
122 doReturn(MRN).when(entitlementPoolDao).getManufacturerReferenceNumber(anyObject());
123 Collection<FeatureGroupEntity> retrieved = vendorLicenseFacadeImpl.listFeatureGroups(VLM_ID,
125 retrieved.stream().forEach(fg -> Assert.assertEquals(MRN,fg.getManufacturerReferenceNumber()));
129 public void testSubmitLAWithoutFG()
132 resetFieldModifiers();
134 VersionInfo info = new VersionInfo();
135 info.getViewableVersions().add(VERSION01);
136 info.setActiveVersion(VERSION01);
138 LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity();
139 List<LicenseAgreementEntity> licenseAgreementEntities = new ArrayList<LicenseAgreementEntity>(){{
140 add(licenseAgreementEntity);
143 doReturn(info).when(vendorLicenseFacadeImpl).getVersionInfo(anyObject(),anyObject(),anyObject());
144 doReturn(licenseAgreementEntities).when(licenseAgreementDao).list(anyObject());
146 vendorLicenseFacadeImpl.submit(VLM_ID, USER);
148 } catch (CoreException exception) {
149 org.testng.Assert.assertEquals(exception.code().message(), SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG.getErrorMessage());
154 public void testSubmitLAWithFGWithoutEP()
157 resetFieldModifiers();
159 VersionInfo info = new VersionInfo();
160 info.getViewableVersions().add(VERSION01);
161 info.setActiveVersion(VERSION01);
163 LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity();
164 FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity();
165 licenseAgreementEntity.setFeatureGroupIds(new HashSet<String>(){{
166 add("54654654asdas5");
168 List<LicenseAgreementEntity> licenseAgreementEntities = new ArrayList<LicenseAgreementEntity>(){{
169 add(licenseAgreementEntity);
172 doReturn(info).when(vendorLicenseFacadeImpl).getVersionInfo(anyObject(),anyObject(),anyObject());
173 doReturn(licenseAgreementEntities).when(licenseAgreementDao).list(anyObject());
174 doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
176 vendorLicenseFacadeImpl.submit(VLM_ID, USER);
179 } catch (CoreException exception) {
180 org.testng.Assert.assertEquals(exception.code().message(), SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP.getErrorMessage());
184 private void resetFieldModifiers() {
186 Field fgField = VendorLicenseFacadeImpl.class.getDeclaredField("featureGroupDao");
187 fgField.setAccessible(true);
188 Field modifiersField = Field.class.getDeclaredField("modifiers");
189 modifiersField.setAccessible(true);
190 modifiersField.setInt(fgField, fgField.getModifiers() & ~Modifier.FINAL);
191 fgField.set(null, featureGroupDao);
193 Field epField = VendorLicenseFacadeImpl.class.getDeclaredField("entitlementPoolDao");
194 epField.setAccessible(true);
195 modifiersField = Field.class.getDeclaredField("modifiers");
196 modifiersField.setAccessible(true);
197 modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
198 epField.set(null, entitlementPoolDao);
200 Field laField = VendorLicenseFacadeImpl.class.getDeclaredField("licenseAgreementDao");
201 laField.setAccessible(true);
202 modifiersField = Field.class.getDeclaredField("modifiers");
203 modifiersField.setAccessible(true);
204 modifiersField.setInt(laField, laField.getModifiers() & ~Modifier.FINAL);
205 laField.set(null, licenseAgreementDao);
206 } catch(NoSuchFieldException | IllegalAccessException e)
208 org.testng.Assert.fail();
212 private FeatureGroupEntity createFeatureGroup() {
213 FeatureGroupEntity featureGroup = new FeatureGroupEntity(VLM_ID, VERSION01, USER);
214 featureGroup.setManufacturerReferenceNumber(null);
215 VersionInfo info = new VersionInfo();
216 info.getViewableVersions().add(VERSION01);
217 info.setActiveVersion(VERSION01);
219 Set<String> entitlementPoolIds;
220 entitlementPoolIds = new HashSet<>();
221 entitlementPoolIds.add(EP1);
223 featureGroup.setEntitlementPoolIds(entitlementPoolIds);
227 private EntitlementPoolEntity createEP() {
228 EntitlementPoolEntity ep = new EntitlementPoolEntity(VLM_ID,VERSION01, EP1);
229 ep.setManufacturerReferenceNumber(MRN);