feat: POJO mappers

This commit is contained in:
Piotr Dec 2024-05-22 19:13:43 +02:00
parent b947f95305
commit 3121caf8f7
Signed by: stawros
GPG key ID: F89F27AD8F881A91
3 changed files with 68 additions and 0 deletions

View 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() {
}
}

View 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() {
}
}

View 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);
}
}