06ba0626658231e234840096f7a29e41529d246c
[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 java.io.PrintWriter;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
44
45 import javax.servlet.ServletContext;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import javax.servlet.http.HttpSession;
49
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;
63
64 @RunWith(PowerMockRunner.class)
65 @PrepareForTest({UsageUtils.class})
66 public class UsageListControllerTest {
67
68         @InjectMocks
69         private UsageListController usageListController;
70
71         @Mock
72         private ProfileService service;
73
74         @Test
75         public void usageListTest() {
76                 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
77
78                 HttpSession httpSession = Mockito.mock(HttpSession.class);
79                 Mockito.when(request.getSession()).thenReturn(httpSession);
80                 ServletContext context = Mockito.mock(ServletContext.class);
81
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");
91                 rows.add(bean);
92                 rows.add(bean2);
93                 PowerMockito.mockStatic(UsageUtils.class);
94                 Mockito.when(UsageUtils.getActiveUsers(activeUsers)).thenReturn(rows);
95                 ModelAndView view = usageListController.usageList(request);
96                 Assert.assertNotNull(view);
97         }
98         
99         @Test
100         public void usageListExceptionTest() {
101                 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
102
103                 HttpSession httpSession = Mockito.mock(HttpSession.class);
104                 Mockito.when(request.getSession()).thenReturn(httpSession);
105                 ServletContext context = Mockito.mock(ServletContext.class);
106
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");
115                 rows.add(bean);
116                 rows.add(bean2);
117                 PowerMockito.mockStatic(UsageUtils.class);
118                 Mockito.when(UsageUtils.getActiveUsers(activeUsers)).thenReturn(rows);
119                 ModelAndView view = usageListController.usageList(request);
120                 Assert.assertNotNull(view);
121         }
122         
123         @Test
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);
129                 
130                 HttpSession httpSession = Mockito.mock(HttpSession.class);
131                 Mockito.when(request.getSession()).thenReturn(httpSession);
132                 ServletContext context = Mockito.mock(ServletContext.class);
133
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");
143                 rows.add(bean);
144                 rows.add(bean2);
145                 PowerMockito.mockStatic(UsageUtils.class);
146                 Mockito.when(UsageUtils.getActiveUsers(activeUsers)).thenReturn(rows);
147                 usageListController.getUsageList(request, response);
148                 Assert.assertTrue(true);
149         }
150         
151         @Test
152         public void getUsageListExceptionTest() throws Exception  {
153                 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
154                 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
155                 
156                 HttpSession httpSession = Mockito.mock(HttpSession.class);
157                 Mockito.when(request.getSession()).thenReturn(httpSession);
158                 ServletContext context = Mockito.mock(ServletContext.class);
159
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");
168                 rows.add(bean);
169                 rows.add(bean2);
170                 PowerMockito.mockStatic(UsageUtils.class);
171                 Mockito.when(UsageUtils.getActiveUsers(activeUsers)).thenReturn(rows);
172                 usageListController.getUsageList(request, response);
173                 Assert.assertTrue(true);
174         }
175         
176         @Test
177         public void removeSessionTest() throws Exception {
178
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);
183                 
184                 HttpSession httpSession = Mockito.mock(HttpSession.class);
185                 Mockito.when(request.getSession()).thenReturn(httpSession);
186                 ServletContext context = Mockito.mock(ServletContext.class);
187
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");
197                 rows.add(bean);
198                 rows.add(bean2);
199                 PowerMockito.mockStatic(UsageUtils.class);
200                 Mockito.when(UsageUtils.getActiveUsers(activeUsers)).thenReturn(rows);
201                 usageListController.removeSession(request, response);
202                 Assert.assertTrue(true);
203         }
204         
205         @Test
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);
211                 
212                 HttpSession httpSession = Mockito.mock(HttpSession.class);
213                 Mockito.when(request.getSession()).thenReturn(httpSession);
214                 ServletContext context = Mockito.mock(ServletContext.class);
215
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");
224                 rows.add(bean);
225                 rows.add(bean2);
226                 PowerMockito.mockStatic(UsageUtils.class);
227                 Mockito.when(UsageUtils.getActiveUsers(activeUsers)).thenReturn(rows);
228                 usageListController.removeSession(request, response);
229                 Assert.assertTrue(true);
230         }
231 }