PAN-46 Added validation to TheoremController
This commit is contained in:
+20
-18
@@ -1,6 +1,8 @@
|
|||||||
package edu.msudenver.tsp.persistence.controller;
|
package edu.msudenver.tsp.persistence.controller;
|
||||||
|
|
||||||
import edu.msudenver.tsp.persistence.dto.Theorem;
|
import edu.msudenver.tsp.persistence.dto.Theorem;
|
||||||
|
import edu.msudenver.tsp.persistence.exception.BadRequestException;
|
||||||
|
import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException;
|
||||||
import edu.msudenver.tsp.persistence.repository.TheoremRepository;
|
import edu.msudenver.tsp.persistence.repository.TheoremRepository;
|
||||||
import edu.msudenver.tsp.utilities.PersistenceUtilities;
|
import edu.msudenver.tsp.utilities.PersistenceUtilities;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
@@ -45,11 +47,11 @@ public class TheoremController {
|
|||||||
|
|
||||||
@GetMapping("/branch")
|
@GetMapping("/branch")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<List<Theorem>> getAllTheoremsByBranch(@RequestParam("branch") String branch) {
|
ResponseEntity<List<Theorem>> getAllTheoremsByBranch(@RequestParam("branch") String branch) throws BadRequestException {
|
||||||
LOG.info("Received request to query for theorems related to the {} branch of mathematics", branch);
|
LOG.info("Received request to query for theorems related to the {} branch of mathematics", branch);
|
||||||
if (branch == null) {
|
if (branch == null) {
|
||||||
LOG.error("ERROR: branch was null");
|
LOG.error("ERROR: branch was null");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("Specified branch is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (branch.contains("_") || branch.contains("-")) {
|
if (branch.contains("_") || branch.contains("-")) {
|
||||||
@@ -80,11 +82,11 @@ public class TheoremController {
|
|||||||
|
|
||||||
@GetMapping("/proven_status")
|
@GetMapping("/proven_status")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<List<Theorem>> getAllTheoremsByProvenStatus(@RequestParam("proven_status") final String provenStatus) {
|
ResponseEntity<List<Theorem>> getAllTheoremsByProvenStatus(@RequestParam("proven_status") final String provenStatus) throws BadRequestException {
|
||||||
LOG.info("Received request to query for theorems whose proven status is {}", provenStatus);
|
LOG.info("Received request to query for theorems whose proven status is {}", provenStatus);
|
||||||
if (provenStatus == null) {
|
if (provenStatus == null) {
|
||||||
LOG.error("ERROR: status was null");
|
LOG.error("ERROR: status was null");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("Specified status is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
final Boolean isProven = Boolean.parseBoolean(provenStatus);
|
final Boolean isProven = Boolean.parseBoolean(provenStatus);
|
||||||
@@ -112,11 +114,11 @@ public class TheoremController {
|
|||||||
|
|
||||||
@GetMapping("/name")
|
@GetMapping("/name")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<List<Theorem>> getAllTheoremsByName(@RequestParam("name") String name) {
|
ResponseEntity<List<Theorem>> getAllTheoremsByName(@RequestParam("name") String name) throws BadRequestException {
|
||||||
LOG.info("Received request to query for theorems whose name is {}", name);
|
LOG.info("Received request to query for theorems whose name is {}", name);
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
LOG.error("ERROR: name was null");
|
LOG.error("ERROR: name was null");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("Specified name is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.contains("_") || name.contains("-")) {
|
if (name.contains("_") || name.contains("-")) {
|
||||||
@@ -147,11 +149,11 @@ public class TheoremController {
|
|||||||
|
|
||||||
@GetMapping("/id")
|
@GetMapping("/id")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<Theorem> getTheoremById(@RequestParam("id") final Integer id) {
|
ResponseEntity<Theorem> getTheoremById(@RequestParam("id") final Integer id) throws BadRequestException {
|
||||||
LOG.info("Received request to query for theorem with id {}", id);
|
LOG.info("Received request to query for theorem with id {}", id);
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
LOG.error("ERROR: ID was null");
|
LOG.error("ERROR: ID was null");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("Specified ID is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.debug("Querying for theorem with id {}", id);
|
LOG.debug("Querying for theorem with id {}", id);
|
||||||
@@ -179,16 +181,16 @@ public class TheoremController {
|
|||||||
@Validated({Theorem.Insert.class, Default.class})
|
@Validated({Theorem.Insert.class, Default.class})
|
||||||
public @ResponseBody ResponseEntity<Theorem> insertTheorem(
|
public @ResponseBody ResponseEntity<Theorem> insertTheorem(
|
||||||
@Valid @RequestBody final Theorem theorem,
|
@Valid @RequestBody final Theorem theorem,
|
||||||
final BindingResult bindingResult) {
|
final BindingResult bindingResult) throws UnprocessableEntityException, BadRequestException {
|
||||||
LOG.info("Received request to insert a new theorem");
|
LOG.info("Received request to insert a new theorem");
|
||||||
if (bindingResult.hasErrors()) {
|
if (bindingResult.hasErrors()) {
|
||||||
LOG.error("Binding result is unprocessable");
|
LOG.error("Binding result is unprocessable");
|
||||||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
throw new UnprocessableEntityException(bindingResult.getAllErrors().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (theorem == null) {
|
if (theorem == null) {
|
||||||
LOG.error("Passed entity is null");
|
LOG.error("Passed entity is null");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("Passed theorem is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.debug("Saving new theorem");
|
LOG.debug("Saving new theorem");
|
||||||
@@ -208,22 +210,22 @@ public class TheoremController {
|
|||||||
@PatchMapping("/{id}")
|
@PatchMapping("/{id}")
|
||||||
public @ResponseBody ResponseEntity<Theorem> updateTheorem(
|
public @ResponseBody ResponseEntity<Theorem> updateTheorem(
|
||||||
@PathVariable("id") final Integer id,
|
@PathVariable("id") final Integer id,
|
||||||
@RequestBody final Theorem theorem, final BindingResult bindingResult) {
|
@RequestBody final Theorem theorem, final BindingResult bindingResult) throws UnprocessableEntityException, BadRequestException {
|
||||||
|
|
||||||
LOG.info("Received request to update a theorem");
|
LOG.info("Received request to update a theorem");
|
||||||
if (bindingResult.hasErrors()) {
|
if (bindingResult.hasErrors()) {
|
||||||
LOG.error("Binding result is unprocessable");
|
LOG.error("Binding result is unprocessable");
|
||||||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
throw new UnprocessableEntityException(bindingResult.getAllErrors().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (theorem == null) {
|
if (theorem == null) {
|
||||||
LOG.error("Passed entity is null");
|
LOG.error("Passed entity is null");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("Passed theorem is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
LOG.error("Theorem ID must be specified");
|
LOG.error("Theorem ID must be specified");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("Specified ID is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.debug("Checking for existence of theorem with id {}", id);
|
LOG.debug("Checking for existence of theorem with id {}", id);
|
||||||
@@ -239,7 +241,7 @@ public class TheoremController {
|
|||||||
|
|
||||||
if (!existingTheorem.isPresent()) {
|
if (!existingTheorem.isPresent()) {
|
||||||
LOG.error("No theorem associated with id {}", id);
|
LOG.error("No theorem associated with id {}", id);
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
PersistenceUtilities.copyNonNullProperties(theorem, existingTheorem.get());
|
PersistenceUtilities.copyNonNullProperties(theorem, existingTheorem.get());
|
||||||
@@ -260,11 +262,11 @@ public class TheoremController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public @ResponseBody ResponseEntity<Void> deleteTheoremById(@PathVariable("id") final Integer id) {
|
public @ResponseBody ResponseEntity<Void> deleteTheoremById(@PathVariable("id") final Integer id) throws BadRequestException {
|
||||||
LOG.info("Received request to delete theorem with id {}", id);
|
LOG.info("Received request to delete theorem with id {}", id);
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
LOG.error("Specified id is null");
|
LOG.error("Specified id is null");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("Specified ID is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.debug("Deleting theorem with id {}", id);
|
LOG.debug("Deleting theorem with id {}", id);
|
||||||
|
|||||||
+49
-97
@@ -2,6 +2,8 @@ package edu.msudenver.tsp.persistence.controller;
|
|||||||
|
|
||||||
import edu.msudenver.tsp.persistence.dto.Theorem;
|
import edu.msudenver.tsp.persistence.dto.Theorem;
|
||||||
import edu.msudenver.tsp.persistence.dto.TheoremType;
|
import edu.msudenver.tsp.persistence.dto.TheoremType;
|
||||||
|
import edu.msudenver.tsp.persistence.exception.BadRequestException;
|
||||||
|
import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException;
|
||||||
import edu.msudenver.tsp.persistence.repository.TheoremRepository;
|
import edu.msudenver.tsp.persistence.repository.TheoremRepository;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -50,7 +52,7 @@ public class TheoremControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetTheoremById() {
|
public void testGetTheoremById() throws BadRequestException {
|
||||||
final Theorem theorem = createTheorem();
|
final Theorem theorem = createTheorem();
|
||||||
when(theoremRepository.findById(anyInt())).thenReturn(Optional.ofNullable(theorem));
|
when(theoremRepository.findById(anyInt())).thenReturn(Optional.ofNullable(theorem));
|
||||||
|
|
||||||
@@ -64,18 +66,13 @@ public class TheoremControllerTest {
|
|||||||
verify(theoremRepository).findById(anyInt());
|
verify(theoremRepository).findById(anyInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = BadRequestException.class)
|
||||||
public void testGetTheoremById_nullId() {
|
public void testGetTheoremById_nullId() throws BadRequestException {
|
||||||
final ResponseEntity responseEntity = theoremController.getTheoremById(null);
|
theoremController.getTheoremById(null);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(theoremRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetTheoremById_noTheoremFound() {
|
public void testGetTheoremById_noTheoremFound() throws BadRequestException {
|
||||||
when(theoremRepository.findById(anyInt())).thenReturn(Optional.empty());
|
when(theoremRepository.findById(anyInt())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
final ResponseEntity responseEntity = theoremController.getTheoremById(1);
|
final ResponseEntity responseEntity = theoremController.getTheoremById(1);
|
||||||
@@ -87,7 +84,7 @@ public class TheoremControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAllTheoremsByBranch() {
|
public void testGetAllTheoremsByBranch() throws BadRequestException {
|
||||||
final Theorem theoremDto = createTheorem();
|
final Theorem theoremDto = createTheorem();
|
||||||
final List<Theorem> listOfTheorems = new ArrayList<>();
|
final List<Theorem> listOfTheorems = new ArrayList<>();
|
||||||
listOfTheorems.add(theoremDto);
|
listOfTheorems.add(theoremDto);
|
||||||
@@ -105,18 +102,13 @@ public class TheoremControllerTest {
|
|||||||
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
|
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = BadRequestException.class)
|
||||||
public void testGetAllTheoremsByBranch_nullBranch() {
|
public void testGetAllTheoremsByBranch_nullBranch() throws BadRequestException {
|
||||||
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByBranch(null);
|
theoremController.getAllTheoremsByBranch(null);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(theoremRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAllTheoremsByBranch_noTheoremsFound() {
|
public void testGetAllTheoremsByBranch_noTheoremsFound() throws BadRequestException {
|
||||||
when(theoremRepository.findByBranch(anyString())).thenReturn(Collections.emptyList());
|
when(theoremRepository.findByBranch(anyString())).thenReturn(Collections.emptyList());
|
||||||
|
|
||||||
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByBranch("test_nonexistent_branch");
|
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByBranch("test_nonexistent_branch");
|
||||||
@@ -127,7 +119,7 @@ public class TheoremControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAllTheoremsByProvenStatus() {
|
public void testGetAllTheoremsByProvenStatus() throws BadRequestException {
|
||||||
final Theorem theoremDto = createTheorem();
|
final Theorem theoremDto = createTheorem();
|
||||||
final List<Theorem> listOfTheorems = new ArrayList<>();
|
final List<Theorem> listOfTheorems = new ArrayList<>();
|
||||||
listOfTheorems.add(theoremDto);
|
listOfTheorems.add(theoremDto);
|
||||||
@@ -145,18 +137,13 @@ public class TheoremControllerTest {
|
|||||||
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
|
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = BadRequestException.class)
|
||||||
public void testGetAllTheoremsByProvenStatus_nullProvenStatus() {
|
public void testGetAllTheoremsByProvenStatus_nullProvenStatus() throws BadRequestException {
|
||||||
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByProvenStatus(null);
|
theoremController.getAllTheoremsByProvenStatus(null);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(theoremRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAllTheoremsByProvenStatus_invalidProvenStatus() {
|
public void testGetAllTheoremsByProvenStatus_invalidProvenStatus() throws BadRequestException {
|
||||||
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByProvenStatus("test");
|
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByProvenStatus("test");
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
assertNotNull(responseEntity);
|
||||||
@@ -165,7 +152,7 @@ public class TheoremControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAllTheoremsByProvenStatus_noTheoremsFound() {
|
public void testGetAllTheoremsByProvenStatus_noTheoremsFound() throws BadRequestException {
|
||||||
when(theoremRepository.findByProvenStatus(anyBoolean())).thenReturn(Collections.emptyList());
|
when(theoremRepository.findByProvenStatus(anyBoolean())).thenReturn(Collections.emptyList());
|
||||||
|
|
||||||
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByProvenStatus("false");
|
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByProvenStatus("false");
|
||||||
@@ -176,7 +163,7 @@ public class TheoremControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAllTheoremsByName() {
|
public void testGetAllTheoremsByName() throws BadRequestException {
|
||||||
final Theorem theoremDto = createTheorem();
|
final Theorem theoremDto = createTheorem();
|
||||||
final List<Theorem> listOfTheorems = new ArrayList<>();
|
final List<Theorem> listOfTheorems = new ArrayList<>();
|
||||||
listOfTheorems.add(theoremDto);
|
listOfTheorems.add(theoremDto);
|
||||||
@@ -194,18 +181,13 @@ public class TheoremControllerTest {
|
|||||||
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
|
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = BadRequestException.class)
|
||||||
public void testGetAllTheoremsByName_nullName() {
|
public void testGetAllTheoremsByName_nullName() throws BadRequestException {
|
||||||
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByName(null);
|
theoremController.getAllTheoremsByName(null);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(theoremRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAllTheoremsByName_noNameFound() {
|
public void testGetAllTheoremsByName_noNameFound() throws BadRequestException {
|
||||||
when(theoremRepository.findByName(anyString())).thenReturn(Collections.emptyList());
|
when(theoremRepository.findByName(anyString())).thenReturn(Collections.emptyList());
|
||||||
|
|
||||||
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByName("No-name");
|
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByName("No-name");
|
||||||
@@ -216,7 +198,7 @@ public class TheoremControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInsertTheorem() {
|
public void testInsertTheorem() throws BadRequestException, UnprocessableEntityException {
|
||||||
final Theorem theorem = createTheorem();
|
final Theorem theorem = createTheorem();
|
||||||
when(theoremRepository.save(any(Theorem.class))).thenReturn(theorem);
|
when(theoremRepository.save(any(Theorem.class))).thenReturn(theorem);
|
||||||
|
|
||||||
@@ -230,31 +212,21 @@ public class TheoremControllerTest {
|
|||||||
verify(theoremRepository).save(any(Theorem.class));
|
verify(theoremRepository).save(any(Theorem.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = BadRequestException.class)
|
||||||
public void testInsertTheorem_theoremDtoIsNull() {
|
public void testInsertTheorem_theoremDtoIsNull() throws BadRequestException, UnprocessableEntityException {
|
||||||
final ResponseEntity responseEntity = theoremController.insertTheorem(null, bindingResult);
|
theoremController.insertTheorem(null, bindingResult);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(theoremRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = UnprocessableEntityException.class)
|
||||||
public void testInsertTheorem_bindingResultHasErrors() {
|
public void testInsertTheorem_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException {
|
||||||
final Theorem theorem = createTheorem();
|
final Theorem theorem = createTheorem();
|
||||||
when(bindingResult.hasErrors()).thenReturn(true);
|
when(bindingResult.hasErrors()).thenReturn(true);
|
||||||
|
|
||||||
final ResponseEntity responseEntity = theoremController.insertTheorem(theorem, bindingResult);
|
theoremController.insertTheorem(theorem, bindingResult);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(theoremRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateTheorem() {
|
public void testUpdateTheorem() throws BadRequestException, UnprocessableEntityException {
|
||||||
final Theorem existingTheorem = createTheorem();
|
final Theorem existingTheorem = createTheorem();
|
||||||
existingTheorem.setId(1);
|
existingTheorem.setId(1);
|
||||||
existingTheorem.setVersion(1);
|
existingTheorem.setVersion(1);
|
||||||
@@ -275,52 +247,37 @@ public class TheoremControllerTest {
|
|||||||
verify(theoremRepository).save(any(Theorem.class));
|
verify(theoremRepository).save(any(Theorem.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = UnprocessableEntityException.class)
|
||||||
public void testUpdateTheorem_bindingResultHasErrors() {
|
public void testUpdateTheorem_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException {
|
||||||
when(bindingResult.hasErrors()).thenReturn(true);
|
when(bindingResult.hasErrors()).thenReturn(true);
|
||||||
|
|
||||||
final ResponseEntity<Theorem> responseEntity = theoremController.updateTheorem(1, createTheorem(), bindingResult);
|
theoremController.updateTheorem(1, createTheorem(), bindingResult);
|
||||||
|
}
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
@Test(expected = BadRequestException.class)
|
||||||
assertFalse(responseEntity.hasBody());
|
public void testUpdateTheorem_theoremDtoIsNull() throws BadRequestException, UnprocessableEntityException {
|
||||||
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
|
theoremController.updateTheorem(1, null, bindingResult);
|
||||||
verifyZeroInteractions(theoremRepository);
|
}
|
||||||
|
|
||||||
|
@Test(expected = BadRequestException.class)
|
||||||
|
public void testUpdateTheorem_idIsNull() throws BadRequestException, UnprocessableEntityException {
|
||||||
|
theoremController.updateTheorem(null, createTheorem(), bindingResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateTheorem_theoremDtoIsNull() {
|
public void testUpdateTheorem_theoremDoesNotExist() throws BadRequestException, UnprocessableEntityException {
|
||||||
final ResponseEntity<Theorem> responseEntity = theoremController.updateTheorem(1, null, bindingResult);
|
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(theoremRepository);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateTheorem_idIsNull() {
|
|
||||||
final ResponseEntity<Theorem> responseEntity = theoremController.updateTheorem(null, createTheorem(), bindingResult);
|
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(theoremRepository);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateTheorem_theoremDoesNotExist() {
|
|
||||||
when(theoremRepository.findById(anyInt())).thenReturn(Optional.empty());
|
when(theoremRepository.findById(anyInt())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
final ResponseEntity<Theorem> responseEntity = theoremController.updateTheorem(1, createTheorem(), bindingResult);
|
final ResponseEntity<Theorem> responseEntity = theoremController.updateTheorem(1, createTheorem(), bindingResult);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
assertNotNull(responseEntity);
|
||||||
assertFalse(responseEntity.hasBody());
|
assertFalse(responseEntity.hasBody());
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
|
||||||
verify(theoremRepository, times(0)).save(any(Theorem.class));
|
verify(theoremRepository, times(0)).save(any(Theorem.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDeleteTheoremById() {
|
public void testDeleteTheoremById() throws BadRequestException {
|
||||||
doNothing().when(theoremRepository).deleteById(anyInt());
|
doNothing().when(theoremRepository).deleteById(anyInt());
|
||||||
|
|
||||||
final ResponseEntity responseEntity = theoremController.deleteTheoremById(1);
|
final ResponseEntity responseEntity = theoremController.deleteTheoremById(1);
|
||||||
@@ -331,14 +288,9 @@ public class TheoremControllerTest {
|
|||||||
verify(theoremRepository).deleteById(anyInt());
|
verify(theoremRepository).deleteById(anyInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = BadRequestException.class)
|
||||||
public void testDeleteTheoremById_idIsNull() {
|
public void testDeleteTheoremById_idIsNull() throws BadRequestException {
|
||||||
final ResponseEntity responseEntity = theoremController.deleteTheoremById(null);
|
theoremController.deleteTheoremById(null);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(theoremRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Theorem createTheorem() {
|
private Theorem createTheorem() {
|
||||||
|
|||||||
Reference in New Issue
Block a user