TS18: Add a specific exception for the persistence layer

This commit is contained in:
Ivaylo Ivanov 2020-04-05 17:39:55 +02:00
parent c10bbb59af
commit 1c31939b65
13 changed files with 98 additions and 92 deletions

View File

@ -4,12 +4,12 @@ import at.ac.tuwien.sepm.assignment.individual.endpoint.dto.HorseDto;
import at.ac.tuwien.sepm.assignment.individual.endpoint.mapper.HorseMapper; import at.ac.tuwien.sepm.assignment.individual.endpoint.mapper.HorseMapper;
import at.ac.tuwien.sepm.assignment.individual.entity.Horse; import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException; import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import at.ac.tuwien.sepm.assignment.individual.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.individual.service.HorseService; import at.ac.tuwien.sepm.assignment.individual.service.HorseService;
import at.ac.tuwien.sepm.assignment.individual.util.ValidationException; import at.ac.tuwien.sepm.assignment.individual.util.ValidationException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -77,7 +77,7 @@ public class HorseEndpoint {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Error during adding new horse: " + e.getMessage()); "Error during adding new horse: " + e.getMessage());
} catch (DataAccessException e) { } catch (PersistenceException e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
"Something went wrong during the communication with the database"); "Something went wrong during the communication with the database");
@ -96,7 +96,7 @@ public class HorseEndpoint {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Error during updating horse with id " + id + ": " + e.getMessage()); "Error during updating horse with id " + id + ": " + e.getMessage());
} catch (DataAccessException e) { } catch (PersistenceException e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
"Something went wrong during the communication with the database"); "Something went wrong during the communication with the database");
@ -117,7 +117,7 @@ public class HorseEndpoint {
LOGGER.info("DELETE " + BASE_URL + "/{}", id); LOGGER.info("DELETE " + BASE_URL + "/{}", id);
try { try {
horseService.deleteHorse(id); horseService.deleteHorse(id);
} catch (DataAccessException e) { } catch (PersistenceException e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
"Something went wrong during the communication with the database"); "Something went wrong during the communication with the database");

View File

@ -6,6 +6,7 @@ import at.ac.tuwien.sepm.assignment.individual.endpoint.mapper.HorseMapper;
import at.ac.tuwien.sepm.assignment.individual.endpoint.mapper.OwnerMapper; import at.ac.tuwien.sepm.assignment.individual.endpoint.mapper.OwnerMapper;
import at.ac.tuwien.sepm.assignment.individual.entity.Owner; import at.ac.tuwien.sepm.assignment.individual.entity.Owner;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException; import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import at.ac.tuwien.sepm.assignment.individual.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.individual.service.OwnerService; import at.ac.tuwien.sepm.assignment.individual.service.OwnerService;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
@ -16,7 +17,6 @@ import at.ac.tuwien.sepm.assignment.individual.util.ValidationException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -92,7 +92,7 @@ public class OwnerEndpoint {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Error during adding new owner: " + e.getMessage()); "Error during adding new owner: " + e.getMessage());
} catch (DataAccessException e) { } catch (PersistenceException e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
"Something went wrong during the communication with the database"); "Something went wrong during the communication with the database");
@ -111,7 +111,7 @@ public class OwnerEndpoint {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Error during updating owner with id " + id + ": " + e.getMessage()); "Error during updating owner with id " + id + ": " + e.getMessage());
} catch (DataAccessException e) { } catch (PersistenceException e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
"Something went wrong during the communication with the database"); "Something went wrong during the communication with the database");
@ -132,7 +132,7 @@ public class OwnerEndpoint {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.FORBIDDEN, throw new ResponseStatusException(HttpStatus.FORBIDDEN,
"The requested owner cannot be deleted because there are horses that are assigned to him/her"); "The requested owner cannot be deleted because there are horses that are assigned to him/her");
}catch (DataAccessException e) { }catch (PersistenceException e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
"Something went wrong during the communication with the database"); "Something went wrong during the communication with the database");

View File

@ -0,0 +1,13 @@
package at.ac.tuwien.sepm.assignment.individual.exception;
public class PersistenceException extends RuntimeException {
public PersistenceException(String message) {
super(message);
}
public PersistenceException(String message, Exception e) {
super(message, e);
}
}

View File

@ -2,7 +2,7 @@ package at.ac.tuwien.sepm.assignment.individual.persistence;
import at.ac.tuwien.sepm.assignment.individual.entity.Horse; import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException; import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import org.springframework.dao.DataAccessException; import at.ac.tuwien.sepm.assignment.individual.exception.PersistenceException;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -13,46 +13,46 @@ public interface HorseDao {
/** /**
* @param id of the horse to find. * @param id of the horse to find.
* @return the horse with the specified id. * @return the horse with the specified id.
* @throws DataAccessException will be thrown if something goes wrong during the database access. * @throws PersistenceException will be thrown if something goes wrong during the database access.
* @throws NotFoundException will be thrown if the horse could not be found in the database. * @throws NotFoundException will be thrown if the horse could not be found in the database.
*/ */
Horse findOneById(Long id); Horse findOneById(Long id) throws NotFoundException, PersistenceException;
/** /**
* @return a list of all horses in the system * @return a list of all horses in the system
* @throws NotFoundException will be thrown if no horses are present in the database * @throws NotFoundException will be thrown if no horses are present in the database
* @throws DataAccessException will be thrown if something goes wrong during the database access * @throws PersistenceException will be thrown if something goes wrong during the database access
*/ */
List<Horse> getAll() throws NotFoundException; List<Horse> getAll() throws NotFoundException, PersistenceException;
/** /**
* @param filters to use for filtering the horses * @param filters to use for filtering the horses
* @return a list of all horses that fill the criteria * @return a list of all horses that fill the criteria
* @throws NotFoundException wil be thrown if no horses fill the criteria * @throws NotFoundException wil be thrown if no horses fill the criteria
* @throws DataAccessException will be thrown if something goes wrong during the database access * @throws PersistenceException will be thrown if something goes wrong during the database access.
*/ */
List<Horse> getFiltered(Map<String, String> filters) throws NotFoundException; List<Horse> getFiltered(Map<String, String> filters) throws NotFoundException, PersistenceException;
/** /**
* @param horse that specifies the horse to add * @param horse that specifies the horse to add
* @return the newly created horse * @return the newly created horse
* @throws DataAccessException will be thrown if something goes wrong during the database access. * @throws PersistenceException will be thrown if something goes wrong during the database access.
*/ */
Horse addHorse(Horse horse); Horse addHorse(Horse horse) throws PersistenceException;
/** /**
* @param horse that specifies the new horse values alongside with the id of the horse to update * @param horse that specifies the new horse values alongside with the id of the horse to update
* @return the updated horse * @return the updated horse
* @throws DataAccessException will be thrown if something goes wrong during the database access. * @throws PersistenceException will be thrown if something goes wrong during the database access.
* @throws IOException will be thrown if the old horse image could not be deleted * @throws IOException will be thrown if the old horse image could not be deleted
*/ */
Horse updateHorse(Horse horse) throws DataAccessException, IOException; Horse updateHorse(Horse horse) throws PersistenceException, IOException;
/** /**
* @param id of the horse to delete * @param id of the horse to delete
* @throws DataAccessException will be thrown if something goes wrong during the database access. * @throws PersistenceException will be thrown if something goes wrong during the database access.
* @throws NotFoundException will be thrown if the horse could not be found in the database. * @throws NotFoundException will be thrown if the horse could not be found in the database.
* @throws IOException will be thrown if the horse image could not be deleted * @throws IOException will be thrown if the horse image could not be deleted
*/ */
void deleteHorse(Long id) throws DataAccessException, NotFoundException, IOException; void deleteHorse(Long id) throws PersistenceException, NotFoundException, IOException;
} }

View File

@ -3,7 +3,7 @@ package at.ac.tuwien.sepm.assignment.individual.persistence;
import at.ac.tuwien.sepm.assignment.individual.entity.Horse; import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
import at.ac.tuwien.sepm.assignment.individual.entity.Owner; import at.ac.tuwien.sepm.assignment.individual.entity.Owner;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException; import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import org.springframework.dao.DataAccessException; import at.ac.tuwien.sepm.assignment.individual.exception.PersistenceException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -13,15 +13,13 @@ public interface OwnerDao {
/** /**
* @param id of the owner to find. * @param id of the owner to find.
* @return the owner with the specified id. * @return the owner with the specified id.
* @throws DataAccessException will be thrown if something goes wrong during the database access.
* @throws NotFoundException will be thrown if the owner could not be found in the database. * @throws NotFoundException will be thrown if the owner could not be found in the database.
*/ */
Owner findOneById(Long id); Owner findOneById(Long id) throws NotFoundException;
/** /**
* @return a list of all owners in the system * @return a list of all owners in the system
* @throws NotFoundException will be thrown if no owners are present in the database * @throws NotFoundException will be thrown if no owners are present in the database
* @throws DataAccessException will be thrown if something goes wrong during the database access
*/ */
List<Owner> getAll() throws NotFoundException; List<Owner> getAll() throws NotFoundException;
@ -29,7 +27,6 @@ public interface OwnerDao {
* @param filters to use for filtering the owners * @param filters to use for filtering the owners
* @return a list of all owners that fill the criteria * @return a list of all owners that fill the criteria
* @throws NotFoundException wil be thrown if no owners fill the criteria * @throws NotFoundException wil be thrown if no owners fill the criteria
* @throws DataAccessException will be thrown if something goes wrong during the database access
*/ */
List<Owner> getFiltered(Map<String, String> filters) throws NotFoundException; List<Owner> getFiltered(Map<String, String> filters) throws NotFoundException;
@ -37,28 +34,28 @@ public interface OwnerDao {
* @param id of the owner whose horses the database should obtain * @param id of the owner whose horses the database should obtain
* @return a list of all horses that the specified owner owns * @return a list of all horses that the specified owner owns
* @throws NotFoundException will be thrown if the owner owns no horses or if the owner could not be found * @throws NotFoundException will be thrown if the owner owns no horses or if the owner could not be found
* @throws DataAccessException will be thrown if something goes wrong during the database access
*/ */
List<Horse> getOwnedHorses(Long id) throws NotFoundException; List<Horse> getOwnedHorses(Long id) throws NotFoundException;
/** /**
* @param owner that specifies the owner to add * @param owner that specifies the owner to add
* @return the newly created owner * @return the newly created owner
* @throws DataAccessException will be thrown if something goes wrong during the database access. * @throws PersistenceException will be thrown if something goes wrong during the database access.
*/ */
Owner addOwner(Owner owner); Owner addOwner(Owner owner) throws PersistenceException;
/** /**
* @param owner that specifies the new owner values alongside with the id of the owner to update * @param owner that specifies the new owner values alongside with the id of the owner to update
* @return the updated owner * @return the updated owner
* @throws DataAccessException will be thrown if something goes wrong during the database access. * @throws PersistenceException will be thrown if something goes wrong during the database access.
* @throws NotFoundException wil be thrown if no owners fill the criteria
*/ */
Owner updateOwner(Owner owner) throws DataAccessException; Owner updateOwner(Owner owner) throws PersistenceException, NotFoundException;
/** /**
* @param id of the owner to delete * @param id of the owner to delete
* @throws DataAccessException will be thrown if something goes wrong during the database access. * @throws PersistenceException will be thrown if something goes wrong during the database access.
* @throws NotFoundException will be thrown if the owner could not be found in the database. * @throws NotFoundException will be thrown if the owner could not be found in the database.
*/ */
void deleteOwner(Long id) throws DataAccessException, NotFoundException; void deleteOwner(Long id) throws PersistenceException, NotFoundException;
} }

View File

@ -3,6 +3,7 @@ package at.ac.tuwien.sepm.assignment.individual.persistence.impl;
import at.ac.tuwien.sepm.assignment.individual.entity.Horse; import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
import at.ac.tuwien.sepm.assignment.individual.enums.ERace; import at.ac.tuwien.sepm.assignment.individual.enums.ERace;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException; import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import at.ac.tuwien.sepm.assignment.individual.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.individual.persistence.FileDao; import at.ac.tuwien.sepm.assignment.individual.persistence.FileDao;
import at.ac.tuwien.sepm.assignment.individual.persistence.HorseDao; import at.ac.tuwien.sepm.assignment.individual.persistence.HorseDao;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -149,14 +150,13 @@ public class HorseJdbcDao implements HorseDao {
}, keyHolder); }, keyHolder);
if (changes == 0) if (changes == 0)
throw new DataAccessException("Creating horse failed, no rows affected") {}; throw new PersistenceException("Creating horse failed, no rows affected");
horse.setId(((Number)keyHolder.getKeys().get("id")).longValue()); horse.setId(((Number)keyHolder.getKeys().get("id")).longValue());
return horse; return horse;
} catch (DataAccessException e) { } catch (DataAccessException e) {
// We are doing this in order to not change the exception type throw new PersistenceException("Adding new records failed", e);
throw new DataAccessException("Adding new records failed", e) {};
} }
} }
@ -197,7 +197,7 @@ public class HorseJdbcDao implements HorseDao {
}); });
if (changes == 0) if (changes == 0)
throw new DataAccessException("Updating horse failed, no rows affected") {}; throw new PersistenceException("Updating horse failed, no rows affected");
horse.setCreatedAt(oldHorse.getCreatedAt()); horse.setCreatedAt(oldHorse.getCreatedAt());
@ -206,8 +206,7 @@ public class HorseJdbcDao implements HorseDao {
return horse; return horse;
} catch (DataAccessException e) { } catch (DataAccessException e) {
// We are doing this in order to not change the exception type throw new PersistenceException("Updating records failed", e);
throw new DataAccessException("Updating records failed", e) {};
} }
} }
@ -225,12 +224,11 @@ public class HorseJdbcDao implements HorseDao {
}); });
if (changes == 0) if (changes == 0)
throw new DataAccessException("Deleting horse failed, no rows affected") {}; throw new PersistenceException("Deleting horse failed, no rows affected");
fileDao.delete(horseToDelete.getImagePath()); fileDao.delete(horseToDelete.getImagePath());
} catch(DataAccessException e){ } catch(DataAccessException e){
// We are doing this in order to not change the exception type throw new PersistenceException("Deleting records failed", e);
throw new DataAccessException("Deleting records failed", e) {};
} }
} }

View File

@ -3,6 +3,7 @@ package at.ac.tuwien.sepm.assignment.individual.persistence.impl;
import at.ac.tuwien.sepm.assignment.individual.entity.Horse; import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
import at.ac.tuwien.sepm.assignment.individual.entity.Owner; import at.ac.tuwien.sepm.assignment.individual.entity.Owner;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException; import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import at.ac.tuwien.sepm.assignment.individual.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.individual.persistence.OwnerDao; import at.ac.tuwien.sepm.assignment.individual.persistence.OwnerDao;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
@ -133,15 +134,14 @@ public class OwnerJdbcDao implements OwnerDao {
}, keyHolder); }, keyHolder);
if (changes == 0) if (changes == 0)
throw new DataAccessException("Creating owner failed, no rows affected") {}; throw new PersistenceException("Creating owner failed, no rows affected");
owner.setId(((Number)keyHolder.getKeys().get("id")).longValue()); owner.setId(((Number)keyHolder.getKeys().get("id")).longValue());
return owner; return owner;
} catch (DataAccessException e) { } catch (DataAccessException e) {
// We are doing this in order to not change the exception type throw new PersistenceException("Adding new records failed", e);
throw new DataAccessException("Adding new records failed", e) {};
} }
} }
@ -172,14 +172,13 @@ public class OwnerJdbcDao implements OwnerDao {
}); });
if (changes == 0) if (changes == 0)
throw new DataAccessException("Updating owner failed, no rows affected") {}; throw new PersistenceException("Updating owner failed, no rows affected");
owner.setCreatedAt(oldOwner.getCreatedAt()); owner.setCreatedAt(oldOwner.getCreatedAt());
return owner; return owner;
} catch(DataAccessException e) { } catch(DataAccessException e) {
// We are doing this in order to not change the exception type throw new PersistenceException("Updating records failed", e);
throw new DataAccessException("Updating records failed", e) {};
} }
} }
@ -200,11 +199,10 @@ public class OwnerJdbcDao implements OwnerDao {
}); });
if (changes == 0) if (changes == 0)
throw new DataAccessException("Deleting owner failed, no rows affected") {}; throw new PersistenceException("Deleting owner failed, no rows affected");
} catch(DataAccessException e){ } catch(DataAccessException e){
// We are doing this in order to not change the exception type throw new PersistenceException("Deleting records failed", e);
throw new DataAccessException("Deleting records failed", e) {};
} }
} }

View File

@ -2,8 +2,8 @@ package at.ac.tuwien.sepm.assignment.individual.service;
import at.ac.tuwien.sepm.assignment.individual.entity.Horse; import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException; import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import at.ac.tuwien.sepm.assignment.individual.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.individual.util.ValidationException; import at.ac.tuwien.sepm.assignment.individual.util.ValidationException;
import org.springframework.dao.DataAccessException;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
@ -37,7 +37,7 @@ public interface HorseService {
* @param horse to add. * @param horse to add.
* @return the new horse. * @return the new horse.
* @throws ValidationException will be thrown if something goes wrong during verification. * @throws ValidationException will be thrown if something goes wrong during verification.
* @throws DataAccessException will be thrown if the horse could not be saved in the database. * @throws PersistenceException will be thrown if the horse could not be saved in the database.
*/ */
Horse addHorse(Horse horse); Horse addHorse(Horse horse);
@ -45,18 +45,18 @@ public interface HorseService {
* @param horse that specifies the new horse values alongside with the id of the horse to update * @param horse that specifies the new horse values alongside with the id of the horse to update
* @return the updated horse * @return the updated horse
* @throws ValidationException will be thrown if something goes wrong during verification. * @throws ValidationException will be thrown if something goes wrong during verification.
* @throws DataAccessException will be thrown if the horse could not be saved in the database. * @throws PersistenceException will be thrown if the horse could not be saved in the database.
* @throws IOException will be thrown if the old horse image could not be deleted * @throws IOException will be thrown if the old horse image could not be deleted
*/ */
Horse updateHorse(Horse horse) throws ValidationException, DataAccessException, IOException; Horse updateHorse(Horse horse) throws ValidationException, PersistenceException, IOException;
/** /**
* @param id of the horse to delete * @param id of the horse to delete
* @throws NotFoundException will be thrown if the horse could not be found in the system * @throws NotFoundException will be thrown if the horse could not be found in the system
* @throws DataAccessException will be thrown if the horse could not be deleted from the database * @throws PersistenceException will be thrown if the horse could not be deleted from the database
* @throws IOException will be thrown if the horse image could not be deleted * @throws IOException will be thrown if the horse image could not be deleted
*/ */
void deleteHorse(Long id) throws NotFoundException, DataAccessException, IOException; void deleteHorse(Long id) throws NotFoundException, PersistenceException, IOException;
/** /**
* @param img image to upload * @param img image to upload

View File

@ -3,8 +3,8 @@ package at.ac.tuwien.sepm.assignment.individual.service;
import at.ac.tuwien.sepm.assignment.individual.entity.Horse; import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
import at.ac.tuwien.sepm.assignment.individual.entity.Owner; import at.ac.tuwien.sepm.assignment.individual.entity.Owner;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException; import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import at.ac.tuwien.sepm.assignment.individual.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.individual.util.ValidationException; import at.ac.tuwien.sepm.assignment.individual.util.ValidationException;
import org.springframework.dao.DataAccessException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -38,7 +38,7 @@ public interface OwnerService {
* @param id of the owner whose horses the database should obtain * @param id of the owner whose horses the database should obtain
* @return a list of all horses that the specified owner owns * @return a list of all horses that the specified owner owns
* @throws NotFoundException will be thrown if the owner owns no horses or if the owner could not be found * @throws NotFoundException will be thrown if the owner owns no horses or if the owner could not be found
* @throws DataAccessException will be thrown if something goes wrong during the database access * @throws PersistenceException will be thrown if something goes wrong during the database access
*/ */
List<Horse> getOwnedHorses(Long id) throws NotFoundException; List<Horse> getOwnedHorses(Long id) throws NotFoundException;
@ -46,22 +46,22 @@ public interface OwnerService {
* @param owner to create * @param owner to create
* @return the new owner * @return the new owner
* @throws ValidationException will be thrown if something goes wrong during verification. * @throws ValidationException will be thrown if something goes wrong during verification.
* @throws DataAccessException will be thrown if the owner could not be saved in the database. * @throws PersistenceException will be thrown if the owner could not be saved in the database.
*/ */
Owner addOwner(Owner owner) throws ValidationException, DataAccessException; Owner addOwner(Owner owner) throws ValidationException, PersistenceException;
/** /**
* @param owner that specifies the new owner values alongside with the id of the owner to update * @param owner that specifies the new owner values alongside with the id of the owner to update
* @return the updated owner * @return the updated owner
* @throws ValidationException will be thrown if something goes wrong during verification. * @throws ValidationException will be thrown if something goes wrong during verification.
* @throws DataAccessException will be thrown if the owner could not be saved in the database. * @throws PersistenceException will be thrown if the owner could not be saved in the database.
*/ */
Owner updateOwner(Owner owner) throws ValidationException, DataAccessException; Owner updateOwner(Owner owner) throws ValidationException, PersistenceException;
/** /**
* @param id of the owner to delete * @param id of the owner to delete
* @throws NotFoundException will be thrown if the owner could not be found in the system * @throws NotFoundException will be thrown if the owner could not be found in the system
* @throws DataAccessException will be thrown if the owner could not be deleted from the database * @throws PersistenceException will be thrown if the owner could not be deleted from the database
*/ */
void deleteOwner(Long id) throws NotFoundException, DataAccessException; void deleteOwner(Long id) throws NotFoundException, PersistenceException;
} }

View File

@ -2,6 +2,7 @@ package at.ac.tuwien.sepm.assignment.individual.service.impl;
import at.ac.tuwien.sepm.assignment.individual.entity.Horse; import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException; import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import at.ac.tuwien.sepm.assignment.individual.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.individual.persistence.FileDao; import at.ac.tuwien.sepm.assignment.individual.persistence.FileDao;
import at.ac.tuwien.sepm.assignment.individual.persistence.HorseDao; import at.ac.tuwien.sepm.assignment.individual.persistence.HorseDao;
import at.ac.tuwien.sepm.assignment.individual.service.HorseService; import at.ac.tuwien.sepm.assignment.individual.service.HorseService;
@ -10,7 +11,6 @@ import at.ac.tuwien.sepm.assignment.individual.util.Validator;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -53,21 +53,21 @@ public class SimpleHorseService implements HorseService {
} }
@Override @Override
public Horse addHorse(Horse horse) throws ValidationException, DataAccessException { public Horse addHorse(Horse horse) throws ValidationException, PersistenceException {
this.validator.validateNewHorse(horse); this.validator.validateNewHorse(horse);
LOGGER.debug("addHorse({})", horse.toString()); LOGGER.debug("addHorse({})", horse.toString());
return horseJdbcDao.addHorse(horse); return horseJdbcDao.addHorse(horse);
} }
@Override @Override
public Horse updateHorse(Horse horse) throws ValidationException, DataAccessException, IOException { public Horse updateHorse(Horse horse) throws ValidationException, PersistenceException, IOException {
this.validator.validateUpdateHorse(horse); this.validator.validateUpdateHorse(horse);
LOGGER.debug("updateHorse({})", horse.toString()); LOGGER.debug("updateHorse({})", horse.toString());
return horseJdbcDao.updateHorse(horse); return horseJdbcDao.updateHorse(horse);
} }
@Override @Override
public void deleteHorse(Long id) throws NotFoundException, DataAccessException, IOException { public void deleteHorse(Long id) throws NotFoundException, PersistenceException, IOException {
LOGGER.debug("deleteHorse({})", id); LOGGER.debug("deleteHorse({})", id);
horseJdbcDao.deleteHorse(id); horseJdbcDao.deleteHorse(id);
} }

View File

@ -3,6 +3,7 @@ package at.ac.tuwien.sepm.assignment.individual.service.impl;
import at.ac.tuwien.sepm.assignment.individual.entity.Horse; import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
import at.ac.tuwien.sepm.assignment.individual.entity.Owner; import at.ac.tuwien.sepm.assignment.individual.entity.Owner;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException; import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import at.ac.tuwien.sepm.assignment.individual.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.individual.persistence.OwnerDao; import at.ac.tuwien.sepm.assignment.individual.persistence.OwnerDao;
import at.ac.tuwien.sepm.assignment.individual.service.OwnerService; import at.ac.tuwien.sepm.assignment.individual.service.OwnerService;
import at.ac.tuwien.sepm.assignment.individual.util.ValidationException; import at.ac.tuwien.sepm.assignment.individual.util.ValidationException;
@ -15,7 +16,6 @@ import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@ -57,21 +57,21 @@ public class SimpleOwnerService implements OwnerService {
} }
@Override @Override
public Owner addOwner(Owner owner) throws ValidationException, DataAccessException { public Owner addOwner(Owner owner) throws ValidationException, PersistenceException {
LOGGER.debug("addOwner({})", owner); LOGGER.debug("addOwner({})", owner);
this.validator.validateNewOwner(owner); this.validator.validateNewOwner(owner);
return ownerDao.addOwner(owner); return ownerDao.addOwner(owner);
} }
@Override @Override
public Owner updateOwner(Owner owner) throws ValidationException, DataAccessException { public Owner updateOwner(Owner owner) throws ValidationException, PersistenceException {
LOGGER.debug("updateOwner({})", owner); LOGGER.debug("updateOwner({})", owner);
this.validator.validateUpdateOwner(owner); this.validator.validateUpdateOwner(owner);
return ownerDao.updateOwner(owner); return ownerDao.updateOwner(owner);
} }
@Override @Override
public void deleteOwner(Long id) throws NotFoundException, DataAccessException { public void deleteOwner(Long id) throws NotFoundException, PersistenceException {
LOGGER.debug("deleteOwner({})", id); LOGGER.debug("deleteOwner({})", id);
ownerDao.deleteOwner(id); ownerDao.deleteOwner(id);
} }

View File

@ -5,11 +5,11 @@ import static org.junit.jupiter.api.Assertions.*;
import at.ac.tuwien.sepm.assignment.individual.entity.Horse; import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
import at.ac.tuwien.sepm.assignment.individual.enums.ERace; import at.ac.tuwien.sepm.assignment.individual.enums.ERace;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException; import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import at.ac.tuwien.sepm.assignment.individual.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.individual.persistence.HorseDao; import at.ac.tuwien.sepm.assignment.individual.persistence.HorseDao;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import java.io.IOException; import java.io.IOException;
import java.sql.Date; import java.sql.Date;
@ -81,10 +81,10 @@ public abstract class HorseDaoTestBase {
} }
@Test @Test
@DisplayName("Adding a new horse with the incorrect parameters should throw DataAccessException") @DisplayName("Adding a new horse with the incorrect parameters should throw PersistenceException")
public void addingNewHorse_incorrectParameters_shouldThrowDataAccess() { public void addingNewHorse_incorrectParameters_shouldThrowPersistence() {
Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 80, Date.valueOf("2020-01-01"), null, null, null); Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 80, Date.valueOf("2020-01-01"), null, null, null);
assertThrows(DataAccessException.class, () -> horseDao.addHorse(newHorse)); assertThrows(PersistenceException.class, () -> horseDao.addHorse(newHorse));
} }
@Test @Test
@ -117,8 +117,8 @@ public abstract class HorseDaoTestBase {
} }
@Test @Test
@DisplayName("Updating a horse with the incorrect parameters should throw DataAccessException") @DisplayName("Updating a horse with the incorrect parameters should throw PersistenceException")
public void updatingHorse_incorrectParameters_shouldThrowDataAccess() { public void updatingHorse_incorrectParameters_shouldThrowPersistence() {
// Create horse // Create horse
Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", null); Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", null);
Horse savedHorse = horseDao.addHorse(newHorse); Horse savedHorse = horseDao.addHorse(newHorse);
@ -132,7 +132,7 @@ public abstract class HorseDaoTestBase {
newHorse.setRace(null); newHorse.setRace(null);
newHorse.setImagePath(null); newHorse.setImagePath(null);
newHorse.setOwner(null); newHorse.setOwner(null);
assertThrows(DataAccessException.class, () -> horseDao.updateHorse(newHorse)); assertThrows(PersistenceException.class, () -> horseDao.updateHorse(newHorse));
} }
@Test @Test

View File

@ -6,12 +6,12 @@ import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
import at.ac.tuwien.sepm.assignment.individual.entity.Owner; import at.ac.tuwien.sepm.assignment.individual.entity.Owner;
import at.ac.tuwien.sepm.assignment.individual.enums.ERace; import at.ac.tuwien.sepm.assignment.individual.enums.ERace;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException; import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import at.ac.tuwien.sepm.assignment.individual.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.individual.persistence.HorseDao; import at.ac.tuwien.sepm.assignment.individual.persistence.HorseDao;
import at.ac.tuwien.sepm.assignment.individual.persistence.OwnerDao; import at.ac.tuwien.sepm.assignment.individual.persistence.OwnerDao;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import java.io.IOException; import java.io.IOException;
@ -108,10 +108,10 @@ public abstract class OwnerDaoTestBase {
} }
@Test @Test
@DisplayName("Adding a new owner with the incorrect parameters should throw DataAccessException") @DisplayName("Adding a new owner with the incorrect parameters should throw PersistenceException")
public void addingNewOwner_incorrectParameters_shouldThrowDataAccess() { public void addingNewOwner_incorrectParameters_shouldThrowPersistence() {
Owner newOwner = new Owner(""); Owner newOwner = new Owner("");
assertThrows(DataAccessException.class, () -> ownerDao.addOwner(newOwner)); assertThrows(PersistenceException.class, () -> ownerDao.addOwner(newOwner));
} }
@Test @Test
@ -133,8 +133,8 @@ public abstract class OwnerDaoTestBase {
} }
@Test @Test
@DisplayName("Updating a owner with the incorrect parameters should throw DataAccessException") @DisplayName("Updating a owner with the incorrect parameters should throw PersistenceException")
public void updatingOwner_incorrectParameters_shouldThrowDataAccess() { public void updatingOwner_incorrectParameters_shouldThrowPersistence() {
// Create owner // Create owner
Owner newOwner = new Owner("Chad"); Owner newOwner = new Owner("Chad");
Owner savedOwner = ownerDao.addOwner(newOwner); Owner savedOwner = ownerDao.addOwner(newOwner);
@ -142,7 +142,7 @@ public abstract class OwnerDaoTestBase {
// Update owner // Update owner
newOwner.setId(savedOwner.getId()); newOwner.setId(savedOwner.getId());
newOwner.setName(""); newOwner.setName("");
assertThrows(DataAccessException.class, () -> ownerDao.updateOwner(newOwner)); assertThrows(PersistenceException.class, () -> ownerDao.updateOwner(newOwner));
} }
@Test @Test