Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
API-3362 implement questionnaire update (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
jblefkove-va authored Nov 3, 2020
1 parent d854254 commit 2c78a33
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package gov.va.api.health.patientgenerateddata;

import static com.google.common.base.Preconditions.checkState;

import gov.va.api.health.autoconfig.configuration.JacksonConfig;
import gov.va.api.health.autoconfig.logging.Loggable;
import gov.va.api.health.r4.api.resources.Questionnaire;
import java.util.Optional;
import javax.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.DataBinder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -32,10 +42,33 @@ static <T> T deserializedPayload(
}
}

@InitBinder
void initDirectFieldAccess(DataBinder dataBinder) {
dataBinder.initDirectFieldAccess();
}

@GetMapping(value = "/{id}")
Questionnaire read(@PathVariable("id") String id) {
Optional<QuestionnaireEntity> maybeEntity = repository.findById(id);
QuestionnaireEntity entity = maybeEntity.orElseThrow(() -> new Exceptions.NotFound(id));
return deserializedPayload(id, entity.payload(), Questionnaire.class);
}

@SneakyThrows
@PutMapping(value = "/{id}")
@Loggable(arguments = false)
ResponseEntity<Void> update(
@PathVariable("id") String id, @Valid @RequestBody Questionnaire questionnaire) {
String payload = JacksonConfig.createMapper().writeValueAsString(questionnaire);
checkState(id.equals(questionnaire.id()), "%s != %s", id, questionnaire.id());
Optional<QuestionnaireEntity> maybeEntity = repository.findById(id);
if (maybeEntity.isPresent()) {
QuestionnaireEntity entity = maybeEntity.get();
entity.payload(payload);
repository.save(entity);
return ResponseEntity.ok().build();
}
repository.save(QuestionnaireEntity.builder().id(id).payload(payload).build());
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import gov.va.api.health.autoconfig.configuration.JacksonConfig;
import gov.va.api.health.r4.api.resources.Questionnaire;
import java.util.Optional;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.DataBinder;

public class QuestionnaireControllerTest {
@Test
Expand All @@ -19,6 +24,12 @@ void badPayload() {
() -> QuestionnaireController.deserializedPayload("x", "notjson", Questionnaire.class));
}

@Test
void initDirectFieldAccess() {
new QuestionnaireController(mock(QuestionnaireRepository.class))
.initDirectFieldAccess(mock(DataBinder.class));
}

@Test
@SneakyThrows
void read() {
Expand All @@ -37,4 +48,28 @@ void read_notFound() {
assertThrows(
Exceptions.NotFound.class, () -> new QuestionnaireController(repo).read("notfound"));
}

@Test
@SneakyThrows
void update_existing() {
QuestionnaireRepository repo = mock(QuestionnaireRepository.class);
Questionnaire questionnaire = Questionnaire.builder().id("x").build();
String payload = JacksonConfig.createMapper().writeValueAsString(questionnaire);
when(repo.findById("x"))
.thenReturn(Optional.of(QuestionnaireEntity.builder().id("x").payload(payload).build()));
assertThat(new QuestionnaireController(repo).update("x", questionnaire))
.isEqualTo(ResponseEntity.ok().build());
verify(repo, times(1)).save(QuestionnaireEntity.builder().id("x").payload(payload).build());
}

@Test
@SneakyThrows
void update_new() {
QuestionnaireRepository repo = mock(QuestionnaireRepository.class);
Questionnaire questionnaire = Questionnaire.builder().id("x").build();
String payload = JacksonConfig.createMapper().writeValueAsString(questionnaire);
assertThat(new QuestionnaireController(repo).update("x", questionnaire))
.isEqualTo(ResponseEntity.status(HttpStatus.CREATED).build());
verify(repo, times(1)).save(QuestionnaireEntity.builder().id("x").payload(payload).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,34 +101,34 @@ void notFound() {
}

@Test
void sanitizedMessage_Exception() {
void sanitizedMessage_exception() {
assertThat(WebExceptionHandler.sanitizedMessage(new RuntimeException("oh noez")))
.isEqualTo("oh noez");
}

@Test
void sanitizedMessage_JsonEOFException() {
void sanitizedMessage_jsonEOFException() {
JsonEOFException ex = mock(JsonEOFException.class);
when(ex.getLocation()).thenReturn(new JsonLocation(null, 0, 0, 0));
assertThat(WebExceptionHandler.sanitizedMessage(ex)).isEqualTo("line: 0, column: 0");
}

@Test
void sanitizedMessage_JsonMappingException() {
void sanitizedMessage_jsonMappingException() {
JsonMappingException ex = mock(JsonMappingException.class);
when(ex.getPathReference()).thenReturn("x");
assertThat(WebExceptionHandler.sanitizedMessage(ex)).isEqualTo("path: x");
}

@Test
void sanitizedMessage_JsonParseException() {
void sanitizedMessage_jsonParseException() {
JsonParseException ex = mock(JsonParseException.class);
when(ex.getLocation()).thenReturn(new JsonLocation(null, 0, 0, 0));
assertThat(WebExceptionHandler.sanitizedMessage(ex)).isEqualTo("line: 0, column: 0");
}

@Test
void sanitizedMessage_MismatchedInputException() {
void sanitizedMessage_mismatchedInputException() {
MismatchedInputException ex = mock(MismatchedInputException.class);
when(ex.getPathReference()).thenReturn("path");
assertThat(WebExceptionHandler.sanitizedMessage(ex)).isEqualTo("path: path");
Expand Down

0 comments on commit 2c78a33

Please sign in to comment.