@Test public void testWriteItems() throws Exception { List<String> items = Collections.singletonList("foo"); writer.write(items); verify(repository).save("foo"); } }
public Boolean delete(Long id) { this.repo.delete(id); return true; } }
@Test public void test_delete_by_entity() { //Given db has some ids List<String> ids = Arrays.asList("deleteId1", "deleteId2", "deleteId3", "deleteId4"); ids.forEach(id -> dao.save(build(id))); //When an entity is deleted by entity dao.delete(dao.findOne(ids.get(0))); //Then the count is decremented assertThat(countEntities(), is(equalTo(ids.size() - 1))); }
@Test public void test_delete_in_mass() { //Given db has some ids List<String> ids = Arrays.asList("deleteId1", "deleteId2", "deleteId3", "deleteId4"); ids.forEach(id -> dao.save(build(id))); //When we request the deletion giving two existing ids Iterable<T> entities = dao.findAll(ids.subList(0, 2)); dao.delete(entities); //Then the count is decremented assertThat(countEntities(), is(equalTo(ids.size() - 2))); }
@Test public void test_findall_no_filter() { //Given there is some entity in database List<String> ids = Arrays.asList("testFindAll1", "testFindAll2"); dao.save(ids.stream() .map(this::build) .collect(Collectors.toList())); //when we iterate on all objects int count = 0; for (Iterator<T> itElements = dao.findAll().iterator(); itElements.hasNext(); itElements.next()) { count++; } //Then we find the good number of objects assertThat("Retrieving all elements should return the same quantity", count, is(equalTo(ids.size()))); }
public <T, ID> void doOperation(OperationMode operationMode, CrudRepository<T, ID> repo, T object, ID id) { switch (operationMode) { case UPDATE: case INSERT: repo.save(object); break; case DELETE: if (repo.existsById(id)) { repo.deleteById(id); } break; default: throw new IllegalArgumentException(operationMode + " is not supported"); } }
@Test public void test_delete_by_id() { //Given db has some ids List<String> ids = Arrays.asList("deleteId1", "deleteId2", "deleteId3", "deleteId4"); ids.forEach(id -> dao.save(build(id))); //When an entity is deleted by id dao.delete(ids.get(0)); //Then the count is decremented assertThat(countEntities(), is(equalTo(ids.size() - 1))); }
public Object load(Serializable key) { return crudRepository.findOne(key); }
@Test public void test_delete_all() { //Given db has some ids List<String> ids = Arrays.asList("deleteId1", "deleteId2", "deleteId3", "deleteId4"); ids.forEach(id -> dao.save(build(id))); //When all are deleted dao.deleteAll(); //Then the returned count is null assertThat(countEntities(), is(equalTo(0))); }
public Iterable<M> getAll() { return this.repo.findAll(); }
private void deleteContractEventFilter(ContractEventFilter contractEventFilter) { eventFilterRepository.deleteById(contractEventFilter.getId()); }
private int countEntities() { return toIntExact(dao.count()); }
@Test public void test_find_one_returns_null_on_non_existing_id() { //Given an object is inserted String entityId = "someEntityId"; T original = build(entityId); dao.save(original); //When we load a non existing id T other = dao.findOne(entityId + "-fail"); //then the object returned is null assertThat(other, is(nullValue())); }
@Test public void test_findall_filtered() { //Given there is some entity in database List<String> ids = Arrays.asList("testFindAll1", "testFindAll2"); dao.save(ids.stream() .map(this::build) .collect(Collectors.toList())); dao.save(build("testFindAll3")); //when we iterate on filtered objects //then we have objects that existed int count = 0; for (Iterator<T> itElements = dao.findAll(ids).iterator(); itElements.hasNext(); itElements.next()) { count++; } //and we find the good number of objects assertThat("Retrieving filtered elements should return the same quantity", count, is(equalTo(ids.size()))); }
@Test public void test_exists() { //Given there is some entity in database List<String> ids = Arrays.asList("testInsert1", "testInsert2"); ids.forEach(id -> dao.save(build(id))); //When we request each object by id //Then objects are found ids.forEach(id -> assertThat("Each element should exist in DAO", dao.exists(id), is(true))); }
@Override public T find(Class<T> clazz, Object id, String event, CT context) { return repo.findOne((Serializable)id); }
@Override public Iterable<Object> invokeFindAll(Sort sort) { return customFindAllMethod ? super.invokeFindAll(sort) : repository.findAll(); }