test: Commons for suites based on UserRepository
This commit is contained in:
parent
2b0baf6066
commit
28ffe3f962
3 changed files with 121 additions and 29 deletions
80
src/test/java/eu/ztsh/wymiana/EntityCreator.java
Normal file
80
src/test/java/eu/ztsh/wymiana/EntityCreator.java
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
package eu.ztsh.wymiana;
|
||||||
|
|
||||||
|
import eu.ztsh.wymiana.data.entity.CurrencyEntity;
|
||||||
|
import eu.ztsh.wymiana.data.entity.UserEntity;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class EntityCreator {
|
||||||
|
|
||||||
|
public static class Constants {
|
||||||
|
|
||||||
|
public static String PESEL = "00281018264";
|
||||||
|
public static String NAME = "Janina";
|
||||||
|
public static String SURNAME = "Kowalska";
|
||||||
|
public static double PLN = 20.10;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UserEntityBuilder user() {
|
||||||
|
return new UserEntityBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class UserEntityBuilder {
|
||||||
|
|
||||||
|
String name;
|
||||||
|
String surname;
|
||||||
|
String pesel;
|
||||||
|
double pln;
|
||||||
|
double usd;
|
||||||
|
|
||||||
|
public UserEntityBuilder name(String name) {
|
||||||
|
this.name = name;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntityBuilder surname(String surname) {
|
||||||
|
this.surname = surname;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntityBuilder pesel(String pesel) {
|
||||||
|
this.pesel = pesel;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntityBuilder pln(double pln) {
|
||||||
|
this.pln = pln;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntityBuilder usd(double usd) {
|
||||||
|
this.usd = usd;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntity build() {
|
||||||
|
var nonnulPesel = Optional.ofNullable(pesel).orElse(Constants.NAME);
|
||||||
|
List<CurrencyEntity> currencies = new ArrayList<>();
|
||||||
|
if (pln > 0) {
|
||||||
|
currencies.add(new CurrencyEntity(nonnulPesel, "PLN", pln));
|
||||||
|
}
|
||||||
|
if (usd > 0) {
|
||||||
|
currencies.add(new CurrencyEntity(nonnulPesel, "USD", usd));
|
||||||
|
}
|
||||||
|
if (currencies.isEmpty()) {
|
||||||
|
currencies.add(new CurrencyEntity(nonnulPesel, "PLN", Constants.PLN));
|
||||||
|
}
|
||||||
|
return new UserEntity(
|
||||||
|
Optional.ofNullable(name).orElse(Constants.NAME),
|
||||||
|
Optional.ofNullable(surname).orElse(Constants.SURNAME),
|
||||||
|
nonnulPesel,
|
||||||
|
currencies
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
35
src/test/java/eu/ztsh/wymiana/RepositoryBasedTest.java
Normal file
35
src/test/java/eu/ztsh/wymiana/RepositoryBasedTest.java
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package eu.ztsh.wymiana;
|
||||||
|
|
||||||
|
import eu.ztsh.wymiana.data.entity.UserEntity;
|
||||||
|
import eu.ztsh.wymiana.data.repository.UserRepository;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
public abstract class RepositoryBasedTest {
|
||||||
|
|
||||||
|
protected final UserRepository userRepository;
|
||||||
|
|
||||||
|
public RepositoryBasedTest(UserRepository userRepository) {
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void prepareTest() {
|
||||||
|
userRepository.deleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void expect(UserEntity entity) {
|
||||||
|
assertThat(userRepository.findById(entity.getPesel()))
|
||||||
|
.isNotEmpty()
|
||||||
|
.get()
|
||||||
|
.usingRecursiveComparison()
|
||||||
|
.isEqualTo(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,49 +1,26 @@
|
||||||
package eu.ztsh.wymiana.data.repository;
|
package eu.ztsh.wymiana.data.repository;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.data.entity.CurrencyEntity;
|
import eu.ztsh.wymiana.EntityCreator;
|
||||||
import eu.ztsh.wymiana.data.entity.UserEntity;
|
import eu.ztsh.wymiana.RepositoryBasedTest;
|
||||||
import jakarta.transaction.Transactional;
|
import jakarta.transaction.Transactional;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
|
||||||
|
|
||||||
import java.util.List;
|
class UserRepositoryTest extends RepositoryBasedTest {
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
@ExtendWith(SpringExtension.class)
|
|
||||||
class UserRepositoryTest {
|
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
UserRepositoryTest(UserRepository userRepository) {
|
UserRepositoryTest(UserRepository userRepository) {
|
||||||
this.userRepository = userRepository;
|
super(userRepository);
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void prepareTest() {
|
|
||||||
userRepository.deleteAll();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
@DisplayName("Basic insert & get test")
|
@DisplayName("Basic insert & get test")
|
||||||
void basicTest() {
|
void basicTest() {
|
||||||
var pesel = "00281018264";
|
var entity = EntityCreator.user().build();
|
||||||
var entity = new UserEntity(pesel, "Janina", "Kowalska",
|
|
||||||
List.of(new CurrencyEntity(pesel, "PLN", 20.10)));
|
|
||||||
userRepository.save(entity);
|
userRepository.save(entity);
|
||||||
assertThat(userRepository.findById(pesel))
|
expect(entity);
|
||||||
.isNotEmpty()
|
|
||||||
.get()
|
|
||||||
.usingRecursiveComparison()
|
|
||||||
.isEqualTo(entity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue