daee544f7b4f3f9714c37a23aac701546bb68077
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.controller.core;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNull;
42
43 import java.io.IOException;
44 import java.io.PrintWriter;
45 import java.io.StringWriter;
46 import java.util.ArrayList;
47 import java.util.HashMap;
48 import java.util.HashSet;
49 import java.util.List;
50 import java.util.Map;
51 import java.util.Set;
52
53 import javax.servlet.http.HttpServletRequest;
54 import javax.servlet.http.HttpServletResponse;
55 import javax.servlet.http.HttpSession;
56
57 import org.junit.Before;
58 import org.junit.Test;
59 import org.junit.runner.RunWith;
60 import org.mockito.InjectMocks;
61 import org.mockito.Matchers;
62 import org.mockito.Mock;
63 import org.mockito.Mockito;
64 import org.mockito.MockitoAnnotations;
65 import org.onap.portalapp.framework.MockitoTestSuite;
66 import org.onap.portalsdk.core.domain.MenuData;
67 import org.onap.portalsdk.core.domain.User;
68 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
69 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
70 import org.onap.portalsdk.core.restful.client.SharedContextRestClient;
71 import org.onap.portalsdk.core.service.FnMenuService;
72 import org.onap.portalsdk.core.util.SystemProperties;
73 import org.onap.portalsdk.core.web.support.UserUtils;
74 import org.powermock.api.mockito.PowerMockito;
75 import org.powermock.core.classloader.annotations.PrepareForTest;
76 import org.powermock.modules.junit4.PowerMockRunner;
77
78 @RunWith(PowerMockRunner.class)
79 @PrepareForTest({PortalApiProperties.class, PortalApiConstants.class, SystemProperties.class})
80 public class MenuListControllerTest {
81
82         @InjectMocks
83         MenuListController menuListController = new MenuListController();
84                 
85         @Mock
86     FnMenuService fnMenuService;
87         
88         @Mock
89         private SharedContextRestClient sharedContextRestClient;
90                  
91         @Before
92         public void setup() {
93                 MockitoAnnotations.initMocks(this);
94         }
95         
96         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
97         
98         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
99         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
100         
101         NullPointerException nullPointerException = new NullPointerException();
102         
103         User user = new User();
104         
105         @Mock
106         UserUtils userUtils = new UserUtils();
107         
108         @Test
109         public void getMenuTest() throws IOException{
110                 List<List<MenuData>> childItemList = new ArrayList<List<MenuData>>();
111                 List<MenuData> parentList = new ArrayList<MenuData>();
112                 Set<MenuData> menuResult = new HashSet<MenuData>();             
113                 Mockito.doThrow(new NullPointerException()).when(fnMenuService).setMenuDataStructure(childItemList, parentList, menuResult);            
114                 StringWriter sw = new StringWriter();
115                 PrintWriter writer = new PrintWriter(sw);
116                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
117                 
118                 menuListController.getMenu(mockedRequest, mockedResponse);
119         }
120         
121         @SuppressWarnings("unchecked")
122         @Test
123         public void getMenuExceptionTest() throws IOException{                  
124                 Mockito.doThrow(new NullPointerException()).when(fnMenuService).setMenuDataStructure(Matchers.anyList(), Matchers.anyList(), (Set<MenuData>) Matchers.anySet());
125                 StringWriter sw = new StringWriter();
126                 PrintWriter writer = new PrintWriter(sw);
127                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);            
128                 menuListController.getMenu(mockedRequest, mockedResponse);
129         }
130         
131         @Test
132         public void getAppNameTest() throws IOException{
133                 StringWriter sw = new StringWriter();
134                 PrintWriter writer = new PrintWriter(sw);
135                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
136                 menuListController.getAppName(mockedRequest, mockedResponse);
137         }
138         
139         @Test
140         public void getAppNameExceptionTest(){          
141                 menuListController.getAppName(mockedRequest, mockedResponse);
142         }
143         
144         @Test
145         public void getLeftMenuJSPTest() throws IOException{
146                 Map<String, Object> actualResult = new HashMap<>();
147                 List<String> list = new ArrayList<String>();
148                 Map<String, Object> expectedResult = new HashMap<>();
149                 expectedResult.put("childItemList", list);
150                 expectedResult.put("parentList", list);
151                 List<List<MenuData>> childItemList = new ArrayList<List<MenuData>>();
152                 List<MenuData> parentList = new ArrayList<MenuData>();
153                 Set<MenuData> menuResult = new HashSet<MenuData>();     
154                 Mockito.doThrow(new NullPointerException()).when(fnMenuService).setMenuDataStructure(childItemList, parentList, menuResult);            
155                 actualResult = menuListController.getLeftMenuJSP(mockedRequest);
156                 assertEquals(actualResult.size(), expectedResult.size());
157         }
158         
159         @SuppressWarnings("unchecked")
160         @Test
161         public void getLeftMenuJSPExceptionTest() throws IOException{
162                 Mockito.doThrow(new NullPointerException()).when(fnMenuService).setMenuDataStructure(Matchers.anyList(), Matchers.anyList(), Matchers.anySet());                
163                 menuListController.getLeftMenuJSP(mockedRequest);
164         }
165         
166         @Test
167         public void getUserInfoTest() throws IOException{
168                 StringWriter sw = new StringWriter();
169                 PrintWriter writer = new PrintWriter(sw);
170                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
171                 menuListController.getUserInfo(mockedRequest, mockedResponse);
172         }
173         
174         @Test
175         public void getUserInfoExceptionTest() throws IOException{
176                 menuListController.getUserInfo(mockedRequest, mockedResponse);
177         }
178         
179         @Test
180         public void getTopMenuExceptionTest(){          
181                 menuListController.getTopMenu(mockedRequest, mockedResponse);
182         }
183         
184         @Test
185         public void getTopMenuTest() throws IOException{        
186                 PowerMockito.mockStatic(SystemProperties.class);
187                 PowerMockito.mockStatic(PortalApiProperties.class);
188                 PowerMockito.mockStatic(PortalApiConstants.class);
189                 HttpSession session  = mockedRequest.getSession();              
190                 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_NAME)).thenReturn("test");
191                 Mockito.when(session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_NAME))).thenReturn("userName");
192                 Mockito.when(session.getAttribute(SystemProperties.FIRST_NAME)).thenReturn("firstName");
193                 Mockito.when(session.getAttribute(SystemProperties.LAST_NAME)).thenReturn("lastName");
194                 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user");
195                 Mockito.when(session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME))).thenReturn(user);
196                 Mockito.when(PortalApiProperties
197                                                 .getProperty(PortalApiConstants.ECOMP_REDIRECT_URL)).thenReturn("https://portal.openecomp.org/ecompportal/process_csp");
198                 StringWriter sw = new StringWriter();
199                 PrintWriter writer = new PrintWriter(sw);
200                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
201                 menuListController.getTopMenu(mockedRequest, mockedResponse);
202         }
203         
204         @Test
205         public void pageRedirectContactTest() throws IOException{
206                 PowerMockito.mockStatic(PortalApiProperties.class);
207                 PowerMockito.mockStatic(PortalApiConstants.class);
208                 Mockito.when(mockedRequest.getParameter("page")).thenReturn("contact");
209                 Mockito.when(PortalApiProperties
210                                                 .getProperty(PortalApiConstants.ECOMP_REDIRECT_URL)).thenReturn("https://portal.openecomp.org/ecompportal/process_csp");
211                 StringWriter sw = new StringWriter();
212                 PrintWriter writer = new PrintWriter(sw);
213                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
214                 menuListController.pageRedirect(mockedRequest, mockedResponse);
215         }
216         
217         @Test
218         public void pageRedirectAccessTest() throws IOException{
219                 PowerMockito.mockStatic(PortalApiProperties.class);
220                 PowerMockito.mockStatic(PortalApiConstants.class);
221                 Mockito.when(mockedRequest.getParameter("page")).thenReturn("access");
222                 Mockito.when(PortalApiProperties
223                                                 .getProperty(PortalApiConstants.ECOMP_REDIRECT_URL)).thenReturn("https://portal.openecomp.org/ecompportal/process_csp");
224                 StringWriter sw = new StringWriter();
225                 PrintWriter writer = new PrintWriter(sw);
226                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
227                 menuListController.pageRedirect(mockedRequest, mockedResponse);
228         }
229         
230         @Test
231         public void pageRedirectExceptionTest() throws IOException{
232                 PowerMockito.mockStatic(PortalApiProperties.class);
233                 PowerMockito.mockStatic(PortalApiConstants.class);
234                 Mockito.when(PortalApiProperties
235                                                 .getProperty(PortalApiConstants.ECOMP_REDIRECT_URL)).thenReturn("https://portal.openecomp.org/ecompportal/process_csp");
236                 StringWriter sw = new StringWriter();
237                 PrintWriter writer = new PrintWriter(sw);
238                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
239                 menuListController.pageRedirect(mockedRequest, mockedResponse);
240         }
241 }