Search in sources :

Example 76 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.

the class SchedulerServiceTest method testGetJobInfo.

@Test
public void testGetJobInfo() throws Exception {
    String jobId = "jobId";
    Job mockJob = mock(Job.class);
    doReturn(mockJob).when(schedulerService).getJob(jobId);
    ISecurityHelper mockSecurityHelper = mock(ISecurityHelper.class);
    doReturn(mockSecurityHelper).when(schedulerService).getSecurityHelper();
    IPentahoSession mockPentahoSession = mock(IPentahoSession.class);
    doReturn(mockPentahoSession).when(schedulerService).getSession();
    String sessionName = "sessionName";
    doReturn(sessionName).when(mockPentahoSession).getName();
    doReturn(sessionName).when(mockJob).getUserName();
    Map<String, Serializable> mockJobParams = mock(Map.class);
    doReturn(mockJobParams).when(mockJob).getJobParams();
    Set<String> jobParamsKeyset = new HashSet<>();
    doReturn(jobParamsKeyset).when(mockJobParams).keySet();
    String jobParamKey = "key";
    jobParamsKeyset.add(jobParamKey);
    String value = "value";
    String[] testArray = new String[] { value };
    doReturn(testArray).when(mockJobParams).get(jobParamKey);
    // Test 1
    doReturn(true).when(schedulerService).canAdminister(mockPentahoSession);
    Job testJob = schedulerService.getJobInfo(jobId);
    assertEquals(mockJob, testJob);
    // Test 2
    doReturn(false).when(schedulerService).canAdminister(mockPentahoSession);
    testJob = schedulerService.getJobInfo(jobId);
    assertEquals(mockJob, testJob);
    verify(mockJobParams, times(2)).put(eq(jobParamKey), any(Serializable.class));
    verify(schedulerService, times(2)).getJob(jobId);
    verify(schedulerService, times(2)).getSession();
    verify(mockPentahoSession, times(2)).getName();
    verify(mockJob, times(2)).getUserName();
    verify(mockJob, times(6)).getJobParams();
    verify(mockJobParams, times(2)).keySet();
    verify(mockJobParams, times(2)).get(jobParamKey);
    verify(schedulerService, times(2)).canAdminister(null);
}
Also used : Serializable(java.io.Serializable) ISecurityHelper(org.pentaho.platform.api.engine.ISecurityHelper) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Matchers.anyString(org.mockito.Matchers.anyString) Job(org.pentaho.platform.api.scheduler2.Job) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 77 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.

the class UserRoleDaoServiceTest method testAssignRoleToUserNotFoundException.

@Test(expected = NotFoundException.class)
public void testAssignRoleToUserNotFoundException() throws UserRoleListService.UnauthorizedException {
    String userName = "testUser";
    String roleNames = "Power User\tBusiness User\t";
    setupMockSessionUser(SESSION_USER_NAME, true);
    // Create session that will generate tenant
    IPentahoSession session = mock(IPentahoSession.class);
    when(session.getAttribute(IPentahoSession.TENANT_ID_KEY)).thenReturn("testTenantPath");
    PentahoSessionHolder.setSession(session);
    IUserRoleDao roleDao = mock(IUserRoleDao.class);
    when(roleDao.getUserRoles(any(ITenant.class), anyString())).thenThrow(new NotFoundException("expectedTestException"));
    PentahoSystem.registerObject(roleDao);
    userRoleService.assignRolesToUser(userName, roleNames);
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) Matchers.anyString(org.mockito.Matchers.anyString) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Test(org.junit.Test)

Example 78 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.

the class UserRoleDaoServiceTest method testAssignRoleToUserUncategorizedUserRoleDaoException.

@Test(expected = UncategorizedUserRoleDaoException.class)
public void testAssignRoleToUserUncategorizedUserRoleDaoException() throws UserRoleListService.UnauthorizedException {
    String userName = "testUser";
    String roleNames = "Power User\tBusiness User\t";
    setupMockSessionUser(SESSION_USER_NAME, true);
    // Create session that will generate tenant
    IPentahoSession session = mock(IPentahoSession.class);
    when(session.getAttribute(IPentahoSession.TENANT_ID_KEY)).thenReturn("testTenantPath");
    PentahoSessionHolder.setSession(session);
    IUserRoleDao roleDao = mock(IUserRoleDao.class);
    when(roleDao.getUserRoles(any(ITenant.class), anyString())).thenThrow(new UncategorizedUserRoleDaoException("expectedTestException"));
    PentahoSystem.registerObject(roleDao);
    userRoleService.assignRolesToUser(userName, roleNames);
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) UncategorizedUserRoleDaoException(org.pentaho.platform.api.engine.security.userroledao.UncategorizedUserRoleDaoException) Matchers.anyString(org.mockito.Matchers.anyString) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Test(org.junit.Test)

Example 79 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.

the class UserRoleDaoServiceTest method testRemoveRoleFromUser.

@Test
public void testRemoveRoleFromUser() {
    String userName = "testUser";
    String roleNames = "Power User\tBusiness User\t";
    setupMockSessionUser(SESSION_USER_NAME, true);
    // Create session that will generate tenant
    IPentahoSession session = mock(IPentahoSession.class);
    when(session.getAttribute(IPentahoSession.TENANT_ID_KEY)).thenReturn("testTenantPath");
    PentahoSessionHolder.setSession(session);
    IPentahoRole ceoRole = mock(IPentahoRole.class);
    when(ceoRole.getName()).thenReturn("ceo");
    IPentahoRole ctoRole = mock(IPentahoRole.class);
    when(ctoRole.getName()).thenReturn("cto");
    IPentahoRole powerUserRole = mock(IPentahoRole.class);
    when(powerUserRole.getName()).thenReturn("Power User");
    IPentahoRole businessUserRole = mock(IPentahoRole.class);
    when(businessUserRole.getName()).thenReturn("Business User");
    List<IPentahoRole> roleList = new ArrayList<>();
    roleList.add(ceoRole);
    roleList.add(ctoRole);
    roleList.add(powerUserRole);
    roleList.add(businessUserRole);
    IUserRoleDao roleDao = mock(IUserRoleDao.class);
    when(roleDao.getUserRoles(any(ITenant.class), anyString())).thenReturn(roleList);
    PentahoSystem.registerObject(roleDao);
    userRoleService.removeRolesFromUser(userName, roleNames);
    verify(roleDao).setUserRoles(any(ITenant.class), anyString(), argThat(new UnorderedArrayMatcher(new String[] { "ceo", "cto" })));
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Test(org.junit.Test)

Example 80 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.

the class UserRoleDaoServiceTest method registerMockSession.

private IPentahoSession registerMockSession(String username) {
    final IPentahoSession pentahoSession = mock(IPentahoSession.class);
    when(pentahoSession.getName()).thenReturn(username);
    PentahoSessionHolder.setSession(pentahoSession);
    return pentahoSession;
}
Also used : IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession)

Aggregations

IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)231 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)76 Test (org.junit.Test)70 Matchers.anyString (org.mockito.Matchers.anyString)40 ArrayList (java.util.ArrayList)32 ITenant (org.pentaho.platform.api.mt.ITenant)22 IOException (java.io.IOException)20 StandaloneObjectFactory (org.pentaho.platform.engine.core.system.objfac.StandaloneObjectFactory)18 File (java.io.File)17 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)16 Before (org.junit.Before)14 OutputStream (java.io.OutputStream)13 HashMap (java.util.HashMap)13 InputStream (java.io.InputStream)12 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)12 ModelInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo)12 Domain (org.pentaho.metadata.model.Domain)11 ObjectFactoryException (org.pentaho.platform.api.engine.ObjectFactoryException)11 List (java.util.List)10 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)10