parent
b95d79b1cf
commit
f467f3654a
@ -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<Altersgruppe, List<Person>> 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<Altersgruppe, List<Person>> 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<Altersgruppe> 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(); |
||||
} |
||||
} |
||||
@ -0,0 +1,6 @@ |
||||
package de.kreth.vaadin.clubhelper.vaadinclubhelper.data; |
||||
|
||||
public interface EntityAccessor { |
||||
|
||||
boolean hasValidId(); |
||||
} |
||||
@ -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<Person> persons; |
||||
private ClubEvent event; |
||||
|
||||
@Autowired |
||||
private EventBusiness business; |
||||
|
||||
@Autowired |
||||
private EntityManager entityManager; |
||||
|
||||
private TypedQuery<ClubeventHasPerson> 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<Person> stored = entityManager.createNamedQuery(Person.QUERY_FINDALL, Person.class).getResultList(); |
||||
assertEquals(3, stored.size()); |
||||
|
||||
List<ClubEvent> 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<ClubeventHasPerson> 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<Session> r) { |
||||
super.transactional(r); |
||||
} |
||||
} |
||||
} |
||||
@ -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<Person> persons; |
||||
private ClubEvent event; |
||||
|
||||
@Autowired |
||||
private EventBusiness business; |
||||
|
||||
@Autowired |
||||
private EntityManager entityManager; |
||||
|
||||
private TypedQuery<ClubeventHasPerson> 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()); |
||||
|
||||
List<Person> stored = entityManager.createNamedQuery(Person.QUERY_FINDALL, Person.class).getResultList(); |
||||
assertEquals(3, stored.size()); |
||||
|
||||
List<ClubEvent> events = business.loadEvents(); |
||||
assertEquals(1, events.size()); |
||||
// assertNotNull(events.get(0).getPersons());
|
||||
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<Altersgruppe> altersgruppen = TestAltersgruppen.getAltersgruppen(); |
||||
List<Person> 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++; |
||||
} |
||||
|
||||
@Test |
||||
@Disabled |
||||
void testAddPersonsToEvent() { |
||||
assertEquals(0, all.getResultList().size()); |
||||
|
||||
entityManager.getTransaction().begin(); |
||||
business.changePersons(new HashSet<>(persons.subList(0, 1))); |
||||
entityManager.getTransaction().commit(); |
||||
ev.setAltersgruppen(new HashSet<>(altersgruppen)); |
||||
ev.setPersons(new HashSet<>(personen)); |
||||
|
||||
entityManager.getTransaction().begin(); |
||||
business.changePersons(new HashSet<>(persons.subList(0, 2))); |
||||
entityManager.getTransaction().commit(); |
||||
eventBusiness.setSelected(ev); |
||||
EventMeldung meldung = eventBusiness.createMeldung(); |
||||
assertNotNull(meldung); |
||||
|
||||
List<ClubeventHasPerson> result = all.getResultList(); |
||||
assertEquals(2, result.size()); |
||||
} |
||||
|
||||
class DatabaseHelper extends AbstractDatabaseTest { |
||||
public DatabaseHelper(EntityManager em) { |
||||
this((Session) em); |
||||
} |
||||
@Test |
||||
void testAltersgruppeBetween() { |
||||
Altersgruppe gruppe = new Altersgruppe(); |
||||
gruppe.setBezeichnung("bezeichnung"); |
||||
gruppe.setStart(2000); |
||||
gruppe.setEnd(2005); |
||||
|
||||
public DatabaseHelper(Session session) { |
||||
this.session = session; |
||||
} |
||||
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()))); |
||||
|
||||
@Override |
||||
public void transactional(Runnable r) { |
||||
super.transactional(r); |
||||
} |
||||
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()))); |
||||
|
||||
@Override |
||||
public void transactional(Consumer<Session> r) { |
||||
super.transactional(r); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -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<Altersgruppe> getAltersgruppen() { |
||||
List<Pflicht> pflichten = TestPflichten.getFixedPflichten(); |
||||
Collections.sort(pflichten); |
||||
|
||||
List<Altersgruppe> 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; |
||||
} |
||||
} |
||||
@ -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<Person> generatePersonen(int count) { |
||||
List<Person> 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; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue