61244ddd59e2a14253564cc49dab96b97de13ae3
[portal.git] / ecomp-portal-BE-common / 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.service;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotEquals;
42 import static org.junit.Assert.assertNull;
43
44 import java.util.ArrayList;
45 import java.util.List;
46
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.InjectMocks;
51 import org.mockito.Matchers;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.onap.portalapp.portal.core.MockEPUser;
56 import org.onap.portalapp.portal.domain.EPUser;
57 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
58 import org.onap.portalapp.portal.utils.EcompPortalUtils;
59 import org.onap.portalapp.portal.utils.PortalConstants;
60 import org.onap.portalsdk.core.command.support.SearchResult;
61 import org.onap.portalsdk.core.util.SystemProperties;
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, PortalConstants.class, SystemProperties.class,
69                 EPCommonSystemProperties.class })
70 public class SearchServiceImplTest {
71
72         @InjectMocks
73         SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
74
75         @Mock
76         EPLdapService epLdapService;
77
78         @Before
79         public void setup() {
80                 MockitoAnnotations.initMocks(this);
81         }
82         
83         MockEPUser mockUser = new MockEPUser();
84         
85         @SuppressWarnings("unchecked")
86         @Test
87         public void searchUsersInPhoneBookTest() throws Exception {
88                 PowerMockito.mockStatic(EcompPortalUtils.class);
89                 List<String> tokens = new ArrayList<>();
90                 tokens.add("ts1234");
91                 tokens.add("TestName");
92                 EPUser user = new EPUser();
93                 user.setOrgUserId("ts1234");
94                 user.setFirstName("TestName");
95                 Mockito.when(EcompPortalUtils.parsingByRegularExpression(Matchers.anyString(), Matchers.anyString())).thenReturn(tokens);
96                 SearchResult searchResult  = new SearchResult();
97                 searchResult.add(user);
98                 Mockito.when(epLdapService.searchPost(Matchers.any(EPUser.class), Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt())).thenReturn(searchResult);
99                 String actual = searchServiceImpl.searchUsersInPhoneBook("ts1234");
100                 assertNotEquals("[]", actual);
101         }
102         
103         @SuppressWarnings("unchecked")
104         @Test
105         public void searchUsersInPhoneBookFirtAndLastNameTest() throws Exception {
106                 PowerMockito.mockStatic(EcompPortalUtils.class);
107                 List<String> tokens = new ArrayList<>();
108                 tokens.add("ts1234");
109                 tokens.add("TestFirstName");
110                 tokens.add("TestLastName");
111                 EPUser user = new EPUser();
112                 user.setOrgUserId("ts1234");
113                 user.setFirstName("TestFirstName");
114                 user.setLastName("TestLastName");       
115                 Mockito.when(EcompPortalUtils.parsingByRegularExpression(Matchers.anyString(), Matchers.anyString())).thenReturn(tokens);
116                 SearchResult searchResult  = new SearchResult();
117                 searchResult.add(user);
118                 Mockito.when(epLdapService.searchPost(Matchers.any(EPUser.class), Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt())).thenReturn(searchResult);
119                 String actual = searchServiceImpl.searchUsersInPhoneBook("ts1234");
120                 assertNotEquals("[]", actual);
121         }
122         
123         @SuppressWarnings("unchecked")
124         @Test
125         public void searchUserByUserIdTest() throws Exception {
126                 EPUser user = new EPUser();
127                 user.setOrgUserId("ts1234");
128                 SearchResult searchResult  = new SearchResult();
129                 searchResult.add(user);
130                 Mockito.when(epLdapService.searchPost(Matchers.any(EPUser.class), Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt())).thenReturn(searchResult);
131                 EPUser actual = searchServiceImpl.searchUserByUserId("ts1234");
132                 assertEquals(user, actual);
133         }
134         
135         @Test
136         public void searchUserByUserIdExceptionTest() throws Exception {
137                 Mockito.doThrow(new NullPointerException()).when(epLdapService).searchPost(Matchers.any(EPUser.class), Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt());
138                 EPUser actual = searchServiceImpl.searchUserByUserId("ts1234");
139                 assertNull(actual);
140         }
141 }