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;
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.utilities.PersistenceUtilities;
import lombok.AllArgsConstructor;
@@ -23,6 +25,7 @@ import java.util.Optional;
@RestController
@AllArgsConstructor
@RequestMapping("/accounts")
@Validated
public class AccountController {
private final AccountsRepository accountsRepository;
@@ -48,11 +51,11 @@ public class AccountController {
@GetMapping("/id")
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);
if (id == 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);
@@ -77,11 +80,11 @@ public class AccountController {
@GetMapping("/username")
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);
if (username == 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);
@@ -107,17 +110,18 @@ public class AccountController {
@PostMapping({"","/"})
@Validated({Account.Insert.class, Default.class})
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");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
throw new UnprocessableEntityException(bindingResult.getAllErrors().toString());
}
if (account == null) {
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());
@@ -152,22 +156,23 @@ public class AccountController {
@PatchMapping("/{id}")
public @ResponseBody ResponseEntity<Account> updateAccount(
@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");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
throw new UnprocessableEntityException(bindingResult.getAllErrors().toString());
}
if (account == null) {
LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
throw new BadRequestException("Passed account is null");
}
if (id == null) {
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);
@@ -204,11 +209,11 @@ public class AccountController {
}
@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);
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 account with id {}", id);
@@ -1,6 +1,8 @@
package edu.msudenver.tsp.persistence.controller;
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.utilities.PersistenceUtilities;
import lombok.AllArgsConstructor;
@@ -21,6 +23,7 @@ import java.util.Optional;
@RestController
@AllArgsConstructor
@RequestMapping(path = "/definitions")
@Validated
public class DefinitionController {
private final DefinitionRepository definitionRepository;
@@ -45,11 +48,12 @@ public class DefinitionController {
@GetMapping("/{id}")
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);
if (id == null) {
LOG.error("ERROR: ID was null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
LOG.error("ERROR: ID cannot be null");
throw new BadRequestException("ERROR: ID cannot be null");
}
LOG.debug("Querying for definition with id {}", id);
@@ -77,16 +81,17 @@ public class DefinitionController {
@Validated({Definition.Insert.class, Default.class})
public @ResponseBody ResponseEntity<Definition> insertDefinition(
@Valid @RequestBody final Definition definition,
final BindingResult bindingResult) {
final BindingResult bindingResult)
throws UnprocessableEntityException, BadRequestException {
LOG.info("Received request to insert a new definition");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
throw new UnprocessableEntityException(bindingResult.getAllErrors().toString());
}
if (definition == 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");
@@ -106,22 +111,22 @@ public class DefinitionController {
@PatchMapping("/{id}")
public @ResponseBody ResponseEntity<Definition> updateDefinition(
@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");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
throw new UnprocessableEntityException(bindingResult.getAllErrors().toString());
}
if (definition == null) {
LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
throw new BadRequestException("Passed definition is null");
}
if (id == null) {
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);
@@ -137,7 +142,7 @@ public class DefinitionController {
if (!existingDefinition.isPresent()) {
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());
@@ -158,11 +163,11 @@ public class DefinitionController {
}
@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);
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 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;
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<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);
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<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);
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<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);
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<Proof> 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<Proof> 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<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);
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);
@@ -1,6 +1,8 @@
package edu.msudenver.tsp.persistence.controller;
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.utilities.PersistenceUtilities;
import lombok.AllArgsConstructor;
@@ -45,11 +47,11 @@ public class TheoremController {
@GetMapping("/branch")
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);
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("-")) {
@@ -80,11 +82,11 @@ public class TheoremController {
@GetMapping("/proven_status")
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);
if (provenStatus == 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);
@@ -112,11 +114,11 @@ public class TheoremController {
@GetMapping("/name")
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);
if (name == 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("-")) {
@@ -147,11 +149,11 @@ public class TheoremController {
@GetMapping("/id")
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);
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 theorem with id {}", id);
@@ -179,16 +181,16 @@ public class TheoremController {
@Validated({Theorem.Insert.class, Default.class})
public @ResponseBody ResponseEntity<Theorem> insertTheorem(
@Valid @RequestBody final Theorem theorem,
final BindingResult bindingResult) {
final BindingResult bindingResult) throws UnprocessableEntityException, BadRequestException {
LOG.info("Received request to insert a new theorem");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
throw new UnprocessableEntityException(bindingResult.getAllErrors().toString());
}
if (theorem == 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");
@@ -208,22 +210,22 @@ public class TheoremController {
@PatchMapping("/{id}")
public @ResponseBody ResponseEntity<Theorem> updateTheorem(
@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");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
throw new UnprocessableEntityException(bindingResult.getAllErrors().toString());
}
if (theorem == null) {
LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
throw new BadRequestException("Passed theorem is null");
}
if (id == null) {
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);
@@ -239,7 +241,7 @@ public class TheoremController {
if (!existingTheorem.isPresent()) {
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());
@@ -260,11 +262,11 @@ public class TheoremController {
}
@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);
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 theorem with id {}", id);
@@ -10,6 +10,7 @@ import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
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")
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> 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;
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 org.junit.Test;
import org.junit.runner.RunWith;
@@ -49,7 +51,7 @@ public class AccountControllerTest {
}
@Test
public void testGetAccountById() {
public void testGetAccountById() throws BadRequestException {
final Account account = createAccount();
when(accountsRepository.findById(anyInt())).thenReturn(Optional.ofNullable(account));
@@ -63,18 +65,13 @@ public class AccountControllerTest {
verify(accountsRepository).findById(anyInt());
}
@Test
public void testGetAccountById_nullId() {
final ResponseEntity responseEntity = accountController.getAccountById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(accountsRepository);
@Test(expected = BadRequestException.class)
public void testGetAccountById_nullId() throws BadRequestException {
accountController.getAccountById(null);
}
@Test
public void testGetAccountById_noAccountFound() {
public void testGetAccountById_noAccountFound() throws BadRequestException {
when(accountsRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity responseEntity = accountController.getAccountById(1);
@@ -86,7 +83,7 @@ public class AccountControllerTest {
}
@Test
public void testGetAccountByUsername() {
public void testGetAccountByUsername() throws BadRequestException {
final Account account = createAccount();
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.ofNullable(account));
@@ -100,18 +97,13 @@ public class AccountControllerTest {
verify(accountsRepository).findByUsername(anyString());
}
@Test
public void testGetAccountById_nullUsername() {
final ResponseEntity responseEntity = accountController.getAccountByUsername(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(accountsRepository);
@Test(expected = BadRequestException.class)
public void testGetAccountById_nullUsername() throws BadRequestException {
accountController.getAccountByUsername(null);
}
@Test
public void testGetAccountByUsername_noAccountFound() {
public void testGetAccountByUsername_noAccountFound() throws BadRequestException {
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.empty());
final ResponseEntity responseEntity = accountController.getAccountByUsername("Test username");
@@ -123,7 +115,7 @@ public class AccountControllerTest {
}
@Test
public void testInsertAccount() {
public void testInsertAccount() throws UnprocessableEntityException, BadRequestException {
final Account account = createAccount();
when(accountsRepository.save(any(Account.class))).thenReturn(account);
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.empty());
@@ -139,7 +131,7 @@ public class AccountControllerTest {
}
@Test
public void testInsertAccount_usernameAlreadyExists() {
public void testInsertAccount_usernameAlreadyExists() throws UnprocessableEntityException, BadRequestException {
final Account account = createAccount();
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.of(account));
@@ -152,8 +144,8 @@ public class AccountControllerTest {
verify(accountsRepository, times(0)).save(any(Account.class));
}
@Test
public void testInsertAccount_accountsDtoIsNull() {
@Test(expected = BadRequestException.class)
public void testInsertAccount_accountsDtoIsNull() throws UnprocessableEntityException, BadRequestException {
final ResponseEntity responseEntity = accountController.insertAccount(null, bindingResult);
assertNotNull(responseEntity);
@@ -162,21 +154,16 @@ public class AccountControllerTest {
verifyZeroInteractions(accountsRepository);
}
@Test
public void testInsertAccount_bindingResultHasErrors() {
@Test(expected = UnprocessableEntityException.class)
public void testInsertAccount_bindingResultHasErrors() throws UnprocessableEntityException, BadRequestException {
final Account account = createAccount();
when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity responseEntity = accountController.insertAccount(account, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
verifyZeroInteractions(accountsRepository);
accountController.insertAccount(account, bindingResult);
}
@Test
public void testUpdateAccount() {
public void testUpdateAccount() throws UnprocessableEntityException, BadRequestException {
final Account existingAccount = createAccount();
existingAccount.setId(1);
existingAccount.setVersion(1);
@@ -197,40 +184,25 @@ public class AccountControllerTest {
verify(accountsRepository).save(any(Account.class));
}
@Test
public void testUpdateAccount_bindingResultHasErrors() {
@Test(expected = UnprocessableEntityException.class)
public void testUpdateAccount_bindingResultHasErrors() throws UnprocessableEntityException, BadRequestException {
when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity<Account> responseEntity = accountController.updateAccount(1, createAccount(), bindingResult);
accountController.updateAccount(1, createAccount(), bindingResult);
}
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
verifyZeroInteractions(accountsRepository);
@Test(expected = BadRequestException.class)
public void testUpdateAccount_accountsDtoIsNull() throws UnprocessableEntityException, BadRequestException {
accountController.updateAccount(1, null, bindingResult);
}
@Test(expected = BadRequestException.class)
public void testUpdateAccount_idIsNull() throws UnprocessableEntityException, BadRequestException {
accountController.updateAccount(null, createAccount(), bindingResult);
}
@Test
public void testUpdateAccount_accountsDtoIsNull() {
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() {
public void testUpdateAccount_accountDoesNotExist() throws UnprocessableEntityException, BadRequestException {
when(accountsRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity<Account> responseEntity = accountController.updateAccount(1, createAccount(), bindingResult);
@@ -242,7 +214,7 @@ public class AccountControllerTest {
}
@Test
public void testDeleteAccountById() {
public void testDeleteAccountById() throws BadRequestException {
doNothing().when(accountsRepository).deleteById(anyInt());
final ResponseEntity responseEntity = accountController.deleteAccountById(1);
@@ -253,14 +225,9 @@ public class AccountControllerTest {
verify(accountsRepository).deleteById(anyInt());
}
@Test
public void testDeleteAccountById_idIsNull() {
final ResponseEntity responseEntity = accountController.deleteAccountById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(accountsRepository);
@Test(expected = BadRequestException.class)
public void testDeleteAccountById_idIsNull() throws BadRequestException {
accountController.deleteAccountById(null);
}
private Account createAccount() {
@@ -1,6 +1,8 @@
package edu.msudenver.tsp.persistence.controller;
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 org.junit.Test;
import org.junit.runner.RunWith;
@@ -48,7 +50,7 @@ public class DefinitionControllerTest {
}
@Test
public void testGetDefinitionsById() {
public void testGetDefinitionsById() throws BadRequestException {
final Definition definition = createDefinition();
when(definitionRepository.findById(anyInt())).thenReturn(Optional.ofNullable(definition));
@@ -62,18 +64,13 @@ public class DefinitionControllerTest {
verify(definitionRepository).findById(anyInt());
}
@Test
public void testGetDefinitionById_nullId() {
final ResponseEntity responseEntity = definitionController.getDefinitionById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(definitionRepository);
@Test(expected = BadRequestException.class)
public void testGetDefinitionById_nullId() throws BadRequestException {
definitionController.getDefinitionById(null);
}
@Test
public void testGetDefinitionById_noDefinitionFound() {
public void testGetDefinitionById_noDefinitionFound() throws BadRequestException {
when(definitionRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity responseEntity = definitionController.getDefinitionById(1);
@@ -85,7 +82,7 @@ public class DefinitionControllerTest {
}
@Test
public void testInsertDefinition() {
public void testInsertDefinition() throws BadRequestException, UnprocessableEntityException {
final Definition definition = createDefinition();
when(definitionRepository.save(any(Definition.class))).thenReturn(definition);
@@ -99,31 +96,21 @@ public class DefinitionControllerTest {
verify(definitionRepository).save(any(Definition.class));
}
@Test
public void testInsertDefinition_definitionDtoIsNull() {
final ResponseEntity responseEntity = definitionController.insertDefinition(null, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(definitionRepository);
@Test(expected = BadRequestException.class)
public void testInsertDefinition_definitionDtoIsNull() throws BadRequestException, UnprocessableEntityException {
definitionController.insertDefinition(null, bindingResult);
}
@Test
public void testInsertDefinition_bindingResultHasErrors() {
@Test(expected = UnprocessableEntityException.class)
public void testInsertDefinition_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException {
final Definition definition = createDefinition();
when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity responseEntity = definitionController.insertDefinition(definition, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
verifyZeroInteractions(definitionRepository);
definitionController.insertDefinition(definition, bindingResult);
}
@Test
public void testUpdateDefinition() {
public void testUpdateDefinition() throws BadRequestException, UnprocessableEntityException {
final Definition existingDefinition = createDefinition();
existingDefinition.setId(1);
existingDefinition.setVersion(1);
@@ -144,52 +131,37 @@ public class DefinitionControllerTest {
verify(definitionRepository).save(any(Definition.class));
}
@Test
public void testUpdateDefinition_bindingResultErrors() {
@Test(expected = UnprocessableEntityException.class)
public void testUpdateDefinition_bindingResultErrors() throws BadRequestException, UnprocessableEntityException {
when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity<Definition> responseEntity = definitionController.updateDefinition(1, createDefinition(), bindingResult);
definitionController.updateDefinition(1, createDefinition(), bindingResult);
}
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
verifyZeroInteractions(definitionRepository);
@Test(expected = BadRequestException.class)
public void testUpdateDefinition_definitionDtoIsNull() throws BadRequestException, UnprocessableEntityException {
definitionController.updateDefinition(1, null, bindingResult);
}
@Test(expected = BadRequestException.class)
public void testUpdateDefinition_idIsNull() throws BadRequestException, UnprocessableEntityException {
definitionController.updateDefinition(null, createDefinition(), bindingResult);
}
@Test
public void testUpdateDefinition_definitionDtoIsNull() {
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() {
public void testUpdateDefinition_definitionDoesntExist() throws BadRequestException, UnprocessableEntityException {
when(definitionRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity<Definition> responseEntity = definitionController.updateDefinition(1, createDefinition(), bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
verify(definitionRepository, times(0)).save(any(Definition.class));
}
@Test
public void testDeleteDefinitionById() {
public void testDeleteDefinitionById() throws BadRequestException {
doNothing().when(definitionRepository).deleteById(anyInt());
final ResponseEntity responseEntity = definitionController.deleteDefinitionById(1);
@@ -200,14 +172,9 @@ public class DefinitionControllerTest {
verify(definitionRepository).deleteById(anyInt());
}
@Test
public void testDeleteDefinitionById_nullId() {
final ResponseEntity responseEntity = definitionController.deleteDefinitionById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(definitionRepository);
@Test(expected = BadRequestException.class)
public void testDeleteDefinitionById_nullId() throws BadRequestException {
definitionController.deleteDefinitionById(null);
}
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;
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<Proof> 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<List<Proof>> 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<List<Proof>> 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<Proof> 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<List<Proof>> 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<List<Proof>> 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<Proof> 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<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() {
public void testUpdateProof_theoremDoesNotExist() throws BadRequestException, UnprocessableEntityException {
when(proofRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity<Proof> 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() {
@@ -2,6 +2,8 @@ package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.dto.Theorem;
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 org.junit.Test;
import org.junit.runner.RunWith;
@@ -50,7 +52,7 @@ public class TheoremControllerTest {
}
@Test
public void testGetTheoremById() {
public void testGetTheoremById() throws BadRequestException {
final Theorem theorem = createTheorem();
when(theoremRepository.findById(anyInt())).thenReturn(Optional.ofNullable(theorem));
@@ -64,18 +66,13 @@ public class TheoremControllerTest {
verify(theoremRepository).findById(anyInt());
}
@Test
public void testGetTheoremById_nullId() {
final ResponseEntity responseEntity = theoremController.getTheoremById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
@Test(expected = BadRequestException.class)
public void testGetTheoremById_nullId() throws BadRequestException {
theoremController.getTheoremById(null);
}
@Test
public void testGetTheoremById_noTheoremFound() {
public void testGetTheoremById_noTheoremFound() throws BadRequestException {
when(theoremRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity responseEntity = theoremController.getTheoremById(1);
@@ -87,7 +84,7 @@ public class TheoremControllerTest {
}
@Test
public void testGetAllTheoremsByBranch() {
public void testGetAllTheoremsByBranch() throws BadRequestException {
final Theorem theoremDto = createTheorem();
final List<Theorem> listOfTheorems = new ArrayList<>();
listOfTheorems.add(theoremDto);
@@ -105,18 +102,13 @@ public class TheoremControllerTest {
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
}
@Test
public void testGetAllTheoremsByBranch_nullBranch() {
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByBranch(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
@Test(expected = BadRequestException.class)
public void testGetAllTheoremsByBranch_nullBranch() throws BadRequestException {
theoremController.getAllTheoremsByBranch(null);
}
@Test
public void testGetAllTheoremsByBranch_noTheoremsFound() {
public void testGetAllTheoremsByBranch_noTheoremsFound() throws BadRequestException {
when(theoremRepository.findByBranch(anyString())).thenReturn(Collections.emptyList());
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByBranch("test_nonexistent_branch");
@@ -127,7 +119,7 @@ public class TheoremControllerTest {
}
@Test
public void testGetAllTheoremsByProvenStatus() {
public void testGetAllTheoremsByProvenStatus() throws BadRequestException {
final Theorem theoremDto = createTheorem();
final List<Theorem> listOfTheorems = new ArrayList<>();
listOfTheorems.add(theoremDto);
@@ -145,18 +137,13 @@ public class TheoremControllerTest {
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
}
@Test
public void testGetAllTheoremsByProvenStatus_nullProvenStatus() {
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByProvenStatus(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
@Test(expected = BadRequestException.class)
public void testGetAllTheoremsByProvenStatus_nullProvenStatus() throws BadRequestException {
theoremController.getAllTheoremsByProvenStatus(null);
}
@Test
public void testGetAllTheoremsByProvenStatus_invalidProvenStatus() {
public void testGetAllTheoremsByProvenStatus_invalidProvenStatus() throws BadRequestException {
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByProvenStatus("test");
assertNotNull(responseEntity);
@@ -165,7 +152,7 @@ public class TheoremControllerTest {
}
@Test
public void testGetAllTheoremsByProvenStatus_noTheoremsFound() {
public void testGetAllTheoremsByProvenStatus_noTheoremsFound() throws BadRequestException {
when(theoremRepository.findByProvenStatus(anyBoolean())).thenReturn(Collections.emptyList());
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByProvenStatus("false");
@@ -176,7 +163,7 @@ public class TheoremControllerTest {
}
@Test
public void testGetAllTheoremsByName() {
public void testGetAllTheoremsByName() throws BadRequestException {
final Theorem theoremDto = createTheorem();
final List<Theorem> listOfTheorems = new ArrayList<>();
listOfTheorems.add(theoremDto);
@@ -194,18 +181,13 @@ public class TheoremControllerTest {
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
}
@Test
public void testGetAllTheoremsByName_nullName() {
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByName(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
@Test(expected = BadRequestException.class)
public void testGetAllTheoremsByName_nullName() throws BadRequestException {
theoremController.getAllTheoremsByName(null);
}
@Test
public void testGetAllTheoremsByName_noNameFound() {
public void testGetAllTheoremsByName_noNameFound() throws BadRequestException {
when(theoremRepository.findByName(anyString())).thenReturn(Collections.emptyList());
final ResponseEntity<List<Theorem>> responseEntity = theoremController.getAllTheoremsByName("No-name");
@@ -216,7 +198,7 @@ public class TheoremControllerTest {
}
@Test
public void testInsertTheorem() {
public void testInsertTheorem() throws BadRequestException, UnprocessableEntityException {
final Theorem theorem = createTheorem();
when(theoremRepository.save(any(Theorem.class))).thenReturn(theorem);
@@ -230,31 +212,21 @@ public class TheoremControllerTest {
verify(theoremRepository).save(any(Theorem.class));
}
@Test
public void testInsertTheorem_theoremDtoIsNull() {
final ResponseEntity responseEntity = theoremController.insertTheorem(null, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
@Test(expected = BadRequestException.class)
public void testInsertTheorem_theoremDtoIsNull() throws BadRequestException, UnprocessableEntityException {
theoremController.insertTheorem(null, bindingResult);
}
@Test
public void testInsertTheorem_bindingResultHasErrors() {
@Test(expected = UnprocessableEntityException.class)
public void testInsertTheorem_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException {
final Theorem theorem = createTheorem();
when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity responseEntity = theoremController.insertTheorem(theorem, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
theoremController.insertTheorem(theorem, bindingResult);
}
@Test
public void testUpdateTheorem() {
public void testUpdateTheorem() throws BadRequestException, UnprocessableEntityException {
final Theorem existingTheorem = createTheorem();
existingTheorem.setId(1);
existingTheorem.setVersion(1);
@@ -275,52 +247,37 @@ public class TheoremControllerTest {
verify(theoremRepository).save(any(Theorem.class));
}
@Test
public void testUpdateTheorem_bindingResultHasErrors() {
@Test(expected = UnprocessableEntityException.class)
public void testUpdateTheorem_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException {
when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity<Theorem> responseEntity = theoremController.updateTheorem(1, createTheorem(), bindingResult);
theoremController.updateTheorem(1, createTheorem(), bindingResult);
}
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
@Test(expected = BadRequestException.class)
public void testUpdateTheorem_theoremDtoIsNull() throws BadRequestException, UnprocessableEntityException {
theoremController.updateTheorem(1, null, bindingResult);
}
@Test(expected = BadRequestException.class)
public void testUpdateTheorem_idIsNull() throws BadRequestException, UnprocessableEntityException {
theoremController.updateTheorem(null, createTheorem(), bindingResult);
}
@Test
public void testUpdateTheorem_theoremDtoIsNull() {
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() {
public void testUpdateTheorem_theoremDoesNotExist() throws BadRequestException, UnprocessableEntityException {
when(theoremRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity<Theorem> responseEntity = theoremController.updateTheorem(1, createTheorem(), bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
verify(theoremRepository, times(0)).save(any(Theorem.class));
}
@Test
public void testDeleteTheoremById() {
public void testDeleteTheoremById() throws BadRequestException {
doNothing().when(theoremRepository).deleteById(anyInt());
final ResponseEntity responseEntity = theoremController.deleteTheoremById(1);
@@ -331,14 +288,9 @@ public class TheoremControllerTest {
verify(theoremRepository).deleteById(anyInt());
}
@Test
public void testDeleteTheoremById_idIsNull() {
final ResponseEntity responseEntity = theoremController.deleteTheoremById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
@Test(expected = BadRequestException.class)
public void testDeleteTheoremById_idIsNull() throws BadRequestException {
theoremController.deleteTheoremById(null);
}
private Theorem createTheorem() {