1f0e5524a54f685aad11aca40ddf928f7d431ce2
[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.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;
46
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
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.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;
68
69 @RunWith(PowerMockRunner.class)
70 @PrepareForTest({UserUtils.class})
71 public class PostSearchControllerTest {
72
73         @InjectMocks
74         private PostSearchController postSearchController;
75
76         @Mock
77         private PostSearchService postSearchService;
78
79         @Mock
80         private LdapService ldapService;
81
82         @Mock
83         private ProfileService profileService;
84
85         @Test
86         public void welcomeTest() throws Exception {
87                 PostSearchBean postSearchBean = new PostSearchBean();
88                 Profile profile = new Profile();
89                 profile.setId(123L);
90                 profile.setOrgUserId("123");
91                 List<Profile> list = new ArrayList<>();
92                 list.add(profile);
93                 Mockito.when(profileService.findAll()).thenReturn(list);
94
95                 ModelAndView modelAndView = postSearchController.welcome(postSearchBean);
96                 Assert.assertNotNull(modelAndView);
97         }
98
99         @Test
100         public void getPostSearchProfileTest() throws Exception {
101                 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
102                 PostSearchBean postSearchBean = new PostSearchBean();
103
104                 PrintWriter writer = Mockito.mock(PrintWriter.class);
105                 Mockito.when(response.getWriter()).thenReturn(writer);
106                 postSearchController.getPostSearchProfile(response, postSearchBean);
107                 Assert.assertTrue(true);
108         }
109
110         @Test
111         public void getPostSearchProfileExceptionTest() throws Exception {
112                 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
113                 PostSearchBean postSearchBean = new PostSearchBean();
114
115                 postSearchController.getPostSearchProfile(response, postSearchBean);
116                 Assert.assertTrue(true);
117         }
118
119         @Test
120         public void searchTest() throws Exception {
121                 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
122                 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
123
124                 String json = " { \"postSearchBean\": { \"selected\": [\"test\" ] }}";
125                 Reader inputString = new StringReader(json);
126                 BufferedReader buffer = new BufferedReader(inputString);
127                 
128                 Mockito.when(request.getReader()).thenReturn(buffer);
129                 
130                 PowerMockito.mockStatic(UserUtils.class);
131                 User user = new User();
132                 user.setId(123L);
133                 Mockito.when(UserUtils.getUserSession(request)).thenReturn(user);
134                 
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);
139         }
140
141         @Test
142         public void processExceptionTest() throws Exception {
143                 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
144                 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
145                 
146                 String json = " { \"postSearchBean\": { \"selected\": [\"test\" ] }}";
147                 Reader inputString = new StringReader(json);
148                 BufferedReader buffer = new BufferedReader(inputString);
149                 
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);
155         }
156 }