/** * Traverse the navigation tree and determine the maximum depth. * * @param element * @param depth * @return */ private int maxDepth(NavigationElement element, int depth) { int max = depth; if (element.getChildren() != null) { for (NavigationElement child : element.getChildren()) { int foundDepth = maxDepth(child, depth + 1); if (foundDepth > max) { max = foundDepth; } } } return max; }
/** * Internal validation method that can be called recursively. * * @param element * @param level * @return */ private int validateNavigation(NavigationElement element, int level) { assertNotNull("Level: " + level + " The given root element must not be null", element); assertNotNull("Level: " + level + " The node field within one of the navigation elements was null.", element.getNode()); assertNotNull("Level: " + level + " The uuid of the navigation element must not be null", element.getUuid()); assertEquals("Level: " + level + " The uuid of the navigation element and the nested node did not match.", element.getUuid(), element.getNode().getUuid()); int elements = 1; if (element.getChildren() != null) { if (element.getChildren().isEmpty()) { fail("Level: " + level + " The children field should never be empty. Instead it should be null to avoid it being serialized to JSON"); } for (NavigationElement child : element.getChildren()) { elements += validateNavigation(child, level++); } } return elements; }
public NavigationResponse getNavigationResponse() { NavigationResponse response = new NavigationResponse(); String rootUuid = NODE_ROOT_UUID; // Level 0 NodeResponse rootElement = getNodeResponseWithAllFields(); rootElement.setUuid(rootUuid); response.setUuid(rootUuid); response.setNode(rootElement); response.setChildren(new ArrayList<>()); // Level 1 NavigationElement navElement = new NavigationElement(); String navElementUuid = UUID_1; NodeResponse navElementNode = getNodeResponseWithAllFields(); navElementNode.setUuid(navElementUuid); navElement.setUuid(navElementUuid); navElement.setNode(navElementNode); response.getChildren().add(navElement); return response; }