diff options
Diffstat (limited to 'src/test/java/at/ac')
6 files changed, 303 insertions, 0 deletions
diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/CreateNewEmployeeApplication.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/CreateNewEmployeeApplication.java new file mode 100644 index 0000000..e9f4801 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/CreateNewEmployeeApplication.java @@ -0,0 +1,53 @@ +package at.ac.tuwien.sepm.assignment.groupphase.employee; + +import at.ac.tuwien.sepm.assignment.groupphase.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 CreateNewEmployeeApplication 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(CreateNewEmployeeApplication.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"); + } + + @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..da8a6ac --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/CreateNewEmployeeControllerTest.java @@ -0,0 +1,105 @@ +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.ServiceException; +import at.ac.tuwien.sepm.assignment.groupphase.util.HighDpiAwareApplicationTest; +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 CreateNewEmployeeControllerTest extends HighDpiAwareApplicationTest { + + private EmployeeService employeeService; + + @Before + public void setup() throws Exception { + // TODO: check if testfx can be run in headless mode on Jenkins + FxToolkit.registerPrimaryStage(); + FxToolkit.setupApplication(CreateNewEmployeeApplication.class); + employeeService = CreateNewEmployeeApplication.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 = getTopModalStage(); + Assert.assertNotNull(alertDialog); + + DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot(); + Assert.assertEquals("Erfolgreich angelegt", 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 = getTopModalStage(); + Assert.assertNotNull(alertDialog); + + DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot(); + Assert.assertEquals("Ungültige Eingabe", 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 = 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/EmployeeServiceTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/EmployeeServiceTest.java new file mode 100644 index 0000000..47328b3 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/EmployeeServiceTest.java @@ -0,0 +1,68 @@ +package at.ac.tuwien.sepm.assignment.groupphase.employee; + +import static junit.framework.TestCase.fail; +import static org.hamcrest.CoreMatchers.is; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.EmployeeDAO; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.EmployeeDatabaseDao; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Employee; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Employee.EducationLevel; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service.EmployeeService; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service.EmployeeServiceImpl; +import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidEmployeeException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException; +import java.time.LocalDate; +import org.junit.Assert; +import org.junit.Test; + +public class EmployeeServiceTest { + + private final EmployeeDAO employeePersistence = mock(EmployeeDatabaseDao.class); + private final EmployeeService employeeService = new EmployeeServiceImpl(employeePersistence); + + public EmployeeServiceTest() throws PersistenceException { + when(employeePersistence.add(any())).thenReturn(1L); + } + + @Test + public void testAddValidEmployee() { + + Employee employee = + Employee.builder() + .name("Testperson") + .birthday(LocalDate.MIN) + .educationLevel(EducationLevel.NA) + .isDriver(true) + .isPilot(false) + .build(); + + try { + Assert.assertThat(employeeService.add(employee), is(1L)); + } catch (InvalidEmployeeException | ServiceException e) { + fail(); + } + } + + @Test(expected = InvalidEmployeeException.class) + public void testAddInvalidEmployee() throws InvalidEmployeeException { + + Employee employee = + Employee.builder() + .name("") + .birthday(LocalDate.MIN) + .educationLevel(EducationLevel.NA) + .isDriver(true) + .isPilot(false) + .build(); + + try { + employeeService.add(employee); + } catch (ServiceException e) { + fail(); + } + } +} 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); + } +} diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/util/HighDpiAwareApplicationTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/util/HighDpiAwareApplicationTest.java new file mode 100644 index 0000000..c9816a1 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/util/HighDpiAwareApplicationTest.java @@ -0,0 +1,24 @@ +package at.ac.tuwien.sepm.assignment.groupphase.util; + +import javafx.geometry.Bounds; +import javafx.scene.Node; +import org.testfx.api.FxRobotContext; +import org.testfx.framework.junit.ApplicationTest; +import org.testfx.service.locator.impl.BoundsLocatorImpl; +import org.testfx.service.locator.impl.PointLocatorImpl; + +public class HighDpiAwareApplicationTest extends ApplicationTest { + + public HighDpiAwareApplicationTest() { + FxRobotContext context = robotContext(); + context.setBoundsLocator( + new BoundsLocatorImpl() { + @Override + public Bounds boundsOnScreenFor(Node node) { + Bounds bounds = super.boundsOnScreenFor(node); + return ScaledBounds.wrap(bounds); + } + }); + robotContext().setPointLocator(new PointLocatorImpl(context.getBoundsLocator())); + } +} diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/util/ScaledBounds.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/util/ScaledBounds.java new file mode 100644 index 0000000..02c15c4 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/util/ScaledBounds.java @@ -0,0 +1,34 @@ +package at.ac.tuwien.sepm.assignment.groupphase.util; + +import java.awt.GraphicsEnvironment; +import javafx.geometry.BoundingBox; +import javafx.geometry.Bounds; + +public class ScaledBounds extends BoundingBox { + + private static final double scale; + + static { + scale = + 1 + / GraphicsEnvironment.getLocalGraphicsEnvironment() + .getDefaultScreenDevice() + .getDefaultConfiguration() + .getDefaultTransform() + .getScaleX(); + } + + public static ScaledBounds wrap(Bounds bounds) { + return new ScaledBounds(bounds); + } + + private ScaledBounds(Bounds wrapped) { + super( + wrapped.getMinX() * scale, + wrapped.getMinY() * scale, + wrapped.getMinZ() * scale, + wrapped.getWidth() * scale, + wrapped.getHeight() * scale, + wrapped.getDepth()); + } +} |