4b085ac13addbb880bbf16772e96b9e454f34903
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.controller.core;
39
40 import static org.junit.Assert.assertEquals;
41
42 import java.io.IOException;
43 import java.io.PrintWriter;
44 import java.io.StringWriter;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.HashSet;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.Set;
51
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54 import javax.servlet.http.HttpSession;
55
56 import org.junit.Before;
57 import org.junit.Test;
58 import org.junit.runner.RunWith;
59 import org.mockito.InjectMocks;
60 import org.mockito.Matchers;
61 import org.mockito.Mock;
62 import org.mockito.Mockito;
63 import org.mockito.MockitoAnnotations;
64 import org.onap.portalapp.framework.MockitoTestSuite;
65 import org.onap.portalsdk.core.domain.MenuData;
66 import org.onap.portalsdk.core.domain.User;
67 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
68 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
69 import org.onap.portalsdk.core.restful.client.SharedContextRestClient;
70 import org.onap.portalsdk.core.service.FnMenuService;
71 import org.onap.portalsdk.core.util.SystemProperties;
72 import org.onap.portalsdk.core.web.support.UserUtils;
73 import org.powermock.api.mockito.PowerMockito;
74 import org.powermock.core.classloader.annotations.PrepareForTest;
75 import org.powermock.modules.junit4.PowerMockRunner;
76
77 @RunWith(PowerMockRunner.class)
78 @PrepareForTest({PortalApiProperties.class, PortalApiConstants.class, SystemProperties.class})
79 public class MenuListControllerTest {
80
81         @InjectMocks
82         MenuListController menuListController = new MenuListController();
83                 
84         @Mock
85     FnMenuService fnMenuService;
86         
87         @Mock
88         private SharedContextRestClient sharedContextRestClient;
89                  
90         @Before
91         public void setup() {
92                 MockitoAnnotations.initMocks(this);
93         }
94         
95         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
96         
97         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
98         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
99         
100         NullPointerException nullPointerException = new NullPointerException();
101         
102         User user = new User();
103         
104         @Mock
105         UserUtils userUtils = new UserUtils();
106         
107         @Test
108         public void getMenuTest() throws IOException{
109                 List<List<MenuData>> childItemList = new ArrayList<List<MenuData>>();
110                 List<MenuData> parentList = new ArrayList<MenuData>();
111                 Set<MenuData> menuResult = new HashSet<MenuData>();             
112                 Mockito.doThrow(new NullPointerException()).when(fnMenuService).setMenuDataStructure(childItemList, parentList, menuResult);            
113                 StringWriter sw = new StringWriter();
114                 PrintWriter writer = new PrintWriter(sw);
115                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
116                 
117                 menuListController.getMenu(mockedRequest, mockedResponse);
118         }
119         
120         @SuppressWarnings("unchecked")
121         @Test
122         public void getMenuExceptionTest() throws IOException{                  
123                 Mockito.doThrow(new NullPointerException()).when(fnMenuService).setMenuDataStructure(Matchers.anyList(), Matchers.anyList(), (Set<MenuData>) Matchers.anySet());
124                 StringWriter sw = new StringWriter();
125                 PrintWriter writer = new PrintWriter(sw);
126                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);            
127                 menuListController.getMenu(mockedRequest, mockedResponse);
128         }
129         
130         @Test
131         public void getAppNameTest() throws IOException{
132                 StringWriter sw = new StringWriter();
133                 PrintWriter writer = new PrintWriter(sw);
134                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
135                 menuListController.getAppName(mockedRequest, mockedResponse);
136         }
137         
138         @Test
139         public void getAppNameExceptionTest(){          
140                 menuListController.getAppName(mockedRequest, mockedResponse);
141         }
142         
143         @Test
144         public void getLeftMenuJSPTest() throws IOException{
145                 Map<String, Object> actualResult = new HashMap<>();
146                 List<String> list = new ArrayList<String>();
147                 Map<String, Object> expectedResult = new HashMap<>();
148                 expectedResult.put("childItemList", list);
149                 expectedResult.put("parentList", list);
150                 List<List<MenuData>> childItemList = new ArrayList<List<MenuData>>();
151                 List<MenuData> parentList = new ArrayList<MenuData>();
152                 Set<MenuData> menuResult = new HashSet<MenuData>();     
153                 Mockito.doThrow(new NullPointerException()).when(fnMenuService).setMenuDataStructure(childItemList, parentList, menuResult);            
154                 actualResult = menuListController.getLeftMenuJSP(mockedRequest);
155                 assertEquals(actualResult.size(), expectedResult.size());
156         }
157         
158         @SuppressWarnings("unchecked")
159         @Test
160         public void getLeftMenuJSPExceptionTest() throws IOException{
161                 Mockito.doThrow(new NullPointerException()).when(fnMenuService).setMenuDataStructure(Matchers.anyList(), Matchers.anyList(), Matchers.anySet());                
162                 menuListController.getLeftMenuJSP(mockedRequest);
163         }
164         
165         @Test
166         public void getUserInfoTest() throws IOException{
167                 StringWriter sw = new StringWriter();
168                 PrintWriter writer = new PrintWriter(sw);
169                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
170                 menuListController.getUserInfo(mockedRequest, mockedResponse);
171         }
172         
173         @Test
174         public void getUserInfoExceptionTest() throws IOException{
175                 menuListController.getUserInfo(mockedRequest, mockedResponse);
176         }
177         
178         @Test
179         public void getTopMenuExceptionTest(){          
180                 menuListController.getTopMenu(mockedRequest, mockedResponse);
181         }
182         
183         @Test
184         public void getTopMenuTest() throws IOException{        
185                 PowerMockito.mockStatic(SystemProperties.class);
186                 PowerMockito.mockStatic(PortalApiProperties.class);
187                 PowerMockito.mockStatic(PortalApiConstants.class);
188                 HttpSession session  = mockedRequest.getSession();              
189                 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_NAME)).thenReturn("test");
190                 Mockito.when(session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_NAME))).thenReturn("userName");
191                 Mockito.when(session.getAttribute(SystemProperties.FIRST_NAME)).thenReturn("firstName");
192                 Mockito.when(session.getAttribute(SystemProperties.LAST_NAME)).thenReturn("lastName");
193                 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user");
194                 Mockito.when(session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME))).thenReturn(user);
195                 Mockito.when(PortalApiProperties
196                                                 .getProperty(PortalApiConstants.ECOMP_REDIRECT_URL)).thenReturn("https://portal.openecomp.org/ecompportal/process_csp");
197                 StringWriter sw = new StringWriter();
198                 PrintWriter writer = new PrintWriter(sw);
199                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
200                 menuListController.getTopMenu(mockedRequest, mockedResponse);
201         }
202         
203         @Test
204         public void pageRedirectContactTest() throws IOException{
205                 PowerMockito.mockStatic(PortalApiProperties.class);
206                 PowerMockito.mockStatic(PortalApiConstants.class);
207                 Mockito.when(mockedRequest.getParameter("page")).thenReturn("contact");
208                 Mockito.when(PortalApiProperties
209                                                 .getProperty(PortalApiConstants.ECOMP_REDIRECT_URL)).thenReturn("https://portal.openecomp.org/ecompportal/process_csp");
210                 StringWriter sw = new StringWriter();
211                 PrintWriter writer = new PrintWriter(sw);
212                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
213                 menuListController.pageRedirect(mockedRequest, mockedResponse);
214         }
215         
216         @Test
217         public void pageRedirectAccessTest() throws IOException{
218                 PowerMockito.mockStatic(PortalApiProperties.class);
219                 PowerMockito.mockStatic(PortalApiConstants.class);
220                 Mockito.when(mockedRequest.getParameter("page")).thenReturn("access");
221                 Mockito.when(PortalApiProperties
222                                                 .getProperty(PortalApiConstants.ECOMP_REDIRECT_URL)).thenReturn("https://portal.openecomp.org/ecompportal/process_csp");
223                 StringWriter sw = new StringWriter();
224                 PrintWriter writer = new PrintWriter(sw);
225                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
226                 menuListController.pageRedirect(mockedRequest, mockedResponse);
227         }
228         
229         @Test
230         public void pageRedirectExceptionTest() throws IOException{
231                 PowerMockito.mockStatic(PortalApiProperties.class);
232                 PowerMockito.mockStatic(PortalApiConstants.class);
233                 Mockito.when(PortalApiProperties
234                                                 .getProperty(PortalApiConstants.ECOMP_REDIRECT_URL)).thenReturn("https://portal.openecomp.org/ecompportal/process_csp");
235                 StringWriter sw = new StringWriter();
236                 PrintWriter writer = new PrintWriter(sw);
237                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
238                 menuListController.pageRedirect(mockedRequest, mockedResponse);
239         }
240 }