From f467f3654aabe1b5bd85dcf59daab35bc5926a7c Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Fri, 18 Jan 2019 02:12:29 +0100 Subject: [PATCH] EventMeldung is generated. --- .../business/EventBusiness.java | 4 + .../business/EventMeldung.java | 61 ++++++ .../vaadinclubhelper/dao/AbstractDaoImpl.java | 6 +- .../vaadinclubhelper/data/Altersgruppe.java | 7 + .../vaadinclubhelper/data/BaseEntity.java | 7 +- .../vaadinclubhelper/data/ClubEvent.java | 7 +- .../vaadinclubhelper/data/EntityAccessor.java | 6 + .../vaadinclubhelper/data/Pflicht.java | 7 +- .../vaadinclubhelper/ui/EventDetails.java | 51 +++-- .../ui/components/EventAltersgruppen.java | 31 ++- .../ui/components/PersonGrid.java | 39 ++-- .../business/EventBusinessSpringTest.java | 168 ++++++++++++++++ .../business/EventBusinessTest.java | 185 +++++------------- .../ui/tests/TestAltersgruppen.java | 71 +++++++ .../ui/tests/TestPersonGenerator.java | 30 +++ 15 files changed, 503 insertions(+), 177 deletions(-) create mode 100644 src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventMeldung.java create mode 100644 src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/EntityAccessor.java create mode 100644 src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessSpringTest.java create mode 100644 src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/TestAltersgruppen.java create mode 100644 src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/TestPersonGenerator.java diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java index a5e5c6a..4b0c070 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java @@ -81,4 +81,8 @@ public class EventBusiness { altersgruppeDao.save(edited); dao.update(current); } + + public EventMeldung createMeldung() { + return new EventMeldung(current); + } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventMeldung.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventMeldung.java new file mode 100644 index 0000000..817f222 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventMeldung.java @@ -0,0 +1,61 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.business; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Altersgruppe; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; + +public class EventMeldung { + + private final ClubEvent event; + private final Map> groups; + + public EventMeldung(ClubEvent event) { + super(); + this.event = event; + groups = new HashMap<>(); + for (Person p : event.getPersons()) { + group(p); + } + } + + private void group(Person p) { + for (Altersgruppe g : event.getAltersgruppen()) { + if (g.isBetween(p.getBirth())) { + if (!groups.containsKey(g)) { + groups.put(g, new ArrayList<>()); + } + groups.get(g).add(p); + return; + } + } + throw new IllegalStateException("No Group found for " + p); + } + + public Map> getGroups() { + return Collections.unmodifiableMap(groups); + } + + @Override + public String toString() { + StringBuilder txt = new StringBuilder(); + txt.append("\n\n für den Wettkampf "); + txt.append(event.getCaption()).append(" am ").append(event.getStart().toLocalDate().toString()) + .append(" melden wir für den MTV Groß-Buchholz folgende Starter:"); + List groupList = new ArrayList<>(groups.keySet()); + groupList.sort((o1, o2) -> Integer.compare(o2.getStart(), o1.getStart())); + for (Altersgruppe g : groupList) { + txt.append("\n\n").append(g.getBezeichnung()); + for (Person p : groups.get(g)) { + txt.append("\n").append(p.getPrename()).append(" ").append(p.getSurname()).append("\t") + .append(p.getBirth().getYear()).append("\t").append(g.getPflicht().getName()); + } + } + return txt.toString(); + } +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDaoImpl.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDaoImpl.java index 775ded8..1754c79 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDaoImpl.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDaoImpl.java @@ -8,7 +8,9 @@ import javax.persistence.TypedQuery; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; -public abstract class AbstractDaoImpl implements IDao { +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.EntityAccessor; + +public abstract class AbstractDaoImpl implements IDao { @Autowired protected EntityManager em; @@ -24,7 +26,7 @@ public abstract class AbstractDaoImpl implements IDao { @Transactional public void save(T obj) { - if (em.contains(obj)) { + if (em.contains(obj) || obj.hasValidId()) { em.merge(obj); } else { em.persist(obj); diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Altersgruppe.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Altersgruppe.java index a0df821..6a9cf64 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Altersgruppe.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Altersgruppe.java @@ -1,6 +1,8 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.data; import java.io.Serializable; +import java.time.temporal.ChronoField; +import java.time.temporal.Temporal; import javax.persistence.Entity; import javax.persistence.Inheritance; @@ -119,4 +121,9 @@ public class Altersgruppe extends BaseEntity implements Serializable { + "]"; } + public boolean isBetween(Temporal startDate) { + int year = startDate.get(ChronoField.YEAR); + return year >= start && year <= end; + } + } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/BaseEntity.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/BaseEntity.java index 3cc3c67..d7abceb 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/BaseEntity.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/BaseEntity.java @@ -10,7 +10,7 @@ import javax.persistence.Temporal; import javax.persistence.TemporalType; @MappedSuperclass -public abstract class BaseEntity { +public abstract class BaseEntity implements EntityAccessor { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -57,6 +57,11 @@ public abstract class BaseEntity { this.deleted = new Date(deleted.getTime()); } + @Override + public boolean hasValidId() { + return id > 0; + } + @Override public int hashCode() { final int prime = 31; diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java index 1a67709..8703719 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java @@ -16,7 +16,7 @@ import org.vaadin.addon.calendar.item.BasicItem; // Entity must not be used, this class is persisted by ClubEvent.hbm.xml @Entity -public class ClubEvent extends BasicItem { +public class ClubEvent extends BasicItem implements EntityAccessor { private static final long serialVersionUID = -3600971939167437577L; @@ -196,4 +196,9 @@ public class ClubEvent extends BasicItem { } } + @Override + public boolean hasValidId() { + return id != null && !id.isBlank(); + } + } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/EntityAccessor.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/EntityAccessor.java new file mode 100644 index 0000000..1c3a373 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/EntityAccessor.java @@ -0,0 +1,6 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.data; + +public interface EntityAccessor { + + boolean hasValidId(); +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Pflicht.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Pflicht.java index 40aa9b5..bd7bb2f 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Pflicht.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Pflicht.java @@ -12,7 +12,7 @@ import javax.persistence.Table; @Table(name = "pflichten") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @NamedQuery(name = "Pflicht.findAll", query = "SELECT p FROM Pflicht p") -public class Pflicht extends BaseEntity implements Serializable { +public class Pflicht extends BaseEntity implements Serializable, Comparable { private static final long serialVersionUID = -1309514158086518524L; @@ -67,4 +67,9 @@ public class Pflicht extends BaseEntity implements Serializable { public String toString() { return name; } + + @Override + public int compareTo(Pflicht o) { + return Integer.compare(ordered, o.ordered); + } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/EventDetails.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/EventDetails.java index 018bafc..454d137 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/EventDetails.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/EventDetails.java @@ -5,8 +5,12 @@ import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent; import com.vaadin.ui.Button; import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.GridLayout; +import com.vaadin.ui.Notification; +import com.vaadin.ui.Notification.Type; +import com.vaadin.ui.VerticalLayout; import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.EventBusiness; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.EventMeldung; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.GroupDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PersonDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PflichtenDao; @@ -41,29 +45,44 @@ public class EventDetails extends GridLayout implements NamedView { @Override public void enter(ViewChangeEvent event) { - Navigator navigator = event.getNavigator(); currentEvent = eventBusiness.getCurrent(); + if (eventView == null) { - eventView = new SingleEventView(); - eventView.setEvent(currentEvent); + Navigator navigator = event.getNavigator(); - personGrid = new PersonGrid(groupDao, personDao); - personGrid.setEvent(currentEvent); - personGrid.setSelectedOnly(); - personGrid.hideFilter(); - personGrid.setSelectionMode(SelectionMode.NONE); + eventView = new SingleEventView(); + + personGrid = new PersonGrid(groupDao, personDao); + personGrid.hideFilter(); + personGrid.setSelectionMode(SelectionMode.NONE); + + eventAltersgruppen = new EventAltersgruppen(pflichtenDao, eventBusiness); - eventAltersgruppen = new EventAltersgruppen(pflichtenDao, eventBusiness); + Button back = new Button("Zurück"); + back.addClickListener(ev -> navigator.navigateTo(((NamedView) event.getOldView()).getViewName())); + Button createMeldung = new Button("Meldung"); + createMeldung.addClickListener(ev -> show(eventBusiness.createMeldung())); - Button back = new Button("Zurück"); - back.addClickListener(ev -> navigator.navigateTo(((NamedView) event.getOldView()).getViewName())); + VerticalLayout buttonLayout = new VerticalLayout(back, createMeldung); + buttonLayout.setMargin(true); + buttonLayout.setSpacing(true); + + addComponent(eventView, 0, 0); + addComponent(eventAltersgruppen, 1, 0); + addComponent(personGrid, 2, 0); + addComponent(buttonLayout, 0, 4, 2, 4); + setSizeFull(); + } else { + eventAltersgruppen.updateData(); + } + eventView.setEvent(currentEvent); + personGrid.setEvent(currentEvent); + } - addComponent(eventView, 0, 0); - addComponent(eventAltersgruppen, 1, 0); - addComponent(personGrid, 2, 0); - addComponent(back, 0, 4); - setSizeFull(); + private void show(EventMeldung createMeldung) { + Notification.show("Meldung für " + eventBusiness.getCurrent().getCaption(), createMeldung.toString(), + Type.HUMANIZED_MESSAGE); } @Override diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventAltersgruppen.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventAltersgruppen.java index 3e47ed4..e24931a 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventAltersgruppen.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/EventAltersgruppen.java @@ -1,9 +1,10 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; import java.time.LocalDateTime; -import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Optional; +import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,6 +35,7 @@ import com.vaadin.ui.components.grid.EditorSaveEvent; import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.EventBusiness; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PflichtenDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Altersgruppe; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Pflicht; public class EventAltersgruppen extends VerticalLayout { @@ -47,18 +49,18 @@ public class EventAltersgruppen extends VerticalLayout { private final Grid gruppen; private final ListDataProvider provider; private final Binder binder; - private final EventBusiness eventSupplier; + private final EventBusiness eventBusiness; public EventAltersgruppen(PflichtenDao pflichtenDao, EventBusiness eventBusiness) { setCaption("Altersklassen"); - this.eventSupplier = eventBusiness; + this.eventBusiness = eventBusiness; Button addButton = new Button("Hinzufügen"); addButton.addClickListener(ev -> addGruppe()); HorizontalLayout buttonLayout = new HorizontalLayout(addButton); - provider = DataProvider.ofCollection(new ArrayList()); + provider = DataProvider.ofCollection(eventBusiness.getCurrent().getAltersgruppen()); gruppen = new Grid<>(); gruppen.setDataProvider(provider); @@ -100,9 +102,9 @@ public class EventAltersgruppen extends VerticalLayout { private void gridRowSaved(EditorSaveEvent ev) { binder.validate(); - if (binder.isValid()) { - Altersgruppe edited = binder.getBean(); - validateAndStore(edited); + Altersgruppe bean = ev.getBean(); + if (binder.writeBeanIfValid(bean)) { + validateAndStore(bean); } provider.refreshAll(); } @@ -127,10 +129,21 @@ public class EventAltersgruppen extends VerticalLayout { LOGGER.trace("Changed: {}, value={}, old={}", componentId, value, oldValue); } + public void updateData() { + Collection items = provider.getItems(); + items.clear(); + ClubEvent current = eventBusiness.getCurrent(); + Set altersgruppen = current.getAltersgruppen(); + items.addAll(altersgruppen); + } + public void validateAndStore(Altersgruppe edited) { + if (edited == null) { + return; + } if (edited.getBezeichnung() != null && !edited.getBezeichnung().isBlank() && edited.getPflicht() != null && edited.getClubEvent() != null) { - eventSupplier.storeAltersgruppe(edited); + eventBusiness.storeAltersgruppe(edited); LOGGER.info("Stored: {}", edited); } } @@ -150,7 +163,7 @@ public class EventAltersgruppen extends VerticalLayout { private void addGruppe() { - Altersgruppe e = eventSupplier.createAltersgruppe(); + Altersgruppe e = eventBusiness.createAltersgruppe(); provider.getItems().add(e); binder.setBean(e); provider.refreshAll(); diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGrid.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGrid.java index 161bda9..7cec033 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGrid.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGrid.java @@ -3,6 +3,7 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.Set; import java.util.function.Consumer; @@ -24,9 +25,8 @@ import com.vaadin.ui.Grid.Column; import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Layout; -import com.vaadin.ui.MultiSelect; import com.vaadin.ui.VerticalLayout; -import com.vaadin.ui.components.grid.GridSelectionModel; +import com.vaadin.ui.components.grid.GridMultiSelect; import com.vaadin.ui.themes.ValoTheme; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.GroupDao; @@ -58,6 +58,7 @@ public class PersonGrid extends VerticalLayout { private Column startpassColumn; private Column editButtonColumn; private Layout filters; + private SelectionMode currentSelectionMode; public PersonGrid(GroupDao groupDao, PersonDao personDao) { @@ -104,8 +105,9 @@ public class PersonGrid extends VerticalLayout { editButtonColumn.setHidden(true); } - public GridSelectionModel setSelectionMode(SelectionMode selectionMode) { - return grid.setSelectionMode(selectionMode); + public void setSelectionMode(SelectionMode selectionMode) { + grid.setSelectionMode(selectionMode); + currentSelectionMode = selectionMode; } private Layout setupFilterComponents() { @@ -166,13 +168,15 @@ public class PersonGrid extends VerticalLayout { } private void selectItems(Person... items) { - MultiSelect asMultiSelect = grid.asMultiSelect(); - asMultiSelect.deselectAll(); - if (items == null || items.length == 0) { - log.debug("No Persons selected."); - } else { - log.debug("Selecting Persons: {}", Arrays.asList(items)); - asMultiSelect.select(items); + if (currentSelectionMode == SelectionMode.MULTI) { + GridMultiSelect asMultiSelect = grid.asMultiSelect(); + asMultiSelect.deselectAll(); + if (items == null || items.length == 0) { + log.debug("No Persons selected."); + } else { + log.debug("Selecting Persons: {}", Arrays.asList(items)); + asMultiSelect.selectItems(items); + } } } @@ -209,13 +213,18 @@ public class PersonGrid extends VerticalLayout { public void setEvent(ClubEvent ev) { - if (ev != null) { - updateSelection(ev); + if (currentSelectionMode == SelectionMode.MULTI) { + if (ev != null) { + updateSelection(ev); + } else { + selectItems(new Person[0]); + } } else { - selectItems(new Person[0]); + Collection items = dataProvider.getItems(); + items.clear(); + items.addAll(ev.getPersons()); } this.currentEvent = ev; -// updateFilter(); } public void updateSelection(ClubEvent ev) { diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessSpringTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessSpringTest.java new file mode 100644 index 0000000..f4fd9db --- /dev/null +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessSpringTest.java @@ -0,0 +1,168 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.business; + +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.function.Consumer; + +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import de.kreth.vaadin.clubhelper.HibernateHolder; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.AbstractDatabaseTest; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.AbstractDatabaseTest.DB_TYPE; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.ClubEventDao; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.ClubEventDaoImpl; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubeventHasPerson; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; + +@ExtendWith(SpringExtension.class) +class EventBusinessSpringTest { + + EventBusiness eventBusiness; + + private List persons; + private ClubEvent event; + + @Autowired + private EventBusiness business; + + @Autowired + private EntityManager entityManager; + + private TypedQuery all; +// +// @Autowired +// private InnerConfig innerConfig; + + @Configuration + public static class InnerConfig { + + private SessionFactory sessionFactory; + + public InnerConfig() { + + org.hibernate.cfg.Configuration configuration = HibernateHolder.configuration(); + + sessionFactory = configuration.buildSessionFactory(); + } + + @Bean + public EntityManager getEntityManager() { + return sessionFactory.openSession(); + } + + @Bean + public ClubEventDao getClubEventDao() { + return new ClubEventDaoImpl(); + } + + @Bean + public EventBusiness getEventBusiness() { + return new EventBusiness(); + } + + } + + @BeforeEach + void setUp() throws Exception { + insertTestData(); + business.setSelected(event); + + all = entityManager.createQuery("from ClubeventHasPerson", ClubeventHasPerson.class); + } + + @AfterEach + void shutdown() { + entityManager.clear(); + ((Session) entityManager).doWork(conn -> AbstractDatabaseTest.cleanDatabase(conn, DB_TYPE.H2)); +// entityManager.flush(); + } + + private void insertTestData() { + persons = new ArrayList<>(); + + entityManager.getTransaction().begin(); + for (int i = 0; i < 3; i++) { + + Person p = new Person(); + p.setPrename("prename_" + i); + p.setSurname("surname_" + i); + p.setBirth(LocalDate.now()); + entityManager.persist(p); + persons.add(p); + } + event = AbstractDatabaseTest.creteEvent(); + assertNull(event.getPersons()); + entityManager.persist(event); + entityManager.getTransaction().commit(); + } + + @Test + void testDataCorrectlyCreated() { + + assertEquals(0, all.getResultList().size()); + + List stored = entityManager.createNamedQuery(Person.QUERY_FINDALL, Person.class).getResultList(); + assertEquals(3, stored.size()); + + List events = business.loadEvents(); + assertEquals(1, events.size()); +// assertNotNull(events.get(0).getPersons()); + + } + + @Test + @Disabled + void testAddPersonsToEvent() { + assertEquals(0, all.getResultList().size()); + + entityManager.getTransaction().begin(); + business.changePersons(new HashSet<>(persons.subList(0, 1))); + entityManager.getTransaction().commit(); + + entityManager.getTransaction().begin(); + business.changePersons(new HashSet<>(persons.subList(0, 2))); + entityManager.getTransaction().commit(); + + List result = all.getResultList(); + assertEquals(2, result.size()); + } + + class DatabaseHelper extends AbstractDatabaseTest { + public DatabaseHelper(EntityManager em) { + this((Session) em); + } + + public DatabaseHelper(Session session) { + this.session = session; + } + + @Override + public void transactional(Runnable r) { + super.transactional(r); + } + + @Override + public void transactional(Consumer r) { + super.transactional(r); + } + } +} diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessTest.java index 1bb2310..884d7fa 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessTest.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessTest.java @@ -1,168 +1,89 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.business; -import static org.junit.Assert.assertNull; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.time.LocalDate; -import java.util.ArrayList; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Arrays; import java.util.HashSet; import java.util.List; -import java.util.function.Consumer; -import javax.persistence.EntityManager; -import javax.persistence.TypedQuery; - -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -import de.kreth.vaadin.clubhelper.HibernateHolder; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.AbstractDatabaseTest; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.AbstractDatabaseTest.DB_TYPE; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.AltersgruppeDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.ClubEventDao; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.ClubEventDaoImpl; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Altersgruppe; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; -import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubeventHasPerson; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEventBuilder; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.tests.TestAltersgruppen; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.tests.TestPersonGenerator; -@ExtendWith(SpringExtension.class) class EventBusinessTest { EventBusiness eventBusiness; - private List persons; - private ClubEvent event; - - @Autowired - private EventBusiness business; - - @Autowired - private EntityManager entityManager; - - private TypedQuery all; -// -// @Autowired -// private InnerConfig innerConfig; - - @Configuration - public static class InnerConfig { - - private SessionFactory sessionFactory; - - public InnerConfig() { - - org.hibernate.cfg.Configuration configuration = HibernateHolder.configuration(); + ZonedDateTime startDate = ZonedDateTime.of(LocalDateTime.of(2019, 1, 1, 0, 0), ZoneId.systemDefault()); - sessionFactory = configuration.buildSessionFactory(); - } + @Mock + ClubEventDao dao; - @Bean - public EntityManager getEntityManager() { - return sessionFactory.openSession(); - } - - @Bean - public ClubEventDao getClubEventDao() { - return new ClubEventDaoImpl(); - } - - @Bean - public EventBusiness getEventBusiness() { - return new EventBusiness(); - } - - } + @Mock + AltersgruppeDao altersgruppeDao; @BeforeEach void setUp() throws Exception { - insertTestData(); - business.setSelected(event); - - all = entityManager.createQuery("from ClubeventHasPerson", ClubeventHasPerson.class); - } - - @AfterEach - void shutdown() { - entityManager.clear(); - ((Session) entityManager).doWork(conn -> AbstractDatabaseTest.cleanDatabase(conn, DB_TYPE.H2)); -// entityManager.flush(); - } - - private void insertTestData() { - persons = new ArrayList<>(); - - entityManager.getTransaction().begin(); - for (int i = 0; i < 3; i++) { - - Person p = new Person(); - p.setPrename("prename_" + i); - p.setSurname("surname_" + i); - p.setBirth(LocalDate.now()); - entityManager.persist(p); - persons.add(p); - } - event = AbstractDatabaseTest.creteEvent(); - assertNull(event.getPersons()); - entityManager.persist(event); - entityManager.getTransaction().commit(); + MockitoAnnotations.initMocks(this); + eventBusiness = new EventBusiness(); + eventBusiness.dao = dao; + eventBusiness.altersgruppeDao = altersgruppeDao; } @Test - void testDataCorrectlyCreated() { - - assertEquals(0, all.getResultList().size()); + void testCreateMeldung() { + ClubEvent ev = new ClubEventBuilder().withId("id").withiCalUID("iCalUID").withLocation("location") + .withCaption("caption").withDescription("description").withOrganizerDisplayName("organizerDisplayName") + .withStart(startDate).withEnd(startDate).withAllDay(true).build(); + + List altersgruppen = TestAltersgruppen.getAltersgruppen(); + List personen = TestPersonGenerator.generatePersonen(10); + int count = 1; + for (Person p : personen) { + p.setEvents(new HashSet<>(Arrays.asList(ev))); + p.setBirth(LocalDate.of(1993 + (count * 3), count, count + 2)); + count++; + } - List stored = entityManager.createNamedQuery(Person.QUERY_FINDALL, Person.class).getResultList(); - assertEquals(3, stored.size()); + ev.setAltersgruppen(new HashSet<>(altersgruppen)); + ev.setPersons(new HashSet<>(personen)); - List events = business.loadEvents(); - assertEquals(1, events.size()); -// assertNotNull(events.get(0).getPersons()); + eventBusiness.setSelected(ev); + EventMeldung meldung = eventBusiness.createMeldung(); + assertNotNull(meldung); } @Test - @Disabled - void testAddPersonsToEvent() { - assertEquals(0, all.getResultList().size()); + void testAltersgruppeBetween() { + Altersgruppe gruppe = new Altersgruppe(); + gruppe.setBezeichnung("bezeichnung"); + gruppe.setStart(2000); + gruppe.setEnd(2005); - entityManager.getTransaction().begin(); - business.changePersons(new HashSet<>(persons.subList(0, 1))); - entityManager.getTransaction().commit(); + assertFalse(gruppe.isBetween(startDate)); + assertTrue(gruppe.isBetween(ZonedDateTime.of(LocalDateTime.of(2000, 1, 1, 0, 0), ZoneId.systemDefault()))); + assertTrue(gruppe.isBetween(ZonedDateTime.of(LocalDateTime.of(2005, 12, 31, 23, 59), ZoneId.systemDefault()))); + assertTrue(gruppe.isBetween(ZonedDateTime.of(LocalDateTime.of(2002, 1, 1, 0, 0), ZoneId.systemDefault()))); - entityManager.getTransaction().begin(); - business.changePersons(new HashSet<>(persons.subList(0, 2))); - entityManager.getTransaction().commit(); + assertFalse(gruppe.isBetween(ZonedDateTime.of(LocalDateTime.of(1999, 12, 31, 23, 59), ZoneId.systemDefault()))); + assertFalse(gruppe.isBetween(ZonedDateTime.of(LocalDateTime.of(2006, 1, 1, 0, 0), ZoneId.systemDefault()))); - List result = all.getResultList(); - assertEquals(2, result.size()); - } - - class DatabaseHelper extends AbstractDatabaseTest { - public DatabaseHelper(EntityManager em) { - this((Session) em); - } - - public DatabaseHelper(Session session) { - this.session = session; - } - - @Override - public void transactional(Runnable r) { - super.transactional(r); - } - - @Override - public void transactional(Consumer r) { - super.transactional(r); - } } } diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/TestAltersgruppen.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/TestAltersgruppen.java new file mode 100644 index 0000000..47ccc15 --- /dev/null +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/TestAltersgruppen.java @@ -0,0 +1,71 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.tests; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Altersgruppe; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Pflicht; + +public class TestAltersgruppen { + + static final AtomicInteger idGenerator = new AtomicInteger(1); + + public static List getAltersgruppen() { + List pflichten = TestPflichten.getFixedPflichten(); + Collections.sort(pflichten); + + List gruppen = new ArrayList<>(); + + Altersgruppe g = new Altersgruppe(); + g.setBezeichnung("Schüler/innen Jahrg. 2013 und jünger"); + g.setId(idGenerator.getAndIncrement()); + g.setStart(2013); + g.setEnd(2099); + g.setPflicht(pflichten.get(g.getId())); + gruppen.add(g); + + g = new Altersgruppe(); + g.setBezeichnung("Schüler/innen Jahrg. 2010 - 2012"); + g.setId(idGenerator.getAndIncrement()); + g.setStart(2010); + g.setEnd(2012); + g.setPflicht(pflichten.get(g.getId())); + gruppen.add(g); + + g = new Altersgruppe(); + g.setBezeichnung("Schüler/innen Jahrg. 2004 und 2010"); + g.setId(idGenerator.getAndIncrement()); + g.setStart(2004); + g.setEnd(2010); + g.setPflicht(pflichten.get(g.getId())); + gruppen.add(g); + + g = new Altersgruppe(); + g.setBezeichnung("Schüler/innen Jahrg. 2000 und 2003"); + g.setId(idGenerator.getAndIncrement()); + g.setStart(2000); + g.setEnd(2003); + g.setPflicht(pflichten.get(g.getId())); + gruppen.add(g); + + g = new Altersgruppe(); + g.setBezeichnung("Heranwachsene Jahrg. 1994 bis 1999"); + g.setId(idGenerator.getAndIncrement()); + g.setStart(1994); + g.setEnd(1999); + g.setPflicht(pflichten.get(g.getId())); + gruppen.add(g); + + g = new Altersgruppe(); + g.setBezeichnung("Turner/innen Jahrg. 1900 und 1993"); + g.setId(idGenerator.getAndIncrement()); + g.setStart(1900); + g.setEnd(1993); + g.setPflicht(pflichten.get(g.getId())); + gruppen.add(g); + + return gruppen; + } +} diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/TestPersonGenerator.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/TestPersonGenerator.java new file mode 100644 index 0000000..fc64142 --- /dev/null +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/TestPersonGenerator.java @@ -0,0 +1,30 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.tests; + +import java.util.ArrayList; +import java.util.List; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Startpass; + +public class TestPersonGenerator { + + public static List generatePersonen(int count) { + List personen = new ArrayList<>(); + for (int i = 0; i < count; i++) { + Person p = new Person(); + p.setId(i); + p.setPrename("prename_" + i); + p.setSurname("surname_" + i); + p.setUsername("username_" + i); + p.setPassword("password_" + i); + + Startpass sp = new Startpass(); + sp.setStartpassNr("startpassNr_" + i); + sp.setId(i); + sp.setPerson(p); + p.setStartpass(sp); + personen.add(p); + } + return personen; + } +}