DefaultReferenceEntityReferenceResolver implements EntityReferenceResolver<EntityReference> { public EntityReference resolve(EntityReference referenceToResolve, EntityType type) { EntityReference normalizedReference; if (type.ordinal() > referenceToResolve.getType().ordinal()) { normalizedReference = new EntityReference(getDefaultValue(type), type, referenceToResolve.clone()); } else { normalizedReference = referenceToResolve.clone(); } EntityReference reference = normalizedReference; while (reference != null) { List<EntityType> types = this.nextAllowedEntityTypes.get(reference.getType()); if (reference.getParent() != null && !types.isEmpty() && !types.contains(reference.getParent().getType())) { EntityReference newReference = new EntityReference( getDefaultValue(types.get(0)), types.get(0), reference.getParent()); reference.setParent(newReference); } else if (reference.getParent() == null && !types.isEmpty()) { EntityReference newReference = new EntityReference( getDefaultValue(types.get(0)), types.get(0)); reference.setParent(newReference); } else if (reference.getParent() != null && types.isEmpty()) { throw new InvalidEntityReferenceException("Invalid reference [" + referenceToResolve + "]"); } reference = reference.getParent(); } if (type.ordinal() < referenceToResolve.getType().ordinal()) { normalizedReference = normalizedReference.extractReference(type); } return normalizedReference; }  EntityReference resolve(EntityReference referenceToResolve, EntityType type);  }
@Test public void testResolveDocumentReferenceWhenMissingParents() { EntityReference reference = resolver.resolve(new EntityReference("page", EntityType.DOCUMENT), EntityType.DOCUMENT); Assert.assertEquals("defspace", reference.getParent().getName()); Assert.assertEquals(EntityType.SPACE, reference.getParent().getType()); Assert.assertEquals("defwiki", reference.getParent().getParent().getName()); Assert.assertEquals(EntityType.WIKI, reference.getParent().getParent().getType()); }
@Test public void testResolveAttachmentReferenceWhenMissingParents() { EntityReference reference = resolver.resolve( new EntityReference("filename", EntityType.ATTACHMENT), EntityType.ATTACHMENT); Assert.assertEquals("defpage", reference.getParent().getName()); Assert.assertEquals(EntityType.DOCUMENT, reference.getParent().getType()); Assert.assertEquals("defspace", reference.getParent().getParent().getName()); Assert.assertEquals(EntityType.SPACE, reference.getParent().getParent().getType()); Assert.assertEquals("defwiki", reference.getParent().getParent().getParent().getName()); Assert.assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getType()); }
@Test public void testResolveDocumentReferenceWhenMissingParentBetweenReferences() { EntityReference reference = resolver.resolve(new EntityReference("page", EntityType.DOCUMENT, new EntityReference("wiki", EntityType.WIKI)), EntityType.DOCUMENT); Assert.assertEquals("defspace", reference.getParent().getName()); Assert.assertEquals(EntityType.SPACE, reference.getParent().getType()); Assert.assertEquals("wiki", reference.getParent().getParent().getName()); Assert.assertEquals(EntityType.WIKI, reference.getParent().getParent().getType()); }
@Test public void testResolveAttachmentReferenceWhenMissingParentBetweenReferences() { EntityReference reference = resolver.resolve( new EntityReference("filename", EntityType.ATTACHMENT, new EntityReference("wiki", EntityType.WIKI)), EntityType.ATTACHMENT); Assert.assertEquals("defpage", reference.getParent().getName()); Assert.assertEquals(EntityType.DOCUMENT, reference.getParent().getType()); Assert.assertEquals("defspace", reference.getParent().getParent().getName()); Assert.assertEquals(EntityType.SPACE, reference.getParent().getParent().getType()); Assert.assertEquals("wiki", reference.getParent().getParent().getParent().getName()); Assert.assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getType()); }
@Test public void testResolveDocumentReferenceWhenInvalidReference() { try { resolver.resolve(new EntityReference("page", EntityType.DOCUMENT, new EntityReference("filename", EntityType.ATTACHMENT)), EntityType.DOCUMENT); Assert.fail("Should have thrown an exception here"); } catch (InvalidEntityReferenceException expected) { Assert.assertEquals("Invalid reference [name = [page], type = [DOCUMENT], parent = [name = [filename], " + "type = [ATTACHMENT], parent = [null]]]", expected.getMessage()); } }
@Test public void testResolveDocumentReferenceWhenTypeIsSpace() { EntityReference reference = resolver.resolve(new EntityReference("space", EntityType.SPACE), EntityType.DOCUMENT); Assert.assertEquals(EntityType.DOCUMENT, reference.getType()); Assert.assertEquals("defpage", reference.getName()); Assert.assertEquals(EntityType.SPACE, reference.getParent().getType()); Assert.assertEquals("space", reference.getParent().getName()); Assert.assertEquals(EntityType.WIKI, reference.getParent().getParent().getType()); Assert.assertEquals("defwiki", reference.getParent().getParent().getName()); }
@Test public void testResolveSpaceReferenceWhenTypeIsDocument() { EntityReference reference = resolver.resolve(new EntityReference("page", EntityType.DOCUMENT), EntityType.SPACE); Assert.assertEquals(EntityType.SPACE, reference.getType()); Assert.assertEquals("defspace", reference.getName()); Assert.assertEquals(EntityType.WIKI, reference.getParent().getType()); Assert.assertEquals("defwiki", reference.getParent().getName()); }
@Test public void testResolveObjectReferenceWhenMissingParents() { EntityReference reference = resolver.resolve(new EntityReference("object", EntityType.OBJECT), EntityType.OBJECT); Assert.assertEquals(EntityType.OBJECT, reference.getType()); Assert.assertEquals("object", reference.getName()); Assert.assertEquals(EntityType.DOCUMENT, reference.getParent().getType()); Assert.assertEquals("defpage", reference.getParent().getName()); Assert.assertEquals(EntityType.SPACE, reference.getParent().getParent().getType()); Assert.assertEquals("defspace", reference.getParent().getParent().getName()); Assert.assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getType()); Assert.assertEquals("defwiki", reference.getParent().getParent().getParent().getName()); }
@Test public void testResolveObjectPropertyReferenceWhenMissingParents() { EntityReference reference = resolver.resolve(new EntityReference("property", EntityType.OBJECT_PROPERTY), EntityType.OBJECT_PROPERTY); Assert.assertEquals(EntityType.OBJECT_PROPERTY, reference.getType()); Assert.assertEquals("property", reference.getName()); Assert.assertEquals(EntityType.OBJECT, reference.getParent().getType()); Assert.assertEquals("defobject", reference.getParent().getName()); Assert.assertEquals(EntityType.DOCUMENT, reference.getParent().getParent().getType()); Assert.assertEquals("defpage", reference.getParent().getParent().getName()); Assert.assertEquals(EntityType.SPACE, reference.getParent().getParent().getParent().getType()); Assert.assertEquals("defspace", reference.getParent().getParent().getParent().getName()); Assert.assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getParent().getType()); Assert.assertEquals("defwiki", reference.getParent().getParent().getParent().getParent().getName()); }
@Test public void testResolveObjectReferenceWhenTypeIsDocument() { EntityReference reference = resolver.resolve(new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI))), EntityType.OBJECT); Assert.assertEquals(EntityType.OBJECT, reference.getType()); Assert.assertEquals("defobject", reference.getName()); Assert.assertEquals(EntityType.DOCUMENT, reference.getParent().getType()); Assert.assertEquals("page", reference.getParent().getName()); Assert.assertEquals(EntityType.SPACE, reference.getParent().getParent().getType()); Assert.assertEquals("space", reference.getParent().getParent().getName()); Assert.assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getType()); Assert.assertEquals("wiki", reference.getParent().getParent().getParent().getName()); }
@Test public void testResolveObjectPropertyReferenceWhenTypeIsDocument() { EntityReference reference = resolver.resolve(new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI))), EntityType.OBJECT_PROPERTY); Assert.assertEquals(EntityType.OBJECT_PROPERTY, reference.getType()); Assert.assertEquals("defproperty", reference.getName()); Assert.assertEquals(EntityType.OBJECT, reference.getParent().getType()); Assert.assertEquals("defobject", reference.getParent().getName()); Assert.assertEquals(EntityType.DOCUMENT, reference.getParent().getParent().getType()); Assert.assertEquals("page", reference.getParent().getParent().getName()); Assert.assertEquals(EntityType.SPACE, reference.getParent().getParent().getParent().getType()); Assert.assertEquals("space", reference.getParent().getParent().getParent().getName()); Assert.assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getParent().getType()); Assert.assertEquals("wiki", reference.getParent().getParent().getParent().getParent().getName()); }