### Question:
DocumentTreeNode extends AbstractDocumentTreeNode implements Initializable { @Override protected EntityReference getParent(DocumentReference documentReference) throws Exception { if (getDefaultDocumentName().equals(documentReference.getName())) { EntityReference parentReference = documentReference.getParent().getParent(); if (parentReference.getType() == EntityType.SPACE) { return new DocumentReference(getDefaultDocumentName(), new SpaceReference(parentReference)); } else { return parentReference; } } else { return new DocumentReference(getDefaultDocumentName(), documentReference.getLastSpaceReference()); } } DocumentTreeNode(); @Override void initialize();  }### Answer:
@Test public void getParentForTerminalPage() { DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "Page"); when(this.entityTreeNodeIdConverter.convert(EntityReference.class, "document:wiki:Path.To.Page")) .thenReturn(documentReference); when(this.entityTreeNodeIdConverter.convert(String.class, new DocumentReference("WebHome", documentReference.getLastSpaceReference()))) .thenReturn("document:wiki:Path.To.WebHome"); assertEquals("document:wiki:Path.To.WebHome", this.documentTreeNode.getParent("document:wiki:Path.To.Page")); }
@Test public void getParentForNestedPage() { DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "WebHome"); when(this.entityTreeNodeIdConverter.convert(EntityReference.class, "document:wiki:Path.To.WebHome")) .thenReturn(documentReference); when(this.entityTreeNodeIdConverter.convert(String.class, new DocumentReference("wiki", "Path", "WebHome"))) .thenReturn("document:wiki:Path.WebHome"); assertEquals("document:wiki:Path.WebHome", this.documentTreeNode.getParent("document:wiki:Path.To.WebHome")); }
@Test public void getParentForTopLevelPage() { DocumentReference documentReference = new DocumentReference("wiki", "Path", "WebHome"); when(this.entityTreeNodeIdConverter.convert(EntityReference.class, "document:wiki:Path.WebHome")) .thenReturn(documentReference); when(this.entityTreeNodeIdConverter.convert(String.class, new WikiReference("wiki"))).thenReturn("wiki:wiki"); assertEquals("wiki:wiki", this.documentTreeNode.getParent("document:wiki:Path.WebHome")); }
@Test public void getParentForInvalidNode() { SpaceReference spaceReference = new SpaceReference("wiki", "Path"); when(this.entityTreeNodeIdConverter.convert(EntityReference.class, "space:wiki:Path")) .thenReturn(spaceReference); assertEquals(null, this.documentTreeNode.getParent("space:wiki:Path")); }