Skip to content
Snippets Groups Projects
Commit e651a987 authored by Sára Juhošová's avatar Sára Juhošová
Browse files

Merge branch 16-change-entitynotfoundexception-into-resourcenotfoundexception...

Merge branch 16-change-entitynotfoundexception-into-resourcenotfoundexception with refs/heads/development into refs/merge-requests/16/train
parents 73a0a49c 98729d75
No related branches found
No related tags found
No related merge requests found
Pipeline #318562 passed
Showing
with 45 additions and 87 deletions
/*
* Labracore - A connecting core service for Labrador products
* Copyright (C) 2020- Delft University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package nl.tudelft.labracore.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* An exception that is thrown if a resource cannot be found.
*/
@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}
......@@ -17,23 +17,23 @@
*/
package nl.tudelft.labracore.repository;
import javax.persistence.EntityNotFoundException;
import nl.tudelft.labracore.model.Assignment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
public interface AssignmentRepository extends JpaRepository<Assignment, Long> {
/**
* Finds the assignment by id or throws a NoSuchElementException.
* Finds the assignment by id or throws a ResourceNotFoundException.
*
* @param id the id of the assignment
* @return the Assignment object
*/
default Assignment findByIdOrThrow(Long id) {
// Spring will actually wrap this in a JpaObjectRetrievalFailureException
return findById(id).orElseThrow(() -> new EntityNotFoundException("Assignment was not found: " + id));
return findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Assignment was not found: " + id));
}
}
......@@ -17,10 +17,10 @@
*/
package nl.tudelft.labracore.repository;
import nl.tudelft.labracore.exception.ResourceNotFoundException;
import nl.tudelft.labracore.model.Building;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
/**
* Repository for CRUD operations on Buildings within the database.
......
......
......@@ -19,23 +19,22 @@ package nl.tudelft.labracore.repository;
import java.util.List;
import javax.persistence.EntityNotFoundException;
import nl.tudelft.labracore.model.Cluster;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
public interface ClusterRepository extends JpaRepository<Cluster, Long> {
/**
* Finds the cluster by id or throws a NoSuchElementException.
* Finds the cluster by id or throws a ResourceNotFoundException.
*
* @param id the id of the cluster
* @return the Cluster object
*/
default Cluster findByIdOrThrow(Long id) {
// Spring will actually wrap this in a JpaObjectRetrievalFailureException
return findById(id).orElseThrow(() -> new EntityNotFoundException("Cluster was not found: " + id));
return findById(id).orElseThrow(() -> new ResourceNotFoundException("Cluster was not found: " + id));
}
List<Cluster> findAllById(List<Long> ids);
......
......
......@@ -17,23 +17,22 @@
*/
package nl.tudelft.labracore.repository;
import javax.persistence.EntityNotFoundException;
import nl.tudelft.labracore.model.Cohort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
public interface CohortRepository extends JpaRepository<Cohort, Long> {
/**
* Finds the cohort by id or throws a NoSuchElementException.
* Finds the cohort by id or throws a ResourceNotFoundException.
*
* @param id the id of the cohort
* @return the Cohort object
*/
default Cohort findByIdOrThrow(Long id) {
// Spring will actually wrap this in a JpaObjectRetrievalFailureException
return findById(id).orElseThrow(() -> new EntityNotFoundException("Cohort was not found: " + id));
return findById(id).orElseThrow(() -> new ResourceNotFoundException("Cohort was not found: " + id));
}
}
......@@ -17,22 +17,21 @@
*/
package nl.tudelft.labracore.repository;
import javax.persistence.EntityNotFoundException;
import nl.tudelft.labracore.model.Course;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
public interface CourseRepository extends JpaRepository<Course, Long> {
/**
* Finds the course by id or throws a NoSuchElementException.
* Finds the course by id or throws a ResourceNotFoundException.
*
* @param id the id of the course
* @return the Course object
*/
default Course findByIdOrThrow(Long id) {
// Spring will actually wrap this in a JpaObjectRetrievalFailureException
return findById(id).orElseThrow(() -> new EntityNotFoundException("Course was not found: " + id));
return findById(id).orElseThrow(() -> new ResourceNotFoundException("Course was not found: " + id));
}
}
......@@ -17,23 +17,22 @@
*/
package nl.tudelft.labracore.repository;
import javax.persistence.EntityNotFoundException;
import nl.tudelft.labracore.model.Edition;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
public interface EditionRepository extends JpaRepository<Edition, Long> {
/**
* Finds the edition by id or throws a NoSuchElementException.
* Finds the edition by id or throws a ResourceNotFoundException.
*
* @param id the id of the edition
* @return the Edition object
*/
default Edition findByIdOrThrow(Long id) {
// Spring will actually wrap this in a JpaObjectRetrievalFailureException
return findById(id).orElseThrow(() -> new EntityNotFoundException("Edition was not found: " + id));
return findById(id).orElseThrow(() -> new ResourceNotFoundException("Edition was not found: " + id));
}
}
......@@ -17,16 +17,15 @@
*/
package nl.tudelft.labracore.repository;
import javax.persistence.EntityNotFoundException;
import nl.tudelft.labracore.model.ModuleDivision;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
public interface ModuleDivisionRepository extends JpaRepository<ModuleDivision, Long> {
/**
* Finds the moduleDivision by id or throws a NoSuchElementException.
* Finds the moduleDivision by id or throws a ResourceNotFoundException.
*
* @param id the id of the moduleDivision
* @return the ModuleDivision object
......@@ -34,7 +33,7 @@ public interface ModuleDivisionRepository extends JpaRepository<ModuleDivision,
default ModuleDivision findByIdOrThrow(Long id) {
// Spring will actually wrap this in a JpaObjectRetrievalFailureException
return findById(id)
.orElseThrow(() -> new EntityNotFoundException("ModuleDivision was not found: " + id));
.orElseThrow(() -> new ResourceNotFoundException("ModuleDivision was not found: " + id));
}
}
......@@ -17,23 +17,22 @@
*/
package nl.tudelft.labracore.repository;
import javax.persistence.EntityNotFoundException;
import nl.tudelft.labracore.model.Module;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
public interface ModuleRepository extends JpaRepository<Module, Long> {
/**
* Finds the module by id or throws a NoSuchElementException.
* Finds the module by id or throws a ResourceNotFoundException.
*
* @param id the id of the module
* @return the Module object
*/
default Module findByIdOrThrow(Long id) {
// Spring will actually wrap this in a JpaObjectRetrievalFailureException
return findById(id).orElseThrow(() -> new EntityNotFoundException("Module was not found: " + id));
return findById(id).orElseThrow(() -> new ResourceNotFoundException("Module was not found: " + id));
}
}
......@@ -20,23 +20,22 @@ package nl.tudelft.labracore.repository;
import java.util.List;
import java.util.Optional;
import javax.persistence.EntityNotFoundException;
import nl.tudelft.labracore.model.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
public interface PersonRepository extends JpaRepository<Person, Long> {
/**
* Finds the person by id or throws a NoSuchElementException.
* Finds the person by id or throws a ResourceNotFoundException.
*
* @param id the id of the person
* @return the Person object
*/
default Person findByIdOrThrow(Long id) {
// Spring will actually wrap this in a JpaObjectRetrievalFailureException
return findById(id).orElseThrow(() -> new EntityNotFoundException("Person was not found: " + id));
return findById(id).orElseThrow(() -> new ResourceNotFoundException("Person was not found: " + id));
}
List<Person> findAllByUsername(List<String> users);
......@@ -47,11 +46,11 @@ public interface PersonRepository extends JpaRepository<Person, Long> {
default Person findByUsernameOrThrow(String username) {
return findByUsername(username).orElseThrow(
() -> new EntityNotFoundException("Person was not found by username: " + username));
() -> new ResourceNotFoundException("Person was not found by username: " + username));
}
default Person findByExternalIdOrThrow(String externalId) {
return findByExternalId(externalId).orElseThrow(
() -> new EntityNotFoundException("Person was not found by external ID: " + externalId));
() -> new ResourceNotFoundException("Person was not found by external ID: " + externalId));
}
}
......@@ -17,23 +17,22 @@
*/
package nl.tudelft.labracore.repository;
import javax.persistence.EntityNotFoundException;
import nl.tudelft.labracore.model.Program;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
public interface ProgramRepository extends JpaRepository<Program, Long> {
/**
* Finds the program by id or throws a NoSuchElementException.
* Finds the program by id or throws a ResourceNotFoundException.
*
* @param id the id of the program
* @return the Program object
*/
default Program findByIdOrThrow(Long id) {
// Spring will actually wrap this in a JpaObjectRetrievalFailureException
return findById(id).orElseThrow(() -> new EntityNotFoundException("Program was not found: " + id));
return findById(id).orElseThrow(() -> new ResourceNotFoundException("Program was not found: " + id));
}
}
......@@ -20,25 +20,24 @@ package nl.tudelft.labracore.repository;
import java.util.List;
import java.util.Optional;
import javax.persistence.EntityNotFoundException;
import nl.tudelft.labracore.model.Edition;
import nl.tudelft.labracore.model.Role;
import nl.tudelft.labracore.model.RoleType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
public interface RoleRepository extends JpaRepository<Role, Long> {
/**
* Finds the role by id or throws a NoSuchElementException.
* Finds the role by id or throws a ResourceNotFoundException.
*
* @param id the id of the role
* @return the Role object
*/
default Role findByIdOrThrow(Long id) {
// Spring will actually wrap this in a JpaObjectRetrievalFailureException
return findById(id).orElseThrow(() -> new EntityNotFoundException("Role was not found: " + id));
return findById(id).orElseThrow(() -> new ResourceNotFoundException("Role was not found: " + id));
}
/**
......@@ -51,7 +50,7 @@ public interface RoleRepository extends JpaRepository<Role, Long> {
*/
default Role findByEditionAndUsername(Edition edition, String username) {
return findByEditionAndPerson_Username(edition, username)
.orElseThrow(() -> new EntityNotFoundException(
.orElseThrow(() -> new ResourceNotFoundException(
"Role was not found for user " + username + " in course edition "
+ edition.getName()));
}
......
......
......@@ -17,10 +17,10 @@
*/
package nl.tudelft.labracore.repository;
import nl.tudelft.labracore.exception.ResourceNotFoundException;
import nl.tudelft.labracore.model.Room;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
/**
* Repository for CRUD operations on Rooms within the database.
......
......
......@@ -17,23 +17,22 @@
*/
package nl.tudelft.labracore.repository;
import javax.persistence.EntityNotFoundException;
import nl.tudelft.labracore.model.Session;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
public interface SessionRepository extends JpaRepository<Session, Long> {
/**
* Finds the session by id or throws a NoSuchElementException.
* Finds the session by id or throws a ResourceNotFoundException.
*
* @param id the id of the session
* @return the Session object
*/
default Session findByIdOrThrow(Long id) {
// Spring will actually wrap this in a JpaObjectRetrievalFailureException
return findById(id).orElseThrow(() -> new EntityNotFoundException("Session was not found: " + id));
return findById(id).orElseThrow(() -> new ResourceNotFoundException("Session was not found: " + id));
}
}
......@@ -17,16 +17,15 @@
*/
package nl.tudelft.labracore.repository;
import javax.persistence.EntityNotFoundException;
import nl.tudelft.labracore.model.StudentGroup;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
public interface StudentGroupRepository extends JpaRepository<StudentGroup, Long> {
/**
* Finds the studentGroup by id or throws a NoSuchElementException.
* Finds the studentGroup by id or throws a ResourceNotFoundException.
*
* @param id the id of the studentGroup
* @return the StudentGroup object
......@@ -34,7 +33,7 @@ public interface StudentGroupRepository extends JpaRepository<StudentGroup, Long
default StudentGroup findByIdOrThrow(Long id) {
// Spring will actually wrap this in a JpaObjectRetrievalFailureException
return findById(id)
.orElseThrow(() -> new EntityNotFoundException("StudentGroup was not found: " + id));
.orElseThrow(() -> new ResourceNotFoundException("StudentGroup was not found: " + id));
}
}
......@@ -32,7 +32,7 @@ import nl.tudelft.labracore.repository.ProgramRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.orm.jpa.JpaObjectRetrievalFailureException;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.transaction.annotation.Transactional;
@SpringBootTest
......@@ -74,12 +74,12 @@ public class CourseServiceTest {
@Test
public void findByIdOrThrow() {
assertThrows(JpaObjectRetrievalFailureException.class, () -> pr.findByIdOrThrow(-1L));
assertThrows(ResourceNotFoundException.class, () -> pr.findByIdOrThrow(-1L));
}
@Test
public void getCoursesByProgramExceptionTest() {
assertThrows(JpaObjectRetrievalFailureException.class, () -> cs.getCoursesbyProgram(-1L));
assertThrows(ResourceNotFoundException.class, () -> cs.getCoursesbyProgram(-1L));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment