2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.controller.core;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNull;
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;
53 import javax.servlet.http.HttpServletRequest;
54 import javax.servlet.http.HttpServletResponse;
55 import javax.servlet.http.HttpSession;
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;
78 @RunWith(PowerMockRunner.class)
79 @PrepareForTest({PortalApiProperties.class, PortalApiConstants.class, SystemProperties.class})
80 public class MenuListControllerTest {
83 MenuListController menuListController = new MenuListController();
86 FnMenuService fnMenuService;
89 private SharedContextRestClient sharedContextRestClient;
93 MockitoAnnotations.initMocks(this);
96 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
98 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
99 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
101 NullPointerException nullPointerException = new NullPointerException();
103 User user = new User();
106 UserUtils userUtils = new UserUtils();
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);
118 menuListController.getMenu(mockedRequest, mockedResponse);
121 @SuppressWarnings("unchecked")
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);
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);
140 public void getAppNameExceptionTest(){
141 menuListController.getAppName(mockedRequest, mockedResponse);
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());
159 @SuppressWarnings("unchecked")
161 public void getLeftMenuJSPExceptionTest() throws IOException{
162 Mockito.doThrow(new NullPointerException()).when(fnMenuService).setMenuDataStructure(Matchers.anyList(), Matchers.anyList(), Matchers.anySet());
163 menuListController.getLeftMenuJSP(mockedRequest);
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);
175 public void getUserInfoExceptionTest() throws IOException{
176 menuListController.getUserInfo(mockedRequest, mockedResponse);
180 public void getTopMenuExceptionTest(){
181 menuListController.getTopMenu(mockedRequest, mockedResponse);
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);
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);
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);
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);