package at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.controller; import static at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.controller.Helper.ALERT_TITLE_SERVICE_EXCEPTION; import static at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.controller.Helper.ALERT_TITLE_SUCCESS; import static at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.controller.Helper.ALERT_TITLE_VALIDATION_ERROR; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException; import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service.VehicleService; import at.ac.tuwien.sepm.assignment.groupphase.util.Helper; import at.ac.tuwien.sepm.assignment.groupphase.util.HighDpiAwareApplicationTest; import javafx.scene.control.DialogPane; import javafx.scene.input.MouseButton; import javafx.stage.Stage; 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 CreateNewVehicleControllerTest extends HighDpiAwareApplicationTest { private VehicleService vehicleService; @Before public void setup() throws Exception { FxToolkit.registerPrimaryStage(); FxToolkit.setupApplication(GuiTestApplication.class, "createCar.fxml"); vehicleService = GuiTestApplication.context.getBean(VehicleService.class); } @After public void cleanup() throws Exception { FxToolkit.cleanupStages(); } @Test public void testClickAddValidVehicle() throws ServiceException, InvalidVehicleException { when(vehicleService.add(any())).thenReturn(1L); clickOn("#btnCreate", Motion.DIRECT, MouseButton.PRIMARY); Stage alertDialog = Helper.getTopModalStage(robotContext()); Assert.assertNotNull(alertDialog); DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot(); Assert.assertEquals(ALERT_TITLE_SUCCESS, dialogPane.getHeaderText()); } @Test public void testClickInvalidVehicleEx() throws ServiceException, InvalidVehicleException { when(vehicleService.add(any())).thenThrow(InvalidVehicleException.class); clickOn("#btnCreate", Motion.DIRECT, MouseButton.PRIMARY); Stage alertDialog = Helper.getTopModalStage(robotContext()); Assert.assertNotNull(alertDialog); DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot(); Assert.assertEquals(ALERT_TITLE_VALIDATION_ERROR, dialogPane.getHeaderText()); } @Test public void testClickInvalidServiceEx() throws ServiceException, InvalidVehicleException { when(vehicleService.add(any())).thenThrow(ServiceException.class); clickOn("#btnCreate", Motion.DIRECT, MouseButton.PRIMARY); Stage alertDialog = Helper.getTopModalStage(robotContext()); Assert.assertNotNull(alertDialog); DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot(); Assert.assertEquals(ALERT_TITLE_SERVICE_EXCEPTION, dialogPane.getHeaderText()); } }