diff --git a/persistence/scripts/mysql/local_development.sql b/persistence/scripts/mysql/local_development.sql index 27005d4..9650f22 100644 --- a/persistence/scripts/mysql/local_development.sql +++ b/persistence/scripts/mysql/local_development.sql @@ -5,11 +5,11 @@ create table accounts ( id int not null auto_increment primary key unique, username varchar(50) not null unique, password varchar(256) not null, -administrator_status boolean default false, +administrator boolean default false, last_login date, version int default 1 ); -insert into accounts (username, password, administrator_status) +insert into accounts (username, password, administrator) values ('admin', 'secret', true), ('atusa', 'secret', true), ('dantanxiaotian', 'secret', true), @@ -34,17 +34,17 @@ referenced_theorems json, proven_status boolean default false, version int default 1 ); -CREATE TABLE proofs +create table proofs ( - id INT NOT NULL AUTO_INCREMENT, - theorem_name VARCHAR(512) NOT NULL, - proof VARCHAR(4096) NOT NULL, - branch VARCHAR(512) NOT NULL, - theorem INT NOT NULL, - referenced_definitions JSON, - referenced_theorems JSON, - date_added DATE, - last_updated DATE, - version INT DEFAULT 1, - PRIMARY KEY (id) + id int not null auto_increment, + theorem_name varchar(512) not null, + proof varchar(4096) not null, + branch varchar(512) not null, + theorem int not null, + referenced_definitions json, + referenced_theorems json, + date_added date, + last_updated date, + version int default 1, + primary key (id) ); \ No newline at end of file diff --git a/persistence/src/integrationTest/java/edu/msudenver/tsp/persistence/AccountsIntegrationTest.java b/persistence/src/integrationTest/java/edu/msudenver/tsp/persistence/AccountsIntegrationTest.java index 42cce04..7cd5abd 100644 --- a/persistence/src/integrationTest/java/edu/msudenver/tsp/persistence/AccountsIntegrationTest.java +++ b/persistence/src/integrationTest/java/edu/msudenver/tsp/persistence/AccountsIntegrationTest.java @@ -29,7 +29,7 @@ public class AccountsIntegrationTest { assertEquals("Test username", savedAccount.getUsername()); assertEquals("test password", savedAccount.getPassword()); - assertTrue(savedAccount.getAdministratorStatus()); + assertTrue(savedAccount.isAdministrator()); savedAccount.setPassword("Test Update"); @@ -37,7 +37,7 @@ public class AccountsIntegrationTest { assertEquals("Test username", savedAccount.getUsername()); assertEquals("Test Update", savedAccount.getPassword()); - assertTrue(savedAccount.getAdministratorStatus()); + assertTrue(savedAccount.isAdministrator()); assertEquals(updatedAccount.getId(), id); accountsRepository.delete(account); @@ -49,7 +49,7 @@ public class AccountsIntegrationTest { final Account account = new Account(); account.setUsername("Test username"); account.setPassword("test password"); - account.setAdministratorStatus(true); + account.setAdministrator(true); return account; } diff --git a/persistence/src/integrationTest/java/edu/msudenver/tsp/persistence/ProofsIntegrationTest.java b/persistence/src/integrationTest/java/edu/msudenver/tsp/persistence/ProofsIntegrationTest.java index b18c47d..9fa7edb 100644 --- a/persistence/src/integrationTest/java/edu/msudenver/tsp/persistence/ProofsIntegrationTest.java +++ b/persistence/src/integrationTest/java/edu/msudenver/tsp/persistence/ProofsIntegrationTest.java @@ -18,8 +18,7 @@ import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = PersistenceTestConfig.class) public class ProofsIntegrationTest { - @Autowired - private ProofRepository proofRepository; + @Autowired private ProofRepository proofRepository; @Test public void testCRUDFunctionality() { diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/AccountController.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/AccountController.java index 3301f65..f94eb99 100644 --- a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/AccountController.java +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/AccountController.java @@ -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 getAccountById(@RequestParam("id") final Integer id) { + ResponseEntity 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 getAccountByUsername(@RequestParam("username") final String username) { + ResponseEntity 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 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 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 deleteAccountById(@PathVariable("id") final Integer id) { + public @ResponseBody ResponseEntity 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); diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/DefinitionController.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/DefinitionController.java index 720e5df..b89f5b7 100644 --- a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/DefinitionController.java +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/DefinitionController.java @@ -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 getDefinitionById(@PathVariable("id") final Integer id) { + ResponseEntity 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 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 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 deleteDefinitionById(@PathVariable("id") final Integer id) { + public @ResponseBody ResponseEntity 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); diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/ExceptionHandlingController.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/ExceptionHandlingController.java new file mode 100644 index 0000000..737298f --- /dev/null +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/ExceptionHandlingController.java @@ -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 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 handleBadRequestException(final BadRequestException e) { + return e.getErrors(); + } + + @ExceptionHandler(UnprocessableEntityException.class) + @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY) + public List handleUnprocessableEntityException(final UnprocessableEntityException e) { + return Collections.singletonList("Request did not evaluate. Experienced the following error: " + e.getMessage()); + } +} diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/ProofController.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/ProofController.java index d682c32..77a9dc7 100644 --- a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/ProofController.java +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/ProofController.java @@ -1,6 +1,8 @@ package edu.msudenver.tsp.persistence.controller; import edu.msudenver.tsp.persistence.dto.Proof; +import edu.msudenver.tsp.persistence.exception.BadRequestException; +import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException; import edu.msudenver.tsp.persistence.repository.ProofRepository; import edu.msudenver.tsp.utilities.PersistenceUtilities; import lombok.AllArgsConstructor; @@ -45,11 +47,11 @@ public class ProofController { @GetMapping("/id") public @ResponseBody - ResponseEntity getProofById(@RequestParam("id") final Integer id) { + ResponseEntity getProofById(@RequestParam("id") final Integer id) throws BadRequestException { LOG.info("Received request to query for proof with id {}", id); if (id == null) { LOG.error("ERROR: ID was null"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Specified ID is null"); } LOG.debug("Querying for proof with id {}", id); @@ -75,11 +77,12 @@ public class ProofController { @GetMapping("/branch") public @ResponseBody - ResponseEntity> getAllProofsByBranch(@RequestParam("branch") String branch) { + ResponseEntity> getAllProofsByBranch(@RequestParam("branch") String branch) + throws BadRequestException { LOG.info("Received request to query for proofs related to the {} branch of mathematics", branch); if (branch == null) { LOG.error("ERROR: branch was null"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Specified branch is null"); } if (branch.contains("_") || branch.contains("-")) { @@ -110,11 +113,12 @@ public class ProofController { @GetMapping("/theorem_name") public @ResponseBody - ResponseEntity> getAllProofsByTheoremName(@RequestParam("theorem_name") String theoremName) { + ResponseEntity> getAllProofsByTheoremName(@RequestParam("theorem_name") String theoremName) + throws BadRequestException { LOG.info("Received request to query for proofs of the theorem {}", theoremName); if (theoremName == null) { LOG.error("ERROR: theorem name was null"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Specified theorem name is null"); } if (theoremName.contains("_") || theoremName.contains("-")) { @@ -147,16 +151,16 @@ public class ProofController { @Validated({Proof.Insert.class, Default.class}) public @ResponseBody ResponseEntity insertProof( @Valid @RequestBody final Proof proof, - final BindingResult bindingResult) { + final BindingResult bindingResult) throws UnprocessableEntityException, BadRequestException { LOG.info("Received request to insert a new proof"); if (bindingResult.hasErrors()) { LOG.error("Binding result is unprocessable"); - return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); + throw new UnprocessableEntityException(bindingResult.getAllErrors().toString()); } if (proof == null) { LOG.error("Passed entity is null"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Passed proof is null"); } LOG.debug("Saving new proof"); @@ -176,22 +180,23 @@ public class ProofController { @PatchMapping("/{id}") public @ResponseBody ResponseEntity updateProof( @PathVariable("id") final Integer id, - @RequestBody final Proof proof, final BindingResult bindingResult) { + @RequestBody final Proof proof, final BindingResult bindingResult) + throws UnprocessableEntityException, BadRequestException { LOG.info("Received request to update a proof"); if (bindingResult.hasErrors()) { LOG.error("Binding result is unprocessable"); - return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); + throw new UnprocessableEntityException(bindingResult.getAllErrors().toString()); } if (proof == null) { LOG.error("Passed entity is null"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Passed proof is null"); } if (id == null) { LOG.error("Proof ID must be specified"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Specified ID is null"); } LOG.debug("Checking for existence of proof with id {}", id); @@ -207,7 +212,7 @@ public class ProofController { if (!existingProof.isPresent()) { LOG.error("No proof associated with id {}", id); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(HttpStatus.NOT_FOUND); } PersistenceUtilities.copyNonNullProperties(proof, existingProof.get()); @@ -228,11 +233,11 @@ public class ProofController { } @DeleteMapping("/{id}") - public @ResponseBody ResponseEntity deleteProofById(@PathVariable("id") final Integer id) { + public @ResponseBody ResponseEntity deleteProofById(@PathVariable("id") final Integer id) throws BadRequestException { LOG.info("Received request to delete proof with id {}", id); if (id == null) { LOG.error("Specified id is null"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + throw new BadRequestException("Specified ID is null"); } LOG.debug("Deleting proof with id {}", id); diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/TheoremController.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/TheoremController.java index 457ac10..fc78e77 100644 --- a/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/TheoremController.java +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/controller/TheoremController.java @@ -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> getAllTheoremsByBranch(@RequestParam("branch") String branch) { + ResponseEntity> 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> getAllTheoremsByProvenStatus(@RequestParam("proven_status") final String provenStatus) { + ResponseEntity> 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> getAllTheoremsByName(@RequestParam("name") String name) { + ResponseEntity> 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 getTheoremById(@RequestParam("id") final Integer id) { + ResponseEntity 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 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 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 deleteTheoremById(@PathVariable("id") final Integer id) { + public @ResponseBody ResponseEntity 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); diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/dto/Account.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/dto/Account.java index a8fa430..674d83b 100644 --- a/persistence/src/main/java/edu/msudenver/tsp/persistence/dto/Account.java +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/dto/Account.java @@ -19,21 +19,11 @@ import java.util.Date; public class Account extends BaseDto implements Serializable { @NotBlank(groups = Insert.class, message = "A username must be specified") @Size(max = 50) private String username; @NotBlank(groups = Insert.class, message = "A password must be specified") @Size(max = 256) private String password; - @NotNull @Column(name = "administrator_status") private boolean administratorStatus; + @NotNull private boolean administrator; @Temporal(TemporalType.DATE) @Column(name = "last_login") private Date lastLogin; private static final long serialVersionUID = 7095627971593953734L; - @JsonProperty("administrator_status") - public boolean getAdministratorStatus() { - return administratorStatus; - } - - @JsonProperty("administrator_status") - public void setAdministratorStatus(final boolean administratorStatus) { - this.administratorStatus = administratorStatus; - } - @JsonProperty("last_login") public Date getLastLogin() { return lastLogin; diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/dto/Definition.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/dto/Definition.java index b18d743..43b95f4 100644 --- a/persistence/src/main/java/edu/msudenver/tsp/persistence/dto/Definition.java +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/dto/Definition.java @@ -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 definition; @Type(type = "json") @Column(columnDefinition = "jsonb") private List notation; diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/exception/BadRequestException.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/exception/BadRequestException.java new file mode 100644 index 0000000..ec37a6d --- /dev/null +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/exception/BadRequestException.java @@ -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 errors; + + public BadRequestException(final String message) { + super(message); + errors = Collections.singletonList(message); + } + + public BadRequestException(final List errorMessages) { + super(String.join(" ", errorMessages)); + errors = errorMessages; + } +} diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/exception/UnprocessableEntityException.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/exception/UnprocessableEntityException.java new file mode 100644 index 0000000..758e33e --- /dev/null +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/exception/UnprocessableEntityException.java @@ -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 errors; + + public UnprocessableEntityException(final String message) { + super(message); + errors = Collections.singletonList(message); + } + + public UnprocessableEntityException(final List errorMessages) { + super(String.join(" ", errorMessages)); + errors = errorMessages; + } +} diff --git a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/AccountControllerTest.java b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/AccountControllerTest.java index 503e2f6..456e5e6 100644 --- a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/AccountControllerTest.java +++ b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/AccountControllerTest.java @@ -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; @@ -25,10 +27,8 @@ import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) @WebMvcTest(controllers = AccountController.class) public class AccountControllerTest { - @Mock - private AccountsRepository accountsRepository; - @InjectMocks - private AccountController accountController; + @Mock private AccountsRepository accountsRepository; + @InjectMocks private AccountController accountController; @Mock private BindingResult bindingResult; @Test @@ -51,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)); @@ -65,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); @@ -88,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)); @@ -102,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"); @@ -125,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()); @@ -141,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)); @@ -154,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); @@ -164,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); @@ -199,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 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 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 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 responseEntity = accountController.updateAccount(1, createAccount(), bindingResult); @@ -244,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); @@ -255,21 +225,16 @@ 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() { final Account account = new Account(); account.setUsername("Test username"); account.setPassword("test password"); - account.setAdministratorStatus(true); + account.setAdministrator(true); return account; } diff --git a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/DefinitionControllerTest.java b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/DefinitionControllerTest.java index 4b7fa77..492002f 100644 --- a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/DefinitionControllerTest.java +++ b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/DefinitionControllerTest.java @@ -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 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 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 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 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() { diff --git a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/ExceptionHandlingControllerTest.java b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/ExceptionHandlingControllerTest.java new file mode 100644 index 0000000..217bcbf --- /dev/null +++ b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/ExceptionHandlingControllerTest.java @@ -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()); + } +} \ No newline at end of file diff --git a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/ProofControllerTest.java b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/ProofControllerTest.java index 14c2482..6953b0a 100644 --- a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/ProofControllerTest.java +++ b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/ProofControllerTest.java @@ -1,6 +1,8 @@ package edu.msudenver.tsp.persistence.controller; import edu.msudenver.tsp.persistence.dto.Proof; +import edu.msudenver.tsp.persistence.exception.BadRequestException; +import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException; import edu.msudenver.tsp.persistence.repository.ProofRepository; import org.junit.Test; import org.junit.runner.RunWith; @@ -45,7 +47,7 @@ public class ProofControllerTest { } @Test - public void testGetAllProofsByBranch() { + public void testGetAllProofsByBranch() throws BadRequestException { final Proof proofDto = createProof(); final List listOfProofs = new ArrayList<>(); listOfProofs.add(proofDto); @@ -63,18 +65,13 @@ public class ProofControllerTest { responseEntity.getBody().forEach(proof -> assertEquals(proofDto, proof)); } - @Test - public void testGetAllProfsByBranch_nullBranch() { - final ResponseEntity> responseEntity = proofController.getAllProofsByBranch(null); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + @Test(expected = BadRequestException.class) + public void testGetAllProfsByBranch_nullBranch() throws BadRequestException { + proofController.getAllProofsByBranch(null); } @Test - public void testGetAllProofsByBranch_noProofsFound() { + public void testGetAllProofsByBranch_noProofsFound() throws BadRequestException { when(proofRepository.findByBranch(anyString())).thenReturn(Collections.emptyList()); final ResponseEntity> responseEntity = proofController.getAllProofsByBranch("test-nonexistent-branch"); @@ -85,7 +82,7 @@ public class ProofControllerTest { } @Test - public void testGetAllProofsByTheoremName() { + public void testGetAllProofsByTheoremName() throws BadRequestException { final Proof proofDto = createProof(); final List listOfProofs = new ArrayList<>(); listOfProofs.add(proofDto); @@ -103,18 +100,13 @@ public class ProofControllerTest { responseEntity.getBody().forEach(proof -> assertEquals(proofDto, proof)); } - @Test - public void testGetAllProofsByTheoremName_nullTheoremName() { - final ResponseEntity> responseEntity = proofController.getAllProofsByTheoremName(null); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + @Test(expected = BadRequestException.class) + public void testGetAllProofsByTheoremName_nullTheoremName() throws BadRequestException { + proofController.getAllProofsByTheoremName(null); } @Test - public void testGetAllProofsByTheoremName_noProofsFound() { + public void testGetAllProofsByTheoremName_noProofsFound() throws BadRequestException { when(proofRepository.findByTheoremName(anyString())).thenReturn(Collections.emptyList()); final ResponseEntity> responseEntity = proofController.getAllProofsByTheoremName("test-nonexistent-proof"); @@ -125,7 +117,7 @@ public class ProofControllerTest { } @Test - public void testGetProofById() { + public void testGetProofById() throws BadRequestException { final Proof proof = createProof(); when(proofRepository.findById(anyInt())).thenReturn(Optional.ofNullable(proof)); @@ -139,18 +131,13 @@ public class ProofControllerTest { verify(proofRepository).findById(anyInt()); } - @Test - public void testGetProofById_nullId() { - final ResponseEntity responseEntity = proofController.getProofById(null); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + @Test(expected = BadRequestException.class) + public void testGetProofById_nullId() throws BadRequestException { + proofController.getProofById(null); } @Test - public void testGetProofById_noProofFound() { + public void testGetProofById_noProofFound() throws BadRequestException { when(proofRepository.findById(anyInt())).thenReturn(Optional.empty()); final ResponseEntity responseEntity = proofController.getProofById(1); @@ -162,7 +149,7 @@ public class ProofControllerTest { } @Test - public void testInsertProof() { + public void testInsertProof() throws BadRequestException, UnprocessableEntityException { final Proof proof = createProof(); when(proofRepository.save(any(Proof.class))).thenReturn(proof); @@ -176,31 +163,21 @@ public class ProofControllerTest { verify(proofRepository).save(any(Proof.class)); } - @Test - public void testInsertProof_proofDtoIsNull() { - final ResponseEntity responseEntity = proofController.insertProof(null, bindingResult); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + @Test(expected = BadRequestException.class) + public void testInsertProof_proofDtoIsNull() throws BadRequestException, UnprocessableEntityException { + proofController.insertProof(null, bindingResult); } - @Test - public void testInsertProof_bindingResultHasErrors() { + @Test(expected = UnprocessableEntityException.class) + public void testInsertProof_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException { final Proof proof = createProof(); when(bindingResult.hasErrors()).thenReturn(true); - final ResponseEntity responseEntity = proofController.insertProof(proof, bindingResult); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + proofController.insertProof(proof, bindingResult); } @Test - public void testUpdateProof() { + public void testUpdateProof() throws BadRequestException, UnprocessableEntityException { final Proof existingProof = createProof(); existingProof.setId(1); existingProof.setVersion(1); @@ -221,52 +198,37 @@ public class ProofControllerTest { verify(proofRepository).save(any(Proof.class)); } - @Test - public void testUpdateProof_bindingResultHasErrors() { + @Test(expected = UnprocessableEntityException.class) + public void testUpdateProof_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException { when(bindingResult.hasErrors()).thenReturn(true); - final ResponseEntity responseEntity = proofController.updateProof(1, createProof(), bindingResult); + proofController.updateProof(1, createProof(), bindingResult); + } - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + @Test(expected = BadRequestException.class) + public void testUpdateProof_proofDtoIsNull() throws BadRequestException, UnprocessableEntityException { + proofController.updateProof(1, null, bindingResult); + } + + @Test(expected = BadRequestException.class) + public void testUpdateProof_idIsNull() throws BadRequestException, UnprocessableEntityException { + proofController.updateProof(null, createProof(), bindingResult); } @Test - public void testUpdateProof_proofDtoIsNull() { - final ResponseEntity responseEntity = proofController.updateProof(1, null, bindingResult); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); - } - - @Test - public void testUpdateProof_idIsNull() { - final ResponseEntity responseEntity = proofController.updateProof(null, createProof(), bindingResult); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); - } - - @Test - public void testUpdateProof_theoremDoesNotExist() { + public void testUpdateProof_theoremDoesNotExist() throws BadRequestException, UnprocessableEntityException { when(proofRepository.findById(anyInt())).thenReturn(Optional.empty()); final ResponseEntity responseEntity = proofController.updateProof(1, createProof(), bindingResult); assertNotNull(responseEntity); assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); + assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); verify(proofRepository, times(0)).save(any(Proof.class)); } @Test - public void testDeleteProofById() { + public void testDeleteProofById() throws BadRequestException { doNothing().when(proofRepository).deleteById(anyInt()); final ResponseEntity responseEntity = proofController.deleteProofById(1); @@ -277,14 +239,9 @@ public class ProofControllerTest { verify(proofRepository).deleteById(anyInt()); } - @Test - public void testDeleteProofById_idIsNull() { - final ResponseEntity responseEntity = proofController.deleteProofById(null); - - assertNotNull(responseEntity); - assertFalse(responseEntity.hasBody()); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - verifyZeroInteractions(proofRepository); + @Test(expected = BadRequestException.class) + public void testDeleteProofById_idIsNull() throws BadRequestException { + proofController.deleteProofById(null); } private Proof createProof() { diff --git a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/TheoremControllerTest.java b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/TheoremControllerTest.java index a667d0a..7fee5c8 100644 --- a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/TheoremControllerTest.java +++ b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/TheoremControllerTest.java @@ -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 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> 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> 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 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> 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> 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> 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 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> 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> 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 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 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 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 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() { diff --git a/services/build.gradle b/services/build.gradle index 95d21d7..0873510 100644 --- a/services/build.gradle +++ b/services/build.gradle @@ -18,10 +18,12 @@ repositories { } dependencies { - compile project(':persistence') compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.11' compile group: 'org.apache.httpcomponents', name: 'fluent-hc', version: '4.5.7' compile group: 'com.google.code.gson', name: 'gson', version: '2.7' + compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.0.5.RELEASE' + compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.0.5.RELEASE' + compile group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2' compile fileTree(dir: 'lib', include: '**/*.jar') testCompile "org.springframework:spring-test:5.0.9.RELEASE" diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/DefinitionServiceIntegrationTest.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/DefinitionServiceIntegrationTest.java new file mode 100644 index 0000000..9aff2fc --- /dev/null +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/DefinitionServiceIntegrationTest.java @@ -0,0 +1,85 @@ +package edu.msudenver.tsp.services; + +import edu.msudenver.tsp.services.dto.Definition; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = ServicesTestConfig.class) +@TestPropertySource(locations = "classpath:test.properties") +public class DefinitionServiceIntegrationTest { + @Autowired private DefinitionService definitionService; + + @Test + public void testCRUD() { + final Definition testDefinition = createDefinition(); + final Optional createdDefinition = definitionService.createDefinition(testDefinition); + + assertTrue(createdDefinition.isPresent()); + assertNotEquals(0, createdDefinition.get().getId()); + assertThat(createdDefinition.get().getVersion(), is(0)); + assertThat(createdDefinition.get().getName(), is("Test Name")); + assertNotNull(createdDefinition.get().getDefinition()); + assertThat(createdDefinition.get().getDefinition().size(), is(1)); + assertThat(createdDefinition.get().getDefinition().get(0), is("Test definition 1")); + assertNotNull(createdDefinition.get().getNotation()); + assertThat(createdDefinition.get().getNotation().size(), is(1)); + assertThat(createdDefinition.get().getNotation().get(0), is("\\testLaTeX")); + + final Optional definitionFoundById = definitionService.findById(createdDefinition.get().getId()); + + assertThat(definitionFoundById.get(), is(equalTo(createdDefinition.get()))); + + final Definition definitionUpdate = new Definition(); + definitionUpdate.setId(createdDefinition.get().getId()); + definitionUpdate.setName("Test Update"); + + final Optional updatedDefinition = definitionService.updateDefinition(definitionUpdate); + + assertTrue(updatedDefinition.isPresent()); + assertNotEquals(0, updatedDefinition.get().getId()); + assertThat(updatedDefinition.get().getVersion(), is(1)); + assertThat(updatedDefinition.get().getName(), is("Test Update")); + assertNotNull(updatedDefinition.get().getDefinition()); + assertThat(updatedDefinition.get().getDefinition().size(), is(1)); + assertThat(updatedDefinition.get().getDefinition().get(0), is("Test definition 1")); + assertNotNull(updatedDefinition.get().getNotation()); + assertThat(updatedDefinition.get().getNotation().size(), is(1)); + assertThat(updatedDefinition.get().getNotation().get(0), is("\\testLaTeX")); + + final boolean deletionWasSuccessful = definitionService.deleteDefinition(updatedDefinition.get()); + + assertThat(deletionWasSuccessful, is(true)); + + final Optional deletedDefinitionFoundById = definitionService.findById(createdDefinition.get().getId()); + + assertFalse(deletedDefinitionFoundById.isPresent()); + } + + private Definition createDefinition() { + final List definitionList = new ArrayList<>(); + definitionList.add("Test definition 1"); + + final List notationList = new ArrayList<>(); + notationList.add("\\testLaTeX"); + + final Definition definition = new Definition(); + definition.setName("Test Name"); + definition.setDefinition(definitionList); + definition.setNotation(notationList); + + return definition; + } +} diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java index be36c25..64fd7ad 100644 --- a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java @@ -23,7 +23,7 @@ public class UserServiceIntegrationTest { private UserService userService; @Test - public void testUserService(){ + public void testCRUD() { final Account testAccount = createAccount(); final Optional testCreatedAccount = userService.createAccount(testAccount); @@ -31,21 +31,21 @@ public class UserServiceIntegrationTest { final Account returnedAccount = testCreatedAccount.get(); assertEquals("test_user", returnedAccount.getUsername()); assertEquals("test_password", returnedAccount.getPassword()); - assertFalse(returnedAccount.isAdministratorStatus()); + assertFalse(returnedAccount.isAdministrator()); final Optional getAccountById = userService.findAccountById(returnedAccount.getId()); assertTrue(getAccountById.isPresent()); final Account returnedAccountById = getAccountById.get(); assertEquals("test_user", returnedAccountById.getUsername()); assertEquals("test_password", returnedAccountById.getPassword()); - assertFalse(returnedAccountById.isAdministratorStatus()); + assertFalse(returnedAccountById.isAdministrator()); final Optional getAccountByUsername = userService.findAccountByUsername(returnedAccount.getUsername()); assertTrue(getAccountByUsername.isPresent()); final Account returnedAccountByUsername = getAccountByUsername.get(); assertEquals("test_user", returnedAccountByUsername.getUsername()); assertEquals("test_password", returnedAccountByUsername.getPassword()); - assertFalse(returnedAccountById.isAdministratorStatus()); + assertFalse(returnedAccountById.isAdministrator()); returnedAccount.setUsername("test_updatedUser"); returnedAccount.setPassword("test_updatedPassword"); @@ -55,7 +55,7 @@ public class UserServiceIntegrationTest { final Account returnedUpdatedAccount = updatedAccount.get(); assertEquals("test_updatedUser", returnedUpdatedAccount.getUsername()); assertEquals("test_updatedPassword", returnedUpdatedAccount.getPassword()); - assertFalse(returnedUpdatedAccount.isAdministratorStatus()); + assertFalse(returnedUpdatedAccount.isAdministrator()); final boolean result = userService.deleteAccount(returnedUpdatedAccount); assertTrue(result); @@ -65,7 +65,7 @@ public class UserServiceIntegrationTest { final Account testAccount = new Account(); testAccount.setUsername("test_user"); testAccount.setPassword("test_password"); - testAccount.setAdministratorStatus(false); + testAccount.setAdministrator(false); testAccount.setLastLogin(new Date()); return testAccount; diff --git a/services/src/main/java/edu/msudenver/tsp/services/DefinitionService.java b/services/src/main/java/edu/msudenver/tsp/services/DefinitionService.java new file mode 100644 index 0000000..68472ea --- /dev/null +++ b/services/src/main/java/edu/msudenver/tsp/services/DefinitionService.java @@ -0,0 +1,186 @@ +package edu.msudenver.tsp.services; + +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import edu.msudenver.tsp.services.dto.Definition; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; + +import java.time.Duration; +import java.time.Instant; +import java.util.List; +import java.util.Optional; + +@Slf4j +@Service +public class DefinitionService { + private final RestService restService; + @Value("${persistence.api.connection.timeout.milliseconds}") private int connectionTimeoutMilliseconds; + @Value("${persistence.api.socket.timeout.milliseconds}") private int socketTimeoutMilliseconds; + @Value("${persistence.api.base.url}") private String persistenceApiBaseUrl; + + @Autowired + public DefinitionService(final RestService restService) { + this.restService = restService; + } + + public Optional> getAllDefinitions() { + final Instant start = Instant.now(); + + try { + final TypeToken> typeToken = new TypeToken>(){}; + final Optional> persistenceApiResponse = + restService.get(persistenceApiBaseUrl + "definitions/", + typeToken, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, null); + + if (persistenceApiResponse.isPresent()) { + LOG.info("Returning {}", persistenceApiResponse.get()); + } else { + LOG.info("Unable to get list of definitions"); + } + + return persistenceApiResponse; + } catch (final Exception e) { + LOG.error("Error getting list of definitions!", e); + return Optional.empty(); + } finally { + LOG.info("Get all definitions request took {}ms", Duration.between(start, Instant.now()).toMillis()); + } + } + + public Optional findById(final int id) { + if (id == 0) { + LOG.error("Null id specified; returning {}"); + return Optional.empty(); + } + + LOG.info("Sending request to find definition by id {}", id); + final Instant start = Instant.now(); + + try { + final TypeToken typeToken = new TypeToken(){}; + final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "definitions/" + id, + typeToken, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, null); + + if (persistenceApiResponse.isPresent()) { + LOG.info("Returning {}", persistenceApiResponse.get()); + } else { + LOG.info("Unable to find definition with id {}", id); + } + + return persistenceApiResponse; + } catch (final Exception e) { + LOG.error("Error finding definition by id", e); + return Optional.empty(); + } finally { + LOG.info("Find by id request took {}ms", Duration.between(start, Instant.now()).toMillis()); + } + } + + public Optional createDefinition(final Definition definition) { + if (definition == null) { + LOG.error("Given null definition, returning {}"); + return Optional.empty(); + } + + LOG.info("Sending request to insert definition {}", definition); + final Instant start = Instant.now(); + + try { + final TypeToken typeToken = new TypeToken() {}; + final Optional persistenceApiResponse = restService.post(persistenceApiBaseUrl + "definitions/", + new GsonBuilder().create().toJson(definition), + typeToken, + connectionTimeoutMilliseconds, + socketTimeoutMilliseconds); + + if (persistenceApiResponse.isPresent()) { + LOG.info("Creation successful. Returning {}", persistenceApiResponse.get()); + } else { + LOG.info("Unable to create new definition {}", definition); + } + + return persistenceApiResponse; + } catch (final Exception e) { + LOG.error("Error creating new definition", e); + return Optional.empty(); + } finally { + LOG.info("Create new definition request took {}ms", Duration.between(start, Instant.now()).toMillis()); + } + } + + public Optional updateDefinition(final Definition definition) { + if (definition == null) { + LOG.error("Given null definition, returning {}"); + return Optional.empty(); + } + + if (definition.getId() == 0) { + LOG.error("Given invalid id 0, returning {}"); + return Optional.empty(); + } + + LOG.info("Sending request to update definition {}", definition); + final Instant start = Instant.now(); + + try { + final TypeToken typeToken = new TypeToken(){}; + final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "definitions/" + definition.getId(), + new GsonBuilder().create().toJson(definition), + typeToken, + connectionTimeoutMilliseconds, + socketTimeoutMilliseconds); + + if (persistenceApiResponse.isPresent()) { + LOG.info("Update successful. Returning {}", persistenceApiResponse.get()); + } else { + LOG.info("Unable to update definition {}", definition); + } + + return persistenceApiResponse; + } catch (final Exception e) { + LOG.error("Error updating definition", e); + return Optional.empty(); + } finally { + LOG.info("Update definition request took {}ms", Duration.between(start, Instant.now()).toMillis()); + } + } + + public boolean deleteDefinition(final Definition definition) { + if (definition == null) { + LOG.error("Given null definition, returning false"); + return false; + } + + if (definition.getId() == 0) { + LOG.error("Given invalid id 0, returning false"); + return false; + } + + LOG.info("Sending request to delete definition {}", definition); + final Instant start = Instant.now(); + + try { + final boolean deleteIsSuccessful = restService.delete(persistenceApiBaseUrl + "definitions/" + definition.getId(), + connectionTimeoutMilliseconds, + socketTimeoutMilliseconds, + HttpStatus.NO_CONTENT); + + if (deleteIsSuccessful) { + LOG.info("Deletion successful. Returning true"); + } else { + LOG.info("Unable to delete definition {}", definition); + } + + return deleteIsSuccessful; + } catch (final Exception e) { + LOG.error("Error when deleting definition", e); + return false; + } finally { + LOG.info("Delete definition request took {}ms", Duration.between(start, Instant.now()).toMillis()); + } + } +} diff --git a/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java b/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java index 05872fc..f04a108 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java +++ b/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java @@ -16,7 +16,7 @@ import java.util.Date; public class Account extends BaseDto implements Serializable { @Size(max = 50) private String username; @Size(max = 256) private String password; - @NotNull @SerializedName("administrator_status") private boolean administratorStatus; + @NotNull private boolean administrator; @Temporal(TemporalType.DATE) @SerializedName("last_login") private Date lastLogin; private static final long serialVersionUID = 7095627971593953734L; diff --git a/services/src/main/java/edu/msudenver/tsp/services/dto/Definition.java b/services/src/main/java/edu/msudenver/tsp/services/dto/Definition.java new file mode 100644 index 0000000..a208e83 --- /dev/null +++ b/services/src/main/java/edu/msudenver/tsp/services/dto/Definition.java @@ -0,0 +1,24 @@ +package edu.msudenver.tsp.services.dto; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; +import java.io.Serializable; +import java.util.List; + +@Data +@EqualsAndHashCode(callSuper = true) +public class Definition extends BaseDto implements Serializable { + @NotBlank(groups = Insert.class, message = "A name must be specified") + @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") + private List definition; + private List notation; + + private static final long serialVersionUID = 3412178112996807691L; + + public interface Insert {} +} diff --git a/services/src/test/java/edu/msudenver/tsp/services/DefinitionServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/DefinitionServiceTest.java new file mode 100644 index 0000000..c9c8f42 --- /dev/null +++ b/services/src/test/java/edu/msudenver/tsp/services/DefinitionServiceTest.java @@ -0,0 +1,290 @@ +package edu.msudenver.tsp.services; + +import com.google.gson.GsonBuilder; +import edu.msudenver.tsp.services.dto.Definition; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static junit.framework.TestCase.assertTrue; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.*; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class DefinitionServiceTest { + @Mock private RestService restService; + @InjectMocks private DefinitionService definitionService; + + @Test + public void testGetAllDefinitions() { + final List definitionList = new ArrayList<>(); + final Definition testDefinition = createDefinition(); + definitionList.add(testDefinition); + definitionList.add(testDefinition); + + when(restService.get(anyString(), any(), anyInt(), anyInt(), anyString())) + .thenReturn(Optional.of(definitionList)); + + final Optional> listOfDefinitions = definitionService.getAllDefinitions(); + + assertTrue(listOfDefinitions.isPresent()); + assertThat(listOfDefinitions.get().size(), is(2)); + listOfDefinitions.get().forEach(definition -> assertThat(definition, equalTo(testDefinition))); + verify(restService).get(anyString(), any(), anyInt(), anyInt(), anyString()); + } + + @Test + public void testGetAllDefinitions_RequestReturnsEmptyOptional() { + when(restService.get(anyString(), any(), anyInt(), anyInt(), anyString())) + .thenReturn(Optional.empty()); + + final Optional> listOfDefinitions = definitionService.getAllDefinitions(); + + assertFalse(listOfDefinitions.isPresent()); + verify(restService).get(anyString(), any(), anyInt(), anyInt(), anyString()); + } + + @Test + public void testGetAllDefinitions_ExceptionThrownWhenSendingRequest() { + when(restService.get(anyString(), any(), anyInt(), anyInt(), anyString())) + .thenThrow(new UnsupportedOperationException("Test exception")); + + final Optional> listOfDefinitions = definitionService.getAllDefinitions(); + + assertFalse(listOfDefinitions.isPresent()); + verify(restService).get(anyString(), any(), anyInt(), anyInt(), anyString()); + } + + @Test + public void testFindById() { + final Definition testDefinition = createDefinition(); + when(restService.get(anyString(), any(), anyInt(), anyInt(), anyString())) + .thenReturn(Optional.of(testDefinition)); + + final Optional foundDefinition = definitionService.findById(testDefinition.getId()); + + assertTrue(foundDefinition.isPresent()); + assertThat(foundDefinition.get().getId(), is(1)); + assertThat(foundDefinition.get(), is(equalTo(testDefinition))); + verify(restService).get(anyString(), any(), anyInt(), anyInt(), anyString()); + } + + @Test + public void testFindById_RequestReturnsEmptyOptional() { + when(restService.get(anyString(), any(), anyInt(), anyInt(), anyString())) + .thenReturn(Optional.empty()); + + final Optional nonExistentDefinition = definitionService.findById(1); + + assertFalse(nonExistentDefinition.isPresent()); + verify(restService).get(anyString(), any(), anyInt(), anyInt(), anyString()); + } + + @Test + public void testFindById_ExceptionThrownWhenSendingRequest() { + when(restService.get(anyString(), any(), anyInt(), anyInt(), anyString())) + .thenThrow(new UnsupportedOperationException("test exception")); + + final Optional exceptionThrowingDefinition = definitionService.findById(1); + + assertFalse(exceptionThrowingDefinition.isPresent()); + verify(restService).get(anyString(), any(), anyInt(), anyInt(), anyString()); + } + + @Test + public void testFindById_IdIsZero() { + final Optional impossibleDefinition = definitionService.findById(0); + + assertFalse(impossibleDefinition.isPresent()); + verifyZeroInteractions(restService); + } + + @Test + public void testCreateDefinition() { + final Definition testDefinition = createDefinition(); + final String testDefinitionJson = new GsonBuilder().create().toJson(testDefinition); + + when(restService.post(anyString(), anyString(), any(), anyInt(), anyInt())) + .thenReturn(Optional.of(testDefinition)); + + final Optional createdDefinition = definitionService.createDefinition(testDefinition); + + assertNotNull(createdDefinition); + assertTrue(createdDefinition.isPresent()); + assertEquals(testDefinition, createdDefinition.get()); + verify(restService).post(anyString(), eq(testDefinitionJson), any(), anyInt(), anyInt()); + } + + @Test + public void testCreateDefinition_NullDefinition() { + final Optional nullDefinition = definitionService.createDefinition(null); + + assertFalse(nullDefinition.isPresent()); + verifyZeroInteractions(restService); + } + + @Test + public void testCreateDefinition_UnableToCreateDefinition() { + final Definition testDefinition = createDefinition(); + final String testDefinitionJson = new GsonBuilder().create().toJson(testDefinition); + + when(restService.post(anyString(), anyString(), any(), anyInt(), anyInt())) + .thenReturn(Optional.empty()); + + final Optional createdDefinition = definitionService.createDefinition(testDefinition); + + assertNotNull(createdDefinition); + assertFalse(createdDefinition.isPresent()); + verify(restService).post(anyString(), eq(testDefinitionJson), any(), anyInt(), anyInt()); + } + + @Test + public void testCreateDefinition_RestServiceThrowsException() { + final Definition testDefinition = createDefinition(); + final String testDefinitionJson = new GsonBuilder().create().toJson(testDefinition); + + when(restService.post(anyString(), anyString(), any(), anyInt(), anyInt())) + .thenThrow(new UnsupportedOperationException("test exception")); + + final Optional createdDefinition = definitionService.createDefinition(testDefinition); + + assertNotNull(createdDefinition); + assertFalse(createdDefinition.isPresent()); + verify(restService).post(anyString(), eq(testDefinitionJson), any(), anyInt(), anyInt()); + } + + @Test + public void testUpdateDefinition() { + when(restService.patch(anyString(), anyString(), any(), anyInt(), anyInt())) + .thenReturn(Optional.of(createDefinition().setName("Test update"))); + + final Definition testDefinition = new Definition(); + testDefinition.setName("Test update"); + testDefinition.setId(1); + + final Optional updatedDefinition = definitionService.updateDefinition(testDefinition); + + assertTrue(updatedDefinition.isPresent()); + assertThat(updatedDefinition.get().getId(), is(1)); + assertThat(updatedDefinition.get().getName(), is(equalTo("Test update"))); + verify(restService).patch(anyString(), anyString(), any(), anyInt(), anyInt()); + } + + @Test + public void testUpdateDefinition_nullDefinition() { + final Optional testUpdate = definitionService.updateDefinition(null); + + assertFalse(testUpdate.isPresent()); + verifyZeroInteractions(restService); + } + + @Test + public void testUpdateDefinition_IdIsZero() { + final Definition impossibleDefinition = createDefinition(); + impossibleDefinition.setId(0); + + final Optional testUpdate = definitionService.updateDefinition(impossibleDefinition); + + assertFalse(testUpdate.isPresent()); + verifyZeroInteractions(restService); + } + + @Test + public void testUpdateDefinition_RequestReturnsEmptyOptional() { + when(restService.patch(anyString(), anyString(), any(), anyInt(), anyInt())) + .thenReturn(Optional.empty()); + + final Optional nonExistentDefinition = definitionService.updateDefinition(createDefinition()); + + assertFalse(nonExistentDefinition.isPresent()); + verify(restService).patch(anyString(), anyString(), any(), anyInt(), anyInt()); + } + + @Test + public void testUpdateDefinition_ExceptionThrownWhenSendingRequest() { + when(restService.patch(anyString(), anyString(), any(), anyInt(), anyInt())) + .thenThrow(new UnsupportedOperationException("test exception")); + + final Optional exceptionThrowingDefinition = definitionService.updateDefinition(createDefinition()); + + assertFalse(exceptionThrowingDefinition.isPresent()); + verify(restService).patch(anyString(), anyString(), any(), anyInt(), anyInt()); + } + + @Test + public void testDeleteDefinition() { + when(restService.delete(anyString(), anyInt(), anyInt(), any())) + .thenReturn(true); + + final boolean deleteIsSuccessful = definitionService.deleteDefinition(createDefinition()); + + assertTrue(deleteIsSuccessful); + verify(restService).delete(anyString(), anyInt(), anyInt(), any()); + } + + @Test + public void testDeleteDefinition_NullDefinition() { + final boolean deleteIsSuccessful = definitionService.deleteDefinition(null); + + assertFalse(deleteIsSuccessful); + verifyZeroInteractions(restService); + } + + @Test + public void testDeleteDefinition_IdIsZero() { + final Definition testDefinition = createDefinition(); + testDefinition.setId(0); + + final boolean deleteIsSuccessful = definitionService.deleteDefinition(testDefinition); + + assertFalse(deleteIsSuccessful); + verifyZeroInteractions(restService); + } + + @Test + public void testDeleteDefinition_RequestReturnsFalse() { + when(restService.delete(anyString(), anyInt(), anyInt(), any())) + .thenReturn(false); + + final boolean deleteIsSuccessful = definitionService.deleteDefinition(createDefinition()); + + assertFalse(deleteIsSuccessful); + verify(restService).delete(anyString(), anyInt(), anyInt(), any()); + } + + @Test + public void testDeleteDefinition_ExceptionThrownWhenSendingRequest() { + when(restService.delete(anyString(), anyInt(), anyInt(), any())) + .thenThrow(new UnsupportedOperationException("test exception")); + + final boolean deleteIsSuccessful = definitionService.deleteDefinition(createDefinition()); + + assertFalse(deleteIsSuccessful); + verify(restService).delete(anyString(), anyInt(), anyInt(), any()); + } + + private Definition createDefinition() { + final List definitionList = new ArrayList<>(); + definitionList.add("Test definition 1"); + + final List notationList = new ArrayList<>(); + notationList.add("\\testLaTeX"); + + final Definition definition = new Definition(); + definition.setName("Test Name"); + definition.setDefinition(definitionList); + definition.setNotation(notationList); + definition.setId(1); + + return definition; + } +} \ No newline at end of file diff --git a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java index 1d42311..1f7812f 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java @@ -306,7 +306,7 @@ public class UserServiceTest { final Account account = new Account(); account.setUsername("Test username"); account.setPassword("test password"); - account.setAdministratorStatus(true); + account.setAdministrator(true); account.setLastLogin(new Date()); return account; }