Merge pull request #28 from atusa17/PAN-46

Pan 46
This commit is contained in:
Alex Tusa
2019-04-11 21:51:52 -06:00
committed by GitHub
13 changed files with 410 additions and 380 deletions
@@ -1,6 +1,8 @@
package edu.msudenver.tsp.persistence.controller; package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.dto.Account; import edu.msudenver.tsp.persistence.dto.Account;
import edu.msudenver.tsp.persistence.exception.BadRequestException;
import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException;
import edu.msudenver.tsp.persistence.repository.AccountsRepository; import edu.msudenver.tsp.persistence.repository.AccountsRepository;
import edu.msudenver.tsp.utilities.PersistenceUtilities; import edu.msudenver.tsp.utilities.PersistenceUtilities;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@@ -23,6 +25,7 @@ import java.util.Optional;
@RestController @RestController
@AllArgsConstructor @AllArgsConstructor
@RequestMapping("/accounts") @RequestMapping("/accounts")
@Validated
public class AccountController { public class AccountController {
private final AccountsRepository accountsRepository; private final AccountsRepository accountsRepository;
@@ -48,11 +51,11 @@ public class AccountController {
@GetMapping("/id") @GetMapping("/id")
public @ResponseBody public @ResponseBody
ResponseEntity<Account> getAccountById(@RequestParam("id") final Integer id) { ResponseEntity<Account> getAccountById(@RequestParam("id") final Integer id) throws BadRequestException {
LOG.info("Received request to query for account with id {}", id); LOG.info("Received request to query for account 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("ERROR: ID cannot be null");
} }
LOG.debug("Querying for account with id {}", id); LOG.debug("Querying for account with id {}", id);
@@ -77,11 +80,11 @@ public class AccountController {
@GetMapping("/username") @GetMapping("/username")
public @ResponseBody public @ResponseBody
ResponseEntity<Account> getAccountByUsername(@RequestParam("username") final String username) { ResponseEntity<Account> getAccountByUsername(@RequestParam("username") final String username) throws BadRequestException {
LOG.info("Received request to query for account with username {}", username); LOG.info("Received request to query for account with username {}", username);
if (username == null) { if (username == null) {
LOG.error("ERROR: username was null"); LOG.error("ERROR: username was null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); throw new BadRequestException("ERROR: Username cannot be null");
} }
LOG.debug("Querying for account with username {}", username); LOG.debug("Querying for account with username {}", username);
@@ -107,17 +110,18 @@ public class AccountController {
@PostMapping({"","/"}) @PostMapping({"","/"})
@Validated({Account.Insert.class, Default.class}) @Validated({Account.Insert.class, Default.class})
public @ResponseBody ResponseEntity<Account> insertAccount( public @ResponseBody ResponseEntity<Account> insertAccount(
@Valid @RequestBody final Account account, final BindingResult bindingResult) { @Valid @RequestBody final Account account, final BindingResult bindingResult)
throws UnprocessableEntityException, BadRequestException {
LOG.info("Received request to insert a new account"); LOG.info("Received request to insert a new account");
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 (account == null) { if (account == null) {
LOG.error("Passed account is unprocessable"); LOG.error("Passed account is unprocessable");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); throw new BadRequestException("Passed account is unprocessable");
} }
LOG.info("Checking for any existing users with username {}", account.getUsername()); LOG.info("Checking for any existing users with username {}", account.getUsername());
@@ -152,22 +156,23 @@ public class AccountController {
@PatchMapping("/{id}") @PatchMapping("/{id}")
public @ResponseBody ResponseEntity<Account> updateAccount( public @ResponseBody ResponseEntity<Account> updateAccount(
@PathVariable("id") final Integer id, @PathVariable("id") final Integer id,
@RequestBody final Account account, final BindingResult bindingResult) { @RequestBody final Account account, final BindingResult bindingResult)
throws UnprocessableEntityException, BadRequestException {
LOG.info("Received request to update an account"); LOG.info("Received request to update an account");
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 (account == null) { if (account == null) {
LOG.error("Passed entity is null"); LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); throw new BadRequestException("Passed account is null");
} }
if (id == null) { if (id == null) {
LOG.error("Account ID must be specified"); LOG.error("Account ID must be specified");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); throw new BadRequestException("Account ID must be specified");
} }
LOG.debug("Checking for existence of account with id {}", id); LOG.debug("Checking for existence of account with id {}", id);
@@ -204,11 +209,11 @@ public class AccountController {
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public @ResponseBody ResponseEntity<Void> deleteAccountById(@PathVariable("id") final Integer id) { public @ResponseBody ResponseEntity<Void> deleteAccountById(@PathVariable("id") final Integer id) throws BadRequestException {
LOG.info("Received request to delete account with id {}", id); LOG.info("Received request to delete account 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 account with id {}", id); LOG.debug("Deleting account with id {}", id);
@@ -1,6 +1,8 @@
package edu.msudenver.tsp.persistence.controller; package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.dto.Definition; import edu.msudenver.tsp.persistence.dto.Definition;
import edu.msudenver.tsp.persistence.exception.BadRequestException;
import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException;
import edu.msudenver.tsp.persistence.repository.DefinitionRepository; import edu.msudenver.tsp.persistence.repository.DefinitionRepository;
import edu.msudenver.tsp.utilities.PersistenceUtilities; import edu.msudenver.tsp.utilities.PersistenceUtilities;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@@ -21,6 +23,7 @@ import java.util.Optional;
@RestController @RestController
@AllArgsConstructor @AllArgsConstructor
@RequestMapping(path = "/definitions") @RequestMapping(path = "/definitions")
@Validated
public class DefinitionController { public class DefinitionController {
private final DefinitionRepository definitionRepository; private final DefinitionRepository definitionRepository;
@@ -45,11 +48,12 @@ public class DefinitionController {
@GetMapping("/{id}") @GetMapping("/{id}")
public @ResponseBody public @ResponseBody
ResponseEntity<Definition> getDefinitionById(@PathVariable("id") final Integer id) { ResponseEntity<Definition> getDefinitionById(@PathVariable("id") final Integer id)
throws BadRequestException {
LOG.info("Received request to query for definition with id {}", id); LOG.info("Received request to query for definition with id {}", id);
if (id == null) { if (id == null) {
LOG.error("ERROR: ID was null"); LOG.error("ERROR: ID cannot be null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); throw new BadRequestException("ERROR: ID cannot be null");
} }
LOG.debug("Querying for definition with id {}", id); LOG.debug("Querying for definition with id {}", id);
@@ -77,16 +81,17 @@ public class DefinitionController {
@Validated({Definition.Insert.class, Default.class}) @Validated({Definition.Insert.class, Default.class})
public @ResponseBody ResponseEntity<Definition> insertDefinition( public @ResponseBody ResponseEntity<Definition> insertDefinition(
@Valid @RequestBody final Definition definition, @Valid @RequestBody final Definition definition,
final BindingResult bindingResult) { final BindingResult bindingResult)
throws UnprocessableEntityException, BadRequestException {
LOG.info("Received request to insert a new definition"); LOG.info("Received request to insert a new definition");
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 (definition == null) { if (definition == null) {
LOG.error("Passed entity is null"); LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); throw new BadRequestException("Passed definition is be null");
} }
LOG.debug("Saving new definition"); LOG.debug("Saving new definition");
@@ -106,22 +111,22 @@ public class DefinitionController {
@PatchMapping("/{id}") @PatchMapping("/{id}")
public @ResponseBody ResponseEntity<Definition> updateDefinition( public @ResponseBody ResponseEntity<Definition> updateDefinition(
@PathVariable("id") final Integer id, @PathVariable("id") final Integer id,
@RequestBody final Definition definition, final BindingResult bindingResult) { @RequestBody final Definition definition, final BindingResult bindingResult) throws UnprocessableEntityException, BadRequestException {
LOG.info("Received request to update an account"); LOG.info("Received request to update an account");
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 (definition == null) { if (definition == null) {
LOG.error("Passed entity is null"); LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); throw new BadRequestException("Passed definition is null");
} }
if (id == null) { if (id == null) {
LOG.error("Definition ID must be specified"); LOG.error("Definition ID must be specified");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); throw new BadRequestException("Definition ID must be specified");
} }
LOG.debug("Checking for existence of definition with id {}", id); LOG.debug("Checking for existence of definition with id {}", id);
@@ -137,7 +142,7 @@ public class DefinitionController {
if (!existingDefinition.isPresent()) { if (!existingDefinition.isPresent()) {
LOG.error("No definition associated with id {}", id); LOG.error("No definition associated with id {}", id);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} }
PersistenceUtilities.copyNonNullProperties(definition, existingDefinition.get()); PersistenceUtilities.copyNonNullProperties(definition, existingDefinition.get());
@@ -158,11 +163,11 @@ public class DefinitionController {
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public @ResponseBody ResponseEntity<Void> deleteDefinitionById(@PathVariable("id") final Integer id) { public @ResponseBody ResponseEntity<Void> deleteDefinitionById(@PathVariable("id") final Integer id) throws BadRequestException {
LOG.info("Received request to delete definition with id {}", id); LOG.info("Received request to delete definition 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 definition with id {}", id); LOG.debug("Deleting definition with id {}", id);
@@ -0,0 +1,74 @@
package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.exception.BadRequestException;
import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ValidationException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RestControllerAdvice
public class ExceptionHandlingController {
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public void handleMessageNotReadableException(final HttpServletRequest request, final HttpMessageNotReadableException e) {
LOG.error("Unable to bind post data sent to: {} \nCaught Exception: {}", request.getRequestURI(), e.getMessage());
}
@ExceptionHandler(ValidationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String handleMessageInvalidRequest(final HttpServletRequest request, final ValidationException e) {
LOG.error("Request did not validate. Experienced the following error: {}", e.getMessage());
return "Request did not evaluate. Experienced the following error: " + e.getMessage();
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public void handleHttpRequestMethodNotSupported(final HttpServletRequest request) {
LOG.error("Unsupported request method ({}) for URL: {}", request.getMethod(), request.getRequestURI());
}
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
public void handleHttpMediaTypeNotSupportedException(final HttpServletRequest request, final HttpMediaTypeNotSupportedException e) {
LOG.error("Unsupported media type ({}) for URL: {}", e.getContentType(), request.getRequestURI());
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public void handleException(final HttpServletRequest request, final Exception e) {
LOG.error("Exception caught for URL: {}", request.getRequestURI(), e);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public List<String> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
LOG.error("Request did not evaluate. Experienced the following error: {}", e.getMessage());
return e.getBindingResult().getFieldErrors().stream().map(x -> x.getField() + " " + x.getDefaultMessage()).collect(Collectors.toList());
}
@ExceptionHandler(BadRequestException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public List<String> handleBadRequestException(final BadRequestException e) {
return e.getErrors();
}
@ExceptionHandler(UnprocessableEntityException.class)
@ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
public List<String> handleUnprocessableEntityException(final UnprocessableEntityException e) {
return Collections.singletonList("Request did not evaluate. Experienced the following error: " + e.getMessage());
}
}
@@ -1,6 +1,8 @@
package edu.msudenver.tsp.persistence.controller; package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.dto.Proof; 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.persistence.repository.ProofRepository;
import edu.msudenver.tsp.utilities.PersistenceUtilities; import edu.msudenver.tsp.utilities.PersistenceUtilities;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@@ -45,11 +47,11 @@ public class ProofController {
@GetMapping("/id") @GetMapping("/id")
public @ResponseBody public @ResponseBody
ResponseEntity<Proof> getProofById(@RequestParam("id") final Integer id) { ResponseEntity<Proof> getProofById(@RequestParam("id") final Integer id) throws BadRequestException {
LOG.info("Received request to query for proof with id {}", id); LOG.info("Received request to query for proof 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 proof with id {}", id); LOG.debug("Querying for proof with id {}", id);
@@ -75,11 +77,12 @@ public class ProofController {
@GetMapping("/branch") @GetMapping("/branch")
public @ResponseBody public @ResponseBody
ResponseEntity<List<Proof>> getAllProofsByBranch(@RequestParam("branch") String branch) { ResponseEntity<List<Proof>> getAllProofsByBranch(@RequestParam("branch") String branch)
throws BadRequestException {
LOG.info("Received request to query for proofs related to the {} branch of mathematics", branch); LOG.info("Received request to query for proofs 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("-")) {
@@ -110,11 +113,12 @@ public class ProofController {
@GetMapping("/theorem_name") @GetMapping("/theorem_name")
public @ResponseBody public @ResponseBody
ResponseEntity<List<Proof>> getAllProofsByTheoremName(@RequestParam("theorem_name") String theoremName) { ResponseEntity<List<Proof>> getAllProofsByTheoremName(@RequestParam("theorem_name") String theoremName)
throws BadRequestException {
LOG.info("Received request to query for proofs of the theorem {}", theoremName); LOG.info("Received request to query for proofs of the theorem {}", theoremName);
if (theoremName == null) { if (theoremName == null) {
LOG.error("ERROR: theorem name was 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("-")) { if (theoremName.contains("_") || theoremName.contains("-")) {
@@ -147,16 +151,16 @@ public class ProofController {
@Validated({Proof.Insert.class, Default.class}) @Validated({Proof.Insert.class, Default.class})
public @ResponseBody ResponseEntity<Proof> insertProof( public @ResponseBody ResponseEntity<Proof> insertProof(
@Valid @RequestBody final Proof proof, @Valid @RequestBody final Proof proof,
final BindingResult bindingResult) { final BindingResult bindingResult) throws UnprocessableEntityException, BadRequestException {
LOG.info("Received request to insert a new proof"); LOG.info("Received request to insert a new proof");
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 (proof == null) { if (proof == null) {
LOG.error("Passed entity is 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"); LOG.debug("Saving new proof");
@@ -176,22 +180,23 @@ public class ProofController {
@PatchMapping("/{id}") @PatchMapping("/{id}")
public @ResponseBody ResponseEntity<Proof> updateProof( public @ResponseBody ResponseEntity<Proof> updateProof(
@PathVariable("id") final Integer id, @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"); LOG.info("Received request to update a proof");
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 (proof == null) { if (proof == null) {
LOG.error("Passed entity is null"); LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); throw new BadRequestException("Passed proof is null");
} }
if (id == null) { if (id == null) {
LOG.error("Proof ID must be specified"); 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); LOG.debug("Checking for existence of proof with id {}", id);
@@ -207,7 +212,7 @@ public class ProofController {
if (!existingProof.isPresent()) { if (!existingProof.isPresent()) {
LOG.error("No proof associated with id {}", id); 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()); PersistenceUtilities.copyNonNullProperties(proof, existingProof.get());
@@ -228,11 +233,11 @@ public class ProofController {
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public @ResponseBody ResponseEntity<Void> deleteProofById(@PathVariable("id") final Integer id) { public @ResponseBody ResponseEntity<Void> deleteProofById(@PathVariable("id") final Integer id) throws BadRequestException {
LOG.info("Received request to delete proof with id {}", id); LOG.info("Received request to delete proof 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 proof with id {}", id); LOG.debug("Deleting proof with id {}", id);
@@ -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);
@@ -10,6 +10,7 @@ import javax.persistence.Entity;
import javax.persistence.EntityListeners; import javax.persistence.EntityListeners;
import javax.persistence.Table; import javax.persistence.Table;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
@@ -24,7 +25,7 @@ public class Definition extends BaseDto implements Serializable {
@Size(min = 1, max = 200, message = "Must be between 1 and 200 characters") @Size(min = 1, max = 200, message = "Must be between 1 and 200 characters")
private String name; private String name;
@NotBlank(groups = Insert.class, message = "At least one (1) definition must be specified") @NotNull(groups = Insert.class, message = "At least one (1) definition must be specified")
@Type(type = "json") @Column(columnDefinition = "jsonb") private List<String> definition; @Type(type = "json") @Column(columnDefinition = "jsonb") private List<String> definition;
@Type(type = "json") @Column(columnDefinition = "jsonb") private List<String> notation; @Type(type = "json") @Column(columnDefinition = "jsonb") private List<String> notation;
@@ -0,0 +1,20 @@
package edu.msudenver.tsp.persistence.exception;
import lombok.Getter;
import java.util.Collections;
import java.util.List;
public class BadRequestException extends Exception {
@Getter private final List<String> errors;
public BadRequestException(final String message) {
super(message);
errors = Collections.singletonList(message);
}
public BadRequestException(final List<String> errorMessages) {
super(String.join(" ", errorMessages));
errors = errorMessages;
}
}
@@ -0,0 +1,21 @@
package edu.msudenver.tsp.persistence.exception;
import lombok.Getter;
import java.util.Collections;
import java.util.List;
public class UnprocessableEntityException extends Exception {
@Getter
private final List<String> errors;
public UnprocessableEntityException(final String message) {
super(message);
errors = Collections.singletonList(message);
}
public UnprocessableEntityException(final List<String> errorMessages) {
super(String.join(" ", errorMessages));
errors = errorMessages;
}
}
@@ -1,6 +1,8 @@
package edu.msudenver.tsp.persistence.controller; package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.dto.Account; import edu.msudenver.tsp.persistence.dto.Account;
import edu.msudenver.tsp.persistence.exception.BadRequestException;
import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException;
import edu.msudenver.tsp.persistence.repository.AccountsRepository; import edu.msudenver.tsp.persistence.repository.AccountsRepository;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -49,7 +51,7 @@ public class AccountControllerTest {
} }
@Test @Test
public void testGetAccountById() { public void testGetAccountById() throws BadRequestException {
final Account account = createAccount(); final Account account = createAccount();
when(accountsRepository.findById(anyInt())).thenReturn(Optional.ofNullable(account)); when(accountsRepository.findById(anyInt())).thenReturn(Optional.ofNullable(account));
@@ -63,18 +65,13 @@ public class AccountControllerTest {
verify(accountsRepository).findById(anyInt()); verify(accountsRepository).findById(anyInt());
} }
@Test @Test(expected = BadRequestException.class)
public void testGetAccountById_nullId() { public void testGetAccountById_nullId() throws BadRequestException {
final ResponseEntity responseEntity = accountController.getAccountById(null); accountController.getAccountById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(accountsRepository);
} }
@Test @Test
public void testGetAccountById_noAccountFound() { public void testGetAccountById_noAccountFound() throws BadRequestException {
when(accountsRepository.findById(anyInt())).thenReturn(Optional.empty()); when(accountsRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity responseEntity = accountController.getAccountById(1); final ResponseEntity responseEntity = accountController.getAccountById(1);
@@ -86,7 +83,7 @@ public class AccountControllerTest {
} }
@Test @Test
public void testGetAccountByUsername() { public void testGetAccountByUsername() throws BadRequestException {
final Account account = createAccount(); final Account account = createAccount();
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.ofNullable(account)); when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.ofNullable(account));
@@ -100,18 +97,13 @@ public class AccountControllerTest {
verify(accountsRepository).findByUsername(anyString()); verify(accountsRepository).findByUsername(anyString());
} }
@Test @Test(expected = BadRequestException.class)
public void testGetAccountById_nullUsername() { public void testGetAccountById_nullUsername() throws BadRequestException {
final ResponseEntity responseEntity = accountController.getAccountByUsername(null); accountController.getAccountByUsername(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(accountsRepository);
} }
@Test @Test
public void testGetAccountByUsername_noAccountFound() { public void testGetAccountByUsername_noAccountFound() throws BadRequestException {
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.empty()); when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.empty());
final ResponseEntity responseEntity = accountController.getAccountByUsername("Test username"); final ResponseEntity responseEntity = accountController.getAccountByUsername("Test username");
@@ -123,7 +115,7 @@ public class AccountControllerTest {
} }
@Test @Test
public void testInsertAccount() { public void testInsertAccount() throws UnprocessableEntityException, BadRequestException {
final Account account = createAccount(); final Account account = createAccount();
when(accountsRepository.save(any(Account.class))).thenReturn(account); when(accountsRepository.save(any(Account.class))).thenReturn(account);
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.empty()); when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.empty());
@@ -139,7 +131,7 @@ public class AccountControllerTest {
} }
@Test @Test
public void testInsertAccount_usernameAlreadyExists() { public void testInsertAccount_usernameAlreadyExists() throws UnprocessableEntityException, BadRequestException {
final Account account = createAccount(); final Account account = createAccount();
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.of(account)); when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.of(account));
@@ -152,8 +144,8 @@ public class AccountControllerTest {
verify(accountsRepository, times(0)).save(any(Account.class)); verify(accountsRepository, times(0)).save(any(Account.class));
} }
@Test @Test(expected = BadRequestException.class)
public void testInsertAccount_accountsDtoIsNull() { public void testInsertAccount_accountsDtoIsNull() throws UnprocessableEntityException, BadRequestException {
final ResponseEntity responseEntity = accountController.insertAccount(null, bindingResult); final ResponseEntity responseEntity = accountController.insertAccount(null, bindingResult);
assertNotNull(responseEntity); assertNotNull(responseEntity);
@@ -162,21 +154,16 @@ public class AccountControllerTest {
verifyZeroInteractions(accountsRepository); verifyZeroInteractions(accountsRepository);
} }
@Test @Test(expected = UnprocessableEntityException.class)
public void testInsertAccount_bindingResultHasErrors() { public void testInsertAccount_bindingResultHasErrors() throws UnprocessableEntityException, BadRequestException {
final Account account = createAccount(); final Account account = createAccount();
when(bindingResult.hasErrors()).thenReturn(true); when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity responseEntity = accountController.insertAccount(account, bindingResult); accountController.insertAccount(account, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
verifyZeroInteractions(accountsRepository);
} }
@Test @Test
public void testUpdateAccount() { public void testUpdateAccount() throws UnprocessableEntityException, BadRequestException {
final Account existingAccount = createAccount(); final Account existingAccount = createAccount();
existingAccount.setId(1); existingAccount.setId(1);
existingAccount.setVersion(1); existingAccount.setVersion(1);
@@ -197,40 +184,25 @@ public class AccountControllerTest {
verify(accountsRepository).save(any(Account.class)); verify(accountsRepository).save(any(Account.class));
} }
@Test @Test(expected = UnprocessableEntityException.class)
public void testUpdateAccount_bindingResultHasErrors() { public void testUpdateAccount_bindingResultHasErrors() throws UnprocessableEntityException, BadRequestException {
when(bindingResult.hasErrors()).thenReturn(true); when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity<Account> responseEntity = accountController.updateAccount(1, createAccount(), bindingResult); accountController.updateAccount(1, createAccount(), bindingResult);
}
assertNotNull(responseEntity); @Test(expected = BadRequestException.class)
assertFalse(responseEntity.hasBody()); public void testUpdateAccount_accountsDtoIsNull() throws UnprocessableEntityException, BadRequestException {
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode()); accountController.updateAccount(1, null, bindingResult);
verifyZeroInteractions(accountsRepository); }
@Test(expected = BadRequestException.class)
public void testUpdateAccount_idIsNull() throws UnprocessableEntityException, BadRequestException {
accountController.updateAccount(null, createAccount(), bindingResult);
} }
@Test @Test
public void testUpdateAccount_accountsDtoIsNull() { public void testUpdateAccount_accountDoesNotExist() throws UnprocessableEntityException, BadRequestException {
final ResponseEntity<Account> responseEntity = accountController.updateAccount(1, null, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(accountsRepository);
}
@Test
public void testUpdateAccount_idIsNull() {
final ResponseEntity<Account> responseEntity = accountController.updateAccount(null, createAccount(), bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(accountsRepository);
}
@Test
public void testUpdateAccount_accountDoesNotExist() {
when(accountsRepository.findById(anyInt())).thenReturn(Optional.empty()); when(accountsRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity<Account> responseEntity = accountController.updateAccount(1, createAccount(), bindingResult); final ResponseEntity<Account> responseEntity = accountController.updateAccount(1, createAccount(), bindingResult);
@@ -242,7 +214,7 @@ public class AccountControllerTest {
} }
@Test @Test
public void testDeleteAccountById() { public void testDeleteAccountById() throws BadRequestException {
doNothing().when(accountsRepository).deleteById(anyInt()); doNothing().when(accountsRepository).deleteById(anyInt());
final ResponseEntity responseEntity = accountController.deleteAccountById(1); final ResponseEntity responseEntity = accountController.deleteAccountById(1);
@@ -253,14 +225,9 @@ public class AccountControllerTest {
verify(accountsRepository).deleteById(anyInt()); verify(accountsRepository).deleteById(anyInt());
} }
@Test @Test(expected = BadRequestException.class)
public void testDeleteAccountById_idIsNull() { public void testDeleteAccountById_idIsNull() throws BadRequestException {
final ResponseEntity responseEntity = accountController.deleteAccountById(null); accountController.deleteAccountById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(accountsRepository);
} }
private Account createAccount() { private Account createAccount() {
@@ -1,6 +1,8 @@
package edu.msudenver.tsp.persistence.controller; package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.dto.Definition; import edu.msudenver.tsp.persistence.dto.Definition;
import edu.msudenver.tsp.persistence.exception.BadRequestException;
import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException;
import edu.msudenver.tsp.persistence.repository.DefinitionRepository; import edu.msudenver.tsp.persistence.repository.DefinitionRepository;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -48,7 +50,7 @@ public class DefinitionControllerTest {
} }
@Test @Test
public void testGetDefinitionsById() { public void testGetDefinitionsById() throws BadRequestException {
final Definition definition = createDefinition(); final Definition definition = createDefinition();
when(definitionRepository.findById(anyInt())).thenReturn(Optional.ofNullable(definition)); when(definitionRepository.findById(anyInt())).thenReturn(Optional.ofNullable(definition));
@@ -62,18 +64,13 @@ public class DefinitionControllerTest {
verify(definitionRepository).findById(anyInt()); verify(definitionRepository).findById(anyInt());
} }
@Test @Test(expected = BadRequestException.class)
public void testGetDefinitionById_nullId() { public void testGetDefinitionById_nullId() throws BadRequestException {
final ResponseEntity responseEntity = definitionController.getDefinitionById(null); definitionController.getDefinitionById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(definitionRepository);
} }
@Test @Test
public void testGetDefinitionById_noDefinitionFound() { public void testGetDefinitionById_noDefinitionFound() throws BadRequestException {
when(definitionRepository.findById(anyInt())).thenReturn(Optional.empty()); when(definitionRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity responseEntity = definitionController.getDefinitionById(1); final ResponseEntity responseEntity = definitionController.getDefinitionById(1);
@@ -85,7 +82,7 @@ public class DefinitionControllerTest {
} }
@Test @Test
public void testInsertDefinition() { public void testInsertDefinition() throws BadRequestException, UnprocessableEntityException {
final Definition definition = createDefinition(); final Definition definition = createDefinition();
when(definitionRepository.save(any(Definition.class))).thenReturn(definition); when(definitionRepository.save(any(Definition.class))).thenReturn(definition);
@@ -99,31 +96,21 @@ public class DefinitionControllerTest {
verify(definitionRepository).save(any(Definition.class)); verify(definitionRepository).save(any(Definition.class));
} }
@Test @Test(expected = BadRequestException.class)
public void testInsertDefinition_definitionDtoIsNull() { public void testInsertDefinition_definitionDtoIsNull() throws BadRequestException, UnprocessableEntityException {
final ResponseEntity responseEntity = definitionController.insertDefinition(null, bindingResult); definitionController.insertDefinition(null, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(definitionRepository);
} }
@Test @Test(expected = UnprocessableEntityException.class)
public void testInsertDefinition_bindingResultHasErrors() { public void testInsertDefinition_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException {
final Definition definition = createDefinition(); final Definition definition = createDefinition();
when(bindingResult.hasErrors()).thenReturn(true); when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity responseEntity = definitionController.insertDefinition(definition, bindingResult); definitionController.insertDefinition(definition, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
verifyZeroInteractions(definitionRepository);
} }
@Test @Test
public void testUpdateDefinition() { public void testUpdateDefinition() throws BadRequestException, UnprocessableEntityException {
final Definition existingDefinition = createDefinition(); final Definition existingDefinition = createDefinition();
existingDefinition.setId(1); existingDefinition.setId(1);
existingDefinition.setVersion(1); existingDefinition.setVersion(1);
@@ -144,52 +131,37 @@ public class DefinitionControllerTest {
verify(definitionRepository).save(any(Definition.class)); verify(definitionRepository).save(any(Definition.class));
} }
@Test @Test(expected = UnprocessableEntityException.class)
public void testUpdateDefinition_bindingResultErrors() { public void testUpdateDefinition_bindingResultErrors() throws BadRequestException, UnprocessableEntityException {
when(bindingResult.hasErrors()).thenReturn(true); when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity<Definition> responseEntity = definitionController.updateDefinition(1, createDefinition(), bindingResult); definitionController.updateDefinition(1, createDefinition(), bindingResult);
}
assertNotNull(responseEntity); @Test(expected = BadRequestException.class)
assertFalse(responseEntity.hasBody()); public void testUpdateDefinition_definitionDtoIsNull() throws BadRequestException, UnprocessableEntityException {
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode()); definitionController.updateDefinition(1, null, bindingResult);
verifyZeroInteractions(definitionRepository); }
@Test(expected = BadRequestException.class)
public void testUpdateDefinition_idIsNull() throws BadRequestException, UnprocessableEntityException {
definitionController.updateDefinition(null, createDefinition(), bindingResult);
} }
@Test @Test
public void testUpdateDefinition_definitionDtoIsNull() { public void testUpdateDefinition_definitionDoesntExist() throws BadRequestException, UnprocessableEntityException {
final ResponseEntity<Definition> responseEntity = definitionController.updateDefinition(1, null, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(definitionRepository);
}
@Test
public void testUpdateDefinition_idIsNull() {
final ResponseEntity<Definition> responseEntity = definitionController.updateDefinition(null, createDefinition(), bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(definitionRepository);
}
@Test
public void testUpdateDefinition_definitionDoesntExist() {
when(definitionRepository.findById(anyInt())).thenReturn(Optional.empty()); when(definitionRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity<Definition> responseEntity = definitionController.updateDefinition(1, createDefinition(), bindingResult); final ResponseEntity<Definition> responseEntity = definitionController.updateDefinition(1, createDefinition(), bindingResult);
assertNotNull(responseEntity); assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody()); assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
verify(definitionRepository, times(0)).save(any(Definition.class)); verify(definitionRepository, times(0)).save(any(Definition.class));
} }
@Test @Test
public void testDeleteDefinitionById() { public void testDeleteDefinitionById() throws BadRequestException {
doNothing().when(definitionRepository).deleteById(anyInt()); doNothing().when(definitionRepository).deleteById(anyInt());
final ResponseEntity responseEntity = definitionController.deleteDefinitionById(1); final ResponseEntity responseEntity = definitionController.deleteDefinitionById(1);
@@ -200,14 +172,9 @@ public class DefinitionControllerTest {
verify(definitionRepository).deleteById(anyInt()); verify(definitionRepository).deleteById(anyInt());
} }
@Test @Test(expected = BadRequestException.class)
public void testDeleteDefinitionById_nullId() { public void testDeleteDefinitionById_nullId() throws BadRequestException {
final ResponseEntity responseEntity = definitionController.deleteDefinitionById(null); definitionController.deleteDefinitionById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(definitionRepository);
} }
private Definition createDefinition() { private Definition createDefinition() {
@@ -0,0 +1,54 @@
package edu.msudenver.tsp.persistence.controller;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Matchers.any;
@RunWith(MockitoJUnitRunner.class)
public class ExceptionHandlingControllerTest {
@Mock
ExceptionHandlingController exceptionHandlingController;
@Test
public void testHandleMessageNotReadableException() {
exceptionHandlingController.handleMessageNotReadableException(any(), any());
}
@Test
public void testHandleMessageInvalidRequestException() {
exceptionHandlingController.handleMessageInvalidRequest(any(), any());
}
@Test
public void testHandleHttpRequestMethodNotSupportedException() {
exceptionHandlingController.handleHttpRequestMethodNotSupported(any());
}
@Test
public void testHandleHttpMediaTypeNotSupportedException() {
exceptionHandlingController.handleHttpMediaTypeNotSupportedException(any(), any());
}
@Test
public void testHandleException() {
exceptionHandlingController.handleException(any(), any());
}
@Test
public void testHandleMethodArgumentNotValidException() {
exceptionHandlingController.handleMethodArgumentNotValidException(any());
}
@Test
public void testHandleBadRequestException() {
exceptionHandlingController.handleBadRequestException(any());
}
@Test
public void testHandleUnprocessableEntityException() {
exceptionHandlingController.handleUnprocessableEntityException(any());
}
}
@@ -1,6 +1,8 @@
package edu.msudenver.tsp.persistence.controller; package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.dto.Proof; 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.persistence.repository.ProofRepository;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -45,7 +47,7 @@ public class ProofControllerTest {
} }
@Test @Test
public void testGetAllProofsByBranch() { public void testGetAllProofsByBranch() throws BadRequestException {
final Proof proofDto = createProof(); final Proof proofDto = createProof();
final List<Proof> listOfProofs = new ArrayList<>(); final List<Proof> listOfProofs = new ArrayList<>();
listOfProofs.add(proofDto); listOfProofs.add(proofDto);
@@ -63,18 +65,13 @@ public class ProofControllerTest {
responseEntity.getBody().forEach(proof -> assertEquals(proofDto, proof)); responseEntity.getBody().forEach(proof -> assertEquals(proofDto, proof));
} }
@Test @Test(expected = BadRequestException.class)
public void testGetAllProfsByBranch_nullBranch() { public void testGetAllProfsByBranch_nullBranch() throws BadRequestException {
final ResponseEntity<List<Proof>> responseEntity = proofController.getAllProofsByBranch(null); proofController.getAllProofsByBranch(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(proofRepository);
} }
@Test @Test
public void testGetAllProofsByBranch_noProofsFound() { public void testGetAllProofsByBranch_noProofsFound() throws BadRequestException {
when(proofRepository.findByBranch(anyString())).thenReturn(Collections.emptyList()); when(proofRepository.findByBranch(anyString())).thenReturn(Collections.emptyList());
final ResponseEntity<List<Proof>> responseEntity = proofController.getAllProofsByBranch("test-nonexistent-branch"); final ResponseEntity<List<Proof>> responseEntity = proofController.getAllProofsByBranch("test-nonexistent-branch");
@@ -85,7 +82,7 @@ public class ProofControllerTest {
} }
@Test @Test
public void testGetAllProofsByTheoremName() { public void testGetAllProofsByTheoremName() throws BadRequestException {
final Proof proofDto = createProof(); final Proof proofDto = createProof();
final List<Proof> listOfProofs = new ArrayList<>(); final List<Proof> listOfProofs = new ArrayList<>();
listOfProofs.add(proofDto); listOfProofs.add(proofDto);
@@ -103,18 +100,13 @@ public class ProofControllerTest {
responseEntity.getBody().forEach(proof -> assertEquals(proofDto, proof)); responseEntity.getBody().forEach(proof -> assertEquals(proofDto, proof));
} }
@Test @Test(expected = BadRequestException.class)
public void testGetAllProofsByTheoremName_nullTheoremName() { public void testGetAllProofsByTheoremName_nullTheoremName() throws BadRequestException {
final ResponseEntity<List<Proof>> responseEntity = proofController.getAllProofsByTheoremName(null); proofController.getAllProofsByTheoremName(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(proofRepository);
} }
@Test @Test
public void testGetAllProofsByTheoremName_noProofsFound() { public void testGetAllProofsByTheoremName_noProofsFound() throws BadRequestException {
when(proofRepository.findByTheoremName(anyString())).thenReturn(Collections.emptyList()); when(proofRepository.findByTheoremName(anyString())).thenReturn(Collections.emptyList());
final ResponseEntity<List<Proof>> responseEntity = proofController.getAllProofsByTheoremName("test-nonexistent-proof"); final ResponseEntity<List<Proof>> responseEntity = proofController.getAllProofsByTheoremName("test-nonexistent-proof");
@@ -125,7 +117,7 @@ public class ProofControllerTest {
} }
@Test @Test
public void testGetProofById() { public void testGetProofById() throws BadRequestException {
final Proof proof = createProof(); final Proof proof = createProof();
when(proofRepository.findById(anyInt())).thenReturn(Optional.ofNullable(proof)); when(proofRepository.findById(anyInt())).thenReturn(Optional.ofNullable(proof));
@@ -139,18 +131,13 @@ public class ProofControllerTest {
verify(proofRepository).findById(anyInt()); verify(proofRepository).findById(anyInt());
} }
@Test @Test(expected = BadRequestException.class)
public void testGetProofById_nullId() { public void testGetProofById_nullId() throws BadRequestException {
final ResponseEntity responseEntity = proofController.getProofById(null); proofController.getProofById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(proofRepository);
} }
@Test @Test
public void testGetProofById_noProofFound() { public void testGetProofById_noProofFound() throws BadRequestException {
when(proofRepository.findById(anyInt())).thenReturn(Optional.empty()); when(proofRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity responseEntity = proofController.getProofById(1); final ResponseEntity responseEntity = proofController.getProofById(1);
@@ -162,7 +149,7 @@ public class ProofControllerTest {
} }
@Test @Test
public void testInsertProof() { public void testInsertProof() throws BadRequestException, UnprocessableEntityException {
final Proof proof = createProof(); final Proof proof = createProof();
when(proofRepository.save(any(Proof.class))).thenReturn(proof); when(proofRepository.save(any(Proof.class))).thenReturn(proof);
@@ -176,31 +163,21 @@ public class ProofControllerTest {
verify(proofRepository).save(any(Proof.class)); verify(proofRepository).save(any(Proof.class));
} }
@Test @Test(expected = BadRequestException.class)
public void testInsertProof_proofDtoIsNull() { public void testInsertProof_proofDtoIsNull() throws BadRequestException, UnprocessableEntityException {
final ResponseEntity responseEntity = proofController.insertProof(null, bindingResult); proofController.insertProof(null, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(proofRepository);
} }
@Test @Test(expected = UnprocessableEntityException.class)
public void testInsertProof_bindingResultHasErrors() { public void testInsertProof_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException {
final Proof proof = createProof(); final Proof proof = createProof();
when(bindingResult.hasErrors()).thenReturn(true); when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity responseEntity = proofController.insertProof(proof, bindingResult); proofController.insertProof(proof, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
verifyZeroInteractions(proofRepository);
} }
@Test @Test
public void testUpdateProof() { public void testUpdateProof() throws BadRequestException, UnprocessableEntityException {
final Proof existingProof = createProof(); final Proof existingProof = createProof();
existingProof.setId(1); existingProof.setId(1);
existingProof.setVersion(1); existingProof.setVersion(1);
@@ -221,52 +198,37 @@ public class ProofControllerTest {
verify(proofRepository).save(any(Proof.class)); verify(proofRepository).save(any(Proof.class));
} }
@Test @Test(expected = UnprocessableEntityException.class)
public void testUpdateProof_bindingResultHasErrors() { public void testUpdateProof_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException {
when(bindingResult.hasErrors()).thenReturn(true); when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity<Proof> responseEntity = proofController.updateProof(1, createProof(), bindingResult); proofController.updateProof(1, createProof(), bindingResult);
}
assertNotNull(responseEntity); @Test(expected = BadRequestException.class)
assertFalse(responseEntity.hasBody()); public void testUpdateProof_proofDtoIsNull() throws BadRequestException, UnprocessableEntityException {
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode()); proofController.updateProof(1, null, bindingResult);
verifyZeroInteractions(proofRepository); }
@Test(expected = BadRequestException.class)
public void testUpdateProof_idIsNull() throws BadRequestException, UnprocessableEntityException {
proofController.updateProof(null, createProof(), bindingResult);
} }
@Test @Test
public void testUpdateProof_proofDtoIsNull() { public void testUpdateProof_theoremDoesNotExist() throws BadRequestException, UnprocessableEntityException {
final ResponseEntity<Proof> 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<Proof> responseEntity = proofController.updateProof(null, createProof(), bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(proofRepository);
}
@Test
public void testUpdateProof_theoremDoesNotExist() {
when(proofRepository.findById(anyInt())).thenReturn(Optional.empty()); when(proofRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity<Proof> responseEntity = proofController.updateProof(1, createProof(), bindingResult); final ResponseEntity<Proof> responseEntity = proofController.updateProof(1, createProof(), bindingResult);
assertNotNull(responseEntity); assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody()); assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
verify(proofRepository, times(0)).save(any(Proof.class)); verify(proofRepository, times(0)).save(any(Proof.class));
} }
@Test @Test
public void testDeleteProofById() { public void testDeleteProofById() throws BadRequestException {
doNothing().when(proofRepository).deleteById(anyInt()); doNothing().when(proofRepository).deleteById(anyInt());
final ResponseEntity responseEntity = proofController.deleteProofById(1); final ResponseEntity responseEntity = proofController.deleteProofById(1);
@@ -277,14 +239,9 @@ public class ProofControllerTest {
verify(proofRepository).deleteById(anyInt()); verify(proofRepository).deleteById(anyInt());
} }
@Test @Test(expected = BadRequestException.class)
public void testDeleteProofById_idIsNull() { public void testDeleteProofById_idIsNull() throws BadRequestException {
final ResponseEntity responseEntity = proofController.deleteProofById(null); proofController.deleteProofById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(proofRepository);
} }
private Proof createProof() { private Proof createProof() {
@@ -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() {