1f1d2db389a7adbe63c6e407f8e05690533fdbce
[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
39 package org.onap.portalapp.controller.core;
40
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertNull;
43
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;
51
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54
55 import org.junit.Before;
56 import org.junit.Test;
57 import org.mockito.InjectMocks;
58 import org.mockito.Mock;
59 import org.mockito.Mockito;
60 import org.mockito.MockitoAnnotations;
61 import org.onap.portalapp.framework.MockitoTestSuite;
62 import org.onap.portalsdk.core.domain.Menu;
63 import org.onap.portalsdk.core.domain.MenuData;
64 import org.onap.portalsdk.core.domain.RoleFunction;
65 import org.onap.portalsdk.core.domain.User;
66 import org.onap.portalsdk.core.service.FnMenuService;
67 import org.onap.portalsdk.core.service.FunctionalMenuListService;
68 import org.onap.portalsdk.core.web.support.UserUtils;
69
70 import com.fasterxml.jackson.databind.ObjectMapper;
71
72 public class FnMenuControllerTest {
73         
74         @InjectMocks
75         FnMenuController fnMenuController = new FnMenuController();
76         
77         @Mock
78         FnMenuService fnMenuService;
79         
80         @Mock
81         FunctionalMenuListService functionalMenuListService;
82
83         @Before
84         public void setup() {
85                 MockitoAnnotations.initMocks(this);
86         }
87         
88         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
89         
90         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
91         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
92         
93         NullPointerException nullPointerException = new NullPointerException();
94         
95         User user = new User();
96         
97         @Mock
98         UserUtils userUtils = new UserUtils();
99         
100         @Mock
101         ObjectMapper mapper = new ObjectMapper();
102         
103         @Test
104         public void getParentListTest() throws Exception{
105                 @SuppressWarnings("rawtypes")
106                 List<List> list = new ArrayList<>();
107                 Mockito.when(fnMenuService.getParentList()).thenReturn(list);
108                 StringWriter sw = new StringWriter();
109                 PrintWriter writer = new PrintWriter(sw);
110                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
111                 fnMenuController.getParentList(mockedRequest, mockedResponse);
112         }
113         
114         @Test
115         public void getParentListExceptionTest() throws Exception{
116                 Mockito.when(fnMenuService.getParentList()).thenThrow(nullPointerException);
117                 StringWriter sw = new StringWriter();
118                 PrintWriter writer = new PrintWriter(sw);
119                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
120                 fnMenuController.getParentList(mockedRequest, mockedResponse);
121         }
122         
123         @Test
124         public void getFunctionCDListTest() throws Exception{
125                 List<RoleFunction> roleFunctionList = new ArrayList<RoleFunction>();
126                 Mockito.when(functionalMenuListService.getFunctionCDList(mockedRequest)).thenReturn(roleFunctionList);
127                 StringWriter sw = new StringWriter();
128                 PrintWriter writer = new PrintWriter(sw);
129                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
130                 fnMenuController.getFunctionCDList(mockedRequest, mockedResponse);
131         }
132         
133         @Test
134         public void getFunctionCDListExceptionTest() throws Exception{
135                 Mockito.when(functionalMenuListService.getFunctionCDList(mockedRequest)).thenThrow(nullPointerException);
136                 StringWriter sw = new StringWriter();
137                 PrintWriter writer = new PrintWriter(sw);
138                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
139                 fnMenuController.getFunctionCDList(mockedRequest, mockedResponse);
140         }
141         
142         @Test
143         public void getFnMenuListTest() throws IOException{
144                 List<MenuData> menuList = new ArrayList<>();
145                 MenuData menudata = new MenuData();
146                 menudata.setId((long) 1);
147                 menudata.setLabel("test");
148                 menudata.setParentMenu(menudata);
149                 menuList.add(menudata);
150                 Mockito.when(fnMenuService.getFnMenuItems()).thenReturn(menuList);
151                 StringWriter sw = new StringWriter();
152                 PrintWriter writer = new PrintWriter(sw);
153                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
154                 fnMenuController.getFnMenuList(mockedRequest, mockedResponse);
155         }
156         
157         @Test
158         public void getFnMenuListExceptionTest() throws IOException{
159                 List<MenuData> menuList = new ArrayList<>();
160                 MenuData menudata = new MenuData();
161                 menudata.setId((long) 1);
162                 menudata.setLabel("test");
163                 menudata.setParentMenu(menudata);
164                 menuList.add(menudata);
165                 Mockito.when(fnMenuService.getFnMenuItems()).thenThrow(nullPointerException);
166                 fnMenuController.getFnMenuList(mockedRequest, mockedResponse);
167         }
168         
169         @Test
170         public void updateFnMenuTest() throws Exception{
171                 
172                 String fnMenuItem = "{\"availableFnMenuItem\":{\"id\":9,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\""
173                                 + ":null,\"auditTrail\":null,\"menuLevel\":null,\"label\":\"Profile\",\"parentId\":1,\"action\":\"userProfile\",\"functionCd\":\"menu_profile\","
174                                 + "\"sortOrder\":90,\"servlet\":\"N/A\",\"queryString\":\"N/A\",\"externalUrl\":\"test\",\"target\":\"N/A\",\"active\":true,\"menuSetCode\":\"APP\","
175                                 + "\"separator\":false,\"imageSrc\":\"icon-people-oneperson\",\"parentMenu\":null,\"childMenus\":[],\"activeAsString\":\"true\","
176                                 + "\"parentIdAsString\":\"1\",\"separatorAsString\":\"false\",\"active_yn\":false,\"$$hashKey\":\"object:99\"}}";
177                 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(fnMenuItem)));
178                 StringWriter sw = new StringWriter();
179                 PrintWriter writer = new PrintWriter(sw);
180                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
181                 assertNull(fnMenuController.updateFnMenu(mockedRequest, mockedResponse));                               
182         }
183         
184         @Test
185         public void updateFnMenuExceptionTest() throws Exception{
186                 Menu fnMenuItemObj = new Menu();
187                 fnMenuItemObj.setLabel("test");
188                 StringWriter sw = new StringWriter();
189                 PrintWriter writer = new PrintWriter(sw);
190                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
191                 assertNull(fnMenuController.updateFnMenu(mockedRequest, mockedResponse));       
192         }
193         
194         @Test
195         public void removeFnMenuTest() throws Exception{
196                 String fnMenuItem = "{\"fnMenuItem\":{\"id\":9,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\""
197                                 + ":null,\"auditTrail\":null,\"menuLevel\":null,\"label\":\"Profile\",\"parentId\":1,\"action\":\"userProfile\",\"functionCd\":\"menu_profile\","
198                                 + "\"sortOrder\":90,\"servlet\":\"N/A\",\"queryString\":\"N/A\",\"externalUrl\":\"test\",\"target\":\"N/A\",\"active\":true,\"menuSetCode\":\"APP\","
199                                 + "\"separator\":false,\"imageSrc\":\"icon-people-oneperson\",\"parentMenu\":null,\"childMenus\":[],\"activeAsString\":\"true\","
200                                 + "\"parentIdAsString\":\"1\",\"separatorAsString\":\"false\",\"active_yn\":false,\"$$hashKey\":\"object:99\"}}";
201                 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(fnMenuItem)));
202                 StringWriter sw = new StringWriter();
203                 PrintWriter writer = new PrintWriter(sw);
204                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
205                 assertNull(fnMenuController.removeFnMenu(mockedRequest, mockedResponse));
206         }
207         
208         @Test
209         public void removeFnMenuExceptionTest() throws Exception{
210                 String fnMenuItem = "{\"availableFnMenuItem\":{\"id\":9,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\""
211                                 + ":null,\"auditTrail\":null,\"menuLevel\":null,\"label\":\"Profile\",\"parentId\":1,\"action\":\"userProfile\",\"functionCd\":\"menu_profile\","
212                                 + "\"sortOrder\":90,\"servlet\":\"N/A\",\"queryString\":\"N/A\",\"externalUrl\":\"test\",\"target\":\"N/A\",\"active\":true,\"menuSetCode\":\"APP\","
213                                 + "\"separator\":false,\"imageSrc\":\"icon-people-oneperson\",\"parentMenu\":null,\"childMenus\":[],\"activeAsString\":\"true\","
214                                 + "\"parentIdAsString\":\"1\",\"separatorAsString\":\"false\",\"active_yn\":false,\"$$hashKey\":\"object:99\"}}";
215                 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(fnMenuItem)));
216                 StringWriter sw = new StringWriter();
217                 PrintWriter writer = new PrintWriter(sw);
218                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
219                 assertNull(fnMenuController.removeFnMenu(mockedRequest, mockedResponse));
220         }
221         
222         @Test
223         public void getViewNameTest() {
224                 String expectedResult = "test";
225                 fnMenuController.setViewName(expectedResult);
226                 String actualResult = fnMenuController.getViewName();
227                 assertEquals(expectedResult, actualResult);
228         }
229 }