}
@Test(groups = "integration.session")
public void testImplicitGroupMembershipFromComplexGroupUpdates() throws Throwable {
Subject subject = null;
Role role = null;
ResourceGroup recursiveGroup = null;
List<Resource> fullTree = null;
Resource nodeBigA = null;
Resource nodeOne = null;
Resource nodeThree = null;
Resource nodeLittleA = null;
Resource nodeTripleLittleI = null;
List<Resource> resultsExplicit = null;
List<Resource> expectedImplicit = null;
List<Resource> expectedExplicit = new ArrayList<Resource>();
try {
try {
getTransactionManager().begin();
EntityManager em = getEntityManager();
// setup simple test structures
subject = SessionTestHelper.createNewSubject(em, "fake subject");
role = SessionTestHelper.createNewRoleForSubject(em, subject, "fake role", Permission.MANAGE_INVENTORY);
recursiveGroup = SessionTestHelper.createNewMixedGroupForRole(em, role, "fake group", true);
// setup the test tree
fullTree = getSimpleTree(em);
ResourceTreeHelper.printForest(fullTree);
// get the resources from the tree we want to explicitly add
nodeBigA = ResourceTreeHelper.findNode(fullTree, "A");
nodeOne = ResourceTreeHelper.findNode(fullTree, "1");
nodeThree = ResourceTreeHelper.findNode(fullTree, "3");
nodeLittleA = ResourceTreeHelper.findNode(fullTree, "a");
nodeTripleLittleI = ResourceTreeHelper.findNode(fullTree, "iii");
// adding nodeLittleA should give us the subtree under nodeLittleA
expectedImplicit = ResourceTreeHelper.getSubtree(nodeLittleA);
implicitGroupMembershipAddHelper(subject, recursiveGroup, nodeLittleA, expectedImplicit);
resultsExplicit = resourceManager.findExplicitResourcesByResourceGroup(subject, recursiveGroup,
PageControl.getUnlimitedInstance());
expectedExplicit.add(nodeLittleA);
verifyEqualByIds("explicit add 1", expectedExplicit, resultsExplicit);
// adding nodeThree should give us the union of the subtrees under nodeLittleA and nodeThree
expectedImplicit.add(nodeThree);
implicitGroupMembershipAddHelper(subject, recursiveGroup, nodeThree, expectedImplicit);
resultsExplicit = resourceManager.findExplicitResourcesByResourceGroup(subject, recursiveGroup,
PageControl.getUnlimitedInstance());
expectedExplicit.add(nodeThree);
verifyEqualByIds("explicit add 2", expectedExplicit, resultsExplicit);
// adding nodeBigA should give us the union of the entire A tree with the nodeThree subtree
expectedImplicit = ResourceTreeHelper.getSubtree(nodeBigA);
expectedImplicit.addAll(ResourceTreeHelper.getSubtree(nodeThree));
implicitGroupMembershipAddHelper(subject, recursiveGroup, nodeBigA, expectedImplicit);
resultsExplicit = resourceManager.findExplicitResourcesByResourceGroup(subject, recursiveGroup,
PageControl.getUnlimitedInstance());
expectedExplicit.add(nodeBigA);
verifyEqualByIds("explicit add 3", expectedExplicit, resultsExplicit);
// adding nodeOne, which is a child of nodeBigA, shouldn't effect the expected results
implicitGroupMembershipAddHelper(subject, recursiveGroup, nodeOne, expectedImplicit);
resultsExplicit = resourceManager.findExplicitResourcesByResourceGroup(subject, recursiveGroup,
PageControl.getUnlimitedInstance());
expectedExplicit.add(nodeOne);
verifyEqualByIds("explicit add 4", expectedExplicit, resultsExplicit);
// adding nodeTripleLittleI shouldn't effect the expected results either
implicitGroupMembershipAddHelper(subject, recursiveGroup, nodeTripleLittleI, expectedImplicit);
resultsExplicit = resourceManager.findExplicitResourcesByResourceGroup(subject, recursiveGroup,
PageControl.getUnlimitedInstance());
expectedExplicit.add(nodeTripleLittleI);
verifyEqualByIds("explicit add 5", expectedExplicit, resultsExplicit);
} catch (Throwable t) {
handleThrowable(t);
} finally {
handleTransaction();
}
try {
getTransactionManager().begin();
// removing the subtree nodeTripleLittleI shouldn't affect the expected set
implicitGroupMembershipRemoveHelper(subject, recursiveGroup, nodeTripleLittleI, expectedImplicit);
} catch (Throwable t) {
handleThrowable(t);
} finally {
handleTransaction();
}
try {
getTransactionManager().begin();
// removing a descendant of a node that is also in the explicit list should be a no-op
implicitGroupMembershipRemoveHelper(subject, recursiveGroup, nodeOne, expectedImplicit);
expectedImplicit.remove(nodeThree);
// removing the wandering nodeThree, so that results will be the complete nodeBigA subtree
implicitGroupMembershipRemoveHelper(subject, recursiveGroup, nodeThree, expectedImplicit);
// removing a root node should remove all descendants that aren't still in the explicit list
implicitGroupMembershipRemoveHelper(subject, recursiveGroup, nodeBigA,
ResourceTreeHelper.getSubtree(nodeLittleA));
} catch (Throwable t) {
handleThrowable(t);
} finally {
handleTransaction();
}
try {
getTransactionManager().begin();
// remove a node that wasn't in the group - negative testing
try {
// passing the "real" expected list for the results; this way, if the exception doesn't happen, the helper returns true
implicitGroupMembershipRemoveHelper(subject, recursiveGroup, nodeBigA,
ResourceTreeHelper.getSubtree(nodeLittleA));
assert false : "Failed: removed non-existent successfully: node = " + nodeBigA.getName();
} catch (Throwable t) {
// expected
}
} catch (Throwable t) {
handleThrowable(t);
} finally {
handleTransaction();
}
try {
getTransactionManager().begin();
// removing the last resource should leave an empty list
implicitGroupMembershipRemoveHelper(subject, recursiveGroup, nodeLittleA, new ArrayList<Resource>());
} catch (Throwable t) {
handleThrowable(t);
} finally {
handleTransaction();
}
try {
getTransactionManager().begin();
// remove a node that wasn't in the group - negative testing
try {
// passing the "real" expected list for the results; this way, if the exception doesn't happen, the helper returns true
Resource nodeBigB = ResourceTreeHelper.findNode(fullTree, "B");
implicitGroupMembershipRemoveHelper(subject, recursiveGroup, nodeBigB,
ResourceTreeHelper.getSubtree(nodeBigA));
assert false : "Failed: removed non-existent successfully: node = " + nodeBigB.getName();
} catch (Throwable t) {
// expected
}
} catch (Throwable t) {
handleThrowable(t);
} finally {
handleTransaction();
}
try {
getTransactionManager().begin();
resultsExplicit = resourceManager.findExplicitResourcesByResourceGroup(subject, recursiveGroup,
PageControl.getUnlimitedInstance());
verifyEqualByIds("explicit remove 0", new ArrayList<Resource>(), resultsExplicit);
} catch (Throwable t) {
handleThrowable(t);
} finally {
handleTransaction();
}
} finally {
// clean up anything that may have gotten created
try {
getTransactionManager().begin();
EntityManager em = getEntityManager();
Subject overlord = subjectManager.getOverlord();
if (null != subject) {
subjectManager.deleteUsers(overlord, new int[] { subject.getId() });
}
if (null != role) {
roleManager.deleteRoles(overlord, new int[] { role.getId() });
}
if (null != recursiveGroup) {
resourceGroupManager.deleteResourceGroup(overlord, recursiveGroup.getId());
}