From 3705f48ad7ce2d4032febf37d6d0dc5d97fe0e67 Mon Sep 17 00:00:00 2001 From: Julien Hagestedt Date: Wed, 3 Jun 2020 15:58:47 +0200 Subject: [PATCH] fix: no nullable result from lab allowed --- .../testresult/TestResultController.java | 2 +- .../testresult/model/TestResult.java | 2 + src/main/resources/db/changelog.yml | 3 ++ ...002-revert-test-result-result-nullable.yml | 9 ++++ .../testresult/TestResultControllerTest.java | 45 ++++++++++--------- 5 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 src/main/resources/db/changelog/v002-revert-test-result-result-nullable.yml diff --git a/src/main/java/app/coronawarn/testresult/TestResultController.java b/src/main/java/app/coronawarn/testresult/TestResultController.java index 5aa7abc..d397729 100644 --- a/src/main/java/app/coronawarn/testresult/TestResultController.java +++ b/src/main/java/app/coronawarn/testresult/TestResultController.java @@ -65,7 +65,7 @@ public ResponseEntity result( log.info("Received test result request from app."); TestResult result = testResultService.getOrCreate(request.getId()); return ResponseEntity.ok(new TestResultResponse() - .setTestResult(result.getResult() == null ? 0 : result.getResult())); + .setTestResult(result.getResult())); } /** diff --git a/src/main/java/app/coronawarn/testresult/model/TestResult.java b/src/main/java/app/coronawarn/testresult/model/TestResult.java index 6bb4c92..08e5d73 100644 --- a/src/main/java/app/coronawarn/testresult/model/TestResult.java +++ b/src/main/java/app/coronawarn/testresult/model/TestResult.java @@ -25,6 +25,7 @@ import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -56,6 +57,7 @@ public class TestResult { */ @Min(1) @Max(3) + @NotNull private Integer result; public TestResult setId(String id) { diff --git a/src/main/resources/db/changelog.yml b/src/main/resources/db/changelog.yml index e9d363f..37e0723 100644 --- a/src/main/resources/db/changelog.yml +++ b/src/main/resources/db/changelog.yml @@ -5,3 +5,6 @@ databaseChangeLog: - include: file: changelog/v001-update-test-result-result-nullable.yml relativeToChangelogFile: true +- include: + file: changelog/v002-revert-test-result-result-nullable.yml + relativeToChangelogFile: true diff --git a/src/main/resources/db/changelog/v002-revert-test-result-result-nullable.yml b/src/main/resources/db/changelog/v002-revert-test-result-result-nullable.yml new file mode 100644 index 0000000..dac5d70 --- /dev/null +++ b/src/main/resources/db/changelog/v002-revert-test-result-result-nullable.yml @@ -0,0 +1,9 @@ +databaseChangeLog: +- changeSet: + id: revert-test-result-result-nullable + author: jhagestedt + changes: + - addNotNullConstraint: + tableName: test_result + columnName: result + columnDataType: integer diff --git a/src/test/java/app/coronawarn/testresult/TestResultControllerTest.java b/src/test/java/app/coronawarn/testresult/TestResultControllerTest.java index bacc305..07b5f6c 100644 --- a/src/test/java/app/coronawarn/testresult/TestResultControllerTest.java +++ b/src/test/java/app/coronawarn/testresult/TestResultControllerTest.java @@ -59,7 +59,7 @@ public void before() { } @Test - public void insertInvalidShouldReturnBadRequest() throws Exception { + public void insertInvalidIdShouldReturnBadRequest() throws Exception { // data String id = ""; Integer result = 0; @@ -77,25 +77,35 @@ public void insertInvalidShouldReturnBadRequest() throws Exception { } @Test - public void insertValidShouldReturnNoContent() throws Exception { + public void insertInvalidResultShouldReturnBadRequest() throws Exception { // data String id = "a".repeat(64); - Integer result = 1; // create - List valid = Collections.singletonList( - new TestResult().setId(id).setResult(result) + List invalid = Collections.singletonList( + new TestResult().setId(id) ); mockMvc.perform(MockMvcRequestBuilders .post("/api/v1/lab/results") .accept(MediaType.APPLICATION_JSON_VALUE) .contentType(MediaType.APPLICATION_JSON_VALUE) - .content(objectMapper.writeValueAsString(valid))) + .content(objectMapper.writeValueAsString(invalid))) .andDo(MockMvcResultHandlers.print()) - .andExpect(MockMvcResultMatchers.status().isNoContent()); + .andExpect(MockMvcResultMatchers.status().isBadRequest()); + // create + invalid = Collections.singletonList( + new TestResult().setId(id).setResult(4) + ); + mockMvc.perform(MockMvcRequestBuilders + .post("/api/v1/lab/results") + .accept(MediaType.APPLICATION_JSON_VALUE) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content(objectMapper.writeValueAsString(invalid))) + .andDo(MockMvcResultHandlers.print()) + .andExpect(MockMvcResultMatchers.status().isBadRequest()); } @Test - public void insertValidAndGetShouldReturnOk() throws Exception { + public void insertValidShouldReturnNoContent() throws Exception { // data String id = "b".repeat(64); Integer result = 1; @@ -110,25 +120,16 @@ public void insertValidAndGetShouldReturnOk() throws Exception { .content(objectMapper.writeValueAsString(valid))) .andDo(MockMvcResultHandlers.print()) .andExpect(MockMvcResultMatchers.status().isNoContent()); - // get - TestResultRequest request = new TestResultRequest() - .setId(id); - mockMvc.perform(MockMvcRequestBuilders - .post("/api/v1/app/result") - .accept(MediaType.APPLICATION_JSON_VALUE) - .contentType(MediaType.APPLICATION_JSON_VALUE) - .content(objectMapper.writeValueAsString(request))) - .andDo(MockMvcResultHandlers.print()) - .andExpect(MockMvcResultMatchers.status().isOk()); } @Test - public void insertValidPendingAndGetShouldReturnOk() throws Exception { + public void insertValidAndGetShouldReturnOk() throws Exception { // data - String id = "b".repeat(64); + String id = "c".repeat(64); + Integer result = 1; // create List valid = Collections.singletonList( - new TestResult().setId(id) + new TestResult().setId(id).setResult(result) ); mockMvc.perform(MockMvcRequestBuilders .post("/api/v1/lab/results") @@ -152,7 +153,7 @@ public void insertValidPendingAndGetShouldReturnOk() throws Exception { @Test public void notExistingTestResultShouldReturnOk() throws Exception { // data - String id = "b".repeat(64); + String id = "d".repeat(64); Integer result = 1; // create List valid = Collections.singletonList(