2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 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.portal.service;
40 import static org.junit.Assert.*;
42 import java.util.ArrayList;
43 import java.util.List;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.onap.portalapp.portal.domain.EPUser;
56 import org.onap.portalapp.portal.framework.MockEPUser;
57 import org.onap.portalapp.portal.framework.MockitoTestSuite;
58 import org.onap.portalapp.portal.service.SearchServiceImpl;
59 import org.onap.portalapp.portal.service.UserService;
60 import org.onap.portalapp.portal.transport.UserWithNameSurnameTitle;
61 import org.onap.portalapp.portal.utils.EcompPortalUtils;
62 import org.powermock.api.mockito.PowerMockito;
63 import org.powermock.core.classloader.annotations.PrepareForTest;
64 import org.powermock.modules.junit4.PowerMockRunner;
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({ EcompPortalUtils.class})
69 public class SearchServiceImplTest {
72 SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
75 UserService userService;
80 MockitoAnnotations.initMocks(this);
83 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
85 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
86 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
88 NullPointerException nullPointerException = new NullPointerException();
89 MockEPUser mockUser = new MockEPUser();
92 public void searchUsersInPhoneBookTest()
94 PowerMockito.mockStatic(EcompPortalUtils.class);
95 List<String> list = new ArrayList<>();
98 Mockito.when(EcompPortalUtils.parsingByRegularExpression("Test", " ")).thenReturn(list);
99 assertEquals(searchServiceImpl.searchUsersInPhoneBook("Test"), "[]");
103 public void searchUsersInFnTableToFindUserIdTest()
105 PowerMockito.mockStatic(EcompPortalUtils.class);
106 List<String> list = new ArrayList<>();
108 String str2 = "Test new";
109 String str1 = "Test new1";
113 Mockito.when(EcompPortalUtils.parsingByRegularExpression("Test", " ")).thenReturn(list);
114 List<EPUser> userList = new ArrayList();
115 EPUser user = mockUser.mockEPUser();
116 user.setLastName("Test new");
118 Mockito.when( this.userService.getUserByFirstLastName("Test","Test new")).thenReturn(userList);
119 String result = searchServiceImpl.searchUsersInPhoneBook("Test");
120 assertEquals("[{\"orgUserId\":\"guestT\",\"firstName\":\"test\",\"lastName\":\"Test new\",\"jobTitle\":null}]" , result);
126 public void searchUsersInFnTableFirstNameTest()
128 PowerMockito.mockStatic(EcompPortalUtils.class);
129 List<String> list = new ArrayList<>();
130 String str = "TestTT";
131 String str2 = "Test new1";
132 String str1 = "Test new";
133 String str3 = "Test new2";
138 Mockito.when(EcompPortalUtils.parsingByRegularExpression("TestTT", " ")).thenReturn(list);
139 List<EPUser> userList = new ArrayList();
140 EPUser user = mockUser.mockEPUser();
141 user.setLastName("Test new");
142 user.setFirstName(null);
144 Mockito.when( this.userService.getUserByFirstLastName("TestTT","Test new")).thenReturn(userList);
145 assertEquals(searchServiceImpl.searchUsersInPhoneBook("TestTT"), "[]"); }
149 public void searchUsersInFnTableLastNameTest()
151 PowerMockito.mockStatic(EcompPortalUtils.class);
152 List<String> list = new ArrayList<>();
154 String str2 = "Test new";
155 String str1 = "Test new1";
159 Mockito.when(EcompPortalUtils.parsingByRegularExpression("Test", " ")).thenReturn(list);
160 List<EPUser> userList = new ArrayList();
161 EPUser user = mockUser.mockEPUser();
162 user.setLastName(null);
164 Mockito.when( this.userService.getUserByFirstLastName("Test","Test new")).thenReturn(userList);
165 assertEquals(searchServiceImpl.searchUsersInPhoneBook("Test"), "[]"); }
170 public void searchUserByUserIdTest()
172 List<EPUser> userList = new ArrayList();
173 EPUser user = mockUser.mockEPUser();
174 user.setLastName("Test new");
176 List<EPUser> foundUsers = new ArrayList<EPUser>();
177 Mockito.when(this.userService.getUserByUserId("guestT")).thenReturn(userList);
179 EPUser expectedUser = searchServiceImpl.searchUserByUserId("guestT");
180 assertEquals(user, expectedUser);
184 public void searchUserByUserIdExceptionTest()
186 Mockito.when(this.userService.getUserByUserId("guestT")).thenThrow(nullPointerException);
187 assertNull(searchServiceImpl.searchUserByUserId("guestT"));
192 public void searchUsersByUserIdTest()
195 List<EPUser> userList = new ArrayList();
196 EPUser user = mockUser.mockEPUser();
197 user.setLastName("Test new");
199 Mockito.when(this.userService.getUserByUserId("guestT")).thenReturn(userList);
200 List<UserWithNameSurnameTitle> foundUsers = searchServiceImpl.searchUsersByUserId(user);
201 assertEquals(foundUsers.size(), 1);
206 public void searchUsersByUserIdExceptionTest()
208 EPUser user = mockUser.mockEPUser();
209 user.setLastName("Test new");
210 Mockito.when(this.userService.getUserByUserId("guestT")).thenThrow(nullPointerException);
211 List<UserWithNameSurnameTitle> foundUsers = searchServiceImpl.searchUsersByUserId(user);
212 assertEquals(foundUsers.size(), 0);
217 public void searchUsersByNameExceptionTest()
219 EPUser user = mockUser.mockEPUser();
220 user.setLastName("test");
221 user.setFirstName("test");
223 Mockito.when(this.userService.getUserByFirstLastName("test","test")).thenThrow(nullPointerException);
224 List<UserWithNameSurnameTitle> foundUsers = searchServiceImpl.searchUsersByName(user);
225 assertEquals(foundUsers.size(), 0);