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

Commit

Permalink
feat: database improve and mapping (#59)
Browse files Browse the repository at this point in the history
* feat: milestone 0.3.5

* feat: fix documentation

* feat: fix tests

* feat: fix test assert

* feat: fix test comment
  • Loading branch information
jhagestedt authored Jun 2, 2020
1 parent 0b38a8c commit 5824798
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class TestResultController {
public ResponseEntity<TestResultResponse> result(
@RequestBody @Valid TestResultRequest request
) {
TestResult result = testResultService.getOrInsert(request.getId());
TestResult result = testResultService.getOrCreate(request.getId());
return ResponseEntity.ok(new TestResultResponse()
.setTestResult(result.getResult() == null ? 0 : result.getResult()));
}
Expand All @@ -84,7 +84,7 @@ public ResponseEntity<TestResultResponse> result(
public ResponseEntity<?> results(
@RequestBody @NotEmpty List<@Valid TestResult> request
) {
request.forEach(testResultService::insertOrUpdate);
request.forEach(testResultService::createOrUpdate);
return ResponseEntity.noContent().build();
}
}
62 changes: 41 additions & 21 deletions src/main/java/app/coronawarn/testresult/TestResultService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import app.coronawarn.testresult.exception.TestResultException;
import app.coronawarn.testresult.model.TestResult;
import java.time.LocalDateTime;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand All @@ -37,27 +38,49 @@ public class TestResultService {

private final TestResultRepository testResultRepository;

/**
* Map the entity of a test result to a test result model.
*
* @param entity the test result entity to map
* @return the mapped model from entity
*/
public TestResult toModel(TestResultEntity entity) {
return new TestResult()
.setId(entity.getResultId())
.setResult(entity.getResult());
}

/**
* Map the model of a test result to a test result entity.
* This will also set current date time as value for result date.
*
* @param model the test result model to map
* @return the mapped entity from model
*/
public TestResultEntity toEntity(TestResult model) {
return new TestResultEntity()
.setResult(model.getResult())
.setResultId(model.getId())
.setResultDate(LocalDateTime.now());
}

/**
* Insert or update a test result to the repository.
*
* @param result the test result to insert or update
*/
public TestResult insertOrUpdate(final TestResult result) {
public TestResult createOrUpdate(final TestResult result) {
try {
TestResultEntity entity = testResultRepository.findByResultId(result.getId())
.orElseGet(() ->
testResultRepository.save(new TestResultEntity()
.setResult(result.getResult())
.setResultId(result.getId())
.setResultDate(LocalDateTime.now())
)
);
entity.setResult(result.getResult())
.setResultDate(LocalDateTime.now());
entity = testResultRepository.save(entity);
return new TestResult()
.setId(entity.getResultId())
.setResult(entity.getResult());
final Optional<TestResultEntity> optional = testResultRepository.findByResultId(result.getId());
TestResultEntity entity = optional.orElseGet(() ->
testResultRepository.save(toEntity(result))
);
if (optional.isPresent()) {
entity.setResult(result.getResult())
.setResultDate(LocalDateTime.now());
entity = testResultRepository.save(entity);
}
return toModel(entity);
} catch (Exception e) {
throw new TestResultException(HttpStatus.INTERNAL_SERVER_ERROR,
"Failed to insert or update test result.");
Expand All @@ -70,19 +93,16 @@ public TestResult insertOrUpdate(final TestResult result) {
* @param id the test result id
* @return the test result
*/
public TestResult getOrInsert(final String id) {
public TestResult getOrCreate(final String id) {
try {
TestResultEntity entity = testResultRepository.findByResultId(id)
.orElseGet(() ->
testResultRepository.save(new TestResultEntity()
.setResult(TestResultEntity.Result.PENDING.ordinal())
.setResultId(id)
.setResultDate(LocalDateTime.now())
)
.setResultDate(LocalDateTime.now()))
);
return new TestResult()
.setId(entity.getResultId())
.setResult(entity.getResult());
return toModel(entity);
} catch (Exception e) {
throw new TestResultException(HttpStatus.INTERNAL_SERVER_ERROR,
"Failed to get test result.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,23 @@
@Table(name = "test_result")
public class TestResultEntity {

public enum Result {
PENDING, NEGATIVE, POSITIVE, INVALID, REDEEMED
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@CreatedDate
@Column(name = "created_at")
private LocalDateTime createdAt;

@LastModifiedDate
@Column(name = "updated_at")
private LocalDateTime updatedAt;

@Version
@Column(name = "version")
private Long version;

@Column(name = "result")
private Integer result;

@Column(name = "result_id")
private String resultId;

@Column(name = "result_date")
private LocalDateTime resultDate;

Expand All @@ -92,4 +82,8 @@ public TestResultEntity setResultDate(LocalDateTime resultDate) {
this.resultDate = resultDate;
return this;
}

public enum Result {
PENDING, NEGATIVE, POSITIVE, INVALID, REDEEMED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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;
Expand Down
86 changes: 66 additions & 20 deletions src/test/java/app/coronawarn/testresult/TestResultServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

package app.coronawarn.testresult;

import app.coronawarn.testresult.exception.TestResultException;
import app.coronawarn.testresult.entity.TestResultEntity;
import app.coronawarn.testresult.model.TestResult;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -30,7 +30,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

Expand All @@ -51,35 +50,82 @@ public void before() {
}

@Test
public void insert() {
public void toEntityAndToModel() {
// data
String id = "a".repeat(64);
Integer result = 1;
TestResult testResult = new TestResult()
// to entity
TestResult model = new TestResult()
.setId(id)
.setResult(result);
// insert
testResult = testResultService.insertOrUpdate(testResult);
Assert.assertNotNull(testResult);
Assert.assertEquals(result, testResult.getResult());
TestResultEntity entity = testResultService.toEntity(model);
Assert.assertNotNull(entity);
Assert.assertEquals(id, entity.getResultId());
Assert.assertEquals(result, entity.getResult());
// to model
model = testResultService.toModel(entity);
Assert.assertNotNull(model);
Assert.assertEquals(id, model.getId());
Assert.assertEquals(result, model.getResult());
}

@Test
public void insertOrUpdate() {
// data
String id = "a".repeat(64);
Integer result = 1;
TestResult create = new TestResult()
.setId(id)
.setResult(result);
// create
create = testResultService.createOrUpdate(create);
Assert.assertNotNull(create);
Assert.assertEquals(result, create.getResult());
// get
TestResult get = testResultService.getOrCreate(id);
Assert.assertNotNull(get);
Assert.assertEquals(result, get.getResult());
}

@Test
public void insertAndUpdate() {
// data
String id = "a".repeat(64);
Integer resultCreate = 1;
Integer resultUpdate = 2;
TestResult create = new TestResult()
.setId(id)
.setResult(resultCreate);
// create
create = testResultService.createOrUpdate(create);
Assert.assertNotNull(create);
Assert.assertEquals(resultCreate, create.getResult());
// get
TestResult get = testResultService.getOrCreate(id);
Assert.assertNotNull(get);
Assert.assertEquals(resultCreate, get.getResult());
// update
TestResult update = new TestResult()
.setId(id)
.setResult(resultUpdate);
update = testResultService.createOrUpdate(update);
Assert.assertNotNull(update);
Assert.assertEquals(resultUpdate, update.getResult());
// get
testResult = testResultService.getOrInsert(id);
Assert.assertNotNull(testResult);
Assert.assertEquals(result, testResult.getResult());
get = testResultService.getOrCreate(id);
Assert.assertNotNull(get);
Assert.assertEquals(resultUpdate, get.getResult());
}

@Test
public void getNotFound() {
public void getOrCreate() {
// data
String id = "b".repeat(64);
String id = "a".repeat(64);
Integer result = 0;
// get
try {
//should always return pending test result
//testResultService.get(id);
//Assert.fail();
} catch (TestResultException e) {
Assert.assertEquals(HttpStatus.NOT_FOUND, e.getStatus());
}
TestResult get = testResultService.getOrCreate(id);
Assert.assertNotNull(get);
Assert.assertEquals(result, get.getResult());
}

}

0 comments on commit 5824798

Please sign in to comment.