PAN-46 Added validation to the DefinitionController
This commit is contained in:
+18
-13
@@ -1,6 +1,8 @@
|
|||||||
package edu.msudenver.tsp.persistence.controller;
|
package edu.msudenver.tsp.persistence.controller;
|
||||||
|
|
||||||
import edu.msudenver.tsp.persistence.dto.Definition;
|
import edu.msudenver.tsp.persistence.dto.Definition;
|
||||||
|
import edu.msudenver.tsp.persistence.exception.BadRequestException;
|
||||||
|
import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException;
|
||||||
import edu.msudenver.tsp.persistence.repository.DefinitionRepository;
|
import edu.msudenver.tsp.persistence.repository.DefinitionRepository;
|
||||||
import edu.msudenver.tsp.utilities.PersistenceUtilities;
|
import edu.msudenver.tsp.utilities.PersistenceUtilities;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
@@ -21,6 +23,7 @@ import java.util.Optional;
|
|||||||
@RestController
|
@RestController
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@RequestMapping(path = "/definitions")
|
@RequestMapping(path = "/definitions")
|
||||||
|
@Validated
|
||||||
public class DefinitionController {
|
public class DefinitionController {
|
||||||
private final DefinitionRepository definitionRepository;
|
private final DefinitionRepository definitionRepository;
|
||||||
|
|
||||||
@@ -45,11 +48,12 @@ public class DefinitionController {
|
|||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<Definition> getDefinitionById(@PathVariable("id") final Integer id) {
|
ResponseEntity<Definition> getDefinitionById(@PathVariable("id") final Integer id)
|
||||||
|
throws BadRequestException {
|
||||||
LOG.info("Received request to query for definition with id {}", id);
|
LOG.info("Received request to query for definition with id {}", id);
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
LOG.error("ERROR: ID was null");
|
LOG.error("ERROR: ID cannot be null");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("ERROR: ID cannot be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.debug("Querying for definition with id {}", id);
|
LOG.debug("Querying for definition with id {}", id);
|
||||||
@@ -77,16 +81,17 @@ public class DefinitionController {
|
|||||||
@Validated({Definition.Insert.class, Default.class})
|
@Validated({Definition.Insert.class, Default.class})
|
||||||
public @ResponseBody ResponseEntity<Definition> insertDefinition(
|
public @ResponseBody ResponseEntity<Definition> insertDefinition(
|
||||||
@Valid @RequestBody final Definition definition,
|
@Valid @RequestBody final Definition definition,
|
||||||
final BindingResult bindingResult) {
|
final BindingResult bindingResult)
|
||||||
|
throws UnprocessableEntityException, BadRequestException {
|
||||||
LOG.info("Received request to insert a new definition");
|
LOG.info("Received request to insert a new definition");
|
||||||
if (bindingResult.hasErrors()) {
|
if (bindingResult.hasErrors()) {
|
||||||
LOG.error("Binding result is unprocessable");
|
LOG.error("Binding result is unprocessable");
|
||||||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
throw new UnprocessableEntityException(bindingResult.getAllErrors().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (definition == null) {
|
if (definition == null) {
|
||||||
LOG.error("Passed entity is null");
|
LOG.error("Passed entity is null");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("Passed definition is be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.debug("Saving new definition");
|
LOG.debug("Saving new definition");
|
||||||
@@ -106,22 +111,22 @@ public class DefinitionController {
|
|||||||
@PatchMapping("/{id}")
|
@PatchMapping("/{id}")
|
||||||
public @ResponseBody ResponseEntity<Definition> updateDefinition(
|
public @ResponseBody ResponseEntity<Definition> updateDefinition(
|
||||||
@PathVariable("id") final Integer id,
|
@PathVariable("id") final Integer id,
|
||||||
@RequestBody final Definition definition, final BindingResult bindingResult) {
|
@RequestBody final Definition definition, final BindingResult bindingResult) throws UnprocessableEntityException, BadRequestException {
|
||||||
|
|
||||||
LOG.info("Received request to update an account");
|
LOG.info("Received request to update an account");
|
||||||
if (bindingResult.hasErrors()) {
|
if (bindingResult.hasErrors()) {
|
||||||
LOG.error("Binding result is unprocessable");
|
LOG.error("Binding result is unprocessable");
|
||||||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
throw new UnprocessableEntityException(bindingResult.getAllErrors().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (definition == null) {
|
if (definition == null) {
|
||||||
LOG.error("Passed entity is null");
|
LOG.error("Passed entity is null");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("Passed definition is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
LOG.error("Definition ID must be specified");
|
LOG.error("Definition ID must be specified");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("Definition ID must be specified");
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.debug("Checking for existence of definition with id {}", id);
|
LOG.debug("Checking for existence of definition with id {}", id);
|
||||||
@@ -137,7 +142,7 @@ public class DefinitionController {
|
|||||||
|
|
||||||
if (!existingDefinition.isPresent()) {
|
if (!existingDefinition.isPresent()) {
|
||||||
LOG.error("No definition associated with id {}", id);
|
LOG.error("No definition associated with id {}", id);
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
PersistenceUtilities.copyNonNullProperties(definition, existingDefinition.get());
|
PersistenceUtilities.copyNonNullProperties(definition, existingDefinition.get());
|
||||||
@@ -158,11 +163,11 @@ public class DefinitionController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public @ResponseBody ResponseEntity<Void> deleteDefinitionById(@PathVariable("id") final Integer id) {
|
public @ResponseBody ResponseEntity<Void> deleteDefinitionById(@PathVariable("id") final Integer id) throws BadRequestException {
|
||||||
LOG.info("Received request to delete definition with id {}", id);
|
LOG.info("Received request to delete definition with id {}", id);
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
LOG.error("Specified id is null");
|
LOG.error("Specified id is null");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
throw new BadRequestException("Specified ID is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.debug("Deleting definition with id {}", id);
|
LOG.debug("Deleting definition with id {}", id);
|
||||||
|
|||||||
+33
-66
@@ -1,6 +1,8 @@
|
|||||||
package edu.msudenver.tsp.persistence.controller;
|
package edu.msudenver.tsp.persistence.controller;
|
||||||
|
|
||||||
import edu.msudenver.tsp.persistence.dto.Definition;
|
import edu.msudenver.tsp.persistence.dto.Definition;
|
||||||
|
import edu.msudenver.tsp.persistence.exception.BadRequestException;
|
||||||
|
import edu.msudenver.tsp.persistence.exception.UnprocessableEntityException;
|
||||||
import edu.msudenver.tsp.persistence.repository.DefinitionRepository;
|
import edu.msudenver.tsp.persistence.repository.DefinitionRepository;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -48,7 +50,7 @@ public class DefinitionControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetDefinitionsById() {
|
public void testGetDefinitionsById() throws BadRequestException {
|
||||||
final Definition definition = createDefinition();
|
final Definition definition = createDefinition();
|
||||||
when(definitionRepository.findById(anyInt())).thenReturn(Optional.ofNullable(definition));
|
when(definitionRepository.findById(anyInt())).thenReturn(Optional.ofNullable(definition));
|
||||||
|
|
||||||
@@ -62,18 +64,13 @@ public class DefinitionControllerTest {
|
|||||||
verify(definitionRepository).findById(anyInt());
|
verify(definitionRepository).findById(anyInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = BadRequestException.class)
|
||||||
public void testGetDefinitionById_nullId() {
|
public void testGetDefinitionById_nullId() throws BadRequestException {
|
||||||
final ResponseEntity responseEntity = definitionController.getDefinitionById(null);
|
definitionController.getDefinitionById(null);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(definitionRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetDefinitionById_noDefinitionFound() {
|
public void testGetDefinitionById_noDefinitionFound() throws BadRequestException {
|
||||||
when(definitionRepository.findById(anyInt())).thenReturn(Optional.empty());
|
when(definitionRepository.findById(anyInt())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
final ResponseEntity responseEntity = definitionController.getDefinitionById(1);
|
final ResponseEntity responseEntity = definitionController.getDefinitionById(1);
|
||||||
@@ -85,7 +82,7 @@ public class DefinitionControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInsertDefinition() {
|
public void testInsertDefinition() throws BadRequestException, UnprocessableEntityException {
|
||||||
final Definition definition = createDefinition();
|
final Definition definition = createDefinition();
|
||||||
when(definitionRepository.save(any(Definition.class))).thenReturn(definition);
|
when(definitionRepository.save(any(Definition.class))).thenReturn(definition);
|
||||||
|
|
||||||
@@ -99,31 +96,21 @@ public class DefinitionControllerTest {
|
|||||||
verify(definitionRepository).save(any(Definition.class));
|
verify(definitionRepository).save(any(Definition.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = BadRequestException.class)
|
||||||
public void testInsertDefinition_definitionDtoIsNull() {
|
public void testInsertDefinition_definitionDtoIsNull() throws BadRequestException, UnprocessableEntityException {
|
||||||
final ResponseEntity responseEntity = definitionController.insertDefinition(null, bindingResult);
|
definitionController.insertDefinition(null, bindingResult);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(definitionRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = UnprocessableEntityException.class)
|
||||||
public void testInsertDefinition_bindingResultHasErrors() {
|
public void testInsertDefinition_bindingResultHasErrors() throws BadRequestException, UnprocessableEntityException {
|
||||||
final Definition definition = createDefinition();
|
final Definition definition = createDefinition();
|
||||||
when(bindingResult.hasErrors()).thenReturn(true);
|
when(bindingResult.hasErrors()).thenReturn(true);
|
||||||
|
|
||||||
final ResponseEntity responseEntity = definitionController.insertDefinition(definition, bindingResult);
|
definitionController.insertDefinition(definition, bindingResult);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(definitionRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateDefinition() {
|
public void testUpdateDefinition() throws BadRequestException, UnprocessableEntityException {
|
||||||
final Definition existingDefinition = createDefinition();
|
final Definition existingDefinition = createDefinition();
|
||||||
existingDefinition.setId(1);
|
existingDefinition.setId(1);
|
||||||
existingDefinition.setVersion(1);
|
existingDefinition.setVersion(1);
|
||||||
@@ -144,52 +131,37 @@ public class DefinitionControllerTest {
|
|||||||
verify(definitionRepository).save(any(Definition.class));
|
verify(definitionRepository).save(any(Definition.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = UnprocessableEntityException.class)
|
||||||
public void testUpdateDefinition_bindingResultErrors() {
|
public void testUpdateDefinition_bindingResultErrors() throws BadRequestException, UnprocessableEntityException {
|
||||||
when(bindingResult.hasErrors()).thenReturn(true);
|
when(bindingResult.hasErrors()).thenReturn(true);
|
||||||
|
|
||||||
final ResponseEntity<Definition> responseEntity = definitionController.updateDefinition(1, createDefinition(), bindingResult);
|
definitionController.updateDefinition(1, createDefinition(), bindingResult);
|
||||||
|
}
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
@Test(expected = BadRequestException.class)
|
||||||
assertFalse(responseEntity.hasBody());
|
public void testUpdateDefinition_definitionDtoIsNull() throws BadRequestException, UnprocessableEntityException {
|
||||||
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
|
definitionController.updateDefinition(1, null, bindingResult);
|
||||||
verifyZeroInteractions(definitionRepository);
|
}
|
||||||
|
|
||||||
|
@Test(expected = BadRequestException.class)
|
||||||
|
public void testUpdateDefinition_idIsNull() throws BadRequestException, UnprocessableEntityException {
|
||||||
|
definitionController.updateDefinition(null, createDefinition(), bindingResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateDefinition_definitionDtoIsNull() {
|
public void testUpdateDefinition_definitionDoesntExist() throws BadRequestException, UnprocessableEntityException {
|
||||||
final ResponseEntity<Definition> responseEntity = definitionController.updateDefinition(1, null, bindingResult);
|
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(definitionRepository);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateDefinition_idIsNull() {
|
|
||||||
final ResponseEntity<Definition> responseEntity = definitionController.updateDefinition(null, createDefinition(), bindingResult);
|
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(definitionRepository);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateDefinition_definitionDoesntExist() {
|
|
||||||
when(definitionRepository.findById(anyInt())).thenReturn(Optional.empty());
|
when(definitionRepository.findById(anyInt())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
final ResponseEntity<Definition> responseEntity = definitionController.updateDefinition(1, createDefinition(), bindingResult);
|
final ResponseEntity<Definition> responseEntity = definitionController.updateDefinition(1, createDefinition(), bindingResult);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
assertNotNull(responseEntity);
|
||||||
assertFalse(responseEntity.hasBody());
|
assertFalse(responseEntity.hasBody());
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
|
||||||
verify(definitionRepository, times(0)).save(any(Definition.class));
|
verify(definitionRepository, times(0)).save(any(Definition.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDeleteDefinitionById() {
|
public void testDeleteDefinitionById() throws BadRequestException {
|
||||||
doNothing().when(definitionRepository).deleteById(anyInt());
|
doNothing().when(definitionRepository).deleteById(anyInt());
|
||||||
|
|
||||||
final ResponseEntity responseEntity = definitionController.deleteDefinitionById(1);
|
final ResponseEntity responseEntity = definitionController.deleteDefinitionById(1);
|
||||||
@@ -200,14 +172,9 @@ public class DefinitionControllerTest {
|
|||||||
verify(definitionRepository).deleteById(anyInt());
|
verify(definitionRepository).deleteById(anyInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = BadRequestException.class)
|
||||||
public void testDeleteDefinitionById_nullId() {
|
public void testDeleteDefinitionById_nullId() throws BadRequestException {
|
||||||
final ResponseEntity responseEntity = definitionController.deleteDefinitionById(null);
|
definitionController.deleteDefinitionById(null);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertFalse(responseEntity.hasBody());
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
|
|
||||||
verifyZeroInteractions(definitionRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Definition createDefinition() {
|
private Definition createDefinition() {
|
||||||
|
|||||||
Reference in New Issue
Block a user