-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
207 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
modules/spring-data/src/test/java/org/stubit/springdata/ListCrudRepositoryStubTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
modules/spring-data/src/test/java/org/stubit/springdata/TestEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |