Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XWIKI-22681: Provide a relative ResourceReferenceEntityReferenceResolver #3673

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
*/
@ComponentTest
@ComponentList({ DefaultSymbolScheme.class })
public class DefaultReferenceEntityReferenceResolverTest implements TestConstants
class DefaultReferenceEntityReferenceResolverTest implements TestConstants
{
@MockComponent
private EntityReferenceProvider referenceProvider;
Expand All @@ -59,7 +59,7 @@ public class DefaultReferenceEntityReferenceResolverTest implements TestConstant
private DefaultReferenceEntityReferenceResolver resolver;

@BeforeComponent
public void beforeComponent()
void beforeComponent()
{
when(this.referenceProvider.getDefaultReference(EntityType.WIKI)).thenReturn(DEFAULT_WIKI_REFERENCE);
when(this.referenceProvider.getDefaultReference(EntityType.SPACE)).thenReturn(DEFAULT_SPACE_REFERENCE);
Expand All @@ -78,7 +78,7 @@ public void beforeComponent()
}

@Test
public void resolveDocumentReferenceWhenMissingParents()
void resolveDocumentReferenceWhenMissingParents()
{
EntityReference partialReference = new EntityReference("document", EntityType.DOCUMENT);

Expand All @@ -89,10 +89,29 @@ public void resolveDocumentReferenceWhenMissingParents()
assertEquals(EntityType.SPACE, reference.getParent().getType());
assertEquals(DEFAULT_WIKI, reference.getParent().getParent().getName());
assertEquals(EntityType.WIKI, reference.getParent().getParent().getType());

partialReference = new EntityReference("WebHome", EntityType.DOCUMENT);
reference = this.resolver.resolve(partialReference, EntityType.DOCUMENT);

assertNotSame(partialReference, reference);
assertEquals(DEFAULT_SPACE, reference.getParent().getName());
assertEquals(EntityType.SPACE, reference.getParent().getType());
assertEquals(DEFAULT_WIKI, reference.getParent().getParent().getName());
assertEquals(EntityType.WIKI, reference.getParent().getParent().getType());

EntityReference spaceReference = new EntityReference("Space", EntityType.SPACE);
partialReference = new EntityReference("WebHome", EntityType.DOCUMENT, spaceReference);
reference = this.resolver.resolve(partialReference, EntityType.DOCUMENT);

assertNotSame(partialReference, reference);
assertEquals("Space", reference.getParent().getName());
assertEquals(EntityType.SPACE, reference.getParent().getType());
assertEquals(DEFAULT_WIKI, reference.getParent().getParent().getName());
assertEquals(EntityType.WIKI, reference.getParent().getParent().getType());
}

@Test
public void resolveAttachmentReferenceWhenMissingParents()
void resolveAttachmentReferenceWhenMissingParents()
{
EntityReference reference =
this.resolver.resolve(new EntityReference("filename", EntityType.ATTACHMENT), EntityType.ATTACHMENT);
Expand All @@ -106,7 +125,7 @@ public void resolveAttachmentReferenceWhenMissingParents()
}

@Test
public void resolveDocumentReferenceWhenMissingParentBetweenReferences()
void resolveDocumentReferenceWhenMissingParentBetweenReferences()
{
EntityReference partialReference =
new EntityReference("document", EntityType.DOCUMENT, new EntityReference("wiki", EntityType.WIKI));
Expand All @@ -122,7 +141,7 @@ public void resolveDocumentReferenceWhenMissingParentBetweenReferences()
}

@Test
public void resolveAttachmentReferenceWhenMissingParentBetweenReferences()
void resolveAttachmentReferenceWhenMissingParentBetweenReferences()
{
EntityReference reference = this.resolver.resolve(
new EntityReference("filename", EntityType.ATTACHMENT, new EntityReference("wiki", EntityType.WIKI)),
Expand All @@ -137,7 +156,7 @@ public void resolveAttachmentReferenceWhenMissingParentBetweenReferences()
}

@Test
public void resolveDocumentReferenceWhenInvalidReference()
void resolveDocumentReferenceWhenInvalidReference()
{
try {
this.resolver.resolve(new EntityReference("document", EntityType.DOCUMENT,
Expand All @@ -150,7 +169,7 @@ public void resolveDocumentReferenceWhenInvalidReference()
}

@Test
public void resolveDocumentReferenceWhenTypeIsSpace()
void resolveDocumentReferenceWhenTypeIsSpace()
{
EntityReference reference =
this.resolver.resolve(new EntityReference("space", EntityType.SPACE), EntityType.DOCUMENT);
Expand All @@ -164,7 +183,7 @@ public void resolveDocumentReferenceWhenTypeIsSpace()
}

@Test
public void resolveSpaceReferenceWhenTypeIsDocument()
void resolveSpaceReferenceWhenTypeIsDocument()
{
EntityReference reference =
this.resolver.resolve(new EntityReference("document", EntityType.DOCUMENT), EntityType.SPACE);
Expand All @@ -179,7 +198,7 @@ public void resolveSpaceReferenceWhenTypeIsDocument()
* Tests that a relative object reference is resolved correctly and completed with the default document parent.
*/
@Test
public void resolveObjectReferenceWhenMissingParents()
void resolveObjectReferenceWhenMissingParents()
{
EntityReference reference =
resolver.resolve(new EntityReference("object", EntityType.OBJECT), EntityType.OBJECT);
Expand All @@ -197,7 +216,7 @@ public void resolveObjectReferenceWhenMissingParents()
* Tests that a relative object property is resolved correctly and completed with the default object parent.
*/
@Test
public void resolveObjectPropertyReferenceWhenMissingParents()
void resolveObjectPropertyReferenceWhenMissingParents()
{
EntityReference reference =
resolver.resolve(new EntityReference("property", EntityType.OBJECT_PROPERTY), EntityType.OBJECT_PROPERTY);
Expand All @@ -218,7 +237,7 @@ public void resolveObjectPropertyReferenceWhenMissingParents()
* values for object name.
*/
@Test
public void resolveObjectReferenceWhenTypeIsDocument()
void resolveObjectReferenceWhenTypeIsDocument()
{
EntityReference reference = resolver.resolve(
new EntityReference("document", EntityType.DOCUMENT,
Expand All @@ -239,7 +258,7 @@ public void resolveObjectReferenceWhenTypeIsDocument()
* values for object and property name.
*/
@Test
public void resolveObjectPropertyReferenceWhenTypeIsDocument()
void resolveObjectPropertyReferenceWhenTypeIsDocument()
{
EntityReference reference = resolver.resolve(
new EntityReference("document", EntityType.DOCUMENT,
Expand All @@ -258,7 +277,7 @@ public void resolveObjectPropertyReferenceWhenTypeIsDocument()
}

@Test
public void resolveDocumentReferenceWhenNullReference()
void resolveDocumentReferenceWhenNullReference()
{
EntityReference reference = this.resolver.resolve(null, EntityType.DOCUMENT);

Expand All @@ -271,7 +290,7 @@ public void resolveDocumentReferenceWhenNullReference()
}

@Test
public void resolvePageReferenceWhenTypeIsDocument()
void resolvePageReferenceWhenTypeIsDocument()
{
EntityReference reference =
this.resolver.resolve(new EntityReference("document", EntityType.DOCUMENT), EntityType.PAGE);
Expand All @@ -284,7 +303,7 @@ public void resolvePageReferenceWhenTypeIsDocument()
}

@Test
public void resolvePageReferenceWhenTypeIsSpace()
void resolvePageReferenceWhenTypeIsSpace()
{
EntityReference reference =
this.resolver.resolve(new EntityReference("space", EntityType.SPACE), EntityType.PAGE);
Expand All @@ -293,7 +312,7 @@ public void resolvePageReferenceWhenTypeIsSpace()
}

@Test
public void resolveDocumentReferenceWhenTypeIsPage()
void resolveDocumentReferenceWhenTypeIsPage()
{
EntityReference reference =
this.resolver.resolve(new EntityReference("page", EntityType.PAGE), EntityType.DOCUMENT);
Expand All @@ -303,12 +322,14 @@ public void resolveDocumentReferenceWhenTypeIsPage()
reference = this.resolver.resolve(
new EntityReference("page1", EntityType.PAGE, new EntityReference("page2", EntityType.PAGE)),
EntityType.DOCUMENT);


// FIXME: there should be a check if the page exists or not.
// See https://jira.xwiki.org/browse/XWIKI-22699
assertEquals(new DocumentReference(DEFAULT_WIKI, List.of("page2", "page1"), DEFAULT_DOCUMENT), reference);
}

@Test
public void resolveSpaceReferenceWhenTypeIsPage()
void resolveSpaceReferenceWhenTypeIsPage()
{
EntityReference reference =
this.resolver.resolve(new EntityReference("page", EntityType.PAGE), EntityType.SPACE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.jupiter.api.Test;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.WikiReference;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.junit5.mockito.ComponentTest;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
Expand Down Expand Up @@ -70,5 +71,19 @@ void resolveDocumentReferenceWithBaseReference()
assertNull(reference.extractReference(EntityType.WIKI));
assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
assertNull(reference.extractReference(EntityType.DOCUMENT));

reference =
this.resolver.resolve("", EntityType.DOCUMENT, new EntityReference("wikiFoo", EntityType.WIKI));

assertEquals("wikiFoo", reference.extractReference(EntityType.WIKI).getName());
assertNull(reference.extractReference(EntityType.SPACE));
assertNull(reference.extractReference(EntityType.DOCUMENT));

reference =
this.resolver.resolve("", EntityType.DOCUMENT, new WikiReference("wikiFoo"));

assertEquals("wikiFoo", reference.extractReference(EntityType.WIKI).getName());
assertNull(reference.extractReference(EntityType.SPACE));
assertNull(reference.extractReference(EntityType.DOCUMENT));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.rendering.internal.resolver;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType;

/**
* Abstract class for all relative resource reference resolvers.
*
* @version $Id$
* @since 17.0.0RC1
* @since 16.10.2
*/
public abstract class AbstractRelativeResourceReferenceEntityReferenceResolver
extends AbstractResourceReferenceEntityReferenceResolver
{
@Inject
@Named("relative")
protected EntityReferenceResolver<String> relativeReferenceResolver;

/**
* @param type the resource type that this resolver will support
*/
protected AbstractRelativeResourceReferenceEntityReferenceResolver(ResourceType type)
{
super(type);
}

@Override
protected EntityReference getBaseReference(ResourceReference resourceReference, Object... parameters)
{
EntityReference baseReference =
(parameters.length > 0 && parameters[0] instanceof EntityReference entityReference)
? entityReference : null;

if (!resourceReference.getBaseReferences().isEmpty()) {
// If the passed reference has a base reference, resolve it first with a relative resolver (it should
// normally be absolute but who knows what the API caller has specified...)
baseReference = resolveBaseReference(resourceReference.getBaseReferences(), baseReference);
}

return baseReference;
}

@Override
protected EntityReference resolveBaseReference(List<String> baseReferences, EntityReference defaultBaseReference)
{
EntityReference resolvedBaseReference = defaultBaseReference;
for (String baseReference : baseReferences) {
resolvedBaseReference =
this.relativeReferenceResolver.resolve(baseReference, EntityType.DOCUMENT, resolvedBaseReference);
}

return resolvedBaseReference;
}

@Override
protected EntityReference resolveTyped(ResourceReference resourceReference, EntityReference baseReference)
{
return this.relativeReferenceResolver.resolve(resourceReference.getReference(), getEntityType(), baseReference);
}

/**
*
* @return the entity type of the {@link EntityReference} this resolver produces.
*/
protected abstract EntityType getEntityType();
}
Loading
Loading