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

Commit

Permalink
Feat change endpoint (#90)
Browse files Browse the repository at this point in the history
* changed endpoint to list

* fixed tests
cleaned pom

* feat: resultstates_quicktest,
value range lab results

Co-authored-by: Andreas Scheibal <[email protected]>
  • Loading branch information
mschulte-tsi and ascheibal authored Apr 15, 2021
1 parent 8887252 commit d76756c
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 28 deletions.
4 changes: 0 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-vault-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
Expand Down
41 changes: 34 additions & 7 deletions src/main/java/app/coronawarn/testresult/TestResultController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,14 +32,15 @@
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;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;



@Slf4j
@RequiredArgsConstructor
@Validated
Expand All @@ -65,7 +67,30 @@ public ResponseEntity<TestResultResponse> 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<TestResultResponse> 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()));
}
Expand Down Expand Up @@ -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(
Expand All @@ -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();
}
}
15 changes: 10 additions & 5 deletions src/main/java/app/coronawarn/testresult/TestResultService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<QuickTestResult> testResults) {
this.testResults = testResults;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class TestResult {
* 9: quick-test-Redeemed
*/
@Min(1)
@Max(9)
@Max(3)
@NotNull
private Integer result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand Down

0 comments on commit d76756c

Please sign in to comment.