diff options
author | Tharre <tharre3@gmail.com> | 2018-05-11 15:49:43 +0200 |
---|---|---|
committer | Tharre <tharre3@gmail.com> | 2018-05-11 15:49:43 +0200 |
commit | f7d14a76123911f0bced08356a0c69e61147cb1b (patch) | |
tree | a75b0baa891453a14046c78503603d21986126c5 /src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee | |
parent | acd6b64e494192f7c73032240029bfa53dccb362 (diff) | |
parent | 5298356db60cd971fed686d379130102843db819 (diff) | |
download | sepm-groupproject-f7d14a76123911f0bced08356a0c69e61147cb1b.tar.gz sepm-groupproject-f7d14a76123911f0bced08356a0c69e61147cb1b.tar.xz sepm-groupproject-f7d14a76123911f0bced08356a0c69e61147cb1b.zip |
Merge branch 'develop'
Diffstat (limited to 'src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee')
5 files changed, 380 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..eb1a728 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/employee/CreateNewEmployeeControllerTest.java @@ -0,0 +1,85 @@ +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.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 { + // 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 = Helper.getTopModalStage(robotContext()); + 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 = Helper.getTopModalStage(robotContext()); + 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 = Helper.getTopModalStage(robotContext()); + Assert.assertNotNull(alertDialog); + + DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot(); + Assert.assertEquals("Speicherfehler", dialogPane.getHeaderText()); + } +} 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(); + } + } +} 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); + } +} |