ab6262d8ca181ab6ba8aca7e82832e38fab61c17
[sdc.git] /
1 package org.openecomp.sdc.vendorlicense;
2
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;
23
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;
30 import java.util.Set;
31
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;
36
37 /**
38  * This test just verifies Feature Group Get and List APIs.
39  */
40 public class VendorLicenseFacadeImplTest {
41     /*
42
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";
49
50
51     @Mock
52     private VendorLicenseModelDao vendorLicenseModelDao;
53
54     @Mock
55     private LicenseAgreementDao licenseAgreementDao;
56
57     @Mock
58     private FeatureGroupDao featureGroupDao;
59
60     @Mock
61     private EntitlementPoolDao entitlementPoolDao;
62
63     @Mock
64     private LicenseKeyGroupDao licenseKeyGroupDao;
65
66     @Mock
67     private VersioningManager versioningManager;
68
69     @InjectMocks
70     @Spy
71     private VendorLicenseFacadeImpl vendorLicenseFacadeImpl;
72
73     @BeforeMethod
74     public void setUp() throws Exception{
75         MockitoAnnotations.initMocks(this);
76     }
77
78     @Test
79     public void testGetFeatureGroupWhenMRNNull () {
80         resetFieldModifiers();
81
82         FeatureGroupEntity featureGroup = createFeatureGroup();
83
84         VersionInfo info = new VersionInfo();
85         info.getViewableVersions().add(VERSION01);
86         info.setActiveVersion(VERSION01);
87
88         Set<String> entitlementPoolIds;
89         entitlementPoolIds = new HashSet<>();
90         entitlementPoolIds.add(EP1);
91
92         EntitlementPoolEntity ep = createEP();
93
94         featureGroup.setEntitlementPoolIds(entitlementPoolIds);
95
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());
102     }
103
104     @Test
105     public void testListFeatureGroups () {
106         resetFieldModifiers();
107
108         FeatureGroupEntity featureGroup = createFeatureGroup();
109
110         Collection<FeatureGroupEntity> featureGroups = new ArrayList<FeatureGroupEntity>();
111         featureGroups.add(featureGroup);
112
113         VersionInfo info = new VersionInfo();
114         info.getViewableVersions().add(VERSION01);
115         info.setActiveVersion(VERSION01);
116
117         EntitlementPoolEntity ep = createEP();
118
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,
124             VERSION01, USER);
125         retrieved.stream().forEach(fg -> Assert.assertEquals(MRN,fg.getManufacturerReferenceNumber()));
126     }
127
128     @Test
129     public void testSubmitLAWithoutFG()
130     {
131         try {
132             resetFieldModifiers();
133
134             VersionInfo info = new VersionInfo();
135             info.getViewableVersions().add(VERSION01);
136             info.setActiveVersion(VERSION01);
137
138             LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity();
139             List<LicenseAgreementEntity> licenseAgreementEntities = new ArrayList<LicenseAgreementEntity>(){{
140                 add(licenseAgreementEntity);
141             }};
142
143             doReturn(info).when(vendorLicenseFacadeImpl).getVersionInfo(anyObject(),anyObject(),anyObject());
144             doReturn(licenseAgreementEntities).when(licenseAgreementDao).list(anyObject());
145
146             vendorLicenseFacadeImpl.submit(VLM_ID, USER);
147             Assert.fail();
148         } catch (CoreException exception) {
149             org.testng.Assert.assertEquals(exception.code().message(), SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG.getErrorMessage());
150         }
151     }
152
153     @Test
154     public void testSubmitLAWithFGWithoutEP()
155     {
156         try {
157             resetFieldModifiers();
158
159             VersionInfo info = new VersionInfo();
160             info.getViewableVersions().add(VERSION01);
161             info.setActiveVersion(VERSION01);
162
163             LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity();
164             FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity();
165             licenseAgreementEntity.setFeatureGroupIds(new HashSet<String>(){{
166                 add("54654654asdas5");
167             }});
168             List<LicenseAgreementEntity> licenseAgreementEntities = new ArrayList<LicenseAgreementEntity>(){{
169                 add(licenseAgreementEntity);
170             }};
171
172             doReturn(info).when(vendorLicenseFacadeImpl).getVersionInfo(anyObject(),anyObject(),anyObject());
173             doReturn(licenseAgreementEntities).when(licenseAgreementDao).list(anyObject());
174             doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
175
176             vendorLicenseFacadeImpl.submit(VLM_ID, USER);
177
178             Assert.fail();
179         } catch (CoreException exception) {
180             org.testng.Assert.assertEquals(exception.code().message(), SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP.getErrorMessage());
181         }
182     }
183
184     private void resetFieldModifiers() {
185         try {
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);
192
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);
199
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)
207         {
208             org.testng.Assert.fail();
209         }
210     }
211
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);
218
219         Set<String> entitlementPoolIds;
220         entitlementPoolIds = new HashSet<>();
221         entitlementPoolIds.add(EP1);
222
223         featureGroup.setEntitlementPoolIds(entitlementPoolIds);
224         return featureGroup;
225     }
226
227     private EntitlementPoolEntity createEP() {
228         EntitlementPoolEntity ep = new EntitlementPoolEntity(VLM_ID,VERSION01, EP1);
229         ep.setManufacturerReferenceNumber(MRN);
230         return ep;
231     }
232 */
233 }