Completed mapper tests
This commit is contained in:
@@ -18,6 +18,6 @@ public class EventEntity {
|
|||||||
|
|
||||||
@Column
|
@Column
|
||||||
@OneToMany(targetEntity= DateEntity.class, cascade=ALL, mappedBy="event")
|
@OneToMany(targetEntity= DateEntity.class, cascade=ALL, mappedBy="event")
|
||||||
public Set<DateEntity> timeStamps;
|
public Set<DateEntity> dates;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public class EventMapper implements Mapper<Event, EventEntity> {
|
|||||||
public EventEntity toEntity(Event event) {
|
public EventEntity toEntity(Event event) {
|
||||||
EventEntity ee = new EventEntity();
|
EventEntity ee = new EventEntity();
|
||||||
ee.name = event.getName();
|
ee.name = event.getName();
|
||||||
ee.timeStamps = dateMapper.toEntities(event.getDates(), HashSet::new);
|
ee.dates = dateMapper.toEntities(event.getDates(), HashSet::new);
|
||||||
return ee;
|
return ee;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,12 +31,16 @@ public class EventMapper implements Mapper<Event, EventEntity> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <C extends Collection<EventEntity>> C toEntities(Collection<Event> events, Supplier<C> collectionSupplier) {
|
public <C extends Collection<EventEntity>> C toEntities(Collection<Event> events, Supplier<C> collectionSupplier) {
|
||||||
return null;
|
C result = collectionSupplier.get();
|
||||||
|
events.forEach(e -> result.add(toEntity(e)));
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <C extends Collection<Event>> C toModels(Collection<EventEntity> eventEntities, Supplier<C> collectionSupplier) {
|
public <C extends Collection<Event>> C toModels(Collection<EventEntity> eventEntities, Supplier<C> collectionSupplier) {
|
||||||
return null;
|
C result = collectionSupplier.get();
|
||||||
|
eventEntities.forEach(e -> result.add(toModel(e)));
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,6 @@ spring.datasource.password=${DB_PASSWORD}
|
|||||||
|
|
||||||
spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect
|
spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect
|
||||||
spring.jpa.show-sql=true
|
spring.jpa.show-sql=true
|
||||||
spring.jpa.hibernate.ddl-auto=update
|
spring.jpa.hibernate.ddl-auto=create-drop
|
||||||
spring.jpa.defer-datasource-initialization=true
|
spring.jpa.defer-datasource-initialization=true
|
||||||
spring.user.datasource.driver-class-name=com.mysql.jdbc.Driver
|
spring.user.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||||
|
|||||||
@@ -1,16 +1,67 @@
|
|||||||
package be.naaturel.letsmeet.mappers;
|
package be.naaturel.letsmeet.mappers;
|
||||||
|
|
||||||
import be.naaturel.letsmeet.entities.DateEntity;
|
import be.naaturel.letsmeet.entities.DateEntity;
|
||||||
|
import be.naaturel.letsmeet.entities.EventEntity;
|
||||||
import be.naaturel.letsmeet.models.Date;
|
import be.naaturel.letsmeet.models.Date;
|
||||||
|
import be.naaturel.letsmeet.models.Event;
|
||||||
|
import be.naaturel.letsmeet.models.Participant;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.*;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public class EventMapperTest {
|
public class EventMapperTest {
|
||||||
|
private static final Mapper<Event, EventEntity> mapper = new EventMapper();
|
||||||
|
|
||||||
|
private static final Map<Date, Set<Participant>> participants = Map.of(new Date(0), Set.of(new Participant("Participant 1"), new Participant("Participant 2")),
|
||||||
|
new Date(1), Set.of(new Participant("Participant 3"), new Participant("Participant 4")),
|
||||||
|
new Date(2), Set.of(new Participant("Participant 5"), new Participant("Participant 6")));
|
||||||
|
|
||||||
|
private static final Event model = new Event("Event for tests", participants);
|
||||||
|
|
||||||
|
private static final List<Event> models = List.of(model, model, model);
|
||||||
|
|
||||||
|
private static final EventEntity entity = new EventEntity();
|
||||||
|
private static final List<EventEntity> entities = new ArrayList<>();
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void setup(){
|
||||||
|
entity.id = "BLA BLA BLA FAKE ID";
|
||||||
|
entity.name = "Event entity for tests";
|
||||||
|
entity.dates = Set.of(new DateEntity(), new DateEntity(), new DateEntity());
|
||||||
|
|
||||||
|
entities.add(entity);
|
||||||
|
entities.add(entity);
|
||||||
|
entities.add(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void single_model_to_entity() {
|
||||||
|
EventEntity converted = mapper.toEntity(model);
|
||||||
|
assertEquals(converted.name, model.getName());
|
||||||
|
assertEquals(converted.dates.size(), model.getDates().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void single_entity_to_model() {
|
||||||
|
Event converted = mapper.toModel(entity);
|
||||||
|
assertEquals(converted.getName(), entity.name);
|
||||||
|
//assertEquals(converted.getDates().size(), entity.dates.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void multiple_entities_to_models() {
|
||||||
|
List<Event> events = mapper.toModels(entities, ArrayList::new);
|
||||||
|
assertEquals(events.size(), entities.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void multiple_models_to_entities() {
|
||||||
|
List<EventEntity> eventEntities = mapper.toEntities(models, ArrayList::new);
|
||||||
|
assertEquals(eventEntities.size(), models.size());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user