Search in sources :

Example 6 with UserManageService

use of org.mx.comps.rbac.service.UserManageService in project main by JohnPeng739.

the class TestUser method testUserCrud.

@Test
public void testUserCrud() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    UserManageService userService = context.getBean(UserManageService.class);
    assertNotNull(service);
    try {
        assertEquals(service.count(User.class), 0);
        // insert
        testInsertUser(service, userService);
        // edit
        testEditUser(service, userService);
        // delete
        testDeleteUser(service);
    } catch (Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
Also used : User(org.mx.comps.rbac.dal.entity.User) GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) UserManageService(org.mx.comps.rbac.service.UserManageService) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 7 with UserManageService

use of org.mx.comps.rbac.service.UserManageService in project main by JohnPeng739.

the class TestUser method testInsertUser.

public static void testInsertUser(GeneralDictAccessor service, UserManageService userService) throws ParseException {
    long birthday = new SimpleDateFormat("yyyy-MM-dd").parse("1973-09-18").getTime();
    UserManageService.UserInfo userInfo = UserManageService.UserInfo.valueOf("喜", "明", "彭", User.Sex.MALE, "", birthday, "", "manager", true, "This is John.Peng.");
    User john = userService.saveUser(userInfo);
    assertEquals(1, service.count(User.class));
    johnId = john.getId();
    assertNotNull(john);
    assertNotNull(john.getId());
    assertEquals(john.getFullName(), "彭 明喜");
    assertEquals(john.getLastName(), "彭");
    assertEquals(john.getStation(), "manager");
    assertEquals(john.getSex(), User.Sex.MALE);
    assertEquals(john.getDesc(), "This is John.Peng.");
    assertEquals(birthday, john.getBirthday().getTime());
    assertTrue(john.getCreatedTime() > 0);
    assertTrue(john.getUpdatedTime() > 0);
    assertEquals(john.getOperator(), "admin");
    assertTrue(john.isValid());
    assertEquals(service.count(User.class), 1);
    john = service.getById(john.getId(), User.class);
    assertNotNull(john);
    assertNotNull(john.getId());
    assertEquals(john.getFullName(), "彭 明喜");
    assertEquals(john.getLastName(), "彭");
    assertEquals(john.getStation(), "manager");
    assertEquals(john.getSex(), User.Sex.MALE);
    assertEquals(john.getDesc(), "This is John.Peng.");
    assertEquals(birthday, john.getBirthday().getTime());
    assertTrue(john.getCreatedTime() > 0);
    assertTrue(john.getUpdatedTime() > 0);
    assertEquals(john.getOperator(), "admin");
    assertTrue(john.isValid());
    assertEquals(service.count(User.class), 1);
    assertEquals(service.count(User.class, true), 1);
    assertEquals(service.count(User.class, false), 1);
    userInfo = UserManageService.UserInfo.valueOf("joy", "", "peng", User.Sex.FEMALE);
    User joy = userService.saveUser(userInfo);
    joyId = joy.getId();
    assertEquals(2, service.count(User.class));
    assertNotNull(joy);
    assertNotNull(joy.getId());
    assertEquals(joy.getFullName(), "peng joy");
    joy = service.getById(joy.getId(), User.class);
    assertNotNull(joy);
    assertNotNull(joy.getId());
    assertEquals(joy.getFullName(), "peng joy");
}
Also used : User(org.mx.comps.rbac.dal.entity.User) UserManageService(org.mx.comps.rbac.service.UserManageService) SimpleDateFormat(java.text.SimpleDateFormat)

Example 8 with UserManageService

use of org.mx.comps.rbac.service.UserManageService in project main by JohnPeng739.

the class TestUser method testUserDepartment.

@Test
public void testUserDepartment() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    UserManageService userService = context.getBean(UserManageService.class);
    assertNotNull(service);
    DepartmentManageService departManageService = context.getBean(DepartmentManageService.class);
    assertNotNull(departManageService);
    try {
        TestDepartment.testInsertDepartment(service, departManageService);
        TestDepartment.testEditDepartment(service, departManageService);
        testInsertUser(service, userService);
        testEditUser(service, userService);
        assertEquals(3, service.count(Department.class));
        assertEquals(3, service.count(User.class));
        User joy = service.getById(joyId, User.class);
        assertNotNull(joy);
        assertNull(joy.getDepartment());
        User josh = service.getById(joshId, User.class);
        assertNotNull(josh);
        assertNull(josh.getDepartment());
        Department depart1 = service.getById(TestDepartment.depart1Id, Department.class);
        assertNotNull(depart1);
        UserManageService.UserInfo userInfo = UserManageService.UserInfo.valueOf(joy.getFirstName(), joy.getMiddleName(), joy.getLastName(), joy.getSex(), joy.getId(), 0, depart1.getId(), joy.getStation(), joy.isValid(), joy.getDesc());
        userService.saveUser(userInfo);
        joy = service.getById(joyId, User.class);
        assertNotNull(joy);
        assertNotNull(joy.getDepartment());
        assertEquals(depart1, joy.getDepartment());
        depart1 = service.getById(TestDepartment.depart1Id, Department.class);
        assertNotNull(depart1);
        assertEquals(1, depart1.getEmployees().size());
        assertEquals(new HashSet<>(Arrays.asList(joy)), depart1.getEmployees());
        userInfo = UserManageService.UserInfo.valueOf(josh.getFirstName(), josh.getMiddleName(), josh.getLastName(), josh.getSex(), josh.getId(), 0, depart1.getId(), josh.getStation(), josh.isValid(), josh.getDesc());
        userService.saveUser(userInfo);
        josh = service.getById(joshId, User.class);
        assertNotNull(josh);
        assertNotNull(josh.getDepartment());
        assertEquals(depart1, josh.getDepartment());
        depart1 = service.getById(TestDepartment.depart1Id, Department.class);
        assertNotNull(depart1);
        assertEquals(2, depart1.getEmployees().size());
        assertEquals(new HashSet<>(Arrays.asList(joy, josh)), depart1.getEmployees());
        userInfo = UserManageService.UserInfo.valueOf(joy.getFirstName(), joy.getMiddleName(), joy.getLastName(), joy.getSex(), joy.getId(), 0, null, joy.getStation(), joy.isValid(), joy.getDesc());
        userService.saveUser(userInfo);
        joy = service.getById(joyId, User.class);
        assertNotNull(joy);
        assertNull(joy.getDepartment());
        depart1 = service.getById(TestDepartment.depart1Id, Department.class);
        assertNotNull(depart1);
        assertEquals(1, depart1.getEmployees().size());
        assertEquals(new HashSet<>(Arrays.asList(josh)), depart1.getEmployees());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
Also used : Department(org.mx.comps.rbac.dal.entity.Department) User(org.mx.comps.rbac.dal.entity.User) DepartmentManageService(org.mx.comps.rbac.service.DepartmentManageService) GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) UserManageService(org.mx.comps.rbac.service.UserManageService) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 9 with UserManageService

use of org.mx.comps.rbac.service.UserManageService in project main by JohnPeng739.

the class TestAccount method testAccountRoles.

@Test
public void testAccountRoles() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    AccountManageService accountService = context.getBean(AccountManageService.class);
    assertNotNull(service);
    UserManageService userManageService = context.getBean(UserManageService.class);
    assertNotNull(userManageService);
    RoleManageService roleManageService = context.getBean(RoleManageService.class);
    assertNotNull(roleManageService);
    try {
        TestUser.testInsertUser(service, userManageService);
        TestUser.testEditUser(service, userManageService);
        assertEquals(3, service.count(User.class));
        TestRole.testInsertRole(service, roleManageService);
        TestRole.testEditRole(service, roleManageService);
        testInsertAccount(service, accountService);
        testEditAccount(service, accountService);
        assertEquals(3, service.count(Role.class));
        assertEquals(3, service.count(Account.class));
        Account account1 = service.getById(account1Id, Account.class);
        assertNotNull(account1);
        assertTrue(account1.getRoles().isEmpty());
        Role role1 = service.getById(TestRole.role1Id, Role.class);
        Role role2 = service.getById(TestRole.role2Id, Role.class);
        Role role3 = service.getById(TestRole.role3Id, Role.class);
        assertNotNull(role1);
        assertNotNull(role2);
        assertNotNull(role3);
        AccountManageService.AccountInfo accountInfo = AccountManageService.AccountInfo.valueOf(account1.getCode(), "", account1.getDesc(), account1.getId(), TestUser.johnId, Arrays.asList(role1.getId(), role2.getId(), role3.getId()), account1.isValid());
        accountService.saveAccount(accountInfo);
        assertEquals(3, service.count(Account.class));
        account1 = service.getById(account1Id, Account.class);
        assertNotNull(account1);
        assertEquals(3, account1.getRoles().size());
        assertEquals(new HashSet<>(Arrays.asList(role1, role2, role3)), account1.getRoles());
        accountInfo = AccountManageService.AccountInfo.valueOf(account1.getCode(), "", account1.getDesc(), account1.getId(), TestUser.johnId, Arrays.asList(role1.getId(), role3.getId()), account1.isValid());
        accountService.saveAccount(accountInfo);
        assertEquals(3, service.count(Account.class));
        account1 = service.getById(account1Id, Account.class);
        assertNotNull(account1);
        assertEquals(2, account1.getRoles().size());
        assertEquals(new HashSet<>(Arrays.asList(role1, role3)), account1.getRoles());
        accountInfo = AccountManageService.AccountInfo.valueOf(account1.getCode(), "", account1.getDesc(), account1.getId(), TestUser.johnId, Arrays.asList(), account1.isValid());
        accountService.saveAccount(accountInfo);
        account1 = service.getById(account1Id, Account.class);
        assertNotNull(account1);
        assertTrue(account1.getRoles().isEmpty());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Account(org.mx.comps.rbac.dal.entity.Account) User(org.mx.comps.rbac.dal.entity.User) AccountManageService(org.mx.comps.rbac.service.AccountManageService) GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) UserManageService(org.mx.comps.rbac.service.UserManageService) RoleManageService(org.mx.comps.rbac.service.RoleManageService) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Test(org.junit.Test)

Example 10 with UserManageService

use of org.mx.comps.rbac.service.UserManageService in project main by JohnPeng739.

the class TestAccredit method testAccredit.

@Test
public void testAccredit() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    AccreditManageService accreditService = context.getBean(AccreditManageService.class);
    assertNotNull(service);
    UserManageService userManageService = context.getBean(UserManageService.class);
    assertNotNull(userManageService);
    AccountManageService accountManageService = context.getBean(AccountManageService.class);
    assertNotNull(accountManageService);
    RoleManageService roleManageService = context.getBean(RoleManageService.class);
    assertNotNull(roleManageService);
    try {
        TestUser.testInsertUser(service, userManageService);
        TestUser.testEditUser(service, userManageService);
        assertEquals(3, service.count(User.class));
        TestAccount.testInsertAccount(service, accountManageService);
        TestAccount.testEditAccount(service, accountManageService);
        TestRole.testInsertRole(service, roleManageService);
        TestRole.testEditRole(service, roleManageService);
        assertEquals(3, service.count(Account.class));
        assertEquals(3, service.count(Role.class));
        assertEquals(0, service.count(Accredit.class));
        Account account1 = service.getById(TestAccount.account1Id, Account.class);
        assertNotNull(account1);
        Account account2 = service.getById(TestAccount.account2Id, Account.class);
        assertNotNull(account2);
        Role role1 = service.getById(TestRole.role1Id, Role.class);
        assertNotNull(role1);
        Role role2 = service.getById(TestRole.role2Id, Role.class);
        assertNotNull(role2);
        Role role3 = service.getById(TestRole.role3Id, Role.class);
        assertNotNull(role3);
        long startTime = new Date().getTime();
        AccreditManageService.AccreditInfo accreditInfo;
        // 测试没有设置源
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf("", TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceSystemErrorException ex) {
            assertEquals(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM.getErrorCode(), ex.getErrorCode());
        }
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, "", Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceSystemErrorException ex) {
            assertEquals(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM.getErrorCode(), ex.getErrorCode());
        }
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, null, startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceSystemErrorException ex) {
            assertEquals(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM.getErrorCode(), ex.getErrorCode());
        }
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceSystemErrorException ex) {
            assertEquals(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM.getErrorCode(), ex.getErrorCode());
        }
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf("abcde", TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND.getErrorCode(), ex.getErrorCode());
        }
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, "abcde", Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND.getErrorCode(), ex.getErrorCode());
        }
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, "abcdef", TestRole.role3Id), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.ROLE_NOT_FOUND.getErrorCode(), ex.getErrorCode());
        }
        // 测试正常授权
        long endTime = new Date().getTime() + 500;
        accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, endTime, "desc");
        Accredit accredit = accreditService.accredit(accreditInfo);
        assertEquals(1, service.count(Accredit.class));
        assertNotNull(accredit);
        assertEquals(account1, accredit.getSrc());
        assertEquals(account2, accredit.getTar());
        assertEquals(new HashSet<>(Arrays.asList(role1, role2, role3)), accredit.getRoles());
        assertEquals(startTime, accredit.getStartTime().getTime());
        assertEquals(endTime, accredit.getEndTime().getTime());
        assertTrue(accredit.isValid());
        assertEquals("desc", accredit.getDesc());
        accredit = service.getById(accredit.getId(), Accredit.class);
        assertNotNull(accredit);
        assertEquals(account1, accredit.getSrc());
        assertEquals(account2, accredit.getTar());
        assertEquals(new HashSet<>(Arrays.asList(role1, role2, role3)), accredit.getRoles());
        assertEquals(startTime, accredit.getStartTime().getTime());
        assertEquals(endTime, accredit.getEndTime().getTime());
        assertTrue(accredit.isValid());
        assertEquals("desc", accredit.getDesc());
        // 测试重复授权
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role3Id), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCREDIT_SAME_FOUND.getErrorCode(), ex.getErrorCode());
        }
        // 测试自动时间到达后关闭
        Thread.sleep(600);
        accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role3Id), startTime, -1, "desc");
        accredit = accreditService.accredit(accreditInfo);
        assertEquals(2, service.count(Accredit.class));
        assertEquals(2, service.count(Accredit.class, false));
        accredit = service.getById(accredit.getId(), Accredit.class);
        assertNotNull(accredit);
        assertEquals(account1, accredit.getSrc());
        assertEquals(account2, accredit.getTar());
        assertEquals(new HashSet<>(Arrays.asList(role1, role3)), accredit.getRoles());
        assertEquals(startTime, accredit.getStartTime().getTime());
        assertNull(accredit.getEndTime());
        assertTrue(accredit.isValid());
        assertEquals("desc", accredit.getDesc());
        // 测试关闭
        accreditService.closeAccredit(accredit.getId());
        assertEquals(1, service.count(Accredit.class));
        assertEquals(2, service.count(Accredit.class, false));
        // 再次授权
        accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role3Id), startTime, -1, "desc");
        accredit = accreditService.accredit(accreditInfo);
        assertEquals(2, service.count(Accredit.class));
        assertEquals(3, service.count(Accredit.class, false));
        assertNotNull(accredit);
        accredit = service.getById(accredit.getId(), Accredit.class);
        assertNotNull(accredit);
        assertEquals(account1, accredit.getSrc());
        assertEquals(account2, accredit.getTar());
        assertEquals(new HashSet<>(Arrays.asList(role1, role3)), accredit.getRoles());
        assertEquals(startTime, accredit.getStartTime().getTime());
        assertNull(accredit.getEndTime());
        assertTrue(accredit.isValid());
        assertEquals("desc", accredit.getDesc());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
Also used : Account(org.mx.comps.rbac.dal.entity.Account) Accredit(org.mx.comps.rbac.dal.entity.Accredit) User(org.mx.comps.rbac.dal.entity.User) AccreditManageService(org.mx.comps.rbac.service.AccreditManageService) AccountManageService(org.mx.comps.rbac.service.AccountManageService) GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) UserManageService(org.mx.comps.rbac.service.UserManageService) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) Date(java.util.Date) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) Role(org.mx.comps.rbac.dal.entity.Role) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) RoleManageService(org.mx.comps.rbac.service.RoleManageService) Test(org.junit.Test)

Aggregations

UserManageService (org.mx.comps.rbac.service.UserManageService)12 User (org.mx.comps.rbac.dal.entity.User)11 Test (org.junit.Test)10 GeneralDictAccessor (org.mx.dal.service.GeneralDictAccessor)10 UserInterfaceRbacErrorException (org.mx.comps.rbac.error.UserInterfaceRbacErrorException)8 Account (org.mx.comps.rbac.dal.entity.Account)7 AccountManageService (org.mx.comps.rbac.service.AccountManageService)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 ParseException (java.text.ParseException)3 Role (org.mx.comps.rbac.dal.entity.Role)3 RoleManageService (org.mx.comps.rbac.service.RoleManageService)3 Department (org.mx.comps.rbac.dal.entity.Department)2 DepartmentManageService (org.mx.comps.rbac.service.DepartmentManageService)2 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Accredit (org.mx.comps.rbac.dal.entity.Accredit)1 LoginHistory (org.mx.comps.rbac.dal.entity.LoginHistory)1 AccreditManageService (org.mx.comps.rbac.service.AccreditManageService)1 UserInterfaceSystemErrorException (org.mx.error.UserInterfaceSystemErrorException)1