2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017-2018 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============================================
38 package org.onap.portalapp.portal.service;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNull;
43 import java.util.ArrayList;
44 import java.util.List;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.InjectMocks;
53 import org.mockito.Mock;
54 import org.mockito.Mockito;
55 import org.mockito.MockitoAnnotations;
56 import org.onap.portalapp.portal.domain.EPUser;
57 import org.onap.portalapp.portal.framework.MockEPUser;
58 import org.onap.portalapp.portal.framework.MockitoTestSuite;
59 import org.onap.portalapp.portal.transport.UserWithNameSurnameTitle;
60 import org.onap.portalapp.portal.utils.EcompPortalUtils;
61 import org.powermock.api.mockito.PowerMockito;
62 import org.powermock.core.classloader.annotations.PrepareForTest;
63 import org.powermock.modules.junit4.PowerMockRunner;
66 @RunWith(PowerMockRunner.class)
67 @PrepareForTest({ EcompPortalUtils.class})
68 public class SearchServiceImplTest {
71 SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
74 UserService userService;
79 MockitoAnnotations.initMocks(this);
82 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
84 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
85 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
87 NullPointerException nullPointerException = new NullPointerException();
88 MockEPUser mockUser = new MockEPUser();
91 public void searchUsersInPhoneBookTest()
93 PowerMockito.mockStatic(EcompPortalUtils.class);
94 List<String> list = new ArrayList<>();
97 Mockito.when(EcompPortalUtils.parsingByRegularExpression("Test", " ")).thenReturn(list);
98 assertEquals(searchServiceImpl.searchUsersInPhoneBook("Test"), "[]");
102 public void searchUsersInFnTableToFindUserIdTest()
104 PowerMockito.mockStatic(EcompPortalUtils.class);
105 List<String> list = new ArrayList<>();
107 String str1 = "Testnew";
110 Mockito.when(EcompPortalUtils.parsingByRegularExpression("Test Testnew", " ")).thenReturn(list);
111 @SuppressWarnings({ "unchecked", "rawtypes" })
112 List<EPUser> userList = new ArrayList();
113 EPUser user = mockUser.mockEPUser();
114 user.setLastName("Testnew");
116 Mockito.when( this.userService.getUserByFirstLastName("Test","Testnew")).thenReturn(userList);
117 String result = searchServiceImpl.searchUsersInPhoneBook("Test Testnew");
118 assertEquals("[{\"orgUserId\":\"guestT\",\"firstName\":\"test\",\"lastName\":\"Testnew\",\"jobTitle\":null}]" , result);
124 public void searchUsersInFnTableFirstNameTest()
126 PowerMockito.mockStatic(EcompPortalUtils.class);
127 List<String> list = new ArrayList<>();
128 String str = "TestTT";
129 String str2 = "Test new1";
130 String str1 = "Test new";
131 String str3 = "Test new2";
136 Mockito.when(EcompPortalUtils.parsingByRegularExpression("TestTT", " ")).thenReturn(list);
137 List<EPUser> userList = new ArrayList();
138 EPUser user = mockUser.mockEPUser();
139 user.setLastName("Test new");
140 user.setFirstName(null);
142 Mockito.when( this.userService.getUserByFirstLastName("TestTT","Test new")).thenReturn(userList);
143 assertEquals(searchServiceImpl.searchUsersInPhoneBook("TestTT"), "[]"); }
147 public void searchUsersInFnTableLastNameTest()
149 PowerMockito.mockStatic(EcompPortalUtils.class);
150 List<String> list = new ArrayList<>();
152 String str2 = "Test new";
153 String str1 = "Test new1";
157 Mockito.when(EcompPortalUtils.parsingByRegularExpression("Test", " ")).thenReturn(list);
158 List<EPUser> userList = new ArrayList();
159 EPUser user = mockUser.mockEPUser();
160 user.setLastName(null);
162 Mockito.when( this.userService.getUserByFirstLastName("Test","Test new")).thenReturn(userList);
163 assertEquals(searchServiceImpl.searchUsersInPhoneBook("Test"), "[]"); }
168 public void searchUserByUserIdTest()
170 List<EPUser> userList = new ArrayList();
171 EPUser user = mockUser.mockEPUser();
172 user.setLastName("Test new");
174 List<EPUser> foundUsers = new ArrayList<EPUser>();
175 Mockito.when(this.userService.getUserByUserId("guestT")).thenReturn(userList);
177 EPUser expectedUser = searchServiceImpl.searchUserByUserId("guestT");
178 assertEquals(user, expectedUser);
182 public void searchUserByUserIdExceptionTest()
184 Mockito.when(this.userService.getUserByUserId("guestT")).thenThrow(nullPointerException);
185 assertNull(searchServiceImpl.searchUserByUserId("guestT"));
190 public void searchUsersByUserIdTest()
193 List<EPUser> userList = new ArrayList();
194 EPUser user = mockUser.mockEPUser();
195 user.setLastName("Test new");
197 Mockito.when(this.userService.getUserByUserId("guestT")).thenReturn(userList);
198 List<UserWithNameSurnameTitle> foundUsers = searchServiceImpl.searchUsersByUserId(user);
199 assertEquals(foundUsers.size(), 1);
204 public void searchUsersByUserIdExceptionTest()
206 EPUser user = mockUser.mockEPUser();
207 user.setLastName("Test new");
208 Mockito.when(this.userService.getUserByUserId("guestT")).thenThrow(nullPointerException);
209 List<UserWithNameSurnameTitle> foundUsers = searchServiceImpl.searchUsersByUserId(user);
210 assertEquals(foundUsers.size(), 0);
215 public void searchUsersByNameExceptionTest()
217 EPUser user = mockUser.mockEPUser();
218 user.setLastName("test");
219 user.setFirstName("test");
221 Mockito.when(this.userService.getUserByFirstLastName("test","test")).thenThrow(nullPointerException);
222 List<UserWithNameSurnameTitle> foundUsers = searchServiceImpl.searchUsersByName(user);
223 assertEquals(foundUsers.size(), 0);