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============================================
39 package org.onap.portalapp.controller.core;
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertNull;
44 import java.io.BufferedReader;
45 import java.io.IOException;
46 import java.io.PrintWriter;
47 import java.io.StringReader;
48 import java.io.StringWriter;
49 import java.util.ArrayList;
50 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.Assert;
58 import org.junit.Before;
59 import org.junit.Test;
60 import org.junit.runner.RunWith;
61 import org.mockito.InjectMocks;
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.Menu;
67 import org.onap.portalsdk.core.domain.MenuData;
68 import org.onap.portalsdk.core.domain.RoleFunction;
69 import org.onap.portalsdk.core.domain.User;
70 import org.onap.portalsdk.core.menu.MenuBuilder;
71 import org.onap.portalsdk.core.service.AppService;
72 import org.onap.portalsdk.core.service.DataAccessService;
73 import org.onap.portalsdk.core.service.FnMenuService;
74 import org.onap.portalsdk.core.service.FunctionalMenuListService;
75 import org.onap.portalsdk.core.web.support.UserUtils;
76 import org.powermock.api.mockito.PowerMockito;
77 import org.powermock.core.classloader.annotations.PrepareForTest;
78 import org.powermock.modules.junit4.PowerMockRunner;
80 import com.fasterxml.jackson.databind.ObjectMapper;
82 @RunWith(PowerMockRunner.class)
83 @PrepareForTest({UserUtils.class})
84 public class FnMenuControllerTest {
87 FnMenuController fnMenuController = new FnMenuController();
90 FnMenuService fnMenuService;
93 FunctionalMenuListService functionalMenuListService;
96 private MenuBuilder menuBuilder;
99 private DataAccessService dataAccessService;
102 private AppService appService;
105 public void setup() {
106 MockitoAnnotations.initMocks(this);
109 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
111 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
112 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
114 NullPointerException nullPointerException = new NullPointerException();
116 User user = new User();
119 UserUtils userUtils = new UserUtils();
122 ObjectMapper mapper = new ObjectMapper();
125 public void getParentListTest() throws Exception{
126 @SuppressWarnings("rawtypes")
127 List<List> list = new ArrayList<>();
128 Mockito.when(fnMenuService.getParentList()).thenReturn(list);
129 StringWriter sw = new StringWriter();
130 PrintWriter writer = new PrintWriter(sw);
131 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
132 fnMenuController.getParentList(mockedRequest, mockedResponse);
136 public void getParentListExceptionTest() throws Exception{
137 Mockito.when(fnMenuService.getParentList()).thenThrow(nullPointerException);
138 StringWriter sw = new StringWriter();
139 PrintWriter writer = new PrintWriter(sw);
140 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
141 fnMenuController.getParentList(mockedRequest, mockedResponse);
145 public void getFunctionCDListTest() throws Exception{
146 List<RoleFunction> roleFunctionList = new ArrayList<RoleFunction>();
147 Mockito.when(functionalMenuListService.getFunctionCDList(mockedRequest)).thenReturn(roleFunctionList);
148 StringWriter sw = new StringWriter();
149 PrintWriter writer = new PrintWriter(sw);
150 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
151 fnMenuController.getFunctionCDList(mockedRequest, mockedResponse);
155 public void getFunctionCDListExceptionTest() throws Exception{
156 Mockito.when(functionalMenuListService.getFunctionCDList(mockedRequest)).thenThrow(nullPointerException);
157 StringWriter sw = new StringWriter();
158 PrintWriter writer = new PrintWriter(sw);
159 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
160 fnMenuController.getFunctionCDList(mockedRequest, mockedResponse);
164 public void getFnMenuListTest() throws IOException{
165 List<MenuData> menuList = new ArrayList<>();
166 MenuData menudata = new MenuData();
167 menudata.setId((long) 1);
168 menudata.setLabel("test");
169 menudata.setParentMenu(menudata);
170 menuList.add(menudata);
171 Mockito.when(fnMenuService.getFnMenuItems()).thenReturn(menuList);
172 StringWriter sw = new StringWriter();
173 PrintWriter writer = new PrintWriter(sw);
174 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
175 fnMenuController.getFnMenuList(mockedRequest, mockedResponse);
179 public void getFnMenuListExceptionTest() throws IOException{
180 List<MenuData> menuList = new ArrayList<>();
181 MenuData menudata = new MenuData();
182 menudata.setId((long) 1);
183 menudata.setLabel("test");
184 menudata.setParentMenu(menudata);
185 menuList.add(menudata);
186 Mockito.when(fnMenuService.getFnMenuItems()).thenThrow(nullPointerException);
187 fnMenuController.getFnMenuList(mockedRequest, mockedResponse);
191 public void updateFnMenuTest() throws Exception{
193 String fnMenuItem = "{\"availableFnMenuItem\":{\"id\":9,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\""
194 + ":null,\"auditTrail\":null,\"menuLevel\":null,\"label\":\"Profile\",\"parentId\":1,\"action\":\"userProfile\",\"functionCd\":\"menu_profile\","
195 + "\"sortOrder\":90,\"servlet\":\"N/A\",\"queryString\":\"N/A\",\"externalUrl\":\"test\",\"target\":\"N/A\",\"active\":true,\"menuSetCode\":\"APP\","
196 + "\"separator\":false,\"imageSrc\":\"icon-people-oneperson\",\"parentMenu\":null,\"childMenus\":[],\"activeAsString\":\"true\","
197 + "\"parentIdAsString\":\"1\",\"separatorAsString\":\"false\",\"active_yn\":false,\"$$hashKey\":\"object:99\"}}";
198 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(fnMenuItem)));
199 StringWriter sw = new StringWriter();
200 PrintWriter writer = new PrintWriter(sw);
201 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
202 assertNull(fnMenuController.updateFnMenu(mockedRequest, mockedResponse));
206 public void updateFnMenuExceptionTest() throws Exception{
207 Menu fnMenuItemObj = new Menu();
208 fnMenuItemObj.setLabel("test");
209 StringWriter sw = new StringWriter();
210 PrintWriter writer = new PrintWriter(sw);
211 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
212 assertNull(fnMenuController.updateFnMenu(mockedRequest, mockedResponse));
216 public void removeFnMenuTest() throws Exception{
217 String fnMenuItem = "{\"fnMenuItem\":{\"id\":9,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\""
218 + ":null,\"auditTrail\":null,\"menuLevel\":null,\"label\":\"Profile\",\"parentId\":1,\"action\":\"userProfile\",\"functionCd\":\"menu_profile\","
219 + "\"sortOrder\":90,\"servlet\":\"N/A\",\"queryString\":\"N/A\",\"externalUrl\":\"test\",\"target\":\"N/A\",\"active\":true,\"menuSetCode\":\"APP\","
220 + "\"separator\":false,\"imageSrc\":\"icon-people-oneperson\",\"parentMenu\":null,\"childMenus\":[],\"activeAsString\":\"true\","
221 + "\"parentIdAsString\":\"1\",\"separatorAsString\":\"false\",\"active_yn\":false,\"$$hashKey\":\"object:99\"}}";
222 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(fnMenuItem)));
223 StringWriter sw = new StringWriter();
224 PrintWriter writer = new PrintWriter(sw);
225 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
226 assertNull(fnMenuController.removeFnMenu(mockedRequest, mockedResponse));
230 public void removeFnMenuExceptionTest() throws Exception{
231 String fnMenuItem = "{\"availableFnMenuItem\":{\"id\":9,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\""
232 + ":null,\"auditTrail\":null,\"menuLevel\":null,\"label\":\"Profile\",\"parentId\":1,\"action\":\"userProfile\",\"functionCd\":\"menu_profile\","
233 + "\"sortOrder\":90,\"servlet\":\"N/A\",\"queryString\":\"N/A\",\"externalUrl\":\"test\",\"target\":\"N/A\",\"active\":true,\"menuSetCode\":\"APP\","
234 + "\"separator\":false,\"imageSrc\":\"icon-people-oneperson\",\"parentMenu\":null,\"childMenus\":[],\"activeAsString\":\"true\","
235 + "\"parentIdAsString\":\"1\",\"separatorAsString\":\"false\",\"active_yn\":false,\"$$hashKey\":\"object:99\"}}";
236 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(fnMenuItem)));
237 StringWriter sw = new StringWriter();
238 PrintWriter writer = new PrintWriter(sw);
239 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
240 assertNull(fnMenuController.removeFnMenu(mockedRequest, mockedResponse));
244 public void getViewNameTest() {
245 String expectedResult = "test";
246 fnMenuController.setViewName(expectedResult);
247 String actualResult = fnMenuController.getViewName();
248 assertEquals(expectedResult, actualResult);
252 public void getMenuTest() {
253 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
254 PowerMockito.mockStatic(UserUtils.class);
255 HttpSession session = Mockito.mock(HttpSession.class);
256 Mockito.when(request.getSession()).thenReturn(session);
257 Mockito.when(UserUtils.getUserSession(request)).thenReturn(new User());
258 Map<String, Object> model = fnMenuController.getMenu(request);
259 Assert.assertTrue(model.size() > 0 );