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.InvalidEmployeeException; import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service.EmployeeService; 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 CreateNewEmployeeControllerTest extends HighDpiAwareApplicationTest { private EmployeeService employeeService; @Before public void setup() throws Exception { FxToolkit.registerPrimaryStage(); FxToolkit.setupApplication(GuiTestApplication.class, "createNewEmployee.fxml"); employeeService = GuiTestApplication.context.getBean(EmployeeService.class); } @After public void cleanup() throws Exception { FxToolkit.cleanupStages(); } @Test public void testClickAddValidEmployee() throws InvalidEmployeeException, ServiceException { when(employeeService.add(any())).thenReturn(1L); clickOn("#inputName", Motion.DIRECT, MouseButton.PRIMARY); write("Name"); 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 testClickAddInvalidEmployee() throws InvalidEmployeeException, ServiceException { when(employeeService.add(any())).thenThrow(InvalidEmployeeException.class); moveTo("#inputName"); 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 testClickAddEmployeeWithServiceException() throws InvalidEmployeeException, ServiceException { when(employeeService.add(any())).thenThrow(ServiceException.class); clickOn("#inputName", Motion.DIRECT, MouseButton.PRIMARY); write("Test"); 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()); } }