diff options
Diffstat (limited to 'src')
4 files changed, 77 insertions, 48 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDAO.java index fe12952..2f0df44 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDAO.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDAO.java @@ -34,6 +34,16 @@ public interface VehicleDAO {      List<Vehicle> list() throws PersistenceException;      /** +     * Returns the vehicle with the given id. +     * +     * @param vehicleId id of the vehicle that should be returned +     * @return vehicle with the given id +     * @throws ElementNotFoundException if no vehicle with the given id exists +     * @throws PersistenceException if the vehicle could not be loaded +     */ +    Vehicle get(long vehicleId) throws ElementNotFoundException, PersistenceException; + +    /**       * Remove vehicle with the given id from the store.       *       * @param id of the vehicle that should be removed diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDao.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDao.java index ca1d45c..8f0d28b 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDao.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDao.java @@ -7,6 +7,7 @@ import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle.Veh  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; @@ -114,19 +115,8 @@ public class VehicleDatabaseDao implements VehicleDAO {                                              + "Vehicle where VehicleVersion.id=Vehicle.version");              pstmt.executeQuery();              ResultSet rs = pstmt.getResultSet(); -            while (rs.next()) { -                Vehicle vehicle = -                        Vehicle.builder() -                                .name(rs.getString("name")) -                                .constructionType( -                                        ConstructionType.valueOf(rs.getString("constructionType"))) -                                .status(Status.valueOf(rs.getString("status"))) -                                .id(rs.getInt("id")) -                                .hasNef(rs.getBoolean("hasNef")) -                                .type(VehicleType.valueOf(rs.getString("type").replace("-", "_"))) -                                .build(); -                result.add(vehicle); -            } +            while (rs.next()) result.add(vehicleFromRS(rs)); +          } catch (SQLException e) {              throw new PersistenceException("Die Werte konnten nicht geladen werden.", e);          } finally { @@ -143,5 +133,39 @@ public class VehicleDatabaseDao implements VehicleDAO {      }      @Override +    public Vehicle get(long id) throws ElementNotFoundException, PersistenceException { +        String sql = +                "SELECT a.id, b.name, b.constructionType, b.type, a.status, b.hasNef" +                        + " FROM Vehicle a" +                        + " INNER JOIN VehicleVersion b" +                        + " ON version = b.id" +                        + " WHERE a.id = ?"; + +        try { +            Connection con = jdbcConnectionManager.getConnection(); +            try (PreparedStatement pstmt = con.prepareStatement(sql); +                    ResultSet rs = pstmt.executeQuery()) { + +                if (!rs.first()) throw new ElementNotFoundException("No such vehicle exists"); + +                return vehicleFromRS(rs); +            } +        } catch (SQLException e) { +            throw new PersistenceException(e); +        } +    } + +    @Override      public void remove(long id) throws ElementNotFoundException, PersistenceException {} + +    private Vehicle vehicleFromRS(ResultSet rs) throws SQLException { +        return Vehicle.builder() +                .id(rs.getLong("id")) +                .name(rs.getString("name")) +                .constructionType(ConstructionType.valueOf(rs.getString("constructionType"))) +                .type(VehicleType.valueOf(rs.getString("type"))) +                .status(Status.valueOf(rs.getString("constructionType"))) +                .hasNef(rs.getBoolean("hasNef")) +                .build(); +    }  } 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 index a267b6f..8203ef3 100644 --- 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 @@ -1,15 +1,15 @@  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.dao.VehicleDAO;  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.einsatzverwaltung.dto.Vehicle.Status; +import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException;  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.EnumSet;  import java.util.List;  import org.slf4j.Logger;  import org.slf4j.LoggerFactory; @@ -22,36 +22,31 @@ public class RegistrationServiceImpl implements RegistrationService {      private static final Logger LOG = LoggerFactory.getLogger(RegistrationServiceImpl.class);      private final RegistrationDAO registrationDAO; -    private final VehicleService vehicleService; +    private final VehicleDAO vehicleDAO;      @Autowired -    public RegistrationServiceImpl(RegistrationDAO registrationDAO, VehicleService vehicleService) { +    public RegistrationServiceImpl(RegistrationDAO registrationDAO, VehicleDAO vehicleDAO) {          this.registrationDAO = registrationDAO; -        this.vehicleService = vehicleService; +        this.vehicleDAO = vehicleDAO;      }      @Override      public List<Long> add(long vehicleId, List<Registration> registrations)              throws InvalidVehicleException, InvalidRegistrationException, ServiceException { -        Vehicle vehicle = -                vehicleService -                        .list(EnumSet.of(Status.ABGEMELDET)) -                        .stream() -                        .filter(v -> v.id() == vehicleId) -                        .findFirst() -                        .orElse(null); +        if (vehicleId <= 0) throw new InvalidVehicleException("VehicleId invalid"); -        if (vehicle == null) { -            throw new ServiceException("no vehicle with this id"); -        } - -        RegistrationValidator.validate(vehicle, registrations);          try { +            Vehicle vehicle = vehicleDAO.get(vehicleId); + +            RegistrationValidator.validate(vehicle, registrations); +              return registrationDAO.add(vehicle.id(), registrations);          } catch (PersistenceException e) {              LOG.warn("PersistenceException caught, throwing matching ServiceException");              throw new ServiceException(e); +        } catch (ElementNotFoundException e) { +            throw new InvalidVehicleException(e);          }      } 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 index f3efbef..88642ee 100644 --- 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 @@ -1,23 +1,26 @@  package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service; -import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong;  import static org.mockito.Mockito.when;  import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.RegistrationDAO; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.VehicleDAO;  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.Builder;  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.ElementNotFoundException;  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.time.Instant;  import java.time.LocalDate;  import java.time.temporal.ChronoUnit; -import java.util.Arrays;  import java.util.LinkedList;  import java.util.List;  import org.junit.Before; @@ -31,35 +34,32 @@ import org.mockito.junit.MockitoRule;  public class RegistrationServiceImplTest { -    @Mock RegistrationDAO daoMock; +    @Mock private RegistrationDAO registrationDAO; -    @Mock VehicleService vehicleService; +    @Mock private VehicleDAO vehicleDAO;      @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();      @Rule public ExpectedException thrown = ExpectedException.none();      @Before -    public void setUp() throws ServiceException { +    public void setUp() throws ElementNotFoundException, PersistenceException {          MockitoAnnotations.initMocks(this); -        when(vehicleService.list(any())) -                .thenReturn( -                        Arrays.asList( -                                Vehicle.builder() -                                        .id(1) -                                        .name("RTW-1") -                                        .constructionType(ConstructionType.HOCHDACH) -                                        .status(Status.ABGEMELDET) -                                        .type(VehicleType.RTW) -                                        .hasNef(true) -                                        .build())); +        Builder b = +                Vehicle.builder() +                        .name("RTW-1") +                        .constructionType(ConstructionType.HOCHDACH) +                        .status(Status.ABGEMELDET) +                        .type(VehicleType.RTW) +                        .hasNef(true); +        when(vehicleDAO.get(anyLong())).thenAnswer(ans -> b.id(ans.getArgument(0)).build());      }      @Test      public void addValidRegistrationsShouldSucceed()              throws InvalidRegistrationException, ServiceException, InvalidVehicleException {          RegistrationService registrationService = -                new RegistrationServiceImpl(daoMock, vehicleService); +                new RegistrationServiceImpl(registrationDAO, vehicleDAO);          List<Registration> registrations = new LinkedList<>();          Vehicle vehicle =                  Vehicle.builder() @@ -116,7 +116,7 @@ public class RegistrationServiceImplTest {              throws InvalidRegistrationException, ServiceException, InvalidVehicleException {          thrown.expect(InvalidRegistrationException.class);          RegistrationService registrationService = -                new RegistrationServiceImpl(daoMock, vehicleService); +                new RegistrationServiceImpl(registrationDAO, vehicleDAO);          List<Registration> registrations = new LinkedList<>();          Vehicle vehicle =                  Vehicle.builder()  | 
