package at.ac.tuwien.sepm.assignment.groupphase; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service.VehicleService; import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidEmployeeException; import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException; import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException; import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javafx.scene.control.DialogPane; import javafx.scene.input.MouseButton; import javafx.stage.Modality; import javafx.stage.Stage; import javafx.stage.Window; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.testfx.api.FxToolkit; import org.testfx.robot.Motion; public class CarAddContTest extends HighDpiAwareApplicationTest{ private VehicleService vehicleService; @Before public void setup() throws Exception { // TODO: check if testfx can be run in headless mode on Jenkins FxToolkit.registerPrimaryStage(); FxToolkit.setupApplication(CarAddApp.class); vehicleService = CarAddApp.context.getBean(VehicleService.class); } @After public void cleanup() throws Exception { FxToolkit.cleanupStages(); } @Test public void testClickAddValidEmployee() throws InvalidVehicleException, ServiceException { when(vehicleService.add(any())).thenReturn(1L); //clickOn("#cmb_Ctype", Motion.DIRECT, MouseButton.PRIMARY); clickOn("#btn_create", Motion.DIRECT, MouseButton.PRIMARY); Stage alertDialog = getTopModalStage(); Assert.assertNotNull(alertDialog); DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot(); Assert.assertEquals("Erfolgreich angelegt", dialogPane.getHeaderText()); } @Test public void testClickAddInvalidEmployee() throws InvalidVehicleException, ServiceException { when(vehicleService.add(any())).thenThrow(InvalidEmployeeException.class); moveTo("#inputName"); clickOn("#btnCreate", Motion.DIRECT, MouseButton.PRIMARY); Stage alertDialog = getTopModalStage(); Assert.assertNotNull(alertDialog); DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot(); Assert.assertEquals("Ungültige Eingabe", dialogPane.getHeaderText()); } @Test public void testClickAddEmployeeWithPersistenceException() throws InvalidVehicleException, ServiceException{ when(vehicleService.add(any())).thenThrow(PersistenceException.class); clickOn("#inputName", Motion.DIRECT, MouseButton.PRIMARY); write("Test"); clickOn("#btnCreate", Motion.DIRECT, MouseButton.PRIMARY); Stage alertDialog = getTopModalStage(); Assert.assertNotNull(alertDialog); DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot(); Assert.assertEquals("Speicherfehler", dialogPane.getHeaderText()); } private Stage getTopModalStage() { List allWindows = new ArrayList<>(robotContext().getWindowFinder().listWindows()); Collections.reverse(allWindows); return (Stage) allWindows .stream() .filter(window -> window instanceof Stage) .filter( window -> ((Stage) window).getModality() == Modality.APPLICATION_MODAL) .findFirst() .orElse(null); } }