From 2302062176dc4fdaecf986c353edc9207ab7a693 Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Tue, 18 Dec 2018 17:34:59 +0100 Subject: [PATCH] PersonView is opened in Test. --- .../vaadinclubhelper/ui/MainUi.java | 12 +- .../ui/components/CalendarComponent.java | 1 + .../ui/components/PersonGrid.java | 28 +++- .../VaadinClubhelperApplicationTests.java | 152 +++++++++++++++--- 4 files changed, 155 insertions(+), 38 deletions(-) diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java index d30d94a..33280df 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java @@ -112,14 +112,14 @@ public class MainUi extends UI { ClubEvent ev = (ClubEvent) event.getCalendarItem(); LOGGER.debug("Opening detail view for {}", ev); -// contentLayout.removeComponent(personGrid); -// contentLayout.addComponent(personGrid); + contentLayout.removeComponent(personGrid); + contentLayout.addComponent(personGrid); eventBusiness.setSelected(null); -// personGrid.setEnabled(false); -// personGrid.setEvent(ev); -// personGrid.setVisible(true); -// personGrid.setEnabled(true); + personGrid.setEnabled(false); + personGrid.setEvent(ev); + personGrid.setEnabled(true); + personGrid.setVisible(true); eventBusiness.setSelected(ev); } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java index 8548c17..880d402 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/CalendarComponent.java @@ -71,6 +71,7 @@ public class CalendarComponent extends CustomComponent { dataProvider = new ClubEventProvider(); calendar = new Calendar<>(dataProvider).withMonth(Month.from(LocalDateTime.now())); calendar.setId("calendar.calendar"); + calendar.setCaption("Events"); calendar.setSizeFull(); calendar.addListener(ev -> calendarEvent(ev)); 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 2018b8a..2ae1872 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 @@ -61,7 +61,7 @@ public class PersonGrid extends CustomComponent { public PersonGrid(GroupDao groupDao) { textTitle = new TextField(); - textTitle.setId("person.title"); + textTitle.setId("event.title"); textTitle.setStyleName("title_label"); textTitle.setCaption("Veranstaltung"); textTitle.setEnabled(false); @@ -165,10 +165,14 @@ public class PersonGrid extends CustomComponent { } private void selectItems(Person... items) { - log.debug("Selecting Persons: {}", Arrays.asList(items)); MultiSelect asMultiSelect = grid.asMultiSelect(); asMultiSelect.deselectAll(); - asMultiSelect.select(items); + if (items == null || items.length == 0) { + log.debug("No Persons selected."); + } else { + log.debug("Selecting Persons: {}", Arrays.asList(items)); + asMultiSelect.select(items); + } } public void deselectItems(Person... items) { @@ -217,9 +221,21 @@ public class PersonGrid extends CustomComponent { } public void setEvent(ClubEvent ev) { - setCaption(ev.getCaption()); - setTitle(ev.getCaption()); - selectItems(ev.getPersons().toArray(new Person[0])); + if (ev != null) { + + setCaption(ev.getCaption()); + setTitle(ev.getCaption()); + + Set persons = ev.getPersons(); + if (persons != null) { + selectItems(persons.toArray(new Person[0])); + } else { + selectItems(new Person[0]); + } + } else { + setCaption(""); + setTitle(""); + } } } diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/VaadinClubhelperApplicationTests.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/VaadinClubhelperApplicationTests.java index 5d9de56..228f8c2 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/VaadinClubhelperApplicationTests.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/VaadinClubhelperApplicationTests.java @@ -5,17 +5,27 @@ import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import java.awt.GraphicsEnvironment; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; import java.time.YearMonth; import java.time.ZonedDateTime; import java.time.format.TextStyle; import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; +import javax.persistence.TypedQuery; import org.hamcrest.Matchers; +import org.hibernate.Session; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -31,6 +41,7 @@ import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWeb import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.server.LocalServerPort; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.business.CalendarTaskRefresher; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEventBuilder; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; @@ -41,6 +52,9 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; public class VaadinClubhelperApplicationTests { private static ChromeOptions options; + + private static final AtomicInteger idCount = new AtomicInteger(); + @LocalServerPort int port; @@ -49,6 +63,7 @@ public class VaadinClubhelperApplicationTests { private WebDriver driver; private ZonedDateTime now; + private WebDriverWait driverWait; @BeforeAll static void setupDriverConfiguration() { @@ -60,28 +75,44 @@ public class VaadinClubhelperApplicationTests { options = new ChromeOptions(); options.setHeadless(GraphicsEnvironment.isHeadless()); + System.setProperty(CalendarTaskRefresher.SKIP_EVENT_UPDATE, Boolean.TRUE.toString()); } @BeforeEach void setUp() throws Exception { now = ZonedDateTime.now(); + insertDataIntoDatabase(); + + driver = new ChromeDriver(options); + driver.manage().window().maximize(); + driver.manage().timeouts().implicitlyWait(5L, TimeUnit.SECONDS); + driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS); + driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS); + driverWait = new WebDriverWait(driver, 45L); + } + + public void insertDataIntoDatabase() { + TypedQuery query = em.createQuery("FROM groupDef", GroupDef.class); + if (!query.getResultList().isEmpty()) { + return; + } EntityTransaction tx = em.getTransaction(); tx.begin(); - GroupDef g1 = new GroupDef(); - g1.setName("ADMIN"); - GroupDef g2 = new GroupDef(); - g1.setName("Wettkämpfer"); - GroupDef g3 = new GroupDef(); - g1.setName("ACTIVE"); + GroupDef adminGroup = new GroupDef(); + adminGroup.setName("ADMIN"); + GroupDef competitorGroup = new GroupDef(); + competitorGroup.setName("Wettkämpfer"); + GroupDef participantGroup = new GroupDef(); + participantGroup.setName("ACTIVE"); - em.persist(g1); - em.persist(g2); - em.persist(g3); + em.persist(adminGroup); + em.persist(competitorGroup); + em.persist(participantGroup); - ClubEvent ev = new ClubEventBuilder().withAllDay(true).withId("id").withCaption("caption") - .withDescription("description").withiCalUID("iCalUID").withLocation("location") + ClubEvent ev = new ClubEventBuilder().withAllDay(true).withId("id" + idCount.incrementAndGet()) + .withCaption("caption").withDescription("description").withiCalUID("iCalUID").withLocation("location") .withOrganizerDisplayName("mtv_allgemein").withStart(now).withEnd(now.plusDays(2)).build(); em.persist(ev); @@ -98,26 +129,40 @@ public class VaadinClubhelperApplicationTests { holidayEnd = now.plusDays(30); } - ClubEvent holiday = new ClubEventBuilder().withAllDay(true).withId("holiday").withCaption("holiday") - .withDescription("holiday").withiCalUID("iCalUID").withLocation("") + ClubEvent holiday = new ClubEventBuilder().withAllDay(true).withId("holiday" + idCount.incrementAndGet()) + .withCaption("holiday").withDescription("holiday").withiCalUID("iCalUID").withLocation("") .withOrganizerDisplayName("Schulferien").withStart(holidayStart).withEnd(holidayEnd).build(); em.persist(holiday); Person p = new Person(); p.setPrename("prename"); - p.setSurname("surname"); + p.setSurname("Allgroups"); p.setBirth(now.minusYears(13).toLocalDate()); - p.setUsername("username"); + p.setUsername("Allgroups"); p.setPassword("password"); + p.setPersongroups(Arrays.asList(adminGroup, competitorGroup, participantGroup)); + em.persist(p); - List persongroups = Arrays.asList(g1, g2, g3); - p.setPersongroups(persongroups); + p = new Person(); + p.setPrename("prename"); + p.setSurname("participant"); + p.setBirth(now.minusYears(10).toLocalDate()); + p.setUsername("Allgroups"); + p.setPassword("password"); + p.setPersongroups(Arrays.asList(participantGroup)); em.persist(p); - tx.commit(); + p = new Person(); + p.setPrename("prename"); + p.setSurname("competitor"); + p.setBirth(now.minusYears(10).toLocalDate()); + p.setUsername("Allgroups"); + p.setPassword("password"); + p.setPersongroups(Arrays.asList(competitorGroup, participantGroup)); + em.persist(p); - driver = new ChromeDriver(options); + tx.commit(); } @AfterEach @@ -125,15 +170,38 @@ public class VaadinClubhelperApplicationTests { if (driver != null) { driver.close(); } + org.hibernate.Session session = (Session) em; + + session.doWork(connection -> { + + String TABLE_NAME = "TABLE_NAME"; + String[] TABLE_TYPES = { "TABLE" }; + ResultSet tables = connection.getMetaData().getTables(null, null, null, TABLE_TYPES); + Set tableNames = new HashSet<>(); + + while (tables.next()) { + tableNames.add(tables.getString(TABLE_NAME)); + } + tables.close(); + Statement stm = connection.createStatement(); + while (tableNames.size() > 0) { + for (Iterator iter = tableNames.iterator(); iter.hasNext();) { + String tableName = iter.next(); + try { + stm.executeUpdate("DELETE FROM " + tableName); + iter.remove(); + } catch (SQLException e) { + System.out.println("Must be later: " + tableName); + } + } + } + }); } @Test public void verifyMonthViewComplete() { - WebDriverWait driverWait = new WebDriverWait(driver, 45L); - - driver.get("http://localhost:" + port); - driverWait.until(dr -> dr.findElements(By.id("calendar.month")).size() > 0); + loadApplication(); WebElement monthLabel = driver.findElement(By.id("calendar.month")); String month = monthLabel.getText(); @@ -143,9 +211,8 @@ public class VaadinClubhelperApplicationTests { List days = driver.findElements(By.className("v-calendar-day-number")); assertThat(days, Matchers.hasSize(Matchers.greaterThanOrEqualTo(now.getMonth().length(true)))); - WebElement today = findElementWithContent(String.valueOf(now.getDayOfMonth())); - WebElement parentElement = today.findElement(By.xpath("./..")); - WebElement todayContent = parentElement.findElement(By.className("v-calendar-event")); + WebElement today = driver.findElement(By.className("v-calendar-month-day-today")); + WebElement todayContent = today.findElement(By.className("v-calendar-event-start")); assertEquals("caption", todayContent.getText()); List allEventElements = driver.findElements(By.className("v-calendar-event")); @@ -153,6 +220,39 @@ public class VaadinClubhelperApplicationTests { } + @Test + public void verifyPersonViewForEvent() { + + loadApplication(); + + clickTodaysEvent(); + + By filterGroupId = By.id("person.filter.groups"); + waitFor(filterGroupId); + + WebElement titleElement = driver.findElement(By.id("event.title")); + assertEquals("caption", titleElement.getAttribute("value")); + +// driver.findElement(By.id("person.grid")).findElement(By.tagName("table")); + + } + + public void clickTodaysEvent() { + WebElement today = findElementWithContent("caption"); + today.click(); + } + + public void loadApplication() { + driver.get("http://localhost:" + port); + waitFor(By.id("calendar.month")); + } + + public void waitFor(final By idMonth) { + driverWait.until(dr -> { + return dr.findElements(idMonth).size() > 0 && dr.findElements(idMonth).get(0).isDisplayed(); + }); + } + public WebElement findElementWithContent(String content) { return driver.findElement(By.xpath(String.format("//*[contains(text(), '%s')]", content))); }