Search in sources :

Example 1 with FieldOrderedComparator

use of io.crnk.core.engine.internal.utils.FieldOrderedComparator in project crnk-framework by crnk-project.

the class DefaultResourceInformationProvider method build.

public ResourceInformation build(Class<?> resourceClass, boolean allowNonResourceBaseClass) {
    List<ResourceField> resourceFields = getResourceFields(resourceClass);
    String resourceType = getResourceType(resourceClass, allowNonResourceBaseClass);
    Optional<JsonPropertyOrder> propertyOrder = ClassUtils.getAnnotation(resourceClass, JsonPropertyOrder.class);
    if (propertyOrder.isPresent()) {
        JsonPropertyOrder propertyOrderAnnotation = propertyOrder.get();
        Collections.sort(resourceFields, new FieldOrderedComparator(propertyOrderAnnotation.value(), propertyOrderAnnotation.alphabetic()));
    }
    DefaultResourceInstanceBuilder<?> instanceBuilder = new DefaultResourceInstanceBuilder(resourceClass);
    Class<?> superclass = resourceClass.getSuperclass();
    String superResourceType = superclass != Object.class && context.accept(superclass) ? context.getResourceType(superclass) : null;
    ResourceInformation information = new ResourceInformation(context.getTypeParser(), resourceClass, resourceType, superResourceType, instanceBuilder, resourceFields, pagingBehaviors.get(ClassUtils.getAnnotation(resourceClass, JsonApiResource.class).get().pagingBehavior()));
    if (!allowNonResourceBaseClass && information.getIdField() == null) {
        throw new ResourceIdNotFoundException(resourceClass.getCanonicalName());
    }
    return information;
}
Also used : FieldOrderedComparator(io.crnk.core.engine.internal.utils.FieldOrderedComparator) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) JsonPropertyOrder(com.fasterxml.jackson.annotation.JsonPropertyOrder) ResourceIdNotFoundException(io.crnk.core.exception.ResourceIdNotFoundException)

Example 2 with FieldOrderedComparator

use of io.crnk.core.engine.internal.utils.FieldOrderedComparator in project crnk-framework by crnk-project.

the class FieldOrderedComparatorTest method onNoOrderShouldPersistInsertionOrder.

@Test
public void onNoOrderShouldPersistInsertionOrder() throws Exception {
    // GIVEN
    Set<ResourceField> fields = new TreeSet<>(new FieldOrderedComparator(new String[] {}, false));
    // WHEN
    fields.add(fieldB);
    fields.add(fieldA);
    // THEN
    assertThat(fields).containsSequence(fieldB, fieldA);
}
Also used : FieldOrderedComparator(io.crnk.core.engine.internal.utils.FieldOrderedComparator) ResourceField(io.crnk.core.engine.information.resource.ResourceField) TreeSet(java.util.TreeSet) Test(org.junit.Test)

Example 3 with FieldOrderedComparator

use of io.crnk.core.engine.internal.utils.FieldOrderedComparator in project crnk-framework by crnk-project.

the class FieldOrderedComparatorTest method onAlphabeticOrderShouldSortCorrectly.

@Test
public void onAlphabeticOrderShouldSortCorrectly() throws Exception {
    // GIVEN
    Set<ResourceField> fields = new TreeSet<>(new FieldOrderedComparator(new String[] {}, true));
    // WHEN
    fields.add(fieldB);
    fields.add(fieldA);
    // THEN
    assertThat(fields).containsSequence(fieldA, fieldB);
}
Also used : FieldOrderedComparator(io.crnk.core.engine.internal.utils.FieldOrderedComparator) ResourceField(io.crnk.core.engine.information.resource.ResourceField) TreeSet(java.util.TreeSet) Test(org.junit.Test)

Example 4 with FieldOrderedComparator

use of io.crnk.core.engine.internal.utils.FieldOrderedComparator in project crnk-framework by crnk-project.

the class FieldOrderedComparatorTest method onOneFieldShouldSortCorrectly.

@Test
public void onOneFieldShouldSortCorrectly() throws Exception {
    // GIVEN
    Set<ResourceField> fields = new TreeSet<>(new FieldOrderedComparator(new String[] { "b" }, false));
    // WHEN
    fields.add(fieldA);
    fields.add(fieldB);
    // THEN
    assertThat(fields).containsSequence(fieldB, fieldA);
}
Also used : FieldOrderedComparator(io.crnk.core.engine.internal.utils.FieldOrderedComparator) ResourceField(io.crnk.core.engine.information.resource.ResourceField) TreeSet(java.util.TreeSet) Test(org.junit.Test)

Example 5 with FieldOrderedComparator

use of io.crnk.core.engine.internal.utils.FieldOrderedComparator in project crnk-framework by crnk-project.

the class FieldOrderedComparatorTest method onTwoFieldsShouldSortCorrectly.

@Test
public void onTwoFieldsShouldSortCorrectly() throws Exception {
    // GIVEN
    Set<ResourceField> fields = new TreeSet<>(new FieldOrderedComparator(new String[] { "b", "a" }, false));
    // WHEN
    fields.add(fieldA);
    fields.add(fieldB);
    // THEN
    assertThat(fields).containsSequence(fieldB, fieldA);
}
Also used : FieldOrderedComparator(io.crnk.core.engine.internal.utils.FieldOrderedComparator) ResourceField(io.crnk.core.engine.information.resource.ResourceField) TreeSet(java.util.TreeSet) Test(org.junit.Test)

Aggregations

ResourceField (io.crnk.core.engine.information.resource.ResourceField)5 FieldOrderedComparator (io.crnk.core.engine.internal.utils.FieldOrderedComparator)5 TreeSet (java.util.TreeSet)4 Test (org.junit.Test)4 JsonPropertyOrder (com.fasterxml.jackson.annotation.JsonPropertyOrder)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 ResourceIdNotFoundException (io.crnk.core.exception.ResourceIdNotFoundException)1