diff options
author | Dominic Rogetzer <e1627756@student.tuwien.ac.at> | 2018-05-17 18:23:02 +0200 |
---|---|---|
committer | Tharre <tharre3@gmail.com> | 2018-05-21 17:08:04 +0200 |
commit | b5cfa2ce349b44f4c92a8a4fc397afe040f21c7a (patch) | |
tree | 9ca106cd555facdc9f899ddd9fc7e507cfb74ec9 /src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung | |
parent | 15f55fda6a4338b89363766dfcabc5b4404918f2 (diff) | |
download | sepm-groupproject-b5cfa2ce349b44f4c92a8a4fc397afe040f21c7a.tar.gz sepm-groupproject-b5cfa2ce349b44f4c92a8a4fc397afe040f21c7a.tar.xz sepm-groupproject-b5cfa2ce349b44f4c92a8a4fc397afe040f21c7a.zip |
Restructure test folder
Diffstat (limited to 'src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung')
12 files changed, 1164 insertions, 0 deletions
diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/CreateNewEmployeeApplication.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/CreateNewEmployeeApplication.java new file mode 100644 index 0000000..e1b3714 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/CreateNewEmployeeApplication.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 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/einsatzverwaltung/controller/CreateNewEmployeeControllerTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/CreateNewEmployeeControllerTest.java new file mode 100644 index 0000000..7f95950 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/CreateNewEmployeeControllerTest.java @@ -0,0 +1,85 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.controller; + +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/einsatzverwaltung/controller/CreateNewVehicleApplication.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/CreateNewVehicleApplication.java new file mode 100644 index 0000000..ff46938 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/CreateNewVehicleApplication.java @@ -0,0 +1,51 @@ +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 CreateNewVehicleApplication 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("Fahrzeug anlegen"); + primaryStage.setWidth(1366); + primaryStage.setHeight(768); + primaryStage.centerOnScreen(); + primaryStage.setOnCloseRequest(event -> LOG.debug("Application shutdown initiated")); + + context = new AnnotationConfigApplicationContext(CreateNewVehicleApplication.class); + final var fxmlLoader = context.getBean(SpringFXMLLoader.class); + primaryStage.setScene( + new Scene( + (Parent) + fxmlLoader.load( + getClass().getResourceAsStream("/fxml/createCar.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/controller/CreateNewVehicleControllerTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/CreateNewVehicleControllerTest.java new file mode 100644 index 0000000..08e3fde --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/CreateNewVehicleControllerTest.java @@ -0,0 +1,79 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.controller; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service.VehicleService; +import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException; +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 CreateNewVehicleControllerTest extends HighDpiAwareApplicationTest { + + private VehicleService vehicleService; + + @Before + public void setup() throws Exception { + // TODO: check if testfx can be run in headless mode on Jenkins + FxToolkit.registerPrimaryStage(); + FxToolkit.setupApplication(CreateNewVehicleApplication.class); + vehicleService = CreateNewVehicleApplication.context.getBean(VehicleService.class); + } + + @After + public void cleanup() throws Exception { + FxToolkit.cleanupStages(); + } + + @Test + public void testClickAddValidVehicle() throws ServiceException, InvalidVehicleException { + + when(vehicleService.add(any())).thenReturn(1L); + + clickOn("#btn_create", Motion.DIRECT, MouseButton.PRIMARY); + + Stage alertDialog = Helper.getTopModalStage(robotContext()); + Assert.assertNotNull(alertDialog); + + DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot(); + Assert.assertEquals("Speichern Erfolgreich", dialogPane.getHeaderText()); + } + + @Test + public void testClickInvalidVehicleEx() throws ServiceException, InvalidVehicleException { + + when(vehicleService.add(any())).thenThrow(InvalidVehicleException.class); + + clickOn("#btn_create", 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 testClickInvalidServiceEx() throws ServiceException, InvalidVehicleException { + + when(vehicleService.add(any())).thenThrow(ServiceException.class); + + clickOn("#btn_create", Motion.DIRECT, MouseButton.PRIMARY); + + Stage alertDialog = Helper.getTopModalStage(robotContext()); + Assert.assertNotNull(alertDialog); + + DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot(); + Assert.assertEquals("Fehler", dialogPane.getHeaderText()); + } +} diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/EmployeePersistenceTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/EmployeePersistenceTest.java new file mode 100644 index 0000000..8633b9d --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/EmployeePersistenceTest.java @@ -0,0 +1,85 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao; + +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.JdbcTestCase; +import java.io.InputStream; +import java.time.LocalDate; +import java.util.Set; +import org.dbunit.dataset.DataSetException; +import org.dbunit.dataset.IDataSet; +import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; +import org.junit.Assert; + +public class EmployeePersistenceTest extends JdbcTestCase { + + private EmployeeDAO employeePersistence; + + public EmployeePersistenceTest() throws PersistenceException { + employeePersistence = new EmployeeDatabaseDao(getJdbcConnectionManager()); + } + + @Override + protected IDataSet getDataSet() throws DataSetException { + InputStream res = + getClass().getClassLoader().getResourceAsStream("employeeServiceTestData.xml"); + return new FlatXmlDataSetBuilder().build(res); + } + + public void testListEmployees() throws PersistenceException { + Set<Employee> employees = employeePersistence.list(); + + System.out.println(LocalDate.parse("2010-10-10")); + Employee empOne = + Employee.builder() + .id(1) + .name("Adam") + .birthday(LocalDate.parse("2010-10-10")) + .educationLevel(EducationLevel.RS) + .isDriver(true) + .isPilot(false) + .build(); + + Employee empTwo = + Employee.builder() + .id(2) + .name("Max") + .birthday(LocalDate.parse("1990-11-11")) + .educationLevel(EducationLevel.NFS) + .isDriver(false) + .isPilot(false) + .build(); + + Employee empThree = + Employee.builder() + .id(3) + .name("Lisa") + .birthday(LocalDate.parse("1999-10-16")) + .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()); + } + + public void testEmployeeListNoElement() throws PersistenceException { + Set<Employee> employees = employeePersistence.list(); + + Employee empOne = + Employee.builder() + .id(10) + .name("Adam") + .birthday(LocalDate.parse("2010-10-10")) + .educationLevel(EducationLevel.RS) + .isDriver(true) + .isPilot(false) + .build(); + + Assert.assertFalse(employees.contains(empOne)); + } +} diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationPersistenceTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationPersistenceTest.java new file mode 100644 index 0000000..a5e4993 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationPersistenceTest.java @@ -0,0 +1,127 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao; + +import static junit.framework.TestCase.fail; + +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation.Severity; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation.Status; +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.VehicleType; +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.util.Arrays; +import java.util.Set; +import org.h2.tools.RunScript; +import org.junit.BeforeClass; +import org.junit.Test; + +public class OperationPersistenceTest { + + private final OperationDAO operationDAO = + new DBOperationDAO(new JDBCConnectionManager("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")); + + @BeforeClass + public static void createSchema() throws SQLException { + RunScript.execute( + "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", + "", + "", + "classpath:sql/database.sql", + Charset.forName("UTF8"), + false); + } + + @Test + public void addOperationTest() { + Vehicle vehicle = + Vehicle.builder() + .status(Vehicle.Status.FREI_FUNK) + .constructionType(ConstructionType.HOCHDACH) + .name("BKTW_123") + .hasNef(true) + .type(VehicleType.BKTW) + .build(); + + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode("ALP-95E7") + .created(Instant.now()) + .destination("Wiedner Hauptstraße 35, Wien") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of(vehicle)) + .build(); + try { + operationDAO.add(operation); + } catch (PersistenceException e) { + fail(); + } + } + + @Test(expected = PersistenceException.class) + public void addFaultyOperationTest() throws PersistenceException { + Vehicle vehicle = + Vehicle.builder() + .status(Vehicle.Status.FREI_FUNK) + .constructionType(ConstructionType.HOCHDACH) + .name("BKTW_123") + .hasNef(true) + .type(VehicleType.BKTW) + .build(); + + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode(String.valueOf(Arrays.stream(new int[200]).map(i -> 'a'))) + .created(Instant.now()) + .destination("Wiedner Hauptstraße 35, Wien") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of(vehicle)) + .build(); + operationDAO.add(operation); + } + + @Test(expected = PersistenceException.class) + public void addFaultyOperation1Test() throws PersistenceException { + operationDAO.add(null); + } + + @Test(expected = PersistenceException.class) + public void addFaultyOperation2Test() throws PersistenceException { + Vehicle vehicle = + Vehicle.builder() + .status(Vehicle.Status.FREI_FUNK) + .constructionType(ConstructionType.HOCHDACH) + .name("BKTW_123") + .hasNef(true) + .type(VehicleType.BKTW) + .build(); + + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode("ALP-95E7") + .created(Instant.now()) + .destination( + "Wiednerstraße 888, 1010 Wien Wiednerstraße 888, 1010 Wien Wiednerstraße 888, 1010 Wien Wiednerstraße 888, 1010 Wien Wiednerstraße 888, 1010 Wien ") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of(vehicle)) + .build(); + operationDAO.add(operation); + } + + @Test(expected = PersistenceException.class) + public void addConnectionTest() throws PersistenceException { + operationDAO.connectVehicleToOperation(-1, 0); + } + + // TODO: ADD CONNECTION TESTS + // KOMMT ID ZURÜCK?*/ +} diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/CarAddTestService.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/CarAddTestService.java new file mode 100644 index 0000000..fd8b43f --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/CarAddTestService.java @@ -0,0 +1,281 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service; + +import static junit.framework.TestCase.fail; +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.VehicleDAO; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.VehicleDatabaseDao; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle; +import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException; +import org.junit.Test; + +public class CarAddTestService { + private final VehicleDAO vehicleP = mock(VehicleDatabaseDao.class); + private final VehicleService vehicleService = new VehicleServiceImpl(vehicleP); + + public CarAddTestService() throws PersistenceException { + when(vehicleP.add(any())).thenReturn(1L); + } + + @Test + public void testValidVehicleH() { + Vehicle vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.HOCHDACH) + .type(Vehicle.VehicleType.RTW) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (InvalidVehicleException | ServiceException e) { + fail(); + } + vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.HOCHDACH) + .type(Vehicle.VehicleType.KTW) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (InvalidVehicleException | ServiceException e) { + fail(); + } + vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.HOCHDACH) + .type(Vehicle.VehicleType.KTW_B) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (InvalidVehicleException | ServiceException e) { + fail(); + } + vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.HOCHDACH) + .type(Vehicle.VehicleType.BKTW) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (InvalidVehicleException | ServiceException e) { + fail(); + } + } + + @Test + public void testValidVehicleM() { + Vehicle vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.MITTELHOCHDACH) + .type(Vehicle.VehicleType.KTW) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (InvalidVehicleException | ServiceException e) { + fail(); + } + vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.MITTELHOCHDACH) + .type(Vehicle.VehicleType.KTW_B) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (InvalidVehicleException | ServiceException e) { + fail(); + } + vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.MITTELHOCHDACH) + .type(Vehicle.VehicleType.BKTW) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (InvalidVehicleException | ServiceException e) { + fail(); + } + } + + @Test + public void testValidVehicleN() { + Vehicle vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.NORMAL) + .type(Vehicle.VehicleType.BKTW) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (InvalidVehicleException | ServiceException e) { + fail(); + } + vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.NORMAL) + .type(Vehicle.VehicleType.NEF) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (InvalidVehicleException | ServiceException e) { + fail(); + } + vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.NORMAL) + .type(Vehicle.VehicleType.NAH) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (InvalidVehicleException | ServiceException e) { + fail(); + } + } + + @Test(expected = InvalidVehicleException.class) + public void testInvalidVehicleH() throws InvalidVehicleException { + Vehicle vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.HOCHDACH) + .type(Vehicle.VehicleType.NEF) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (ServiceException e) { + fail(); + } + vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.HOCHDACH) + .type(Vehicle.VehicleType.NAH) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (ServiceException e) { + fail(); + } + } + + @Test(expected = InvalidVehicleException.class) + public void testInvalidVehicleM() throws InvalidVehicleException { + Vehicle vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.MITTELHOCHDACH) + .type(Vehicle.VehicleType.NEF) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (ServiceException e) { + fail(); + } + vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.MITTELHOCHDACH) + .type(Vehicle.VehicleType.NAH) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (ServiceException e) { + fail(); + } + vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.MITTELHOCHDACH) + .type(Vehicle.VehicleType.RTW) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (ServiceException e) { + fail(); + } + } + + @Test(expected = InvalidVehicleException.class) + public void testInvalidVehicleN() throws InvalidVehicleException { + Vehicle vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.NORMAL) + .type(Vehicle.VehicleType.RTW) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (ServiceException e) { + fail(); + } + vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.NORMAL) + .type(Vehicle.VehicleType.KTW_B) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (ServiceException e) { + fail(); + } + vehicle = + Vehicle.builder() + .constructionType(Vehicle.ConstructionType.NORMAL) + .type(Vehicle.VehicleType.KTW) + .hasNef(true) + .status(Vehicle.Status.ABGEMELDET) + .name("") + .build(); + try { + vehicleService.add(vehicle); + } catch (ServiceException e) { + fail(); + } + } +} diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/EmployeeServiceTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/EmployeeServiceTest.java new file mode 100644 index 0000000..c6b1423 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/EmployeeServiceTest.java @@ -0,0 +1,66 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service; + +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.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/einsatzverwaltung/service/EmployeeServiceTestConfiguration.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/EmployeeServiceTestConfiguration.java new file mode 100644 index 0000000..6bf2275 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/EmployeeServiceTestConfiguration.java @@ -0,0 +1,17 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service; + +import static org.mockito.Mockito.mock; + +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/einsatzverwaltung/service/OperationServiceComponentTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceComponentTest.java new file mode 100644 index 0000000..8e13d0e --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceComponentTest.java @@ -0,0 +1,150 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service; + +import static junit.framework.TestCase.fail; +import static org.hamcrest.CoreMatchers.is; + +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.DBOperationDAO; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.OperationDAO; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation.Severity; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation.Status; +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.VehicleType; +import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidOperationException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException; +import at.ac.tuwien.sepm.assignment.groupphase.util.JDBCConnectionManager; +import java.time.Instant; +import java.util.Set; +import org.junit.Assert; +import org.junit.Test; + +public class OperationServiceComponentTest { + + private final OperationDAO operationDAO = + new DBOperationDAO(new JDBCConnectionManager("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")); + private final OperationService operationService = new OperationServiceImpl(operationDAO); + + /*@Test + public void testaddOperationTest() { + Vehicle vehicle = + Vehicle.builder() + .status(Vehicle.Status.FREI_FUNK) + .constructionType(ConstructionType.HOCHDACH) + .name("BKTW_123") + .hasNef(true) + .type(VehicleType.BKTW) + .build(); + + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode("ALP-95E7") + .created(Instant.now()) + .destination("Wiedner Hauptstraße 35, Wien") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of(vehicle)) + .build(); + try { + // TODO: OPERATION DOES NOT WORK + Assert.assertThat(operationService.add(operation), is(1L)); + } catch (InvalidOperationException | ServiceException e) { + e.printStackTrace(); + fail(); + } + }*/ + + @Test(expected = InvalidOperationException.class) + public void testaddFaultyOperationTest() throws InvalidOperationException { + Vehicle vehicle = + Vehicle.builder() + .status(Vehicle.Status.FREI_FUNK) + .constructionType(ConstructionType.HOCHDACH) + .name("BKTW_123") + .hasNef(true) + .type(VehicleType.BKTW) + .build(); + Vehicle vehicle1 = + Vehicle.builder() + .status(Vehicle.Status.ABGEMELDET) + .constructionType(ConstructionType.HOCHDACH) + .name("BKTW_123") + .hasNef(true) + .type(VehicleType.BKTW) + .build(); + + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode("ALP-95E7") + .created(Instant.now()) + .destination("Wiedner Hauptstraße 35, Wien") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of(vehicle, vehicle1)) + .build(); + try { + Assert.assertThat(operationService.add(operation), is(1L)); + } catch (ServiceException e) { + fail(); + } + } + + @Test(expected = InvalidOperationException.class) + public void testaddFaultyOperation2Test() throws InvalidOperationException { + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode("ALP-95E7") + .created(Instant.now()) + .destination("Wiedner Hauptstraße 35, Wien") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of()) + .build(); + try { + Assert.assertThat(operationService.add(operation), is(1L)); + } catch (ServiceException e) { + e.printStackTrace(); + } + } + + @Test(expected = InvalidOperationException.class) + public void testaddFaultyOperation3Test() throws InvalidOperationException { + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode("ALP-95E7") + .created(Instant.now()) + .destination("") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of()) + .build(); + try { + Assert.assertThat(operationService.add(operation), is(1L)); + } catch (ServiceException e) { + e.printStackTrace(); + } + } + + @Test(expected = InvalidOperationException.class) + public void testaddFaultyOperation4Test() throws InvalidOperationException { + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode("") + .created(Instant.now()) + .destination("Römergasse 7, 2500 Baden") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of()) + .build(); + try { + Assert.assertThat(operationService.add(operation), is(1L)); + } catch (ServiceException e) { + e.printStackTrace(); + } + } +} diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceUnitTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceUnitTest.java new file mode 100644 index 0000000..29a840b --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceUnitTest.java @@ -0,0 +1,153 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service; + +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.OperationDAO; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation.Severity; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation.Status; +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.VehicleType; +import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidOperationException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException; +import java.time.Instant; +import java.util.Set; +import org.junit.Assert; +import org.junit.Test; + +public class OperationServiceUnitTest { + private final OperationDAO operationDAO = mock(OperationDAO.class); + private final OperationService operationService = new OperationServiceImpl(operationDAO); + + @Test + public void addOperationTest() { + try { + when(operationDAO.add(any())).thenReturn(1L); + } catch (PersistenceException e) { + fail(); + } + Vehicle vehicle = + Vehicle.builder() + .status(Vehicle.Status.FREI_FUNK) + .constructionType(ConstructionType.HOCHDACH) + .name("BKTW_123") + .hasNef(true) + .type(VehicleType.BKTW) + .build(); + + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode("ALP-95E7") + .created(Instant.now()) + .destination("Wiedner Hauptstraße 35, Wien") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of(vehicle)) + .build(); + try { + Assert.assertThat(operationService.add(operation), is(1L)); + } catch (InvalidOperationException | ServiceException e) { + fail(); + } + } + + @Test(expected = InvalidOperationException.class) + public void addFaultyOperationTest() throws InvalidOperationException { + Vehicle vehicle = + Vehicle.builder() + .status(Vehicle.Status.FREI_FUNK) + .constructionType(ConstructionType.HOCHDACH) + .name("BKTW_123") + .hasNef(true) + .type(VehicleType.BKTW) + .build(); + Vehicle vehicle1 = + Vehicle.builder() + .status(Vehicle.Status.ABGEMELDET) + .constructionType(ConstructionType.HOCHDACH) + .name("BKTW_123") + .hasNef(true) + .type(VehicleType.BKTW) + .build(); + + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode("ALP-95E7") + .created(Instant.now()) + .destination("Wiedner Hauptstraße 35, Wien") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of(vehicle, vehicle1)) + .build(); + try { + Assert.assertThat(operationService.add(operation), is(1L)); + } catch (ServiceException e) { + fail(); + } + } + + @Test(expected = InvalidOperationException.class) + public void addFaultyOperation2Test() throws InvalidOperationException { + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode("ALP-95E7") + .created(Instant.now()) + .destination("Wiedner Hauptstraße 35, Wien") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of()) + .build(); + try { + Assert.assertThat(operationService.add(operation), is(1L)); + } catch (ServiceException e) { + e.printStackTrace(); + } + } + + @Test(expected = InvalidOperationException.class) + public void addFaultyOperation3Test() throws InvalidOperationException { + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode("ALP-95E7") + .created(Instant.now()) + .destination("") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of()) + .build(); + try { + Assert.assertThat(operationService.add(operation), is(1L)); + } catch (ServiceException e) { + e.printStackTrace(); + } + } + + @Test(expected = InvalidOperationException.class) + public void addFaultyOperation4Test() throws InvalidOperationException { + Operation operation = + Operation.builder() + .status(Status.ACTIVE) + .opCode("") + .created(Instant.now()) + .destination("Römergasse 7, 2500 Baden") + .additionalInfo("HTU Wien") + .severity(Severity.B) + .vehicles(Set.of()) + .build(); + try { + Assert.assertThat(operationService.add(operation), is(1L)); + } catch (ServiceException e) { + e.printStackTrace(); + } + } +} diff --git a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/VehicleServiceTestConfiguration.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/VehicleServiceTestConfiguration.java new file mode 100644 index 0000000..895973a --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/VehicleServiceTestConfiguration.java @@ -0,0 +1,17 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service; + +import static org.mockito.Mockito.mock; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +@Configuration +public class VehicleServiceTestConfiguration { + + @Bean + @Primary + public VehicleService vehicleService() { + return mock(VehicleServiceImpl.class); + } +} |