From 126b9a07facec2916156f74b5f632f161df19f11 Mon Sep 17 00:00:00 2001 From: Felix Kehrer Date: Mon, 7 May 2018 15:55:23 +0200 Subject: Rename Registration Dao & service to conventional style --- .../einsatzverwaltung/dao/H2RegistrationDAO.java | 105 ------------- .../dao/RegistrationDatabaseDAO.java | 106 ++++++++++++++ .../service/RegistrationServiceImpl.java | 45 ++++++ .../service/SimpleRegistrationService.java | 45 ------ .../dao/H2RegistrationDAOTest.java | 162 --------------------- .../dao/RegistrationDatabaseDAOTest.java | 162 +++++++++++++++++++++ .../service/RegistrationServiceImplTest.java | 122 ++++++++++++++++ .../service/SimpleRegistrationServiceTest.java | 124 ---------------- 8 files changed, 435 insertions(+), 436 deletions(-) delete mode 100644 src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java create mode 100644 src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java create mode 100644 src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImpl.java delete mode 100644 src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationService.java delete mode 100644 src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAOTest.java create mode 100644 src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAOTest.java create mode 100644 src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImplTest.java delete mode 100644 src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationServiceTest.java (limited to 'src') diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java deleted file mode 100644 index f76c706..0000000 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java +++ /dev/null @@ -1,105 +0,0 @@ -package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao; - -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Registration; -import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException; -import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException; -import at.ac.tuwien.sepm.assignment.groupphase.util.JDBCConnectionManager; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.sql.Timestamp; -import java.util.LinkedList; -import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Repository; - -@Repository -public class H2RegistrationDAO implements RegistrationDAO { - - private static final Logger LOG = LoggerFactory.getLogger(H2RegistrationDAO.class); - - private static final String ADD_REGISTRATION = - "INSERT INTO Registration (vehicleId, employeeId, start, end, active) VALUES (?,?,?,?,?);"; - private static final String UPDATE_VEHICLE = - "UPDATE Vehicle SET status = 'frei_wache' WHERE id = ?;"; - - private PreparedStatement addRegistration; - private PreparedStatement updateVehicle; - - private Connection connection; - - @Autowired - public H2RegistrationDAO(JDBCConnectionManager connectionManager) throws PersistenceException { - try { - connection = connectionManager.getConnection(); - addRegistration = - connection.prepareStatement(ADD_REGISTRATION, Statement.RETURN_GENERATED_KEYS); - updateVehicle = connection.prepareStatement(UPDATE_VEHICLE); - } catch (SQLException e) { - LOG.error("Could not get connection or preparation of statement failed"); - throw new PersistenceException(e); - } - } - - @Override - public List add(long vehicleId, List registrations) - throws PersistenceException { - List returnValues = new LinkedList<>(); - try { - connection.setAutoCommit(false); - for (Registration registration : registrations) { - addRegistration.setLong(1, vehicleId); - addRegistration.setLong(2, registration.employee().id()); - addRegistration.setTimestamp(3, Timestamp.from(registration.start())); - addRegistration.setObject(4, registration.end()); - addRegistration.setBoolean( - 5, true); // ASSUMPTION: Registration gets created as active - addRegistration.executeUpdate(); - try (ResultSet rs = addRegistration.getGeneratedKeys()) { - if (rs.next()) { - returnValues.add(rs.getLong(1)); - } else { - LOG.error("No ResultSet was created while adding registration"); - throw new PersistenceException( - "Anmeldung konnte nicht gespeichert werden."); - } - } - } - - updateVehicle.setLong(1, vehicleId); - updateVehicle.executeUpdate(); - - connection.commit(); - return returnValues; - } catch (SQLException e) { - LOG.error( - "An SQLException occurred while trying to save registrations to database. " - + "Attempting a rollback. Error message: {}", - e.getMessage()); - try { - connection.rollback(); - } catch (SQLException e1) { - LOG.error("Rollback failed :("); - } - throw new PersistenceException(e); - } finally { - try { - connection.setAutoCommit(true); - } catch (SQLException e) { - LOG.error( - "Setting back AutoCommit to false failed! Error message: {}", - e.getMessage()); - // SonarLint insists on me not throwing anything here... - } - } - } - - @Override - public void remove(long id) throws ElementNotFoundException, PersistenceException { - throw new UnsupportedOperationException(); - } -} diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java new file mode 100644 index 0000000..e4bc0ab --- /dev/null +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java @@ -0,0 +1,106 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao; + +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Registration; +import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException; +import at.ac.tuwien.sepm.assignment.groupphase.util.JDBCConnectionManager; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Timestamp; +import java.util.LinkedList; +import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +@Repository +public class RegistrationDatabaseDAO implements RegistrationDAO { + + private static final Logger LOG = LoggerFactory.getLogger(RegistrationDatabaseDAO.class); + + private static final String ADD_REGISTRATION = + "INSERT INTO Registration (vehicleId, employeeId, start, end, active) VALUES (?,?,?,?,?);"; + private static final String UPDATE_VEHICLE = + "UPDATE Vehicle SET status = 'frei_wache' WHERE id = ?;"; + + private PreparedStatement addRegistration; + private PreparedStatement updateVehicle; + + private Connection connection; + + @Autowired + public RegistrationDatabaseDAO(JDBCConnectionManager connectionManager) + throws PersistenceException { + try { + connection = connectionManager.getConnection(); + addRegistration = + connection.prepareStatement(ADD_REGISTRATION, Statement.RETURN_GENERATED_KEYS); + updateVehicle = connection.prepareStatement(UPDATE_VEHICLE); + } catch (SQLException e) { + LOG.error("Could not get connection or preparation of statement failed"); + throw new PersistenceException(e); + } + } + + @Override + public List add(long vehicleId, List registrations) + throws PersistenceException { + List returnValues = new LinkedList<>(); + try { + connection.setAutoCommit(false); + for (Registration registration : registrations) { + addRegistration.setLong(1, vehicleId); + addRegistration.setLong(2, registration.employee().id()); + addRegistration.setTimestamp(3, Timestamp.from(registration.start())); + addRegistration.setObject(4, registration.end()); + addRegistration.setBoolean( + 5, true); // ASSUMPTION: Registration gets created as active + addRegistration.executeUpdate(); + try (ResultSet rs = addRegistration.getGeneratedKeys()) { + if (rs.next()) { + returnValues.add(rs.getLong(1)); + } else { + LOG.error("No ResultSet was created while adding registration"); + throw new PersistenceException( + "Anmeldung konnte nicht gespeichert werden."); + } + } + } + + updateVehicle.setLong(1, vehicleId); + updateVehicle.executeUpdate(); + + connection.commit(); + return returnValues; + } catch (SQLException e) { + LOG.error( + "An SQLException occurred while trying to save registrations to database. " + + "Attempting a rollback. Error message: {}", + e.getMessage()); + try { + connection.rollback(); + } catch (SQLException e1) { + LOG.error("Rollback failed :("); + } + throw new PersistenceException(e); + } finally { + try { + connection.setAutoCommit(true); + } catch (SQLException e) { + LOG.error( + "Setting back AutoCommit to false failed! Error message: {}", + e.getMessage()); + // SonarLint insists on me not throwing anything here... + } + } + } + + @Override + public void remove(long id) throws ElementNotFoundException, PersistenceException { + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImpl.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImpl.java new file mode 100644 index 0000000..b0605f0 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImpl.java @@ -0,0 +1,45 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service; + +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.RegistrationDAO; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Registration; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.RegistrationValidator; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle; +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.PersistenceException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException; +import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class RegistrationServiceImpl implements RegistrationService { + + private static final Logger LOG = LoggerFactory.getLogger(RegistrationServiceImpl.class); + + private final RegistrationDAO registrationDAO; + + @Autowired + public RegistrationServiceImpl(RegistrationDAO registrationDAO) { + this.registrationDAO = registrationDAO; + } + + @Override + public List add(Vehicle vehicle, List registrations) + throws InvalidVehicleException, InvalidRegistrationException, ServiceException { + RegistrationValidator.validate(vehicle, registrations); + try { + return registrationDAO.add(vehicle.id(), registrations); + } catch (PersistenceException e) { + LOG.warn("PersistenceException caught, throwing matching ServiceException"); + throw new ServiceException(e); + } + } + + @Override + public void remove(long registrationId) throws InvalidRegistrationException, ServiceException { + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationService.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationService.java deleted file mode 100644 index 5b26e39..0000000 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationService.java +++ /dev/null @@ -1,45 +0,0 @@ -package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service; - -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.RegistrationDAO; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Registration; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.RegistrationValidator; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle; -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.PersistenceException; -import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException; -import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class SimpleRegistrationService implements RegistrationService { - - private static final Logger LOG = LoggerFactory.getLogger(SimpleRegistrationService.class); - - private final RegistrationDAO registrationDAO; - - @Autowired - public SimpleRegistrationService(RegistrationDAO registrationDAO) { - this.registrationDAO = registrationDAO; - } - - @Override - public List add(Vehicle vehicle, List registrations) - throws InvalidVehicleException, InvalidRegistrationException, ServiceException { - RegistrationValidator.validate(vehicle, registrations); - try { - return registrationDAO.add(vehicle.id(), registrations); - } catch (PersistenceException e) { - LOG.warn("PersistenceException caught, throwing matching ServiceException"); - throw new ServiceException(e); - } - } - - @Override - public void remove(long registrationId) throws InvalidRegistrationException, ServiceException { - throw new UnsupportedOperationException(); - } -} 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 deleted file mode 100644 index 1180bfa..0000000 --- a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAOTest.java +++ /dev/null @@ -1,162 +0,0 @@ -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 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 returnvalues = registrationDAO.add(1, registrations); - assertFalse(returnvalues.isEmpty()); // can be improved... - } - - @Test - public void addRegistrationToInexistentVehicleShouldFail() throws PersistenceException { - thrown.expect(PersistenceException.class); - List 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/dao/RegistrationDatabaseDAOTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAOTest.java new file mode 100644 index 0000000..980c429 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAOTest.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 RegistrationDatabaseDAOTest { + + // 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 RegistrationDatabaseDAOTest() throws PersistenceException { + this.registrationDAO = new RegistrationDatabaseDAO(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 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 returnvalues = registrationDAO.add(1, registrations); + assertFalse(returnvalues.isEmpty()); // can be improved... + } + + @Test + public void addRegistrationToInexistentVehicleShouldFail() throws PersistenceException { + thrown.expect(PersistenceException.class); + List 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/RegistrationServiceImplTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImplTest.java new file mode 100644 index 0000000..7171f83 --- /dev/null +++ b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImplTest.java @@ -0,0 +1,122 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service; + +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 RegistrationServiceImplTest { + + @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 RegistrationServiceImpl(daoMock); + List 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 RegistrationServiceImpl(daoMock); + List 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/einsatzverwaltung/service/SimpleRegistrationServiceTest.java b/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationServiceTest.java deleted file mode 100644 index b1ef38f..0000000 --- a/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationServiceTest.java +++ /dev/null @@ -1,124 +0,0 @@ -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 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 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); - } -} -- cgit v1.2.3-70-g09d2