Skip to content

Commit

Permalink
Make source data standard enum
Browse files Browse the repository at this point in the history
  • Loading branch information
southeo committed May 14, 2024
1 parent 4d44e9a commit cbf08e3
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,8 +38,6 @@ public class MappingController {
private final MappingService service;
private final ObjectMapper mapper;
private final ApplicationProperties appProperties;
private static final ArrayList<String> SOURCE_DATA_SYSTEMS = new ArrayList<>(
List.of("dwc", "abcd", "abcdefg"));

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<JsonApiWrapper> createMapping(Authentication authentication,
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
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
String name,
String description,
@JsonProperty(value = "fieldMapping")
JsonNode mapping,
@NotBlank
String sourceDataStandard) {
@NotNull
SourceDataStandard sourceDataStandard) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public record MappingRecord(
Instant deleted,
@NotBlank
String creator,
@NotNull
Mapping mapping
) {

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

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public record SourceSystemRecord(
@NotNull
Instant created,
Instant deleted,
@NotNull
SourceSystem sourceSystem
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -125,9 +104,6 @@ void testUpdateMappingNoChanges() throws Exception {

@Test
void testGetMappingById() {
// Given
var id = HANDLE;

// When
var result = controller.getMappingById(PREFIX, SUFFIX, mockRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,7 +79,7 @@ private MappingRecord givenUpdatedRecord() {
"An updated name",
"With a nice new description",
MAPPER.createObjectNode(),
"abcd"
SourceDataStandard.ABCD
)
);
}
Expand Down Expand Up @@ -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),
Expand All @@ -230,7 +231,7 @@ private void postMappingRecords(List<MappingRecord> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -192,7 +193,7 @@ public static Mapping givenMapping() {
OBJECT_NAME,
OBJECT_DESCRIPTION,
MAPPER.createObjectNode(),
"dwc");
SourceDataStandard.DWC);
}

public static JsonApiListWrapper givenMappingRecordResponse(List<MappingRecord> mappingRecords,
Expand Down

0 comments on commit cbf08e3

Please sign in to comment.