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());
}
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());
}
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;
}
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());
}
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));
}
Aggregations