From 61c4b3de42b1bac9494a9d3462fd156c7e5fb6b1 Mon Sep 17 00:00:00 2001 From: atusa17 Date: Sun, 7 Apr 2019 21:32:43 -0600 Subject: [PATCH] PAN-46 Added validation to ProofController --- .../controller/ProofController.java | 37 ++--- .../controller/ProofControllerTest.java | 129 ++++++------------ 2 files changed, 64 insertions(+), 102 deletions(-) diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/ProofController.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/ProofController.java index d682c32..77a9dc7 100644 --- a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/ProofController.java +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/ProofController.java @@ -1,6 +1,8 @@ package edu.msudenver.tsp.persistence.controller; import edu.msudenver.tsp.persistence.dto.Proof; +import edu.msudenver.tsp.persistence.exception.BadRequestException; +import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException; import edu.msudenver.tsp.persistence.repository.ProofRepository; import edu.msudenver.tsp.utilities.PersistenceUtilities; import lombok.AllArgsConstructor; @@ -45,11 +47,11 @@ public class ProofController { @GetMapping("/id") public @ResponseBody - ResponseEntity getProofById(@RequestParam("id") final Integer id) { + ResponseEntity getProofById(@RequestParam("id") final Integer id) throws BadRequestException { LOG.info("Received request to query for proof with id {}", id); if (id == null) { LOG.error("ERROR: ID was null"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Specified ID is null"); } LOG.debug("Querying for proof with id {}", id); @@ -75,11 +77,12 @@ public class ProofController { @GetMapping("/branch") public @ResponseBody - ResponseEntity> getAllProofsByBranch(@RequestParam("branch") String branch) { + ResponseEntity> getAllProofsByBranch(@RequestParam("branch") String branch) + throws BadRequestException { LOG.info("Received request to query for proofs related to the {} branch of mathematics", branch); if (branch == 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("-")) { @@ -110,11 +113,12 @@ public class ProofController { @GetMapping("/theorem_name") public @ResponseBody - ResponseEntity> getAllProofsByTheoremName(@RequestParam("theorem_name") String theoremName) { + ResponseEntity> getAllProofsByTheoremName(@RequestParam("theorem_name") String theoremName) + throws BadRequestException { LOG.info("Received request to query for proofs of the theorem {}", theoremName); if (theoremName == null) { LOG.error("ERROR: theorem name was null"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Specified theorem name is null"); } if (theoremName.contains("_") || theoremName.contains("-")) { @@ -147,16 +151,16 @@ public class ProofController { @Validated({Proof.Insert.class, Default.class}) public @ResponseBody ResponseEntity insertProof( @Valid @RequestBody final Proof proof, - final BindingResult bindingResult) { + final BindingResult bindingResult) throws UnprocessableEntityException, BadRequestException { LOG.info("Received request to insert a new proof"); if (bindingResult.hasErrors()) { LOG.error("Binding result is unprocessable"); - return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); + throw new UnprocessableEntityException(bindingResult.getAllErrors().toString()); } if (proof == null) { LOG.error("Passed entity is null"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Passed proof is null"); } LOG.debug("Saving new proof"); @@ -176,22 +180,23 @@ public class ProofController { @PatchMapping("/{id}") public @ResponseBody ResponseEntity updateProof( @PathVariable("id") final Integer id, - @RequestBody final Proof proof, final BindingResult bindingResult) { + @RequestBody final Proof proof, final BindingResult bindingResult) + throws UnprocessableEntityException, BadRequestException { LOG.info("Received request to update a proof"); if (bindingResult.hasErrors()) { LOG.error("Binding result is unprocessable"); - return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); + throw new UnprocessableEntityException(bindingResult.getAllErrors().toString()); } if (proof == null) { LOG.error("Passed entity is null"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Passed proof is null"); } if (id == null) { LOG.error("Proof ID must be specified"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Specified ID is null"); } LOG.debug("Checking for existence of proof with id {}", id); @@ -207,7 +212,7 @@ public class ProofController { if (!existingProof.isPresent()) { LOG.error("No proof associated with id {}", id); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(HttpStatus.NOT_FOUND); } PersistenceUtilities.copyNonNullProperties(proof, existingProof.get()); @@ -228,11 +233,11 @@ public class ProofController { } @DeleteMapping("/{id}") - public @ResponseBody ResponseEntity deleteProofById(@PathVariable("id") final Integer id) { + public @ResponseBody ResponseEntity deleteProofById(@PathVariable("id") final Integer id) throws BadRequestException { LOG.info("Received request to delete proof with id {}", id); if (id == null) { LOG.error("Specified id is null"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Specified ID is null"); } LOG.debug("Deleting proof with id {}", id); diff --git a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/ProofControllerTest.java b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/ProofControllerTest.java index 14c2482..6953b0a 100644 --- a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/ProofControllerTest.java +++ b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/ProofControllerTest.java @@ -1,6 +1,8 @@ package edu.msudenver.tsp.persistence.controller; import edu.msudenver.tsp.persistence.dto.Proof; +import edu.msudenver.tsp.persistence.exception.BadRequestException; +import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException; import edu.msudenver.tsp.persistence.repository.ProofRepository; import org.junit.Test; import org.junit.runner.RunWith; @@ -45,7 +47,7 @@ public class ProofControllerTest { } @Test - public void testGetAllProofsByBranch() { + public void testGetAllProofsByBranch() throws BadRequestException { final Proof proofDto = createProof(); final List listOfProofs = new ArrayList<>(); listOfProofs.add(proofDto); @@ -63,18 +65,13 @@ public class ProofControllerTest { responseEntity.getBody().forEach(proof -> assertEquals(proofDto, proof)); } - @Test - public void testGetAllProfsByBranch_nullBranch() { - final ResponseEntity> responseEntity = proofController.getAllProofsByBranch(null); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + @Test(expected = BadRequestException.class) + public void testGetAllProfsByBranch_nullBranch() throws BadRequestException { + proofController.getAllProofsByBranch(null); } @Test - public void testGetAllProofsByBranch_noProofsFound() { + public void testGetAllProofsByBranch_noProofsFound() throws BadRequestException { when(proofRepository.findByBranch(anyString())).thenReturn(Collections.emptyList()); final ResponseEntity> responseEntity = proofController.getAllProofsByBranch("test-nonexistent-branch"); @@ -85,7 +82,7 @@ public class ProofControllerTest { } @Test - public void testGetAllProofsByTheoremName() { + public void testGetAllProofsByTheoremName() throws BadRequestException { final Proof proofDto = createProof(); final List listOfProofs = new ArrayList<>(); listOfProofs.add(proofDto); @@ -103,18 +100,13 @@ public class ProofControllerTest { responseEntity.getBody().forEach(proof -> assertEquals(proofDto, proof)); } - @Test - public void testGetAllProofsByTheoremName_nullTheoremName() { - final ResponseEntity> responseEntity = proofController.getAllProofsByTheoremName(null); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + @Test(expected = BadRequestException.class) + public void testGetAllProofsByTheoremName_nullTheoremName() throws BadRequestException { + proofController.getAllProofsByTheoremName(null); } @Test - public void testGetAllProofsByTheoremName_noProofsFound() { + public void testGetAllProofsByTheoremName_noProofsFound() throws BadRequestException { when(proofRepository.findByTheoremName(anyString())).thenReturn(Collections.emptyList()); final ResponseEntity> responseEntity = proofController.getAllProofsByTheoremName("test-nonexistent-proof"); @@ -125,7 +117,7 @@ public class ProofControllerTest { } @Test - public void testGetProofById() { + public void testGetProofById() throws BadRequestException { final Proof proof = createProof(); when(proofRepository.findById(anyInt())).thenReturn(Optional.ofNullable(proof)); @@ -139,18 +131,13 @@ public class ProofControllerTest { verify(proofRepository).findById(anyInt()); } - @Test - public void testGetProofById_nullId() { - final ResponseEntity responseEntity = proofController.getProofById(null); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + @Test(expected = BadRequestException.class) + public void testGetProofById_nullId() throws BadRequestException { + proofController.getProofById(null); } @Test - public void testGetProofById_noProofFound() { + public void testGetProofById_noProofFound() throws BadRequestException { when(proofRepository.findById(anyInt())).thenReturn(Optional.empty()); final ResponseEntity responseEntity = proofController.getProofById(1); @@ -162,7 +149,7 @@ public class ProofControllerTest { } @Test - public void testInsertProof() { + public void testInsertProof() throws BadRequestException, UnprocessableEntityException { final Proof proof = createProof(); when(proofRepository.save(any(Proof.class))).thenReturn(proof); @@ -176,31 +163,21 @@ public class ProofControllerTest { verify(proofRepository).save(any(Proof.class)); } - @Test - public void testInsertProof_proofDtoIsNull() { - final ResponseEntity responseEntity = proofController.insertProof(null, bindingResult); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + @Test(expected = BadRequestException.class) + public void testInsertProof_proofDtoIsNull() throws BadRequestException, UnprocessableEntityException { + proofController.insertProof(null, bindingResult); } - @Test - public void testInsertProof_bindingResultHasErrors() { + @Test(expected = UnprocessableEntityException.class) + public void testInsertProof_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException { final Proof proof = createProof(); when(bindingResult.hasErrors()).thenReturn(true); - final ResponseEntity responseEntity = proofController.insertProof(proof, bindingResult); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + proofController.insertProof(proof, bindingResult); } @Test - public void testUpdateProof() { + public void testUpdateProof() throws BadRequestException, UnprocessableEntityException { final Proof existingProof = createProof(); existingProof.setId(1); existingProof.setVersion(1); @@ -221,52 +198,37 @@ public class ProofControllerTest { verify(proofRepository).save(any(Proof.class)); } - @Test - public void testUpdateProof_bindingResultHasErrors() { + @Test(expected = UnprocessableEntityException.class) + public void testUpdateProof_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException { when(bindingResult.hasErrors()).thenReturn(true); - final ResponseEntity responseEntity = proofController.updateProof(1, createProof(), bindingResult); + proofController.updateProof(1, createProof(), bindingResult); + } - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + @Test(expected = BadRequestException.class) + public void testUpdateProof_proofDtoIsNull() throws BadRequestException, UnprocessableEntityException { + proofController.updateProof(1, null, bindingResult); + } + + @Test(expected = BadRequestException.class) + public void testUpdateProof_idIsNull() throws BadRequestException, UnprocessableEntityException { + proofController.updateProof(null, createProof(), bindingResult); } @Test - public void testUpdateProof_proofDtoIsNull() { - final ResponseEntity responseEntity = proofController.updateProof(1, null, bindingResult); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); - } - - @Test - public void testUpdateProof_idIsNull() { - final ResponseEntity responseEntity = proofController.updateProof(null, createProof(), bindingResult); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); - } - - @Test - public void testUpdateProof_theoremDoesNotExist() { + public void testUpdateProof_theoremDoesNotExist() throws BadRequestException, UnprocessableEntityException { when(proofRepository.findById(anyInt())).thenReturn(Optional.empty()); final ResponseEntity responseEntity = proofController.updateProof(1, createProof(), bindingResult); assertNotNull(responseEntity); assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); + assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); verify(proofRepository, times(0)).save(any(Proof.class)); } @Test - public void testDeleteProofById() { + public void testDeleteProofById() throws BadRequestException { doNothing().when(proofRepository).deleteById(anyInt()); final ResponseEntity responseEntity = proofController.deleteProofById(1); @@ -277,14 +239,9 @@ public class ProofControllerTest { verify(proofRepository).deleteById(anyInt()); } - @Test - public void testDeleteProofById_idIsNull() { - final ResponseEntity responseEntity = proofController.deleteProofById(null); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + @Test(expected = BadRequestException.class) + public void testDeleteProofById_idIsNull() throws BadRequestException { + proofController.deleteProofById(null); } private Proof createProof() {