Search in sources :

Example 46 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class JpaResourceInformationProviderTest method test.

@Test
public void test() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    ResourceInformation info = builder.build(TestEntity.class);
    ResourceField idField = info.getIdField();
    assertNotNull(idField);
    assertEquals("id", idField.getJsonName());
    assertEquals("id", idField.getUnderlyingName());
    assertEquals(Long.class, idField.getType());
    assertEquals(Long.class, idField.getGenericType());
    List<ResourceField> attrFields = new ArrayList<>(info.getAttributeFields());
    Collections.sort(attrFields, ResourceFieldComparator.INSTANCE);
    assertEquals(5, attrFields.size());
    ResourceField embField = attrFields.get(1);
    assertEquals(TestEntity.ATTR_embValue, embField.getJsonName());
    assertEquals(TestEntity.ATTR_embValue, embField.getUnderlyingName());
    assertEquals(TestEmbeddable.class, embField.getType());
    assertEquals(TestEmbeddable.class, embField.getGenericType());
    Assert.assertTrue(embField.getAccess().isPostable());
    Assert.assertTrue(embField.getAccess().isPatchable());
    Assert.assertTrue(embField.getAccess().isSortable());
    Assert.assertTrue(embField.getAccess().isFilterable());
    ArrayList<ResourceField> relFields = new ArrayList<ResourceField>(info.getRelationshipFields());
    Collections.sort(relFields, ResourceFieldComparator.INSTANCE);
    assertEquals(4, relFields.size());
    boolean found = false;
    for (ResourceField relField : relFields) {
        if (relField.getUnderlyingName().equals(TestEntity.ATTR_oneRelatedValue)) {
            assertEquals(TestEntity.ATTR_oneRelatedValue, relField.getJsonName());
            assertEquals(RelatedEntity.class, relField.getType());
            assertEquals(RelatedEntity.class, relField.getGenericType());
            found = true;
        }
    }
    Assert.assertTrue(found);
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 47 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class JpaResourceInformationProviderTest method testIdAccess.

@Test
public void testIdAccess() {
    ResourceInformation info = builder.build(TestEntity.class);
    ResourceField idField = info.getIdField();
    Assert.assertTrue(idField.getAccess().isPostable());
    Assert.assertFalse(idField.getAccess().isPatchable());
    Assert.assertTrue(idField.getAccess().isSortable());
    Assert.assertTrue(idField.getAccess().isFilterable());
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Test(org.junit.Test)

Example 48 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class DefaultQuerySpecDeserializerTestBase method testHyphenIsAllowedInResourceName.

@Test
public void testHyphenIsAllowedInResourceName() {
    QuerySpec expectedSpec = new QuerySpec(Task.class);
    expectedSpec.addSort(new SortSpec(Arrays.asList("id"), Direction.ASC));
    Map<String, Set<String>> params = new HashMap<>();
    add(params, "sort[task-with-lookup]", "id");
    ResourceInformation taskWithLookUpInformation = resourceRegistry.getEntryForClass(TaskWithLookup.class).getResourceInformation();
    QuerySpec actualSpec = deserializer.deserialize(taskWithLookUpInformation, params);
    Assert.assertEquals(expectedSpec, actualSpec);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) TaskWithLookup(io.crnk.core.mock.models.TaskWithLookup) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) QuerySpec(io.crnk.core.queryspec.QuerySpec) SortSpec(io.crnk.core.queryspec.SortSpec) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 49 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class ResourceRegistryTest method onExistingResourceShouldReturnUrl.

@Test
public void onExistingResourceShouldReturnUrl() {
    Task task = new Task();
    task.setId(1L);
    ResourceField idField = new ResourceFieldImpl("id", "id", ResourceFieldType.ID, Long.class, Long.class, null);
    ResourceField valueField = new ResourceFieldImpl("value", "value", ResourceFieldType.RELATIONSHIP, String.class, String.class, "projects");
    ResourceInformation resourceInformation = new ResourceInformation(moduleRegistry.getTypeParser(), Task.class, "tasks", null, Arrays.asList(idField, valueField), new OffsetLimitPagingBehavior());
    RegistryEntry registryEntry = new RegistryEntry(new DirectResponseResourceEntry(null, new ResourceRepositoryInformationImpl("tasks", resourceInformation, RepositoryMethodAccess.ALL)));
    resourceRegistry.addEntry(Task.class, registryEntry);
    String resourceUrl = resourceRegistry.getResourceUrl(task);
    assertThat(resourceUrl).isEqualTo(TEST_MODELS_URL + "/tasks/1");
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) Task(io.crnk.core.mock.models.Task) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) OffsetLimitPagingBehavior(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior) ResourceFieldImpl(io.crnk.core.engine.internal.information.resource.ResourceFieldImpl) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) ResourceRepositoryInformationImpl(io.crnk.core.engine.internal.information.repository.ResourceRepositoryInformationImpl) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Test(org.junit.Test)

Example 50 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class ModuleRegistryTest method testInformationBuilder.

@Test
public void testInformationBuilder() throws Exception {
    ResourceInformationProvider informationProvider = moduleRegistry.getResourceInformationBuilder();
    Assert.assertTrue(informationProvider.accept(ComplexPojo.class));
    Assert.assertTrue(informationProvider.accept(Document.class));
    Assert.assertTrue(informationProvider.accept(FancyProject.class));
    Assert.assertTrue(informationProvider.accept(Project.class));
    Assert.assertTrue(informationProvider.accept(Task.class));
    Assert.assertTrue(informationProvider.accept(Thing.class));
    Assert.assertTrue(informationProvider.accept(User.class));
    Assert.assertTrue(informationProvider.accept(TestResource.class));
    Assert.assertFalse(informationProvider.accept(TestRepository.class));
    Assert.assertFalse(informationProvider.accept(DocumentRepository.class));
    Assert.assertFalse(informationProvider.accept(PojoRepository.class));
    Assert.assertFalse(informationProvider.accept(ProjectRepository.class));
    Assert.assertFalse(informationProvider.accept(ResourceWithoutRepositoryToProjectRepository.class));
    Assert.assertFalse(informationProvider.accept(TaskToProjectRepository.class));
    Assert.assertFalse(informationProvider.accept(TaskWithLookupRepository.class));
    Assert.assertFalse(informationProvider.accept(UserRepository.class));
    Assert.assertFalse(informationProvider.accept(UserToProjectRepository.class));
    Assert.assertFalse(informationProvider.accept(Object.class));
    Assert.assertFalse(informationProvider.accept(String.class));
    try {
        informationProvider.build(Object.class);
        Assert.fail();
    } catch (UnsupportedOperationException e) {
    // ok
    }
    ResourceInformation userInfo = informationProvider.build(User.class);
    Assert.assertEquals("id", userInfo.getIdField().getUnderlyingName());
    ResourceInformation testInfo = informationProvider.build(TestResource.class);
    Assert.assertEquals("id", testInfo.getIdField().getUnderlyingName());
    // setup by TestResourceInformationProvider
    Assert.assertEquals("testId", testInfo.getIdField().getJsonName());
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceInformationProvider(io.crnk.core.engine.information.resource.ResourceInformationProvider) Test(org.junit.Test)

Aggregations

ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)167 Test (org.junit.Test)79 ResourceField (io.crnk.core.engine.information.resource.ResourceField)76 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)60 QuerySpec (io.crnk.core.queryspec.QuerySpec)16 Resource (io.crnk.core.engine.document.Resource)15 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)15 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)9 Task (io.crnk.core.mock.models.Task)9 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)9 HashSet (java.util.HashSet)9 Collection (java.util.Collection)8 Relationship (io.crnk.core.engine.document.Relationship)7 HttpMethod (io.crnk.core.engine.http.HttpMethod)7 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)7 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)7 OffsetLimitPagingBehavior (io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior)7 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)7 Set (java.util.Set)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6