Search in sources :

Example 1 with TestDTO

use of io.crnk.jpa.model.dto.TestDTO in project crnk-framework by crnk-project.

the class DtoMappingTest method testReadAndUpdateFromEntity.

@Test
public void testReadAndUpdateFromEntity() throws InstantiationException, IllegalAccessException {
    // create as regular entity
    TestEntity test = new TestEntity();
    test.setId(2L);
    test.setStringValue("test");
    testRepo.create(test);
    // query as regular entity (you may want to disable that in a real application)
    List<TestEntity> list = testRepo.findAll(new QuerySpec(TestEntity.class));
    Assert.assertEquals(1, list.size());
    // query the mapped DTO
    ResourceRepositoryV2<TestDTO, Serializable> dtoRepo = client.getQuerySpecRepository(TestDTO.class);
    List<TestDTO> dtos = dtoRepo.findAll(new QuerySpec(TestDTO.class));
    Assert.assertEquals(1, dtos.size());
    TestDTO dto = dtos.get(0);
    Assert.assertEquals(2L, dto.getId().longValue());
    Assert.assertEquals("test", dto.getStringValue());
    Assert.assertEquals("TEST", dto.getComputedUpperStringValue());
    // update the mapped dto
    dto.setStringValue("newValue");
    dtoRepo.save(dto);
    // read again
    dto = dtoRepo.findOne(2L, new QuerySpec(TestDTO.class));
    Assert.assertEquals(2L, dto.getId().longValue());
    Assert.assertEquals("newValue", dto.getStringValue());
    Assert.assertEquals("NEWVALUE", dto.getComputedUpperStringValue());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) QTestEntity(io.crnk.jpa.model.QTestEntity) TestDTO(io.crnk.jpa.model.dto.TestDTO) Serializable(java.io.Serializable) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 2 with TestDTO

use of io.crnk.jpa.model.dto.TestDTO in project crnk-framework by crnk-project.

the class DtoMappingTest method testMappedManyRelation.

@Test
public void testMappedManyRelation() {
    ResourceRepositoryV2<TestDTO, Serializable> testRepo = client.getQuerySpecRepository(TestDTO.class);
    ResourceRepositoryV2<RelatedDTO, Serializable> relatedRepo = client.getQuerySpecRepository(RelatedDTO.class);
    RelationshipRepositoryV2<TestDTO, Long, RelatedDTO, Long> relRepo = client.getQuerySpecRepository(TestDTO.class, RelatedDTO.class);
    TestDTO test = new TestDTO();
    test.setId(2L);
    test.setStringValue("createdDto");
    test = testRepo.create(test);
    RelatedDTO related1 = new RelatedDTO();
    related1.setId(1L);
    related1.setStringValue("related1");
    related1 = relatedRepo.create(related1);
    RelatedDTO related2 = new RelatedDTO();
    related2.setId(2L);
    related2.setStringValue("related2");
    related2 = relatedRepo.create(related2);
    Assert.assertEquals(1, testRepo.findAll(new QuerySpec(TestDTO.class)).size());
    relRepo.addRelations(test, Arrays.asList(related1.getId(), related2.getId()), TestEntity.ATTR_manyRelatedValues);
    Assert.assertEquals(1, testRepo.findAll(new QuerySpec(TestDTO.class)).size());
    // test relationship access
    List<RelatedDTO> actualRelatedList = relRepo.findManyTargets(test.getId(), TestEntity.ATTR_manyRelatedValues, new QuerySpec(RelatedDTO.class));
    Assert.assertEquals(2, actualRelatedList.size());
    // test include
    Assert.assertEquals(1, testRepo.findAll(new QuerySpec(TestDTO.class)).size());
// TODO distinct problem in H2 to investigate
// QuerySpec querySpec = new QuerySpec(TestDTO.class);
// querySpec.includeRelation(Arrays.asList(TestEntity.ATTR_manyRelatedValues));
// ResourceList<TestDTO> list = testRepo.findAll(querySpec);
// Assert.assertEquals(1, list.size());
// TestDTO actualTest = list.get(0);
// actualRelatedList = actualTest.getManyRelatedValues();
// Assert.assertEquals(2, actualRelatedList.size());
// test removal
// TODO DELETE request with body not supported by jersey?
// relRepo.removeRelations(test, Arrays.asList(related2.getId()), TestEntity.ATTR_manyRelatedValues);
// actualRelatedList = relRepo.findManyTargets(test.getId(), TestEntity.ATTR_manyRelatedValues,
// new QuerySpec(RelatedDTO.class));
// Assert.assertEquals(1, actualRelatedList.size());
// Assert.assertEquals(related1.getId(), actualRelatedList.get(0).getId());
}
Also used : TestDTO(io.crnk.jpa.model.dto.TestDTO) Serializable(java.io.Serializable) RelatedDTO(io.crnk.jpa.model.dto.RelatedDTO) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 3 with TestDTO

use of io.crnk.jpa.model.dto.TestDTO in project crnk-framework by crnk-project.

the class DtoMappingTest method testMappedOneRelation.

@Test
public void testMappedOneRelation() {
    ResourceRepositoryV2<TestDTO, Serializable> testRepo = client.getQuerySpecRepository(TestDTO.class);
    ResourceRepositoryV2<RelatedDTO, Serializable> relatedRepo = client.getQuerySpecRepository(RelatedDTO.class);
    RelationshipRepositoryV2<TestDTO, Serializable, RelatedDTO, Serializable> relRepo = client.getQuerySpecRepository(TestDTO.class, RelatedDTO.class);
    TestDTO test = new TestDTO();
    test.setId(2L);
    test.setStringValue("createdDto");
    test = testRepo.create(test);
    RelatedDTO related = new RelatedDTO();
    related.setId(3L);
    related.setStringValue("createdDto");
    related = relatedRepo.create(related);
    relRepo.setRelation(test, related.getId(), TestEntity.ATTR_oneRelatedValue);
    // test relationship access
    RelatedDTO actualRelated = relRepo.findOneTarget(test.getId(), TestEntity.ATTR_oneRelatedValue, new QuerySpec(RelatedDTO.class));
    Assert.assertNotNull(actualRelated);
    Assert.assertEquals(related.getId(), actualRelated.getId());
    // test include
    QuerySpec querySpec = new QuerySpec(TestDTO.class);
    querySpec.includeRelation(Arrays.asList(TestEntity.ATTR_oneRelatedValue));
    ResourceList<TestDTO> list = testRepo.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    TestDTO actualTest = list.get(0);
    actualRelated = actualTest.getOneRelatedValue();
    Assert.assertNotNull(actualRelated);
    Assert.assertEquals(related.getId(), actualRelated.getId());
}
Also used : TestDTO(io.crnk.jpa.model.dto.TestDTO) Serializable(java.io.Serializable) RelatedDTO(io.crnk.jpa.model.dto.RelatedDTO) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 4 with TestDTO

use of io.crnk.jpa.model.dto.TestDTO in project crnk-framework by crnk-project.

the class DtoMappingTest method testInsertDeleteDto.

@Test
public void testInsertDeleteDto() {
    ResourceRepositoryV2<TestDTO, Serializable> dtoRepo = client.getQuerySpecRepository(TestDTO.class);
    // create a entity with a DTO and check properly saved
    TestDTO dto = new TestDTO();
    dto.setId(2L);
    dto.setStringValue("createdDto");
    dto = dtoRepo.create(dto);
    Assert.assertEquals("createdDto", dto.getStringValue());
    Assert.assertEquals("CREATEDDTO", dto.getComputedUpperStringValue());
    // check both exists
    ResourceList<TestDTO> dtos = dtoRepo.findAll(new QuerySpec(TestDTO.class));
    Assert.assertEquals(1, dtos.size());
    dto = dtos.get(0);
    Assert.assertEquals("createdDto", dto.getStringValue());
    Assert.assertEquals("CREATEDDTO", dto.getComputedUpperStringValue());
    // test delete
    dtoRepo.delete(dto.getId());
    dtos = dtoRepo.findAll(new QuerySpec(TestDTO.class));
    Assert.assertEquals(0, dtos.size());
}
Also used : TestDTO(io.crnk.jpa.model.dto.TestDTO) Serializable(java.io.Serializable) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 5 with TestDTO

use of io.crnk.jpa.model.dto.TestDTO in project crnk-framework by crnk-project.

the class DtoMappingTest method testSubQueryComputation.

@Test
public void testSubQueryComputation() {
    ResourceRepositoryV2<TestDTO, Serializable> dtoRepo = client.getQuerySpecRepository(TestDTO.class);
    int n = 5;
    for (long i = 0; i < n; i++) {
        TestDTO dto = new TestDTO();
        dto.setId(i + 100);
        dto.setStringValue(Long.toString(i));
        dtoRepo.create(dto);
    }
    // select, sort, filter by complex subquery
    QuerySpec querySpec = new QuerySpec(TestDTO.class);
    querySpec.addFilter(new FilterSpec(Arrays.asList(TestDTO.ATTR_COMPUTED_NUMBER_OF_SMALLER_IDS), FilterOperator.LT, 4));
    // TODO enable querySpec parser
    // querySpec.addSort(new SortSpec(Arrays.asList(TestDTO.ATTR_COMPUTED_NUMBER_OF_SMALLER_IDS), Direction.DESC));
    ResourceList<TestDTO> dtos = dtoRepo.findAll(querySpec);
    Assert.assertEquals(4, dtos.size());
    for (int i = 0; i < dtos.size(); i++) {
        TestDTO dto = dtos.get(i);
        // 4 - i;
        int j = i;
        Assert.assertEquals(100 + j, dto.getId().longValue());
        Assert.assertEquals(j, dto.getComputedNumberOfSmallerIds());
    }
}
Also used : TestDTO(io.crnk.jpa.model.dto.TestDTO) Serializable(java.io.Serializable) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Aggregations

TestDTO (io.crnk.jpa.model.dto.TestDTO)6 QuerySpec (io.crnk.core.queryspec.QuerySpec)5 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)5 Serializable (java.io.Serializable)5 Test (org.junit.Test)5 TestEntity (io.crnk.jpa.model.TestEntity)2 RelatedDTO (io.crnk.jpa.model.dto.RelatedDTO)2 FilterSpec (io.crnk.core.queryspec.FilterSpec)1 QTestEntity (io.crnk.jpa.model.QTestEntity)1