diff options
Diffstat (limited to 'src/test/java/at/ac/tuwien/sepm/assignment/groupphase')
3 files changed, 195 insertions, 0 deletions
diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/CreateNewEmployeeApplicationTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/CreateNewEmployeeApplicationTest.java new file mode 100644 index 0000000..b7129e2 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/CreateNewEmployeeApplicationTest.java @@ -0,0 +1,59 @@ +package at.ac.tuwien.sepm.assignment.groupphase.employee; + +import at.ac.tuwien.sepm.assignment.groupphase.application.MainApplication; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.util.SpringFXMLLoader; +import java.lang.invoke.MethodHandles; +import javafx.application.Application; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.stereotype.Component; + +@Component +@ComponentScan("at.ac.tuwien.sepm.assignment.groupphase") +public final class CreateNewEmployeeApplicationTest extends Application { + + private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + public static AnnotationConfigApplicationContext context; + + @Override + public void start(Stage primaryStage) throws Exception { + // setup application + primaryStage.setTitle("Person anlegen"); + primaryStage.setWidth(1366); + primaryStage.setHeight(768); + primaryStage.centerOnScreen(); + primaryStage.setOnCloseRequest(event -> LOG.debug("Application shutdown initiated")); + + context = new AnnotationConfigApplicationContext(CreateNewEmployeeApplicationTest.class); + final var fxmlLoader = context.getBean(SpringFXMLLoader.class); + primaryStage.setScene( + new Scene( + (Parent) + fxmlLoader.load( + getClass() + .getResourceAsStream( + "/fxml/createNewEmployee.fxml")))); + + // show application + primaryStage.show(); + primaryStage.toFront(); + LOG.debug("Application startup complete"); + } + + public static void main(String[] args) { + LOG.debug("Application starting with arguments={}", (Object) args); + Application.launch(MainApplication.class, args); + } + + @Override + public void stop() { + LOG.debug("Stopping application"); + context.close(); + } +} diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/CreateNewEmployeeControllerTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/CreateNewEmployeeControllerTest.java new file mode 100644 index 0000000..58f1394 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/CreateNewEmployeeControllerTest.java @@ -0,0 +1,117 @@ +package at.ac.tuwien.sepm.assignment.groupphase.employee; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service.EmployeeService; +import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidEmployeeException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException; +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.framework.junit.ApplicationTest; +import org.testfx.robot.Motion; + +public class CreateNewEmployeeControllerTest extends ApplicationTest { + + @Before + public void setup() throws Exception { + /*System.setProperty("testfx.robot", "glass"); + System.setProperty("testfx.headless", "true"); + System.setProperty("prism.order", "sw"); + System.setProperty("prism.text", "t2k"); + System.setProperty("java.awt.headless", "true");*/ + + // ATTENTION: testfx seems to not support high dpi monitors! + // TODO: check if testfx can be run in headless mode on Jenkins + + FxToolkit.registerPrimaryStage(); + FxToolkit.setupApplication(CreateNewEmployeeApplicationTest.class); + } + + @After + public void cleanup() throws Exception { + FxToolkit.cleanupStages(); + } + + @Test + public void testClickAddValidEmployee() throws PersistenceException, InvalidEmployeeException { + + EmployeeService employeeService = + CreateNewEmployeeApplicationTest.context.getBean(EmployeeService.class); + when(employeeService.add(any())).thenReturn(1L); + + clickOn("#inputName", Motion.DIRECT, MouseButton.PRIMARY); + write("Name"); + clickOn("#btnCreate", 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 PersistenceException, InvalidEmployeeException { + + EmployeeService employeeService = + CreateNewEmployeeApplicationTest.context.getBean(EmployeeService.class); + when(employeeService.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 PersistenceException, InvalidEmployeeException { + + EmployeeService employeeService = + CreateNewEmployeeApplicationTest.context.getBean(EmployeeService.class); + when(employeeService.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<Window> 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); + } +} diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/EmployeeServiceTestConfiguration.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/EmployeeServiceTestConfiguration.java new file mode 100644 index 0000000..3668ef4 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/EmployeeServiceTestConfiguration.java @@ -0,0 +1,19 @@ +package at.ac.tuwien.sepm.assignment.groupphase.employee; + +import static org.mockito.Mockito.mock; + +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service.EmployeeService; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service.EmployeeServiceImpl; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +@Configuration +public class EmployeeServiceTestConfiguration { + + @Bean + @Primary + public EmployeeService employeeService() { + return mock(EmployeeServiceImpl.class); + } +} |