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.assertEquals;
 
  41 import static org.junit.Assert.assertNull;
 
  43 import java.util.ArrayList;
 
  44 import java.util.HashMap;
 
  45 import java.util.List;
 
  48 import javax.servlet.http.HttpServletResponse;
 
  50 import org.hibernate.Session;
 
  51 import org.hibernate.SessionFactory;
 
  52 import org.hibernate.Transaction;
 
  53 import org.hibernate.criterion.Criterion;
 
  54 import org.hibernate.criterion.Restrictions;
 
  55 import org.junit.After;
 
  56 import org.junit.Before;
 
  57 import org.junit.Test;
 
  58 import org.junit.runner.RunWith;
 
  59 import org.mockito.InjectMocks;
 
  60 import org.mockito.Matchers;
 
  61 import org.mockito.Mock;
 
  62 import org.mockito.Mockito;
 
  63 import org.mockito.MockitoAnnotations;
 
  64 import org.onap.portalapp.portal.core.MockEPUser;
 
  65 import org.onap.portalapp.portal.domain.EPApp;
 
  66 import org.onap.portalapp.portal.domain.EPUser;
 
  67 import org.onap.portalapp.portal.transport.FieldsValidator;
 
  68 import org.onap.portalapp.portal.transport.PortalAdmin;
 
  69 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
 
  70 import org.onap.portalapp.portal.utils.EcompPortalUtils;
 
  71 import org.onap.portalapp.portal.utils.PortalConstants;
 
  72 import org.onap.portalsdk.core.service.DataAccessService;
 
  73 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
 
  74 import org.onap.portalsdk.core.util.SystemProperties;
 
  75 import org.powermock.api.mockito.PowerMockito;
 
  76 import org.powermock.core.classloader.annotations.PrepareForTest;
 
  77 import org.powermock.modules.junit4.PowerMockRunner;
 
  78 import org.springframework.http.HttpEntity;
 
  79 import org.springframework.http.HttpMethod;
 
  80 import org.springframework.http.HttpStatus;
 
  81 import org.springframework.http.ResponseEntity;
 
  82 import org.springframework.web.client.RestTemplate;
 
  84 @RunWith(PowerMockRunner.class)
 
  85 @PrepareForTest({ EcompPortalUtils.class, Criterion.class, Restrictions.class, PortalConstants.class,
 
  86                 SystemProperties.class, EPCommonSystemProperties.class })
 
  87 public class PortalAdminServiceImplTest {
 
  90         DataAccessService dataAccessService = new DataAccessServiceImpl();
 
  93         EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl(); 
 
  96         SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
 
  99         SessionFactory sessionFactory;
 
 105         Transaction transaction;
 
 108         RestTemplate template = new RestTemplate();
 
 111         public void setup() {
 
 112                 MockitoAnnotations.initMocks(this);
 
 113                 Mockito.when(sessionFactory.openSession()).thenReturn(session);
 
 114                 Mockito.when(session.beginTransaction()).thenReturn(transaction);
 
 118         public void after() {
 
 123         PortalAdminServiceImpl portalAdminServiceImpl = new PortalAdminServiceImpl();
 
 125         public EPApp mockApp() {
 
 126                 EPApp app = new EPApp();
 
 128                 app.setImageUrl("test");
 
 129                 app.setNameSpace("com.test.app");
 
 130                 app.setCentralAuth(true);
 
 131                 app.setDescription("test");
 
 132                 app.setNotes("test");
 
 135                 app.setAppRestEndpoint("test");
 
 136                 app.setAlternateUrl("test");
 
 138                 app.setMlAppName("test");
 
 139                 app.setMlAppAdminId("test");
 
 140                 app.setUsername("test");
 
 141                 app.setAppPassword("test");
 
 143                 app.setEnabled(true);
 
 144                 app.setUebKey("test");
 
 145                 app.setUebSecret("test");
 
 146                 app.setUebTopicName("test");
 
 151         MockEPUser mockUser = new MockEPUser();
 
 153         @SuppressWarnings("unchecked")
 
 155         public void getPortalAdminsTest() {
 
 156                 PowerMockito.mockStatic(SystemProperties.class);
 
 157                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
 
 158                 List<PortalAdmin> portalAdmins = new ArrayList<>();
 
 159                 PortalAdmin portalAdmin = new PortalAdmin();
 
 160                 portalAdmin.setFirstName("guest");
 
 161                 portalAdmin.setLastName("test");
 
 162                 portalAdmin.setLoginId("test");
 
 163                 portalAdmin.setUserId(1l);
 
 164                 portalAdmins.add(portalAdmin);
 
 165                 Map<String, String> params = new HashMap<>();
 
 166                 params.put("adminRoleId", "1");
 
 167                 Mockito.when((List<PortalAdmin>) dataAccessService.executeNamedQuery("getPortalAdmins", params, null))
 
 168                                 .thenReturn(portalAdmins);
 
 169                 List<PortalAdmin> actual = portalAdminServiceImpl.getPortalAdmins();
 
 170                 assertEquals(1, actual.size());
 
 174         public void getPortalAdminsExceptionTest() {
 
 175                 PowerMockito.mockStatic(SystemProperties.class);
 
 176                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
 
 177                 List<PortalAdmin> portalAdmins = new ArrayList<>();
 
 178                 PortalAdmin portalAdmin = new PortalAdmin();
 
 179                 portalAdmin.setFirstName("guest");
 
 180                 portalAdmin.setLastName("test");
 
 181                 portalAdmin.setLoginId("test");
 
 182                 portalAdmin.setUserId(1l);
 
 183                 portalAdmins.add(portalAdmin);
 
 184                 Map<String, String> params = new HashMap<>();
 
 185                 params.put("adminRoleId", "1");
 
 186                 Mockito.doThrow(new NullPointerException()).when(dataAccessService).executeNamedQuery("getPortalAdmins", params,
 
 188                 List<PortalAdmin> actual = portalAdminServiceImpl.getPortalAdmins();
 
 192         @SuppressWarnings("unchecked")
 
 194         public void createPortalAdminNewUserTest() {
 
 195                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
 
 196                 PowerMockito.mockStatic(EcompPortalUtils.class);
 
 197                 PowerMockito.mockStatic(PortalConstants.class);
 
 198                 PowerMockito.mockStatic(SystemProperties.class);
 
 199                 PowerMockito.mockStatic(Restrictions.class);
 
 200                 PowerMockito.mockStatic(Criterion.class);
 
 201                 EPUser user = mockUser.mockEPUser();
 
 202                 EPApp app = mockApp();
 
 203                 List<EPUser> users = new ArrayList<>();
 
 204                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
 
 205                 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId", user.getOrgUserId());
 
 206                 restrictionsList.add(orgUserIdCriterion);
 
 207                 Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null))
 
 209                 List<PortalAdmin> portalAdmins = new ArrayList<>();
 
 210                 Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any() , Matchers.anyMap()))
 
 211                                 .thenReturn(portalAdmins);
 
 212                 Mockito.when(searchServiceImpl.searchUserByUserId(user.getOrgUserId())).thenReturn(user);
 
 213                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
 
 215                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
 
 217                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
 
 218                                 .thenReturn("@test.com");
 
 219                 Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(app);
 
 220                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
 
 221                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
 
 222                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
 
 223                 FieldsValidator actual = portalAdminServiceImpl.createPortalAdmin(user.getOrgUserId());
 
 224                 FieldsValidator expected = new FieldsValidator();
 
 225                 expected.setHttpStatusCode(Long.valueOf(HttpServletResponse.SC_OK));
 
 226                 assertEquals(expected, actual);
 
 229         @SuppressWarnings("unchecked")
 
 231         public void createPortalAdminExistingUserTest() {
 
 232                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
 
 233                 PowerMockito.mockStatic(EcompPortalUtils.class);
 
 234                 PowerMockito.mockStatic(PortalConstants.class);
 
 235                 PowerMockito.mockStatic(SystemProperties.class);
 
 236                 PowerMockito.mockStatic(Restrictions.class);
 
 237                 PowerMockito.mockStatic(Criterion.class);
 
 238                 EPUser user = mockUser.mockEPUser();
 
 239                 EPApp app = mockApp();
 
 240                 List<EPUser> users = new ArrayList<>();
 
 242                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
 
 243                 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId", user.getOrgUserId());
 
 244                 restrictionsList.add(orgUserIdCriterion);
 
 245                 Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null))
 
 247                 List<PortalAdmin> portalAdmins = new ArrayList<>();
 
 248                 Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any() , Matchers.anyMap()))
 
 249                                 .thenReturn(portalAdmins);
 
 250                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
 
 252                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
 
 254                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
 
 255                                 .thenReturn("@test.com");
 
 256                 Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(app);
 
 257                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
 
 258                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
 
 259                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
 
 260                 FieldsValidator actual = portalAdminServiceImpl.createPortalAdmin(user.getOrgUserId());
 
 261                 FieldsValidator expected = new FieldsValidator();
 
 262                 expected.setHttpStatusCode(Long.valueOf(HttpServletResponse.SC_OK));
 
 263                 assertEquals(expected, actual);
 
 266         @SuppressWarnings("unchecked")
 
 268         public void deletePortalAdminTest() {
 
 269                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
 
 270                 PowerMockito.mockStatic(EcompPortalUtils.class);
 
 271                 PowerMockito.mockStatic(PortalConstants.class);
 
 272                 PowerMockito.mockStatic(SystemProperties.class);
 
 273                 PowerMockito.mockStatic(Restrictions.class);
 
 274                 PowerMockito.mockStatic(Criterion.class);
 
 275                 EPUser user = mockUser.mockEPUser();
 
 276                 EPApp app = mockApp();
 
 277                 List<EPUser> users = new ArrayList<>();
 
 279                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
 
 280                 Criterion orgUserIdCriterion = Restrictions.eq("id", user.getId());
 
 281                 restrictionsList.add(orgUserIdCriterion);
 
 282                 Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null))
 
 284                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
 
 286                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
 
 288                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
 
 289                                 .thenReturn("@test.com");
 
 290                 Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(app);
 
 291                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
 
 292                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
 
 293                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
 
 294                 FieldsValidator actual = portalAdminServiceImpl.deletePortalAdmin(user.getId());
 
 295                 FieldsValidator expected = new FieldsValidator();
 
 296                 expected.setHttpStatusCode(Long.valueOf(HttpServletResponse.SC_OK));
 
 297                 assertEquals(expected, actual);