/** Test changing group. */ @Test public void testModifyGroup() throws Exception { System.out.println(">>> Modifying group"); UserTransaction tx = getUserTransaction(); try { AdminEJBLocal instance = (AdminEJBLocal) getEJBInstance(AdminEJB.class.getSimpleName()); tx.begin(); // Get group Group group = instance.getGroup(GROUP_ID); if (group != null) { group.getGroupRoles(); } tx.commit(); assertNotNull("Failed to get group", group); assertNotNull("Group name is null", group.getName()); assertNotNull("Group description is null", group.getDescription()); assertNotNull("Group roles is null", group.getGroupRoles()); int rolesSize = group.getGroupRoles().size(); group.setEntityAction(EntityAction.UPDATE); if (rolesSize > 1) { group.getGroupRoles().get(0).setEntityAction(EntityAction.DELETE); rolesSize -= 1; } group.setName("Updated Test group"); group.setDescription("Updated description for group"); tx.begin(); Group updatedGroup = instance.saveGroup(group); tx.commit(); // Re-read group tx.begin(); updatedGroup = instance.getGroup(GROUP_ID); if (updatedGroup != null) { updatedGroup.getGroupRoles(); } tx.commit(); assertNotNull("Failed to get modified group", updatedGroup); assertEquals("Group name wasn't modified", updatedGroup.getName(), group.getName()); assertEquals( "Group description wasn't modified", updatedGroup.getDescription(), group.getDescription()); assertNotNull("Modified group roles is null", group.getGroupRoles()); assertEquals("Group roles wasn't modified", updatedGroup.getGroupRoles().size(), rolesSize); System.out.println(">>> Group has been modified!"); } catch (Exception e) { tx.rollback(); fail(e.getMessage()); } }
/** Test creating group */ @Test public void testCraeteGroup() throws Exception { System.out.println(">>> Create group"); UserTransaction tx = getUserTransaction(); try { AdminEJBLocal instance = (AdminEJBLocal) getEJBInstance(AdminEJB.class.getSimpleName()); tx.begin(); // Get roles List<Role> roles = instance.getRoles(); tx.commit(); assertNotNull("List of roles is null.", roles); tx.begin(); List<GroupRole> groupRoles = new ArrayList<GroupRole>(); if (roles != null) { for (Role role : roles) { GroupRole groupRole = new GroupRole(); groupRole.setRoleCode(role.getCode()); groupRole.setEntityAction(EntityAction.INSERT); groupRoles.add(groupRole); } } Group userGroup = new Group(); userGroup.setId(GROUP_ID); userGroup.setName("Test group"); userGroup.setDescription("This is test group"); userGroup.setLoaded(false); userGroup.setEntityAction(EntityAction.INSERT); userGroup.setGroupRoles(groupRoles); Group result = instance.saveGroup(userGroup); tx.commit(); assertNotNull("Failed to create group", result); System.out.println(">>> Group has been saved!"); } catch (Exception e) { tx.rollback(); fail(e.getMessage()); } }