Improve testing stability
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-license-manager / src / test / java / org / openecomp / sdc / vendorlicense / VendorLicenseFacadeImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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;
22
23 /**
24  * This test just verifies Feature Group Get and List APIs.
25  */
26 public class VendorLicenseFacadeImplTest {
27     /*
28
29     //JUnit Test Cases using Mockito
30     private static final Version VERSION01 = new Version(0, 1);
31     public static final String EP1 = "ep1";
32     public static final String MRN = "mrn";
33     public static final String VLM_ID = "VLM_ID";
34     public static final String USER = "USER1";
35
36
37     @Mock
38     private VendorLicenseModelDao vendorLicenseModelDao;
39
40     @Mock
41     private LicenseAgreementDao licenseAgreementDao;
42
43     @Mock
44     private FeatureGroupDao featureGroupDao;
45
46     @Mock
47     private EntitlementPoolDao entitlementPoolDao;
48
49     @Mock
50     private LicenseKeyGroupDao licenseKeyGroupDao;
51
52     @Mock
53     private VersioningManager versioningManager;
54
55     @InjectMocks
56     @Spy
57     private VendorLicenseFacadeImpl vendorLicenseFacadeImpl;
58
59     @BeforeMethod
60     public void setUp() throws Exception{
61         MockitoAnnotations.openMocks(this);
62     }
63
64     @Test
65     public void testGetFeatureGroupWhenMRNNull () {
66         resetFieldModifiers();
67
68         FeatureGroupEntity featureGroup = createFeatureGroup();
69
70         VersionInfo info = new VersionInfo();
71         info.getViewableVersions().add(VERSION01);
72         info.setActiveVersion(VERSION01);
73
74         Set<String> entitlementPoolIds;
75         entitlementPoolIds = new HashSet<>();
76         entitlementPoolIds.add(EP1);
77
78         EntitlementPoolEntity ep = createEP();
79
80         featureGroup.setEntitlementPoolIds(entitlementPoolIds);
81
82         doReturn(info).when(vendorLicenseFacadeImpl).getVersionInfo(anyObject(),anyObject(),anyObject());
83         doReturn(featureGroup).when(featureGroupDao).get(featureGroup);
84         doReturn(ep).when(entitlementPoolDao).get(anyObject());
85         doReturn(MRN).when(entitlementPoolDao).getManufacturerReferenceNumber(anyObject());
86         FeatureGroupEntity retrieved = vendorLicenseFacadeImpl.getFeatureGroup(featureGroup, USER);
87         Assert.assertEquals(MRN, retrieved.getManufacturerReferenceNumber());
88     }
89
90     @Test
91     public void testListFeatureGroups () {
92         resetFieldModifiers();
93
94         FeatureGroupEntity featureGroup = createFeatureGroup();
95
96         Collection<FeatureGroupEntity> featureGroups = new ArrayList<FeatureGroupEntity>();
97         featureGroups.add(featureGroup);
98
99         VersionInfo info = new VersionInfo();
100         info.getViewableVersions().add(VERSION01);
101         info.setActiveVersion(VERSION01);
102
103         EntitlementPoolEntity ep = createEP();
104
105         doReturn(info).when(vendorLicenseFacadeImpl).getVersionInfo(anyObject(),anyObject(),anyObject());
106         doReturn(featureGroup).when(featureGroupDao).get(featureGroup);
107         doReturn(ep).when(entitlementPoolDao).get(anyObject());
108         doReturn(MRN).when(entitlementPoolDao).getManufacturerReferenceNumber(anyObject());
109         Collection<FeatureGroupEntity> retrieved = vendorLicenseFacadeImpl.listFeatureGroups(VLM_ID,
110             VERSION01, USER);
111         retrieved.stream().forEach(fg -> Assert.assertEquals(MRN,fg.getManufacturerReferenceNumber()));
112     }
113
114     @Test
115     public void testSubmitLAWithoutFG()
116     {
117         try {
118             resetFieldModifiers();
119
120             VersionInfo info = new VersionInfo();
121             info.getViewableVersions().add(VERSION01);
122             info.setActiveVersion(VERSION01);
123
124             LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity();
125             List<LicenseAgreementEntity> licenseAgreementEntities = new ArrayList<LicenseAgreementEntity>(){{
126                 add(licenseAgreementEntity);
127             }};
128
129             doReturn(info).when(vendorLicenseFacadeImpl).getVersionInfo(anyObject(),anyObject(),anyObject());
130             doReturn(licenseAgreementEntities).when(licenseAgreementDao).list(anyObject());
131
132             vendorLicenseFacadeImpl.submit(VLM_ID, USER);
133             Assert.fail();
134         } catch (CoreException exception) {
135             org.testng.Assert.assertEquals(exception.code().message(), SUBMIT_UNCOMPLETED_VLM_MSG_LA_MISSING_FG.getErrorMessage());
136         }
137     }
138
139     @Test
140     public void testSubmitLAWithFGWithoutEP()
141     {
142         try {
143             resetFieldModifiers();
144
145             VersionInfo info = new VersionInfo();
146             info.getViewableVersions().add(VERSION01);
147             info.setActiveVersion(VERSION01);
148
149             LicenseAgreementEntity licenseAgreementEntity = new LicenseAgreementEntity();
150             FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity();
151             licenseAgreementEntity.setFeatureGroupIds(new HashSet<String>(){{
152                 add("54654654asdas5");
153             }});
154             List<LicenseAgreementEntity> licenseAgreementEntities = new ArrayList<LicenseAgreementEntity>(){{
155                 add(licenseAgreementEntity);
156             }};
157
158             doReturn(info).when(vendorLicenseFacadeImpl).getVersionInfo(anyObject(),anyObject(),anyObject());
159             doReturn(licenseAgreementEntities).when(licenseAgreementDao).list(anyObject());
160             doReturn(featureGroupEntity).when(featureGroupDao).get(anyObject());
161
162             vendorLicenseFacadeImpl.submit(VLM_ID, USER);
163
164             Assert.fail();
165         } catch (CoreException exception) {
166             org.testng.Assert.assertEquals(exception.code().message(), SUBMIT_UNCOMPLETED_VLM_MSG_FG_MISSING_EP.getErrorMessage());
167         }
168     }
169
170     private void resetFieldModifiers() {
171         try {
172             Field fgField = VendorLicenseFacadeImpl.class.getDeclaredField("featureGroupDao");
173             fgField.setAccessible(true);
174             Field modifiersField = Field.class.getDeclaredField("modifiers");
175             modifiersField.setAccessible(true);
176             modifiersField.setInt(fgField, fgField.getModifiers() & ~Modifier.FINAL);
177             fgField.set(null, featureGroupDao);
178
179             Field epField = VendorLicenseFacadeImpl.class.getDeclaredField("entitlementPoolDao");
180             epField.setAccessible(true);
181             modifiersField = Field.class.getDeclaredField("modifiers");
182             modifiersField.setAccessible(true);
183             modifiersField.setInt(epField, epField.getModifiers() & ~Modifier.FINAL);
184             epField.set(null, entitlementPoolDao);
185
186             Field laField = VendorLicenseFacadeImpl.class.getDeclaredField("licenseAgreementDao");
187             laField.setAccessible(true);
188             modifiersField = Field.class.getDeclaredField("modifiers");
189             modifiersField.setAccessible(true);
190             modifiersField.setInt(laField, laField.getModifiers() & ~Modifier.FINAL);
191             laField.set(null, licenseAgreementDao);
192         } catch(NoSuchFieldException | IllegalAccessException e)
193         {
194             org.testng.Assert.fail();
195         }
196     }
197
198     private FeatureGroupEntity createFeatureGroup() {
199         FeatureGroupEntity featureGroup = new FeatureGroupEntity(VLM_ID, VERSION01, USER);
200         featureGroup.setManufacturerReferenceNumber(null);
201         VersionInfo info = new VersionInfo();
202         info.getViewableVersions().add(VERSION01);
203         info.setActiveVersion(VERSION01);
204
205         Set<String> entitlementPoolIds;
206         entitlementPoolIds = new HashSet<>();
207         entitlementPoolIds.add(EP1);
208
209         featureGroup.setEntitlementPoolIds(entitlementPoolIds);
210         return featureGroup;
211     }
212
213     private EntitlementPoolEntity createEP() {
214         EntitlementPoolEntity ep = new EntitlementPoolEntity(VLM_ID,VERSION01, EP1);
215         ep.setManufacturerReferenceNumber(MRN);
216         return ep;
217     }
218 */
219 }