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.BufferedReader;
41 import java.io.PrintWriter;
42 import java.io.Reader;
43 import java.io.StringReader;
44 import java.util.ArrayList;
45 import java.util.List;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
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.PostSearchBean;
57 import org.onap.portalsdk.core.command.support.SearchResult;
58 import org.onap.portalsdk.core.domain.Profile;
59 import org.onap.portalsdk.core.domain.User;
60 import org.onap.portalsdk.core.service.LdapService;
61 import org.onap.portalsdk.core.service.PostSearchService;
62 import org.onap.portalsdk.core.service.ProfileService;
63 import org.onap.portalsdk.core.web.support.UserUtils;
64 import org.powermock.api.mockito.PowerMockito;
65 import org.powermock.core.classloader.annotations.PrepareForTest;
66 import org.powermock.modules.junit4.PowerMockRunner;
67 import org.springframework.web.servlet.ModelAndView;
69 @RunWith(PowerMockRunner.class)
70 @PrepareForTest({UserUtils.class})
71 public class PostSearchControllerTest {
74 private PostSearchController postSearchController;
77 private PostSearchService postSearchService;
80 private LdapService ldapService;
83 private ProfileService profileService;
86 public void welcomeTest() throws Exception {
87 PostSearchBean postSearchBean = new PostSearchBean();
88 Profile profile = new Profile();
90 profile.setOrgUserId("123");
91 List<Profile> list = new ArrayList<>();
93 Mockito.when(profileService.findAll()).thenReturn(list);
95 ModelAndView modelAndView = postSearchController.welcome(postSearchBean);
96 Assert.assertNotNull(modelAndView);
100 public void getPostSearchProfileTest() throws Exception {
101 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
102 PostSearchBean postSearchBean = new PostSearchBean();
104 PrintWriter writer = Mockito.mock(PrintWriter.class);
105 Mockito.when(response.getWriter()).thenReturn(writer);
106 postSearchController.getPostSearchProfile(response, postSearchBean);
107 Assert.assertTrue(true);
111 public void getPostSearchProfileExceptionTest() throws Exception {
112 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
113 PostSearchBean postSearchBean = new PostSearchBean();
115 postSearchController.getPostSearchProfile(response, postSearchBean);
116 Assert.assertTrue(true);
120 public void searchTest() throws Exception {
121 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
122 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
124 String json = " { \"postSearchBean\": { \"selected\": [\"test\" ] }}";
125 Reader inputString = new StringReader(json);
126 BufferedReader buffer = new BufferedReader(inputString);
128 Mockito.when(request.getReader()).thenReturn(buffer);
130 PowerMockito.mockStatic(UserUtils.class);
131 User user = new User();
133 Mockito.when(UserUtils.getUserSession(request)).thenReturn(user);
135 Mockito.when(ldapService.searchPost(Mockito.any(), Mockito.anyString(), Mockito.anyString(),
136 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(new SearchResult());
137 postSearchController.search(request, response);
138 Assert.assertTrue(true);
142 public void processExceptionTest() throws Exception {
143 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
144 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
146 String json = " { \"postSearchBean\": { \"selected\": [\"test\" ] }}";
147 Reader inputString = new StringReader(json);
148 BufferedReader buffer = new BufferedReader(inputString);
150 Mockito.when(request.getReader()).thenReturn(buffer);
151 PrintWriter out = Mockito.mock(PrintWriter.class);
152 Mockito.when(response.getWriter()).thenReturn(out);
153 postSearchController.process(request, response);
154 Assert.assertTrue(true);