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