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.HashMap;
 
  44 import java.util.HashSet;
 
  45 import java.util.List;
 
  46 import java.util.SortedSet;
 
  47 import java.util.TreeSet;
 
  49 import javax.servlet.http.HttpServletRequest;
 
  50 import javax.servlet.http.HttpSession;
 
  52 import org.junit.Before;
 
  53 import org.junit.Test;
 
  54 import org.junit.runner.RunWith;
 
  55 import org.mockito.InjectMocks;
 
  56 import org.mockito.Mock;
 
  57 import org.mockito.Mockito;
 
  58 import org.mockito.MockitoAnnotations;
 
  59 import org.onap.portalapp.portal.core.MockEPUser;
 
  60 import org.onap.portalapp.portal.domain.EPRole;
 
  61 import org.onap.portalapp.portal.domain.EPUser;
 
  62 import org.onap.portalapp.portal.framework.MockitoTestSuite;
 
  63 import org.onap.portalapp.portal.service.EPRoleFunctionServiceImpl;
 
  64 import org.onap.portalapp.util.EPUserUtils;
 
  65 import org.onap.portalsdk.core.domain.RoleFunction;
 
  66 import org.onap.portalsdk.core.service.DataAccessService;
 
  67 import org.onap.portalsdk.core.util.SystemProperties;
 
  68 import org.powermock.api.mockito.PowerMockito;
 
  69 import org.powermock.core.classloader.annotations.PrepareForTest;
 
  70 import org.powermock.modules.junit4.PowerMockRunner;
 
  72 @RunWith(PowerMockRunner.class)
 
  73 @PrepareForTest({ SystemProperties.class, EPUserUtils.class })
 
  74 public class EPRoleFunctionServiceImplTest {
 
  77         DataAccessService dataAccessService;
 
  80         EPRoleFunctionServiceImpl ePRoleFunctionServiceImpl = new EPRoleFunctionServiceImpl();
 
  84                 MockitoAnnotations.initMocks(this);
 
  87         NullPointerException nullPointerException = new NullPointerException();
 
  88         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
 
  89         MockEPUser mockUser = new MockEPUser();
 
  91         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
 
  94         public void getRoleFunctionsTest() {
 
  95                 List<RoleFunction> functions = new ArrayList<>();
 
  96                 Mockito.when(dataAccessService.getList(RoleFunction.class, null)).thenReturn(functions);
 
  97                 List<RoleFunction> expectedFunctions = ePRoleFunctionServiceImpl.getRoleFunctions();
 
  98                 assertEquals(expectedFunctions, functions);
 
 102         public void getRoleFunctionsRequestTest() {
 
 103                 EPUser user = mockUser.mockEPUser();
 
 104                 HashSet roleFunctions = new HashSet<>();
 
 105                 PowerMockito.mockStatic(SystemProperties.class);
 
 106                 HttpSession session = mockedRequest.getSession();
 
 107                 Mockito.when(session.getAttribute(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME)))
 
 108                                 .thenReturn(roleFunctions);
 
 109                 HashSet expectedRoleFunctions = (HashSet) ePRoleFunctionServiceImpl.getRoleFunctions(mockedRequest, user);
 
 110                 assertEquals(expectedRoleFunctions, roleFunctions);
 
 113         @SuppressWarnings("unchecked")
 
 115         public void getRoleFunctionsRequestIfNullTest() {
 
 116                 EPUser user = mockUser.mockEPUser();
 
 117                 HashSet roleFunctions = null;
 
 118                 PowerMockito.mockStatic(SystemProperties.class);
 
 119                 PowerMockito.mockStatic(EPUserUtils.class);
 
 120                 HttpSession session = mockedRequest.getSession();
 
 121                 Mockito.when(session.getAttribute(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME)))
 
 122                                 .thenReturn(roleFunctions);
 
 123                 HashMap roles = new HashMap<>();
 
 124                 EPRole role = new EPRole();
 
 125                 SortedSet<RoleFunction> roleFunctionSet = new TreeSet<RoleFunction>();
 
 126                 RoleFunction rolefun = new RoleFunction();
 
 127                 roleFunctionSet.add(rolefun);
 
 128                 role.setRoleFunctions(roleFunctionSet);
 
 129                 roles.put((long) 1, role);
 
 130                 Mockito.when(EPUserUtils.getRoles(mockedRequest)).thenReturn(roles);
 
 131                 HashSet expectedRoleFunctions = (HashSet) ePRoleFunctionServiceImpl.getRoleFunctions(mockedRequest, user);
 
 132                 assertTrue(expectedRoleFunctions.size() == 1);