Search in sources :

Example 11 with AlreadyExistsException

use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.

the class SolutionImportHandlerTest method testImportRoles_roleAlreadyExists.

@Test
public void testImportRoles_roleAlreadyExists() throws Exception {
    String roleName = "ADMIN";
    List<String> permissions = new ArrayList<String>();
    RoleExport role = new RoleExport();
    role.setRolename(roleName);
    role.setPermission(permissions);
    List<RoleExport> roles = new ArrayList<>();
    roles.add(role);
    Map<String, List<String>> roleToUserMap = new HashMap<>();
    final List<String> adminUsers = new ArrayList<>();
    adminUsers.add("admin");
    adminUsers.add("root");
    roleToUserMap.put(roleName, adminUsers);
    String[] userStrings = adminUsers.toArray(new String[] {});
    Mockito.when(userRoleDao.createRole(Mockito.any(ITenant.class), Mockito.anyString(), Mockito.anyString(), Mockito.any(userStrings.getClass()))).thenThrow(new AlreadyExistsException("already there"));
    importHandler.setOverwriteFile(true);
    importHandler.importRoles(roles, roleToUserMap);
    Mockito.verify(userRoleDao).createRole(Mockito.any(ITenant.class), Mockito.anyString(), Mockito.anyString(), Mockito.any(userStrings.getClass()));
    // even if the roles exists, make sure we set the permissions on it Mockito.anyway... they might have changed
    Mockito.verify(roleAuthorizationPolicyRoleBindingDao).setRoleBindings(Mockito.any(ITenant.class), Mockito.eq(roleName), Mockito.eq(permissions));
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) RoleExport(org.pentaho.platform.plugin.services.importexport.RoleExport) Test(org.junit.Test)

Example 12 with AlreadyExistsException

use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.

the class UserRoleDaoResourceTest method testCreateUserDuplicate.

@Test
public void testCreateUserDuplicate() throws Exception {
    UserRoleDaoService mockService = mock(UserRoleDaoService.class);
    doThrow(new AlreadyExistsException("message")).when(mockService).createUser(any(User.class));
    UserRoleDaoResource resource = new UserRoleDaoResource(roleBindingDao, tenantManager, systemRoles, adminRole, mockService);
    try {
        resource.createUser(new User("user", "duplicate"));
    } catch (WebApplicationException e) {
        assertEquals(Response.Status.PRECONDITION_FAILED.getStatusCode(), e.getResponse().getStatus());
    }
}
Also used : UserRoleDaoService(org.pentaho.platform.web.http.api.resources.services.UserRoleDaoService) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Example 13 with AlreadyExistsException

use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.

the class UserRoleWebServiceBase method testCreateDuplicateRole.

@Test
public void testCreateDuplicateRole() throws UserRoleException {
    UserRoleDaoMock userRoleDao = Mockito.mock(UserRoleDaoMock.class);
    Mockito.doThrow(new AlreadyExistsException("That role already exists.")).when(userRoleDao).createRole(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
    UserRoleWebService userRoleWebService = new UserRoleWebService() {

        @Override
        protected IUserRoleDao getDao() throws UserRoleException {
            return userRoleDao;
        }
    };
    ProxyPentahoRole proxyPentahoRole = new ProxyPentahoRole("testrole");
    try {
        userRoleWebService.createRole(proxyPentahoRole);
        Assert.fail();
    } catch (AlreadyExistsException e) {
        Assert.assertEquals(0, e.getStackTrace().length);
    }
}
Also used : AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) ProxyPentahoRole(org.pentaho.platform.security.userroledao.ws.ProxyPentahoRole) UserRoleWebService(org.pentaho.platform.security.userroledao.ws.UserRoleWebService) IUserRoleWebService(org.pentaho.platform.security.userroledao.ws.IUserRoleWebService) Test(org.junit.Test)

Example 14 with AlreadyExistsException

use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.

the class UserRoleWebServiceBase method testCreateDuplicateUser.

@Test
public void testCreateDuplicateUser() throws UserRoleException {
    UserRoleDaoMock userRoleDao = Mockito.mock(UserRoleDaoMock.class);
    Mockito.doThrow(new AlreadyExistsException("That user name already exists.")).when(userRoleDao).createUser(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
    UserRoleWebService userRoleWebService = new UserRoleWebService() {

        @Override
        protected IUserRoleDao getDao() throws UserRoleException {
            return userRoleDao;
        }
    };
    ProxyPentahoUser proxyPentahoUser = new ProxyPentahoUser();
    proxyPentahoUser.setName("test");
    proxyPentahoUser.setEnabled(true);
    proxyPentahoUser.setPassword("test");
    proxyPentahoUser.setDescription("testing");
    try {
        userRoleWebService.createUser(proxyPentahoUser);
        Assert.fail();
    } catch (AlreadyExistsException e) {
        Assert.assertEquals(0, e.getStackTrace().length);
    }
}
Also used : AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) ProxyPentahoUser(org.pentaho.platform.security.userroledao.ws.ProxyPentahoUser) UserRoleWebService(org.pentaho.platform.security.userroledao.ws.UserRoleWebService) IUserRoleWebService(org.pentaho.platform.security.userroledao.ws.IUserRoleWebService) Test(org.junit.Test)

Aggregations

AlreadyExistsException (org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException)14 Test (org.junit.Test)11 ITenant (org.pentaho.platform.api.mt.ITenant)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 IPentahoRole (org.pentaho.platform.api.engine.security.userroledao.IPentahoRole)4 IPentahoUser (org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)4 HashMap (java.util.HashMap)3 RoleExport (org.pentaho.platform.plugin.services.importexport.RoleExport)3 UserExport (org.pentaho.platform.plugin.services.importexport.UserExport)3 IOException (java.io.IOException)2 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)2 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)2 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)2 IUserRoleDao (org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao)2 Tenant (org.pentaho.platform.core.mt.Tenant)2 IUserRoleWebService (org.pentaho.platform.security.userroledao.ws.IUserRoleWebService)2 UserRoleWebService (org.pentaho.platform.security.userroledao.ws.UserRoleWebService)2 HashSet (java.util.HashSet)1 WebApplicationException (javax.ws.rs.WebApplicationException)1