diff --git a/src/main/java/app/coronawarn/testresult/TestResultController.java b/src/main/java/app/coronawarn/testresult/TestResultController.java index 20eed2d..a51d28a 100644 --- a/src/main/java/app/coronawarn/testresult/TestResultController.java +++ b/src/main/java/app/coronawarn/testresult/TestResultController.java @@ -69,7 +69,8 @@ public ResponseEntity result( @RequestBody @Valid TestResultRequest request ) { log.info("Received test result request from app."); - TestResult result = testResultService.getOrCreate(request.getId(),false); + + TestResult result = testResultService.getOrCreate(request.getId(), false, null); return ResponseEntity.ok(new TestResultResponse() .setTestResult(result.getResult(), result.getSc()) ); @@ -115,7 +116,7 @@ public ResponseEntity quickTestResult( @RequestBody @Valid TestResultRequest request ) { log.info("Received test result request from Quicktest."); - TestResult result = testResultService.getOrCreate(request.getId(),true); + TestResult result = testResultService.getOrCreate(request.getId(), true, request.getSc()); return ResponseEntity.ok(new TestResultResponse() .setTestResult(result.getResult())); } diff --git a/src/main/java/app/coronawarn/testresult/service/TestResultService.java b/src/main/java/app/coronawarn/testresult/service/TestResultService.java index 90aa5c8..e2263a1 100644 --- a/src/main/java/app/coronawarn/testresult/service/TestResultService.java +++ b/src/main/java/app/coronawarn/testresult/service/TestResultService.java @@ -64,10 +64,13 @@ public TestResult toModel(TestResultEntity entity) { * @return the mapped entity from model */ public TestResultEntity toEntity(TestResult model) { + if (model.getSc() == null) { + model.setSampleCollection(LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)); + } return new TestResultEntity() .setResult(model.getResult()) .setResultId(model.getId()) - .setResultDate(LocalDateTime.now()); + .setResultDate(LocalDateTime.ofEpochSecond(model.getSc(), 0, ZoneOffset.UTC)); } /** @@ -106,7 +109,7 @@ public TestResult createOrUpdate(final TestResult result) { * @param id the test result id * @return the test result */ - public TestResult getOrCreate(final String id, boolean quicktest) { + public TestResult getOrCreate(final String id, boolean quicktest, Long sc) { try { TestResultEntity entity = testResultRepository.findByResultId(id) .orElseGet(() -> { @@ -119,7 +122,11 @@ public TestResult getOrCreate(final String id, boolean quicktest) { resultEntity.setResult(TestResultEntity.Result.PENDING.ordinal()); resultEntity.setResultId(id); } - resultEntity.setResultDate(LocalDateTime.now()); + if (sc == null) { + resultEntity.setResultDate(LocalDateTime.now()); + } else { + resultEntity.setResultDate(LocalDateTime.ofEpochSecond(sc, 0, ZoneOffset.UTC)); + } return testResultRepository.save(resultEntity); }); return toModel(entity); diff --git a/src/test/java/app/coronawarn/testresult/TestResultServiceTest.java b/src/test/java/app/coronawarn/testresult/TestResultServiceTest.java index 1c6225a..38f65ea 100644 --- a/src/test/java/app/coronawarn/testresult/TestResultServiceTest.java +++ b/src/test/java/app/coronawarn/testresult/TestResultServiceTest.java @@ -83,7 +83,7 @@ public void insertOrUpdate() { Assert.assertNotNull(create); Assert.assertEquals(result, create.getResult()); // get - TestResult get = testResultService.getOrCreate(id,false); + TestResult get = testResultService.getOrCreate(id, false, 0L); Assert.assertNotNull(get); Assert.assertEquals(result, get.getResult()); } @@ -102,7 +102,7 @@ public void insertAndUpdate() { Assert.assertNotNull(create); Assert.assertEquals(resultCreate, create.getResult()); // get - TestResult get = testResultService.getOrCreate(id,false); + TestResult get = testResultService.getOrCreate(id, false, 0L); Assert.assertNotNull(get); Assert.assertEquals(resultCreate, get.getResult()); // update @@ -113,7 +113,7 @@ public void insertAndUpdate() { Assert.assertNotNull(update); Assert.assertEquals(resultUpdate, update.getResult()); // get - get = testResultService.getOrCreate(id,false); + get = testResultService.getOrCreate(id, false, 0L); Assert.assertNotNull(get); Assert.assertEquals(resultUpdate, get.getResult()); } @@ -124,7 +124,7 @@ public void getOrCreate() { String id = "a".repeat(64); Integer result = 0; // get - TestResult get = testResultService.getOrCreate(id,false); + TestResult get = testResultService.getOrCreate(id, false, 0L); Assert.assertNotNull(get); Assert.assertEquals(result, get.getResult()); }