diff options
Diffstat (limited to 'src/test/java/at')
4 files changed, 494 insertions, 0 deletions
diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/RegistrationWindowApplication.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/RegistrationWindowApplication.java new file mode 100644 index 0000000..3293ae9 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/RegistrationWindowApplication.java @@ -0,0 +1,53 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.controller; + +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 class RegistrationWindowApplication 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(RegistrationWindowApplication.class); +        final var fxmlLoader = context.getBean(SpringFXMLLoader.class); +        primaryStage.setScene( +                new Scene( +                        (Parent) +                                fxmlLoader.load( +                                        getClass() +                                                .getResourceAsStream( +                                                        "/fxml/RegistrationWindow.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/einsatzverwaltung/dao/H2RegistrationDAOTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAOTest.java new file mode 100644 index 0000000..1180bfa --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAOTest.java @@ -0,0 +1,162 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao; + +import static org.junit.Assert.*; + +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.dto.Registration; +import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException; +import at.ac.tuwien.sepm.assignment.groupphase.util.JDBCConnectionManager; +import java.nio.charset.Charset; +import java.sql.SQLException; +import java.time.Instant; +import java.time.LocalDate; +import java.util.LinkedList; +import java.util.List; +import org.h2.tools.RunScript; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class H2RegistrationDAOTest { + +    // Base taken from EmployeePersistenceTest + +    private static final String JDBC_DRIVER = org.h2.Driver.class.getName(); +    private static final String JDBC_URL = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"; +    private static final String USER = ""; +    private static final String PASSWORD = ""; + +    private RegistrationDAO registrationDAO; + +    public H2RegistrationDAOTest() throws PersistenceException { +        this.registrationDAO = new H2RegistrationDAO(new JDBCConnectionManager(JDBC_URL)); +    } + +    @BeforeClass +    public static void setupDatabase() throws SQLException { +        RunScript.execute( +                JDBC_URL, +                USER, +                PASSWORD, +                "classpath:sql/database.sql", +                Charset.forName("UTF8"), +                false); +    } + +    @Before +    public void setUp() throws SQLException { +        RunScript.execute( +                JDBC_URL, +                USER, +                PASSWORD, +                "classpath:sql/H2RegistrationDAOTest_populate.sql", +                Charset.forName("UTF8"), +                false); +    } + +    @After +    public void tearDown() throws SQLException { +        RunScript.execute( +                JDBC_URL, +                USER, +                PASSWORD, +                "classpath:sql/H2RegistrationDAOTest_depopulate.sql", +                Charset.forName("UTF8"), +                false); +    } + +    @Rule public ExpectedException thrown = ExpectedException.none(); + +    @Test +    public void addRegistrationsShouldSucceed() throws PersistenceException { +        List<Registration> registrations = new LinkedList<>(); +        /* +        Vehicle vehicle = Vehicle.builder() +                .id(1) +                .name("RTW-1") +                .constructionType(ConstructionType.HOCHDACH) +                .type(VehicleType.RTW) +                .status(Status.ABGEMELDET) +                .hasNef(true) +                .build(); +        */ +        Employee employee1 = +                Employee.builder() +                        .id(1) +                        .name("John Doe") +                        .birthday(LocalDate.now()) // incorrect, but should be irrelevant +                        .educationLevel(EducationLevel.RS) +                        .isDriver(true) +                        .isPilot(true) +                        .build(); +        Employee employee2 = +                Employee.builder() +                        .id(2) +                        .name("Nick \"Kage\" Verily") +                        .birthday(LocalDate.now()) // incorrect, but should be irrelevant +                        .educationLevel(EducationLevel.NKV) +                        .isDriver(true) +                        .isPilot(false) +                        .build(); +        Employee employee3 = +                Employee.builder() +                        .id(3) +                        .name("Nicht Arzt") +                        .birthday(LocalDate.now()) // incorrect, but should be irrelevant +                        .educationLevel(EducationLevel.NA) +                        .isDriver(false) +                        .isPilot(false) +                        .build(); +        Registration registration1 = +                Registration.builder() +                        .start(Instant.now()) // incorrect, but should be irrelevant to outcome +                        .end(Instant.now()) // same +                        .employee(employee1) +                        .build(); +        Registration registration2 = +                Registration.builder() +                        .start(Instant.now()) // incorrect, but should be irrelevant to outcome +                        .end(Instant.now()) // same +                        .employee(employee2) +                        .build(); +        Registration registration3 = +                Registration.builder() +                        .start(Instant.now()) // incorrect, but should be irrelevant to outcome +                        .end(Instant.now()) // same +                        .employee(employee3) +                        .build(); +        registrations.add(registration1); +        registrations.add(registration2); +        registrations.add(registration3); + +        List<Long> returnvalues = registrationDAO.add(1, registrations); +        assertFalse(returnvalues.isEmpty()); // can be improved... +    } + +    @Test +    public void addRegistrationToInexistentVehicleShouldFail() throws PersistenceException { +        thrown.expect(PersistenceException.class); +        List<Registration> registrations = new LinkedList<>(); +        Employee employee = +                Employee.builder() +                        .id(1) +                        .name("John Doe") +                        .birthday(LocalDate.now()) // incorrect, but should be irrelevant +                        .educationLevel(EducationLevel.RS) +                        .isDriver(true) +                        .isPilot(true) +                        .build(); +        Registration registration = +                Registration.builder() +                        .start(Instant.MIN) +                        .end(Instant.MAX) +                        .employee(employee) +                        .build(); +        registrations.add(registration); +        registrationDAO.add(200, registrations); +    } +} diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationServiceTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationServiceTest.java new file mode 100644 index 0000000..b1ef38f --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationServiceTest.java @@ -0,0 +1,124 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service; + +import static org.junit.Assert.*; + +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.RegistrationDAO; +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.dto.Registration; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle.ConstructionType; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle.Status; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle.VehicleType; +import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidRegistrationException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException; +import java.time.Instant; +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; +import java.util.LinkedList; +import java.util.List; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +public class SimpleRegistrationServiceTest { + +    @Mock RegistrationDAO daoMock; + +    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); + +    @Rule public ExpectedException thrown = ExpectedException.none(); + +    @Test +    public void addValidRegistrationsShouldSucceed() +            throws InvalidRegistrationException, ServiceException, InvalidVehicleException { +        RegistrationService registrationService = new SimpleRegistrationService(daoMock); +        List<Registration> registrations = new LinkedList<>(); +        Vehicle vehicle = +                Vehicle.builder() +                        .id(1) +                        .name("RTW-1") +                        .constructionType(ConstructionType.HOCHDACH) +                        .type(VehicleType.RTW) +                        .status(Status.ABGEMELDET) +                        .hasNef(true) +                        .build(); +        Employee employee1 = +                Employee.builder() +                        .id(1) +                        .name("John Doe") +                        .birthday(LocalDate.now()) // incorrect, but should be irrelevant +                        .educationLevel(EducationLevel.RS) +                        .isDriver(true) +                        .isPilot(true) +                        .build(); +        Employee employee2 = +                Employee.builder() +                        .id(2) +                        .name("Nick \"Kage\" Verily") +                        .birthday(LocalDate.now()) // incorrect, but should be irrelevant +                        .educationLevel(EducationLevel.NKV) +                        .isDriver(true) +                        .isPilot(false) +                        .build(); +        Employee employee3 = +                Employee.builder() +                        .id(3) +                        .name("Nicht Arzt") +                        .birthday(LocalDate.now()) // incorrect, but should be irrelevant +                        .educationLevel(EducationLevel.NA) +                        .isDriver(false) +                        .isPilot(false) +                        .build(); +        Instant start = Instant.now(); +        Instant end = start.plus(8, ChronoUnit.HOURS); +        Registration registration1 = +                Registration.builder().start(start).end(end).employee(employee1).build(); +        Registration registration2 = +                Registration.builder().start(start).end(end).employee(employee2).build(); +        Registration registration3 = +                Registration.builder().start(start).end(end).employee(employee3).build(); +        registrations.add(registration1); +        registrations.add(registration2); +        registrations.add(registration3); +        registrationService.add(vehicle, registrations); +    } + +    @Test +    public void addOnlyOnePersonToRTWShouldFail() +            throws InvalidRegistrationException, ServiceException, InvalidVehicleException { +        thrown.expect(InvalidRegistrationException.class); +        RegistrationService registrationService = new SimpleRegistrationService(daoMock); +        List<Registration> registrations = new LinkedList<>(); +        Vehicle vehicle = +                Vehicle.builder() +                        .id(1) +                        .name("RTW-1") +                        .constructionType(ConstructionType.HOCHDACH) +                        .type(VehicleType.RTW) +                        .status(Status.ABGEMELDET) +                        .hasNef(true) +                        .build(); +        Employee employee = +                Employee.builder() +                        .id(1) +                        .name("John Doe") +                        .birthday(LocalDate.now()) // incorrect, but should be irrelevant +                        .educationLevel(EducationLevel.RS) +                        .isDriver(true) +                        .isPilot(true) +                        .build(); +        Registration registration = +                Registration.builder() +                        .start(Instant.MIN) +                        .end(Instant.MAX) +                        .employee(employee) +                        .build(); +        registrations.add(registration); +        registrationService.add(vehicle, registrations); +    } +} diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/EmployeePersistenceTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/EmployeePersistenceTest.java new file mode 100644 index 0000000..f8fe0f3 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/EmployeePersistenceTest.java @@ -0,0 +1,155 @@ +package at.ac.tuwien.sepm.assignment.groupphase.employee; + +import static junit.framework.TestCase.fail; + +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.exception.PersistenceException; +import at.ac.tuwien.sepm.assignment.groupphase.util.JDBCConnectionManager; +import java.nio.charset.Charset; +import java.sql.SQLException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.List; +import org.dbunit.IDatabaseTester; +import org.dbunit.JdbcDatabaseTester; +import org.dbunit.dataset.DataSetException; +import org.dbunit.dataset.IDataSet; +import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; +import org.dbunit.operation.DatabaseOperation; +import org.h2.tools.RunScript; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class EmployeePersistenceTest { + +    private static final String JDBC_DRIVER = org.h2.Driver.class.getName(); +    private static final String JDBC_URL = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"; +    private static final String USER = ""; +    private static final String PASSWORD = ""; + +    private EmployeeDAO employeePersistence; + +    public EmployeePersistenceTest() throws PersistenceException { +        employeePersistence = new EmployeeDatabaseDao(new JDBCConnectionManager(JDBC_URL)); +    } + +    @BeforeClass +    public static void createSchema() throws SQLException { +        RunScript.execute( +                JDBC_URL, +                USER, +                PASSWORD, +                "classpath:sql/database.sql", +                Charset.forName("UTF8"), +                false); +    } + +    @Before +    public void importDataSet() throws Exception { +        IDataSet dataSet = readDataSet(); +        cleanlyInsert(dataSet); +    } + +    private IDataSet readDataSet() throws DataSetException { +        return new FlatXmlDataSetBuilder() +                .build( +                        getClass() +                                .getClassLoader() +                                .getResourceAsStream("employeeServiceTestData.xml")); +    } + +    private void cleanlyInsert(IDataSet dataSet) throws Exception { +        IDatabaseTester databaseTester = +                new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD); + +        databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT); +        databaseTester.setDataSet(dataSet); +        databaseTester.onSetup(); +    } + +    @Test +    public void testListEmployees() { + +        try { +            List<Employee> employees = employeePersistence.list(); + +            Employee empOne = +                    Employee.builder() +                            .id(1) +                            .name("Adam") +                            .birthday( +                                    LocalDate.parse( +                                            "10.10.2010", +                                            DateTimeFormatter.ofPattern("dd.MM.yyyy"))) +                            .educationLevel(EducationLevel.RS) +                            .isDriver(true) +                            .isPilot(false) +                            .build(); + +            Employee empTwo = +                    Employee.builder() +                            .id(2) +                            .name("Max") +                            .birthday( +                                    LocalDate.parse( +                                            "11.11.1990", +                                            DateTimeFormatter.ofPattern("dd.MM.yyyy"))) +                            .educationLevel(EducationLevel.NFS) +                            .isDriver(false) +                            .isPilot(false) +                            .build(); + +            Employee empThree = +                    Employee.builder() +                            .id(3) +                            .name("Lisa") +                            .birthday( +                                    LocalDate.parse( +                                            "16.10.1999", +                                            DateTimeFormatter.ofPattern("dd.MM.yyyy"))) +                            .educationLevel(EducationLevel.NKI) +                            .isDriver(true) +                            .isPilot(false) +                            .build(); + +            Assert.assertTrue(employees.contains(empOne)); +            Assert.assertTrue(employees.contains(empTwo)); +            Assert.assertTrue(employees.contains(empThree)); +            Assert.assertEquals(3, employees.size()); + +        } catch (PersistenceException e) { +            fail(); +        } +    } + +    @Test +    public void testEmployeeListNoElement() { + +        try { +            List<Employee> employees = employeePersistence.list(); + +            Employee empOne = +                    Employee.builder() +                            .id(10) +                            .name("Adam") +                            .birthday( +                                    LocalDate.parse( +                                            "10.10.2010", +                                            DateTimeFormatter.ofPattern("dd.MM.yyyy"))) +                            .educationLevel(EducationLevel.RS) +                            .isDriver(true) +                            .isPilot(false) +                            .build(); + +            Assert.assertFalse(employees.contains(empOne)); + +        } catch (PersistenceException e) { +            fail(); +        } +    } +}  | 
