feat: POJO mappers
This commit is contained in:
parent
b947f95305
commit
3121caf8f7
3 changed files with 68 additions and 0 deletions
24
src/main/java/eu/ztsh/wymiana/util/CurrencyMapper.java
Normal file
24
src/main/java/eu/ztsh/wymiana/util/CurrencyMapper.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
package eu.ztsh.wymiana.util;
|
||||
|
||||
import eu.ztsh.wymiana.data.entity.CurrencyEntity;
|
||||
import eu.ztsh.wymiana.model.Currency;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CurrencyMapper {
|
||||
|
||||
public static Currency entityToPojo(CurrencyEntity entity) {
|
||||
return new Currency(entity.getSymbol(), entity.getAmount());
|
||||
}
|
||||
|
||||
public static Map<String, Currency> entitiesToPojoMap(List<CurrencyEntity> values) {
|
||||
return values.stream().map(CurrencyMapper::entityToPojo)
|
||||
.collect(Collectors.toMap(Currency::symbol, pojo -> pojo));
|
||||
}
|
||||
|
||||
private CurrencyMapper() {
|
||||
}
|
||||
|
||||
}
|
16
src/main/java/eu/ztsh/wymiana/util/UserMapper.java
Normal file
16
src/main/java/eu/ztsh/wymiana/util/UserMapper.java
Normal file
|
@ -0,0 +1,16 @@
|
|||
package eu.ztsh.wymiana.util;
|
||||
|
||||
import eu.ztsh.wymiana.data.entity.UserEntity;
|
||||
import eu.ztsh.wymiana.model.User;
|
||||
|
||||
public class UserMapper {
|
||||
|
||||
public static User entityToPojo(UserEntity entity) {
|
||||
return new User(entity.getName(), entity.getSurname(), entity.getPesel(),
|
||||
CurrencyMapper.entitiesToPojoMap(entity.getCurrencies()));
|
||||
}
|
||||
|
||||
private UserMapper() {
|
||||
}
|
||||
|
||||
}
|
28
src/test/java/eu/ztsh/wymiana/util/UserMapperTest.java
Normal file
28
src/test/java/eu/ztsh/wymiana/util/UserMapperTest.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
package eu.ztsh.wymiana.util;
|
||||
|
||||
import eu.ztsh.wymiana.EntityCreator;
|
||||
import eu.ztsh.wymiana.model.Currency;
|
||||
import eu.ztsh.wymiana.model.User;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class UserMapperTest {
|
||||
|
||||
@Test
|
||||
void entityToPojoTest() {
|
||||
var entity = EntityCreator.user().build();
|
||||
var expected = new User(
|
||||
EntityCreator.Constants.NAME,
|
||||
EntityCreator.Constants.SURNAME,
|
||||
EntityCreator.Constants.PESEL,
|
||||
Map.of("PLN", new Currency("PLN", EntityCreator.Constants.PLN))
|
||||
);
|
||||
assertThat(UserMapper.entityToPojo(entity))
|
||||
.usingRecursiveComparison()
|
||||
.isEqualTo(expected);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue