Skip to content

Commit

Permalink
Add tests for CrudRepositoryStubs
Browse files Browse the repository at this point in the history
  • Loading branch information
mkutz committed Jun 27, 2024
1 parent 9404998 commit 90d090d
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
import static org.assertj.core.api.Assumptions.assumeThat;

import java.util.List;
import java.util.Objects;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.springframework.data.annotation.Id;
import org.stubit.random.RandomString;

class CrudRepositoryStubTest {

Expand Down Expand Up @@ -146,55 +143,11 @@ private static class TestCrudRepositoryStub extends CrudRepositoryStub<TestEntit

@Override
UUID getId(TestEntity entity) {
return entity.uuid;
}
}

private static class TestEntity {

@Id private UUID uuid;
private String name;

public TestEntity(UUID uuid, String name) {
this.uuid = uuid;
this.name = name;
}

public static TestEntity randomEntity() {
return new TestEntity(randomUUID(), RandomString.aRandomString(8));
}

public UUID getUuid() {
return uuid;
}

public void setUuid(UUID uuid) {
this.uuid = uuid;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestEntity entity = (TestEntity) o;
return Objects.equals(uuid, entity.uuid) && Objects.equals(name, entity.name);
}

@Override
public int hashCode() {
return Objects.hash(uuid, name);
return entity.getUuid();
}
}

private static List<UUID> idsOf(Iterable<TestEntity> entities) {
return stream(entities.spliterator(), false).map(entity -> entity.uuid).toList();
return stream(entities.spliterator(), false).map(TestEntity::getUuid).toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package org.stubit.springdata;

import static java.util.UUID.randomUUID;
import static java.util.stream.StreamSupport.stream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.assertj.core.api.Assumptions.assumeThat;

import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.Test;

class ListCrudRepositoryStubTest {

@Test
void save() {
var repository = new TestListCrudRepositoryStub();

TestEntity entity = repository.save(TestEntity.randomEntity());

assertThat(repository.findById(entity.getUuid())).isPresent().get().isEqualTo(entity);
}

@Test
void saveAll() {
var repository = new TestListCrudRepositoryStub();

var entities =
repository.saveAll(List.of(TestEntity.randomEntity(), TestEntity.randomEntity()));

assertThat(repository.findAllById(idsOf(entities))).isEqualTo(entities);
}

@Test
void findById() {
var repository = new TestListCrudRepositoryStub();
var entity = repository.save(TestEntity.randomEntity());

assertThat(repository.findById(entity.getUuid())).isPresent().get().isEqualTo(entity);
}

@Test
void existsById() {
var repository = new TestListCrudRepositoryStub();
var entity = repository.save(TestEntity.randomEntity());

assertThat(repository.existsById(entity.getUuid())).isTrue();
}

@Test
void findAll() {
var repository = new TestListCrudRepositoryStub();
Iterable<TestEntity> entities =
repository.saveAll(List.of(TestEntity.randomEntity(), TestEntity.randomEntity()));

assertThat(repository.findAll()).containsAll(entities);
}

@Test
void findAllById() {
var repository = new TestListCrudRepositoryStub();
var entities = List.of(TestEntity.randomEntity(), TestEntity.randomEntity());
repository.saveAll(entities);

assertThat(repository.findAllById(idsOf(entities))).containsAll(entities);
}

@Test
void findAllById_not_found() {
var repository = new TestListCrudRepositoryStub();
var knownEntity = repository.save(TestEntity.randomEntity());
var unknownId = randomUUID();
var ids = List.of(knownEntity.getUuid(), unknownId);

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> repository.findAllById(ids));
}

@Test
void count() {
var repository = new TestListCrudRepositoryStub();
List<TestEntity> entitiesList = List.of(TestEntity.randomEntity(), TestEntity.randomEntity());
repository.saveAll(entitiesList);

assertThat(repository.count()).isEqualTo(entitiesList.size());
}

@Test
void deleteById() {
var repository = new TestListCrudRepositoryStub();
var entity = repository.save(TestEntity.randomEntity());
assumeThat(repository.existsById(entity.getUuid())).isTrue();

repository.deleteById(entity.getUuid());

assertThat(repository.existsById(entity.getUuid())).isFalse();
}

@Test
void delete() {
var repository = new TestListCrudRepositoryStub();
var entity = repository.save(TestEntity.randomEntity());
assumeThat(repository.existsById(entity.getUuid())).isTrue();

repository.delete(entity);

assertThat(repository.existsById(entity.getUuid())).isFalse();
}

@Test
void deleteAllById() {
var repository = new TestListCrudRepositoryStub();
var entities =
repository.saveAll(List.of(TestEntity.randomEntity(), TestEntity.randomEntity()));

repository.deleteAllById(idsOf(entities));

assertThat(repository.count()).isZero();
}

@Test
void deleteAll_entities() {
var repository = new TestListCrudRepositoryStub();
var entities =
repository.saveAll(List.of(TestEntity.randomEntity(), TestEntity.randomEntity()));

repository.deleteAll(entities);

assertThat(repository.count()).isZero();
}

@Test
void deleteAll() {
var repository = new TestListCrudRepositoryStub();
repository.saveAll(List.of(TestEntity.randomEntity(), TestEntity.randomEntity()));

repository.deleteAll();

assertThat(repository.count()).isZero();
}

private static class TestListCrudRepositoryStub extends ListCrudRepositoryStub<TestEntity, UUID> {

@Override
UUID getId(TestEntity entity) {
return entity.getUuid();
}
}

private static List<UUID> idsOf(Iterable<TestEntity> entities) {
return stream(entities.spliterator(), false).map(TestEntity::getUuid).toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.stubit.springdata;

import static java.util.UUID.randomUUID;

import java.util.Objects;
import java.util.UUID;
import org.springframework.data.annotation.Id;
import org.stubit.random.RandomString;

class TestEntity {

@Id private UUID uuid;
private String name;

public TestEntity(UUID uuid, String name) {
this.uuid = uuid;
this.name = name;
}

public static TestEntity randomEntity() {
return new TestEntity(randomUUID(), RandomString.aRandomString(8));
}

public UUID getUuid() {
return uuid;
}

public void setUuid(UUID uuid) {
this.uuid = uuid;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestEntity entity = (TestEntity) o;
return Objects.equals(uuid, entity.uuid) && Objects.equals(name, entity.name);
}

@Override
public int hashCode() {
return Objects.hash(uuid, name);
}
}

0 comments on commit 90d090d

Please sign in to comment.