Search in sources :

Example 16 with TestEntity

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

the class JpaQueryParamsEndToEndTest method testDelete.

@Test
public void testDelete() {
    TestEntity test = new TestEntity();
    test.setId(1L);
    test.setStringValue("test");
    testRepo.create(test);
    testRepo.delete(1L);
    List<TestEntity> list = testRepo.findAll(new QueryParams());
    Assert.assertEquals(0, list.size());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) QueryParams(io.crnk.legacy.queryParams.QueryParams) Test(org.junit.Test) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest)

Example 17 with TestEntity

use of io.crnk.jpa.model.TestEntity 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 18 with TestEntity

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

the class JpaRelationshipRepositoryTestBase method setupManyRelation.

private TestEntity setupManyRelation(List<Long> ids) {
    TestEntity test = em.find(TestEntity.class, 1L);
    Assert.assertThat(test.getManyRelatedValues().size(), Is.is(0));
    repo.addRelations(test, ids, TestEntity.ATTR_manyRelatedValues);
    em.flush();
    em.clear();
    test = em.find(TestEntity.class, 1L);
    return test;
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity)

Example 19 with TestEntity

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

the class JpaRelationshipRepositoryTestBase method testAddRemoveRelations.

@Test
public void testAddRemoveRelations() throws InstantiationException, IllegalAccessException {
    TestEntity test = em.find(TestEntity.class, 1L);
    Assert.assertEquals(0, test.getManyRelatedValues().size());
    repo.addRelations(test, Arrays.asList(101L), TestEntity.ATTR_manyRelatedValues);
    em.flush();
    em.clear();
    test = em.find(TestEntity.class, 1L);
    Assert.assertEquals(1, test.getManyRelatedValues().size());
    RelatedEntity relatedEntity = test.getManyRelatedValues().iterator().next();
    Assert.assertEquals(101L, relatedEntity.getId().longValue());
    // add second relations
    repo.addRelations(test, Arrays.asList(102L), TestEntity.ATTR_manyRelatedValues);
    em.flush();
    em.clear();
    test = em.find(TestEntity.class, 1L);
    Assert.assertEquals(2, test.getManyRelatedValues().size());
    // remove relation
    repo.removeRelations(test, Arrays.asList(102L), TestEntity.ATTR_manyRelatedValues);
    em.flush();
    em.clear();
    test = em.find(TestEntity.class, 1L);
    Assert.assertEquals(1, test.getManyRelatedValues().size());
    // remove relation
    repo.removeRelations(test, Arrays.asList(101L), TestEntity.ATTR_manyRelatedValues);
    em.flush();
    em.clear();
    test = em.find(TestEntity.class, 1L);
    Assert.assertEquals(0, test.getManyRelatedValues().size());
    // set relations
    repo.setRelations(test, Arrays.asList(101L, 102L), TestEntity.ATTR_manyRelatedValues);
    em.flush();
    em.clear();
    test = em.find(TestEntity.class, 1L);
    Assert.assertEquals(2, test.getManyRelatedValues().size());
    // set relations
    repo.setRelations(test, Arrays.asList(101L), TestEntity.ATTR_manyRelatedValues);
    em.flush();
    em.clear();
    test = em.find(TestEntity.class, 1L);
    Assert.assertEquals(1, test.getManyRelatedValues().size());
    RelatedEntity related = test.getManyRelatedValues().iterator().next();
    Assert.assertEquals(101L, related.getId().longValue());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) Test(org.junit.Test) AbstractJpaTest(io.crnk.jpa.query.AbstractJpaTest)

Example 20 with TestEntity

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

the class JpaRelationshipRepositoryTestBase method testGetManyRelation.

@Test
public void testGetManyRelation() {
    TestEntity test = setupManyRelation(Arrays.asList(101L, 102L));
    QuerySpec querySpec = new QuerySpec(RelatedEntity.class);
    Iterable<RelatedEntity> targets = repo.findManyTargets(test.getId(), TestEntity.ATTR_manyRelatedValues, querySpec);
    List<RelatedEntity> res = new ArrayList<>();
    for (RelatedEntity relatedEntity : targets) {
        res.add(relatedEntity);
    }
    Assert.assertThat(res.size(), Is.is(2));
    Assert.assertThat(res.get(0).getId(), Is.is(101L));
    Assert.assertThat(res.get(1).getId(), Is.is(102L));
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) ArrayList(java.util.ArrayList) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test) AbstractJpaTest(io.crnk.jpa.query.AbstractJpaTest)

Aggregations

TestEntity (io.crnk.jpa.model.TestEntity)61 Test (org.junit.Test)50 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)28 QuerySpec (io.crnk.core.queryspec.QuerySpec)25 RelatedEntity (io.crnk.jpa.model.RelatedEntity)24 AbstractJpaTest (io.crnk.jpa.query.AbstractJpaTest)16 OtherRelatedEntity (io.crnk.jpa.model.OtherRelatedEntity)12 QueryParams (io.crnk.legacy.queryParams.QueryParams)6 CollectionAttributesTestEntity (io.crnk.jpa.model.CollectionAttributesTestEntity)5 UuidTestEntity (io.crnk.jpa.model.UuidTestEntity)5 Serializable (java.io.Serializable)5 JsonLinksInformation (io.crnk.client.response.JsonLinksInformation)4 JsonMetaInformation (io.crnk.client.response.JsonMetaInformation)4 PagedMetaInformation (io.crnk.core.resource.meta.PagedMetaInformation)4 TestDTO (io.crnk.jpa.model.dto.TestDTO)2 ObjectProxy (io.crnk.client.internal.proxy.ObjectProxy)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 FilterSpec (io.crnk.core.queryspec.FilterSpec)1 JpaEntityRepository (io.crnk.jpa.JpaEntityRepository)1 JpaRepositoryFilterBase (io.crnk.jpa.JpaRepositoryFilterBase)1