6b24e96d3d1fdaa106bb249bca6439993e9770a2
[portal.git] / ecomp-portal-BE-os / src / test / java / org / onap / portalapp / portal / service / SearchServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import static org.junit.Assert.*;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
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;
65
66
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({ EcompPortalUtils.class})
69 public class SearchServiceImplTest {
70         
71         @InjectMocks
72         SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
73
74         @Mock
75         UserService userService;
76         
77
78         @Before
79         public void setup() {
80                 MockitoAnnotations.initMocks(this);
81         }
82
83         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
84
85         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
86         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
87
88         NullPointerException nullPointerException = new NullPointerException();
89         MockEPUser mockUser = new MockEPUser();
90         
91         @Test
92         public void searchUsersInPhoneBookTest()
93         {
94                 PowerMockito.mockStatic(EcompPortalUtils.class);
95                 List<String> list = new ArrayList<>();
96                 String str = "Test";
97                 list.add(str);
98                 Mockito.when(EcompPortalUtils.parsingByRegularExpression("Test", " ")).thenReturn(list);
99                 assertEquals(searchServiceImpl.searchUsersInPhoneBook("Test"), "[]");
100         }
101         
102         @Test
103         public void searchUsersInFnTableToFindUserIdTest()
104         {
105         PowerMockito.mockStatic(EcompPortalUtils.class);
106         List<String> list = new ArrayList<>();
107         String str = "Test";
108         String str2 = "Test new";
109         String str1 = "Test new1";
110         list.add(str);
111         list.add(str1);
112         list.add(str2);
113         Mockito.when(EcompPortalUtils.parsingByRegularExpression("Test", " ")).thenReturn(list);
114         List<EPUser> userList = new ArrayList();
115         EPUser user = mockUser.mockEPUser();
116         user.setLastName("Test new");
117         userList.add(user);
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);
121         }
122         
123         
124         
125         @Test
126         public void searchUsersInFnTableFirstNameTest()
127         {
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";
134         list.add(str);
135         list.add(str1);
136         list.add(str2);
137         list.add(str3);
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);
143         userList.add(user);
144         Mockito.when( this.userService.getUserByFirstLastName("TestTT","Test new")).thenReturn(userList);
145         assertEquals(searchServiceImpl.searchUsersInPhoneBook("TestTT"), "[]"); }
146
147         
148         @Test
149         public void searchUsersInFnTableLastNameTest()
150         {
151                 PowerMockito.mockStatic(EcompPortalUtils.class);
152             List<String> list = new ArrayList<>();
153                 String str = "Test";
154                 String str2 = "Test new";
155                 String str1 = "Test new1";
156                 list.add(str);
157                 list.add(str1);
158                 list.add(str2);
159                 Mockito.when(EcompPortalUtils.parsingByRegularExpression("Test", " ")).thenReturn(list);
160                 List<EPUser> userList = new ArrayList();
161                 EPUser user = mockUser.mockEPUser();
162                 user.setLastName(null);
163                 userList.add(user);
164                 Mockito.when( this.userService.getUserByFirstLastName("Test","Test new")).thenReturn(userList);
165                 assertEquals(searchServiceImpl.searchUsersInPhoneBook("Test"), "[]");   }
166                 
167         
168
169         @Test
170         public void searchUserByUserIdTest()
171         {
172                 List<EPUser> userList = new ArrayList();
173                 EPUser user = mockUser.mockEPUser();
174                 user.setLastName("Test new");
175                 userList.add(user);
176                 List<EPUser> foundUsers = new ArrayList<EPUser>();
177                 Mockito.when(this.userService.getUserByUserId("guestT")).thenReturn(userList);
178                 
179                 EPUser expectedUser = searchServiceImpl.searchUserByUserId("guestT");
180                 assertEquals(user, expectedUser);
181         }
182         
183         @Test
184         public void searchUserByUserIdExceptionTest()
185         {
186                 Mockito.when(this.userService.getUserByUserId("guestT")).thenThrow(nullPointerException);
187                 assertNull(searchServiceImpl.searchUserByUserId("guestT"));
188                 
189         }
190         
191         @Test
192         public void searchUsersByUserIdTest()
193         {
194                 
195                 List<EPUser> userList = new ArrayList();
196                 EPUser user = mockUser.mockEPUser();
197                 user.setLastName("Test new");
198                 userList.add(user);
199                 Mockito.when(this.userService.getUserByUserId("guestT")).thenReturn(userList);
200                 List<UserWithNameSurnameTitle> foundUsers = searchServiceImpl.searchUsersByUserId(user);
201                 assertEquals(foundUsers.size(), 1);
202                 
203         }
204         
205         @Test
206         public void searchUsersByUserIdExceptionTest()
207         {
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);
213                 
214         }
215         
216         @Test
217         public void searchUsersByNameExceptionTest()
218         {
219                 EPUser user = mockUser.mockEPUser();
220                 user.setLastName("test");
221                 user.setFirstName("test");
222
223                 Mockito.when(this.userService.getUserByFirstLastName("test","test")).thenThrow(nullPointerException);
224                 List<UserWithNameSurnameTitle> foundUsers =     searchServiceImpl.searchUsersByName(user);
225                 assertEquals(foundUsers.size(), 0);
226                 
227         }
228 }