From cbf08e3c0bfe94174763a832d560761b326c613e Mon Sep 17 00:00:00 2001 From: southeo Date: Tue, 14 May 2024 11:15:03 +0200 Subject: [PATCH] Make source data standard enum --- .../backend/controller/MappingController.java | 15 +------ .../orchestration/backend/domain/Mapping.java | 5 ++- .../backend/domain/MappingRecord.java | 1 + .../backend/domain/SourceDataStandard.java | 40 +++++++++++++++++++ .../backend/domain/SourceSystemRecord.java | 1 + .../backend/repository/MappingRepository.java | 7 ++-- .../backend/service/FdoRecordService.java | 2 +- .../controller/MappingControllerTest.java | 24 ----------- .../repository/MappingRepositoryIT.java | 7 ++-- .../backend/service/MappingServiceTest.java | 5 ++- .../backend/testutils/TestUtils.java | 5 ++- 11 files changed, 61 insertions(+), 51 deletions(-) create mode 100644 src/main/java/eu/dissco/orchestration/backend/domain/SourceDataStandard.java diff --git a/src/main/java/eu/dissco/orchestration/backend/controller/MappingController.java b/src/main/java/eu/dissco/orchestration/backend/controller/MappingController.java index 9604a12..fb21cd7 100644 --- a/src/main/java/eu/dissco/orchestration/backend/controller/MappingController.java +++ b/src/main/java/eu/dissco/orchestration/backend/controller/MappingController.java @@ -12,8 +12,6 @@ import eu.dissco.orchestration.backend.properties.ApplicationProperties; import eu.dissco.orchestration.backend.service.MappingService; import jakarta.servlet.http.HttpServletRequest; -import java.util.ArrayList; -import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; @@ -40,8 +38,6 @@ public class MappingController { private final MappingService service; private final ObjectMapper mapper; private final ApplicationProperties appProperties; - private static final ArrayList SOURCE_DATA_SYSTEMS = new ArrayList<>( - List.of("dwc", "abcd", "abcdefg")); @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity createMapping(Authentication authentication, @@ -113,16 +109,7 @@ private Mapping getMappingFromRequest(JsonApiRequestWrapper requestBody) log.error("Incorrect type for this endpoint: {}", requestBody.data().type()); throw new IllegalArgumentException(); } - var mapping = mapper.treeToValue(requestBody.data().attributes(), Mapping.class); - checkSourceStandard(mapping); - return mapping; - } - - private void checkSourceStandard(Mapping mapping) { - if (!SOURCE_DATA_SYSTEMS.contains(mapping.sourceDataStandard())) { - throw new IllegalArgumentException( - "Invalid source data standard" + mapping.sourceDataStandard()); - } + return mapper.treeToValue(requestBody.data().attributes(), Mapping.class); } } diff --git a/src/main/java/eu/dissco/orchestration/backend/domain/Mapping.java b/src/main/java/eu/dissco/orchestration/backend/domain/Mapping.java index ae9c823..c370bd0 100644 --- a/src/main/java/eu/dissco/orchestration/backend/domain/Mapping.java +++ b/src/main/java/eu/dissco/orchestration/backend/domain/Mapping.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.JsonNode; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; public record Mapping( @NotBlank @@ -10,7 +11,7 @@ public record Mapping( String description, @JsonProperty(value = "fieldMapping") JsonNode mapping, - @NotBlank - String sourceDataStandard) { + @NotNull + SourceDataStandard sourceDataStandard) { } diff --git a/src/main/java/eu/dissco/orchestration/backend/domain/MappingRecord.java b/src/main/java/eu/dissco/orchestration/backend/domain/MappingRecord.java index 77de9e5..6b38534 100644 --- a/src/main/java/eu/dissco/orchestration/backend/domain/MappingRecord.java +++ b/src/main/java/eu/dissco/orchestration/backend/domain/MappingRecord.java @@ -15,6 +15,7 @@ public record MappingRecord( Instant deleted, @NotBlank String creator, + @NotNull Mapping mapping ) { diff --git a/src/main/java/eu/dissco/orchestration/backend/domain/SourceDataStandard.java b/src/main/java/eu/dissco/orchestration/backend/domain/SourceDataStandard.java new file mode 100644 index 0000000..6518554 --- /dev/null +++ b/src/main/java/eu/dissco/orchestration/backend/domain/SourceDataStandard.java @@ -0,0 +1,40 @@ +package eu.dissco.orchestration.backend.domain; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public enum SourceDataStandard { + @JsonProperty("dwc") DWC("dwc"), + @JsonProperty("abcd") ABCD("abcd"), + @JsonProperty("abcdefg") ABCDEFG("abcdefg"); + + final String standard; + + SourceDataStandard(String s){ + standard = s; + } + @Override + public String toString(){ + return standard; + } + + public static SourceDataStandard fromString(String standard){ + switch (standard) { + case "dwc" -> { + return DWC; + } + case "abcd" -> { + return ABCD; + } + case "abcdefg" -> { + return ABCDEFG; + } + default -> { + log.error("Invalid source data standard: {}", standard); + throw new IllegalStateException(); + } + } + + } +} diff --git a/src/main/java/eu/dissco/orchestration/backend/domain/SourceSystemRecord.java b/src/main/java/eu/dissco/orchestration/backend/domain/SourceSystemRecord.java index a4f3360..b953905 100644 --- a/src/main/java/eu/dissco/orchestration/backend/domain/SourceSystemRecord.java +++ b/src/main/java/eu/dissco/orchestration/backend/domain/SourceSystemRecord.java @@ -14,6 +14,7 @@ public record SourceSystemRecord( @NotNull Instant created, Instant deleted, + @NotNull SourceSystem sourceSystem ) { diff --git a/src/main/java/eu/dissco/orchestration/backend/repository/MappingRepository.java b/src/main/java/eu/dissco/orchestration/backend/repository/MappingRepository.java index 4c89447..1fe00aa 100644 --- a/src/main/java/eu/dissco/orchestration/backend/repository/MappingRepository.java +++ b/src/main/java/eu/dissco/orchestration/backend/repository/MappingRepository.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import eu.dissco.orchestration.backend.domain.Mapping; import eu.dissco.orchestration.backend.domain.MappingRecord; +import eu.dissco.orchestration.backend.domain.SourceDataStandard; import eu.dissco.orchestration.backend.exception.DisscoJsonBMappingException; import java.time.Instant; import java.util.List; @@ -31,7 +32,7 @@ public void createMapping(MappingRecord mappingRecord) { .set(MAPPING.VERSION, mappingRecord.version()) .set(MAPPING.NAME, mappingRecord.mapping().name()) .set(MAPPING.DESCRIPTION, mappingRecord.mapping().description()) - .set(MAPPING.SOURCEDATASTANDARD, mappingRecord.mapping().sourceDataStandard()) + .set(MAPPING.SOURCEDATASTANDARD, mappingRecord.mapping().sourceDataStandard().toString()) .set(MAPPING.MAPPING_, JSONB.valueOf(mappingRecord.mapping().mapping().toString())) .set(MAPPING.CREATED, mappingRecord.created()) .set(MAPPING.CREATOR, mappingRecord.creator()) @@ -43,7 +44,7 @@ public void updateMapping(MappingRecord mappingRecord) { .set(MAPPING.VERSION, mappingRecord.version()) .set(MAPPING.NAME, mappingRecord.mapping().name()) .set(MAPPING.DESCRIPTION, mappingRecord.mapping().description()) - .set(MAPPING.SOURCEDATASTANDARD, mappingRecord.mapping().sourceDataStandard()) + .set(MAPPING.SOURCEDATASTANDARD, mappingRecord.mapping().sourceDataStandard().toString()) .set(MAPPING.MAPPING_, JSONB.valueOf(mappingRecord.mapping().mapping().toString())) .set(MAPPING.CREATED, mappingRecord.created()) @@ -95,7 +96,7 @@ private MappingRecord mapToMappingRecord(Record dbRecord) { dbRecord.get(MAPPING.NAME), dbRecord.get(MAPPING.DESCRIPTION), mapToJson(dbRecord.get(MAPPING.MAPPING_)), - dbRecord.get(MAPPING.SOURCEDATASTANDARD)); + SourceDataStandard.fromString(dbRecord.get(MAPPING.SOURCEDATASTANDARD))); return new MappingRecord( dbRecord.get(MAPPING.ID), dbRecord.get(MAPPING.VERSION), diff --git a/src/main/java/eu/dissco/orchestration/backend/service/FdoRecordService.java b/src/main/java/eu/dissco/orchestration/backend/service/FdoRecordService.java index a84b135..e52b3f4 100644 --- a/src/main/java/eu/dissco/orchestration/backend/service/FdoRecordService.java +++ b/src/main/java/eu/dissco/orchestration/backend/service/FdoRecordService.java @@ -57,7 +57,7 @@ private JsonNode buildRequestAttributes(Object object, ObjectType type) { private JsonNode buildMappingAttributes(Mapping mapping) { return buildGeneralAttributes() - .put(SOURCE_DATA_STANDARD.getAttribute(), mapping.sourceDataStandard()); + .put(SOURCE_DATA_STANDARD.getAttribute(), mapping.sourceDataStandard().toString()); } private JsonNode buildMasAttributes(MachineAnnotationService mas) { diff --git a/src/test/java/eu/dissco/orchestration/backend/controller/MappingControllerTest.java b/src/test/java/eu/dissco/orchestration/backend/controller/MappingControllerTest.java index 1aa4424..b8d5b19 100644 --- a/src/test/java/eu/dissco/orchestration/backend/controller/MappingControllerTest.java +++ b/src/test/java/eu/dissco/orchestration/backend/controller/MappingControllerTest.java @@ -17,12 +17,8 @@ import static org.junit.jupiter.api.Assertions.assertThrowsExactly; import static org.mockito.BDDMockito.given; -import eu.dissco.orchestration.backend.domain.Mapping; import eu.dissco.orchestration.backend.domain.MappingRecord; -import eu.dissco.orchestration.backend.domain.ObjectType; import eu.dissco.orchestration.backend.domain.jsonapi.JsonApiLinks; -import eu.dissco.orchestration.backend.domain.jsonapi.JsonApiRequest; -import eu.dissco.orchestration.backend.domain.jsonapi.JsonApiRequestWrapper; import eu.dissco.orchestration.backend.properties.ApplicationProperties; import eu.dissco.orchestration.backend.service.MappingService; import java.util.Collections; @@ -78,23 +74,6 @@ void testCreateMappingBadType() { () -> controller.createMapping(authentication, requestBody, mockRequest)); } - @Test - void testCreateMappingBadSourceSystemStandard() { - // Given - var mapping = new Mapping( - "name", - "description", - MAPPER.createObjectNode(), - "badType" - ); - var requestBody = new JsonApiRequestWrapper( - new JsonApiRequest(ObjectType.MAPPING, MAPPER.valueToTree(mapping))); - - // Then - assertThrowsExactly(IllegalArgumentException.class, - () -> controller.createMapping(authentication, requestBody, mockRequest)); - } - @Test void testUpdateMapping() throws Exception { // Given @@ -125,9 +104,6 @@ void testUpdateMappingNoChanges() throws Exception { @Test void testGetMappingById() { - // Given - var id = HANDLE; - // When var result = controller.getMappingById(PREFIX, SUFFIX, mockRequest); diff --git a/src/test/java/eu/dissco/orchestration/backend/repository/MappingRepositoryIT.java b/src/test/java/eu/dissco/orchestration/backend/repository/MappingRepositoryIT.java index d56dbb0..65befc9 100644 --- a/src/test/java/eu/dissco/orchestration/backend/repository/MappingRepositoryIT.java +++ b/src/test/java/eu/dissco/orchestration/backend/repository/MappingRepositoryIT.java @@ -13,6 +13,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import eu.dissco.orchestration.backend.domain.Mapping; import eu.dissco.orchestration.backend.domain.MappingRecord; +import eu.dissco.orchestration.backend.domain.SourceDataStandard; import java.time.Instant; import java.util.ArrayList; import java.util.List; @@ -78,7 +79,7 @@ private MappingRecord givenUpdatedRecord() { "An updated name", "With a nice new description", MAPPER.createObjectNode(), - "abcd" + SourceDataStandard.ABCD ) ); } @@ -208,7 +209,7 @@ private MappingRecord mapToMappingRecord(Record dbRecord) { dbRecord.get(MAPPING.NAME), dbRecord.get(MAPPING.DESCRIPTION), MAPPER.readTree(dbRecord.get(MAPPING.MAPPING_).data()), - dbRecord.get(MAPPING.SOURCEDATASTANDARD)); + SourceDataStandard.fromString(dbRecord.get(MAPPING.SOURCEDATASTANDARD))); return new MappingRecord( dbRecord.get(MAPPING.ID), dbRecord.get(MAPPING.VERSION), @@ -230,7 +231,7 @@ private void postMappingRecords(List mappingRecords) { .set(MAPPING.VERSION, mappingRecord.version()) .set(MAPPING.NAME, mappingRecord.mapping().name()) .set(MAPPING.DESCRIPTION, mappingRecord.mapping().description()) - .set(MAPPING.SOURCEDATASTANDARD, mappingRecord.mapping().sourceDataStandard()) + .set(MAPPING.SOURCEDATASTANDARD, mappingRecord.mapping().sourceDataStandard().toString()) .set(MAPPING.MAPPING_, JSONB.valueOf(mappingRecord.mapping().mapping().toString())) .set(MAPPING.CREATED, mappingRecord.created()) diff --git a/src/test/java/eu/dissco/orchestration/backend/service/MappingServiceTest.java b/src/test/java/eu/dissco/orchestration/backend/service/MappingServiceTest.java index f7811e8..6cd31ca 100644 --- a/src/test/java/eu/dissco/orchestration/backend/service/MappingServiceTest.java +++ b/src/test/java/eu/dissco/orchestration/backend/service/MappingServiceTest.java @@ -28,6 +28,7 @@ import eu.dissco.orchestration.backend.domain.Mapping; import eu.dissco.orchestration.backend.domain.MappingRecord; import eu.dissco.orchestration.backend.domain.ObjectType; +import eu.dissco.orchestration.backend.domain.SourceDataStandard; import eu.dissco.orchestration.backend.domain.jsonapi.JsonApiData; import eu.dissco.orchestration.backend.domain.jsonapi.JsonApiLinks; import eu.dissco.orchestration.backend.domain.jsonapi.JsonApiWrapper; @@ -156,7 +157,7 @@ void testCreateMappingKafkaAndRollbackFails() throws Exception { void testUpdateMapping() throws Exception { // Given var prevMapping = new Mapping("old name", OBJECT_DESCRIPTION, MAPPER.createObjectNode(), - "dwc"); + SourceDataStandard.DWC); var prevRecord = Optional.of( new MappingRecord(HANDLE, 1, CREATED, null, OBJECT_CREATOR, prevMapping)); var mapping = givenMapping(); @@ -179,7 +180,7 @@ void testUpdateMapping() throws Exception { void testUpdateMappingKafkaFails() throws Exception { // Given var prevMapping = new Mapping("old name", OBJECT_DESCRIPTION, MAPPER.createObjectNode(), - "dwc"); + SourceDataStandard.DWC); var prevRecord = Optional.of( new MappingRecord(HANDLE, 1, CREATED, null, OBJECT_CREATOR, prevMapping)); var mapping = givenMapping(); diff --git a/src/test/java/eu/dissco/orchestration/backend/testutils/TestUtils.java b/src/test/java/eu/dissco/orchestration/backend/testutils/TestUtils.java index 5e98669..ca6b9b2 100644 --- a/src/test/java/eu/dissco/orchestration/backend/testutils/TestUtils.java +++ b/src/test/java/eu/dissco/orchestration/backend/testutils/TestUtils.java @@ -4,11 +4,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import eu.dissco.orchestration.backend.database.jooq.enums.TranslatorType; -import eu.dissco.orchestration.backend.domain.ObjectType; import eu.dissco.orchestration.backend.domain.MachineAnnotationService; import eu.dissco.orchestration.backend.domain.MachineAnnotationServiceRecord; import eu.dissco.orchestration.backend.domain.Mapping; import eu.dissco.orchestration.backend.domain.MappingRecord; +import eu.dissco.orchestration.backend.domain.ObjectType; +import eu.dissco.orchestration.backend.domain.SourceDataStandard; import eu.dissco.orchestration.backend.domain.SourceSystem; import eu.dissco.orchestration.backend.domain.SourceSystemRecord; import eu.dissco.orchestration.backend.domain.jsonapi.JsonApiData; @@ -192,7 +193,7 @@ public static Mapping givenMapping() { OBJECT_NAME, OBJECT_DESCRIPTION, MAPPER.createObjectNode(), - "dwc"); + SourceDataStandard.DWC); } public static JsonApiListWrapper givenMappingRecordResponse(List mappingRecords,