From d76756c06279ecec466eb2366d5a7160cbd0bbbd Mon Sep 17 00:00:00 2001 From: Michael Schulte <65006436+mschulte-tsi@users.noreply.github.com> Date: Thu, 15 Apr 2021 15:07:56 +0200 Subject: [PATCH] Feat change endpoint (#90) * changed endpoint to list * fixed tests cleaned pom * feat: resultstates_quicktest, value range lab results Co-authored-by: Andreas Scheibal --- pom.xml | 4 -- .../testresult/TestResultController.java | 41 +++++++++++--- .../testresult/TestResultService.java | 15 +++-- .../testresult/model/QuickTestResultList.java | 55 +++++++++++++++++++ .../testresult/model/TestResult.java | 2 +- .../testresult/TestResultControllerTest.java | 17 +++--- .../testresult/TestResultServiceTest.java | 8 +-- 7 files changed, 114 insertions(+), 28 deletions(-) create mode 100644 src/main/java/app/coronawarn/testresult/model/QuickTestResultList.java diff --git a/pom.xml b/pom.xml index b5c08ca..3185d2b 100644 --- a/pom.xml +++ b/pom.xml @@ -119,10 +119,6 @@ org.springframework.cloud spring-cloud-starter-vault-config - - org.springframework.boot - spring-boot-starter-validation - org.springframework.cloud spring-cloud-starter-config diff --git a/src/main/java/app/coronawarn/testresult/TestResultController.java b/src/main/java/app/coronawarn/testresult/TestResultController.java index cc4d684..1dbcb69 100644 --- a/src/main/java/app/coronawarn/testresult/TestResultController.java +++ b/src/main/java/app/coronawarn/testresult/TestResultController.java @@ -22,6 +22,7 @@ package app.coronawarn.testresult; import app.coronawarn.testresult.model.QuickTestResult; +import app.coronawarn.testresult.model.QuickTestResultList; import app.coronawarn.testresult.model.TestResult; import app.coronawarn.testresult.model.TestResultList; import app.coronawarn.testresult.model.TestResultRequest; @@ -31,7 +32,6 @@ import javax.validation.constraints.NotNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; @@ -39,6 +39,8 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; + + @Slf4j @RequiredArgsConstructor @Validated @@ -65,7 +67,30 @@ public ResponseEntity result( @RequestBody @Valid TestResultRequest request ) { log.info("Received test result request from app."); - TestResult result = testResultService.getOrCreate(request.getId()); + TestResult result = testResultService.getOrCreate(request.getId(),false); + return ResponseEntity.ok(new TestResultResponse() + .setTestResult(result.getResult())); + } + + /** + * Get the test result response from a request containing the id. + * + * @param request the test result request with id + * @return the test result response + */ + @Operation( + description = "Get test result response from request." + ) + @PostMapping( + value = "/api/v1/quicktest/result", + consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE + ) + public ResponseEntity quickTestResult( + @RequestBody @Valid TestResultRequest request + ) { + log.info("Received test result request from Quicktest."); + TestResult result = testResultService.getOrCreate(request.getId(),true); return ResponseEntity.ok(new TestResultResponse() .setTestResult(result.getResult())); } @@ -95,7 +120,7 @@ public ResponseEntity results( /** * Insert or update the quick test. * - * @param testResult the test result to update. + * @param list the test result list request * @return the response */ @Operation( @@ -107,10 +132,12 @@ public ResponseEntity results( produces = MediaType.APPLICATION_JSON_VALUE ) public ResponseEntity quicktestResults( - @RequestBody @NotNull @Valid QuickTestResult testResult + @RequestBody @NotNull @Valid QuickTestResultList list ) { - log.info("Received test result to insert or update from Quicktests."); - testResultService.createOrUpdate(testResultService.convertQuickTest(testResult)); - return ResponseEntity.status(HttpStatus.CREATED).build(); + log.info("Received {} test result to insert or update from Quicktests. ", list.getTestResults().size()); + for (QuickTestResult result: list.getTestResults()) { + testResultService.createOrUpdate(testResultService.convertQuickTest(result)); + } + return ResponseEntity.noContent().build(); } } diff --git a/src/main/java/app/coronawarn/testresult/TestResultService.java b/src/main/java/app/coronawarn/testresult/TestResultService.java index 80678a6..33b6061 100644 --- a/src/main/java/app/coronawarn/testresult/TestResultService.java +++ b/src/main/java/app/coronawarn/testresult/TestResultService.java @@ -101,15 +101,20 @@ public TestResult createOrUpdate(final TestResult result) { * @param id the test result id * @return the test result */ - public TestResult getOrCreate(final String id) { + public TestResult getOrCreate(final String id, boolean quicktest) { try { TestResultEntity entity = testResultRepository.findByResultId(id) .orElseGet(() -> { log.info("Get failed now creating test result in database."); - return testResultRepository.save(new TestResultEntity() - .setResult(TestResultEntity.Result.PENDING.ordinal()) - .setResultId(id) - .setResultDate(LocalDateTime.now())); + TestResultEntity resultEntity = new TestResultEntity(); + if (quicktest) { + resultEntity.setResult(TestResultEntity.Result.QUICK_PENDING.ordinal()); + } else { + resultEntity.setResult(TestResultEntity.Result.PENDING.ordinal()); + } + resultEntity.setResultId(id); + resultEntity.setResultDate(LocalDateTime.now()); + return testResultRepository.save(resultEntity); }); return toModel(entity); } catch (Exception e) { diff --git a/src/main/java/app/coronawarn/testresult/model/QuickTestResultList.java b/src/main/java/app/coronawarn/testresult/model/QuickTestResultList.java new file mode 100644 index 0000000..9608442 --- /dev/null +++ b/src/main/java/app/coronawarn/testresult/model/QuickTestResultList.java @@ -0,0 +1,55 @@ +/* + * Corona-Warn-App / cwa-testresult-server + * + * (C) 2020, T-Systems International GmbH + * + * Deutsche Telekom AG and all other contributors / + * copyright owners license this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this + * file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package app.coronawarn.testresult.model; + +import io.swagger.v3.oas.annotations.media.Schema; +import java.util.List; +import javax.validation.Valid; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.ToString; + +/** + * Model of the test result list. + */ +@Schema( + description = "The test result list model." +) +@Getter +@ToString +@EqualsAndHashCode +public class QuickTestResultList { + + /** + * The test result entries. + */ + @NotNull + @NotEmpty + private List<@Valid QuickTestResult> testResults; + + public QuickTestResultList setTestResults(List testResults) { + this.testResults = testResults; + return this; + } +} diff --git a/src/main/java/app/coronawarn/testresult/model/TestResult.java b/src/main/java/app/coronawarn/testresult/model/TestResult.java index 7d16aa3..4655987 100644 --- a/src/main/java/app/coronawarn/testresult/model/TestResult.java +++ b/src/main/java/app/coronawarn/testresult/model/TestResult.java @@ -63,7 +63,7 @@ public class TestResult { * 9: quick-test-Redeemed */ @Min(1) - @Max(9) + @Max(3) @NotNull private Integer result; diff --git a/src/test/java/app/coronawarn/testresult/TestResultControllerTest.java b/src/test/java/app/coronawarn/testresult/TestResultControllerTest.java index 8818662..0155d02 100644 --- a/src/test/java/app/coronawarn/testresult/TestResultControllerTest.java +++ b/src/test/java/app/coronawarn/testresult/TestResultControllerTest.java @@ -21,10 +21,7 @@ package app.coronawarn.testresult; -import app.coronawarn.testresult.model.QuickTestResult; -import app.coronawarn.testresult.model.TestResult; -import app.coronawarn.testresult.model.TestResultList; -import app.coronawarn.testresult.model.TestResultRequest; +import app.coronawarn.testresult.model.*; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Collections; import java.util.List; @@ -183,14 +180,17 @@ public void quickInsertValidShouldReturnNoContent() throws Exception { String id = "b".repeat(64); Integer result = 5; // create - QuickTestResult valid = new QuickTestResult().setId(id).setResult(result); + QuickTestResultList valid = new QuickTestResultList(); + valid.setTestResults( Collections.singletonList( + new QuickTestResult().setId(id).setResult(result) + )); mockMvc.perform(MockMvcRequestBuilders .post("/api/v1/quicktest/results") .accept(MediaType.APPLICATION_JSON_VALUE) .contentType(MediaType.APPLICATION_JSON_VALUE) .content(objectMapper.writeValueAsString(valid))) .andDo(MockMvcResultHandlers.print()) - .andExpect(MockMvcResultMatchers.status().isCreated()); + .andExpect(MockMvcResultMatchers.status().isNoContent()); } @Test @@ -199,7 +199,10 @@ public void quickInsertInValidShouldReturnUnprocessableEntity() throws Exception String id = "b".repeat(64); Integer result = 4; // create - QuickTestResult valid = new QuickTestResult().setId(id).setResult(result); + QuickTestResultList valid = new QuickTestResultList(); + valid.setTestResults( Collections.singletonList( + new QuickTestResult().setId(id).setResult(result) + )); mockMvc.perform(MockMvcRequestBuilders .post("/api/v1/quicktest/results") .accept(MediaType.APPLICATION_JSON_VALUE) diff --git a/src/test/java/app/coronawarn/testresult/TestResultServiceTest.java b/src/test/java/app/coronawarn/testresult/TestResultServiceTest.java index 205e046..80ae469 100644 --- a/src/test/java/app/coronawarn/testresult/TestResultServiceTest.java +++ b/src/test/java/app/coronawarn/testresult/TestResultServiceTest.java @@ -82,7 +82,7 @@ public void insertOrUpdate() { Assert.assertNotNull(create); Assert.assertEquals(result, create.getResult()); // get - TestResult get = testResultService.getOrCreate(id); + TestResult get = testResultService.getOrCreate(id,false); Assert.assertNotNull(get); Assert.assertEquals(result, get.getResult()); } @@ -101,7 +101,7 @@ public void insertAndUpdate() { Assert.assertNotNull(create); Assert.assertEquals(resultCreate, create.getResult()); // get - TestResult get = testResultService.getOrCreate(id); + TestResult get = testResultService.getOrCreate(id,false); Assert.assertNotNull(get); Assert.assertEquals(resultCreate, get.getResult()); // update @@ -112,7 +112,7 @@ public void insertAndUpdate() { Assert.assertNotNull(update); Assert.assertEquals(resultUpdate, update.getResult()); // get - get = testResultService.getOrCreate(id); + get = testResultService.getOrCreate(id,false); Assert.assertNotNull(get); Assert.assertEquals(resultUpdate, get.getResult()); } @@ -123,7 +123,7 @@ public void getOrCreate() { String id = "a".repeat(64); Integer result = 0; // get - TestResult get = testResultService.getOrCreate(id); + TestResult get = testResultService.getOrCreate(id,false); Assert.assertNotNull(get); Assert.assertEquals(result, get.getResult()); }