use of org.mx.dal.service.GeneralDictAccessor in project main by JohnPeng739.
the class TestPrivilege method testPrivilegeRoles.
@Test
public void testPrivilegeRoles() {
GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
assertNotNull(service);
RoleManageService roleManageService = context.getBean(RoleManageService.class);
assertNotNull(roleManageService);
try {
TestRole.testInsertRole(service, roleManageService);
TestRole.testEditRole(service, roleManageService);
testInsertPrivilege(service);
testEditPrivilege(service);
assertEquals(3, service.count(Role.class));
assertEquals(3, service.count(Privilege.class));
Privilege p1 = service.getById(p1Id, Privilege.class);
assertNotNull(p1);
assertTrue(p1.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);
assertTrue(role1.getPrivileges().isEmpty());
assertNotNull(role2);
assertTrue(role2.getPrivileges().isEmpty());
assertNotNull(role3);
assertTrue(role3.getPrivileges().isEmpty());
p1.getRoles().add(role1);
p1.getRoles().add(role2);
p1.getRoles().add(role3);
service.save(p1);
p1 = service.getById(p1Id, Privilege.class);
assertNotNull(p1);
assertEquals(3, p1.getRoles().size());
assertEquals(new HashSet<>(Arrays.asList(role1, role2, role3)), p1.getRoles());
role1 = service.getById(TestRole.role1Id, Role.class);
assertNotNull(role1);
assertEquals(1, role1.getPrivileges().size());
assertEquals(new HashSet<>(Arrays.asList(p1)), role1.getPrivileges());
role2 = service.getById(TestRole.role2Id, Role.class);
assertNotNull(role2);
assertEquals(1, role2.getPrivileges().size());
assertEquals(new HashSet<>(Arrays.asList(p1)), role2.getPrivileges());
role3 = service.getById(TestRole.role3Id, Role.class);
assertNotNull(role3);
assertEquals(1, role3.getPrivileges().size());
assertEquals(new HashSet<>(Arrays.asList(p1)), role3.getPrivileges());
p1.getRoles().remove(role2);
service.save(p1);
p1 = service.getById(p1Id, Privilege.class);
assertNotNull(p1);
assertEquals(2, p1.getRoles().size());
assertEquals(new HashSet<>(Arrays.asList(role1, role3)), p1.getRoles());
role1 = service.getById(TestRole.role1Id, Role.class);
assertNotNull(role1);
assertEquals(1, role1.getPrivileges().size());
assertEquals(new HashSet<>(Arrays.asList(p1)), role1.getPrivileges());
role2 = service.getById(TestRole.role2Id, Role.class);
assertNotNull(role2);
assertEquals(0, role2.getPrivileges().size());
role3 = service.getById(TestRole.role3Id, Role.class);
assertNotNull(role3);
assertEquals(1, role3.getPrivileges().size());
assertEquals(new HashSet<>(Arrays.asList(p1)), role3.getPrivileges());
p1.getRoles().clear();
service.save(p1);
p1 = service.getById(p1Id, Privilege.class);
assertNotNull(p1);
assertEquals(0, p1.getRoles().size());
role1 = service.getById(TestRole.role1Id, Role.class);
assertNotNull(role1);
assertEquals(0, role1.getPrivileges().size());
role2 = service.getById(TestRole.role2Id, Role.class);
assertNotNull(role2);
assertEquals(0, role2.getPrivileges().size());
role3 = service.getById(TestRole.role1Id, Role.class);
assertNotNull(role3);
assertEquals(0, role3.getPrivileges().size());
} catch (Exception ex) {
fail(ex.getMessage());
}
}
use of org.mx.dal.service.GeneralDictAccessor in project main by JohnPeng739.
the class TestDatabase method testUserInterface.
@Test
public void testUserInterface() {
GeneralDictAccessor accessor = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
assertNotNull(accessor);
try {
assertEquals(0, accessor.count(User.class));
User user = EntityFactory.createEntity(User.class);
user.setCode("john");
user.setName("John Peng");
user.setAddress("address");
user.setEmail("email");
user.setPostCode("zip");
user.setDesc("description");
User check = accessor.save(user);
assertNotNull(check);
assertNotNull(check.getId());
assertEquals(user.getCode(), check.getCode());
assertEquals(user.getName(), check.getName());
assertEquals(user.getAddress(), check.getAddress());
assertTrue(user.getCreatedTime() > 0);
assertEquals(1, accessor.count(User.class));
check = accessor.getById(user.getId(), User.class);
assertNotNull(check);
assertEquals(user.getCode(), check.getCode());
assertEquals(user.getName(), check.getName());
check = accessor.getByCode(user.getCode(), User.class);
assertNotNull(check);
assertEquals(user.getId(), check.getId());
assertEquals(user.getCode(), check.getCode());
assertEquals(user.getName(), check.getName());
user = accessor.getByCode("john", User.class);
user.setCode("john-1");
user.setName(user.getName() + "(editable)");
user.setDesc("I'm John Peng.");
check = accessor.save(user);
assertNotNull(check);
assertEquals(1, accessor.count(User.class));
assertEquals(user.getId(), check.getId());
assertEquals(user.getCode(), check.getCode());
assertEquals(user.getName(), check.getName());
assertEquals(user.getDesc(), check.getDesc());
check = accessor.getByCode("john", User.class);
assertNotNull(check);
check = accessor.getById(user.getId(), User.class);
assertNotNull(check);
assertEquals(user.getCode(), check.getCode());
check = accessor.getByCode("john-1", User.class);
// 证明修改代码无效
assertNull(check);
user = EntityFactory.createEntity(User.class);
user.setId(DigestUtils.uuid());
user.setCode("josh");
user.setName("Josh Peng");
check = accessor.save(user);
assertNotNull(check);
assertEquals(2, accessor.count(User.class));
check = accessor.getByCode("john", User.class);
assertNotNull(check);
assertTrue(check.isValid());
check = accessor.getByCode("josh", User.class);
assertNotNull(check);
assertTrue(check.isValid());
user = accessor.getByCode("john", User.class);
assertNotNull(user);
check = accessor.remove(user);
assertNotNull(check);
assertEquals(1, accessor.count(User.class));
assertEquals(2, accessor.count(User.class, false));
assertFalse(check.isValid());
List<User> list = ((GeneralAccessor) accessor).find(Arrays.asList(new GeneralAccessor.ConditionTuple("code", "john"), new GeneralAccessor.ConditionTuple("valid", true)), User.class);
assertEquals(0, list.size());
list = ((GeneralAccessor) accessor).find(Arrays.asList(new GeneralAccessor.ConditionTuple("code", "josh"), new GeneralAccessor.ConditionTuple("valid", true)), User.class);
assertEquals(1, list.size());
user = accessor.getByCode("john", User.class);
check = accessor.remove(user, false);
assertNotNull(check);
assertEquals(1, accessor.count(User.class));
check = accessor.getById(user.getId(), User.class);
assertNull(check);
check = accessor.getByCode(user.getCode(), User.class);
assertNull(check);
check = accessor.getByCode("josh", User.class);
assertNotNull(check);
user = accessor.getByCode("josh", User.class);
accessor.remove(user, false);
assertEquals(0, accessor.count(User.class));
} catch (UserInterfaceException ex) {
fail(ex.getMessage());
}
}
use of org.mx.dal.service.GeneralDictAccessor 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());
}
}
use of org.mx.dal.service.GeneralDictAccessor 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());
}
}
use of org.mx.dal.service.GeneralDictAccessor in project main by JohnPeng739.
the class TestPrivilege method testPrivilegeCrud.
@Test
public void testPrivilegeCrud() {
GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
assertNotNull(service);
try {
assertEquals(0, service.count(Privilege.class));
// insert
testInsertPrivilege(service);
// edit
testEditPrivilege(service);
// delete
testDeletePrivilege(service);
} catch (Exception ex) {
ex.printStackTrace();
fail(ex.getMessage());
}
}
Aggregations