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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.controller.core;
40 import java.io.PrintWriter;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
45 import javax.servlet.ServletContext;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import javax.servlet.http.HttpSession;
50 import org.junit.Assert;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.InjectMocks;
54 import org.mockito.Mock;
55 import org.mockito.Mockito;
56 import org.onap.portalsdk.core.command.UserRowBean;
57 import org.onap.portalsdk.core.service.ProfileService;
58 import org.onap.portalsdk.core.util.UsageUtils;
59 import org.powermock.api.mockito.PowerMockito;
60 import org.powermock.core.classloader.annotations.PrepareForTest;
61 import org.powermock.modules.junit4.PowerMockRunner;
62 import org.springframework.web.servlet.ModelAndView;
64 @RunWith(PowerMockRunner.class)
65 @PrepareForTest({UsageUtils.class})
66 public class UsageListControllerTest {
69 private UsageListController usageListController;
72 private ProfileService service;
75 public void usageListTest() {
76 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
78 HttpSession httpSession = Mockito.mock(HttpSession.class);
79 Mockito.when(request.getSession()).thenReturn(httpSession);
80 ServletContext context = Mockito.mock(ServletContext.class);
82 Mockito.when(httpSession.getServletContext()).thenReturn(context);
83 Mockito.when(httpSession.getId()).thenReturn("123");
84 HashMap activeUsers = new HashMap();
85 Mockito.when(context.getAttribute("activeUsers")).thenReturn(activeUsers);
86 List<UserRowBean> rows = new ArrayList<>() ;
87 UserRowBean bean = new UserRowBean();
88 bean.setSessionId("123");
89 UserRowBean bean2 = new UserRowBean();
90 bean2.setSessionId("124");
93 PowerMockito.mockStatic(UsageUtils.class);
94 Mockito.when(UsageUtils.getActiveUsers(activeUsers)).thenReturn(rows);
95 ModelAndView view = usageListController.usageList(request);
96 Assert.assertNotNull(view);
100 public void usageListExceptionTest() {
101 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
103 HttpSession httpSession = Mockito.mock(HttpSession.class);
104 Mockito.when(request.getSession()).thenReturn(httpSession);
105 ServletContext context = Mockito.mock(ServletContext.class);
107 Mockito.when(httpSession.getServletContext()).thenReturn(context);
108 HashMap activeUsers = new HashMap();
109 Mockito.when(context.getAttribute("activeUsers")).thenReturn(activeUsers);
110 List<UserRowBean> rows = new ArrayList<>() ;
111 UserRowBean bean = new UserRowBean();
112 bean.setSessionId("123");
113 UserRowBean bean2 = new UserRowBean();
114 bean2.setSessionId("124");
117 PowerMockito.mockStatic(UsageUtils.class);
118 Mockito.when(UsageUtils.getActiveUsers(activeUsers)).thenReturn(rows);
119 ModelAndView view = usageListController.usageList(request);
120 Assert.assertNotNull(view);
124 public void getUsageListTest() throws Exception {
125 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
126 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
127 PrintWriter mockWriter = Mockito.mock(PrintWriter.class);
128 Mockito.when(response.getWriter()).thenReturn(mockWriter);
130 HttpSession httpSession = Mockito.mock(HttpSession.class);
131 Mockito.when(request.getSession()).thenReturn(httpSession);
132 ServletContext context = Mockito.mock(ServletContext.class);
134 Mockito.when(httpSession.getServletContext()).thenReturn(context);
135 Mockito.when(httpSession.getId()).thenReturn("123");
136 HashMap activeUsers = new HashMap();
137 Mockito.when(context.getAttribute("activeUsers")).thenReturn(activeUsers);
138 List<UserRowBean> rows = new ArrayList<>() ;
139 UserRowBean bean = new UserRowBean();
140 bean.setSessionId("123");
141 UserRowBean bean2 = new UserRowBean();
142 bean2.setSessionId("124");
145 PowerMockito.mockStatic(UsageUtils.class);
146 Mockito.when(UsageUtils.getActiveUsers(activeUsers)).thenReturn(rows);
147 usageListController.getUsageList(request, response);
148 Assert.assertTrue(true);
152 public void getUsageListExceptionTest() throws Exception {
153 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
154 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
156 HttpSession httpSession = Mockito.mock(HttpSession.class);
157 Mockito.when(request.getSession()).thenReturn(httpSession);
158 ServletContext context = Mockito.mock(ServletContext.class);
160 Mockito.when(httpSession.getServletContext()).thenReturn(context);
161 HashMap activeUsers = new HashMap();
162 Mockito.when(context.getAttribute("activeUsers")).thenReturn(activeUsers);
163 List<UserRowBean> rows = new ArrayList<>() ;
164 UserRowBean bean = new UserRowBean();
165 bean.setSessionId("123");
166 UserRowBean bean2 = new UserRowBean();
167 bean2.setSessionId("124");
170 PowerMockito.mockStatic(UsageUtils.class);
171 Mockito.when(UsageUtils.getActiveUsers(activeUsers)).thenReturn(rows);
172 usageListController.getUsageList(request, response);
173 Assert.assertTrue(true);
177 public void removeSessionTest() throws Exception {
179 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
180 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
181 PrintWriter mockWriter = Mockito.mock(PrintWriter.class);
182 Mockito.when(response.getWriter()).thenReturn(mockWriter);
184 HttpSession httpSession = Mockito.mock(HttpSession.class);
185 Mockito.when(request.getSession()).thenReturn(httpSession);
186 ServletContext context = Mockito.mock(ServletContext.class);
188 Mockito.when(httpSession.getServletContext()).thenReturn(context);
189 Mockito.when(httpSession.getId()).thenReturn("123");
190 HashMap activeUsers = new HashMap();
191 Mockito.when(context.getAttribute("activeUsers")).thenReturn(activeUsers);
192 List<UserRowBean> rows = new ArrayList<>() ;
193 UserRowBean bean = new UserRowBean();
194 bean.setSessionId("123");
195 UserRowBean bean2 = new UserRowBean();
196 bean2.setSessionId("124");
199 PowerMockito.mockStatic(UsageUtils.class);
200 Mockito.when(UsageUtils.getActiveUsers(activeUsers)).thenReturn(rows);
201 usageListController.removeSession(request, response);
202 Assert.assertTrue(true);
206 public void removeSessionExceptionTest() throws Exception {
207 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
208 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
209 PrintWriter mockWriter = Mockito.mock(PrintWriter.class);
210 Mockito.when(response.getWriter()).thenReturn(mockWriter);
212 HttpSession httpSession = Mockito.mock(HttpSession.class);
213 Mockito.when(request.getSession()).thenReturn(httpSession);
214 ServletContext context = Mockito.mock(ServletContext.class);
216 Mockito.when(httpSession.getServletContext()).thenReturn(context);
217 HashMap activeUsers = new HashMap();
218 Mockito.when(context.getAttribute("activeUsers")).thenReturn(activeUsers);
219 List<UserRowBean> rows = new ArrayList<>() ;
220 UserRowBean bean = new UserRowBean();
221 bean.setSessionId("123");
222 UserRowBean bean2 = new UserRowBean();
223 bean2.setSessionId("124");
226 PowerMockito.mockStatic(UsageUtils.class);
227 Mockito.when(UsageUtils.getActiveUsers(activeUsers)).thenReturn(rows);
228 usageListController.removeSession(request, response);
229 Assert.assertTrue(true);