Skip to content

Commit

Permalink
use new pid rollback endpoint (#32)
Browse files Browse the repository at this point in the history
* use new pid rollback endpoint

* align with handle api

* align with handle api
  • Loading branch information
southeo authored Jul 25, 2024
1 parent 9ac4b27 commit bb7805d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public enum FdoProfileAttributes {
LINKED_DO_PID("linkedDigitalObjectPid", null),
MEDIA_HOST("mediaHost", null),
IS_DERIVED_FROM_SPECIMEN("isDerivedFromSpecimen", "true"),
LINKED_DO_TYPE("linkedDigitalObjectType", "digital specimen"),
LINKED_DO_TYPE("linkedDigitalObjectType", "https://hdl.handle.net/21.T11148/894b1e6cad57e921764e"),
PRIMARY_MO_ID_TYPE("primaryMediaObjectIdType", "Resolvable"),
PRIMARY_MO_ID_NAME("primaryMediaObjectIdName", "ac:accessUri"),
PRIMARY_MO_TYPE("dcterms:type", null),
LICENSE("license", null),
DCTERMS_TYPE("dcterms:type", "Image"),
LICENSE_NAME("licenseName", ""),
RIGHTSHOLDER_PID_TYPE("rightsholderPidType", "Resolvable"),
MEDIA_FORMAT("dcterms:format", "image");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.ISSUED_FOR_AGENT;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.IS_DERIVED_FROM_SPECIMEN;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.LICENSE;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.LICENSE_NAME;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.LINKED_DO_PID;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.LINKED_DO_TYPE;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.MEDIA_FORMAT;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.MEDIA_HOST;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.PRIMARY_MEDIA_ID;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.PRIMARY_MO_ID_NAME;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.PRIMARY_MO_ID_TYPE;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.PRIMARY_MO_TYPE;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.DCTERMS_TYPE;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.REFERENT_NAME;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.RIGHTSHOLDER_PID_TYPE;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.TYPE;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import eu.dissco.core.digitalmediaprocessor.domain.DigitalMediaRecord;
import eu.dissco.core.digitalmediaprocessor.domain.DigitalMediaWrapper;
import eu.dissco.core.digitalmediaprocessor.exceptions.PidCreationException;
import eu.dissco.core.digitalmediaprocessor.properties.FdoProperties;
import eu.dissco.core.digitalmediaprocessor.schema.DigitalMedia;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -35,6 +35,7 @@ public class FdoRecordService {

private final ObjectMapper mapper;
private final FdoProperties fdoProperties;
private static final String MISSING_ELEMENT_MSG = "Missing mandatory fdo element %s";

public List<JsonNode> buildPostHandleRequest(List<DigitalMediaWrapper> mediaObjects)
throws PidCreationException {
Expand All @@ -57,45 +58,49 @@ private JsonNode buildSingleHandleRequest(DigitalMediaWrapper mediaObject)
}

private JsonNode generateAttributes(DigitalMediaWrapper mediaObject) throws PidCreationException {
validateMandatoryAttributes(mediaObject.attributes());
var attributes = mapper.createObjectNode();
if (mediaObject.attributes().getOdsOrganisationID() != null
&& mediaObject.attributes().getDctermsLicense() != null
&& mediaObject.attributes().getAcAccessURI() != null) {
attributes.put(MEDIA_HOST.getAttribute(),
mediaObject.attributes().getOdsOrganisationID());
attributes.put(LICENSE.getAttribute(),
mediaObject.attributes().getDctermsLicense());
attributes.put(PRIMARY_MEDIA_ID.getAttribute(), mediaObject.attributes().getAcAccessURI());
} else {
log.error("Missing mandatory element for FDO profile: {}", mediaObject);
throw new PidCreationException("Missing mandatory element for FDO profile");
}
attributes.put(MEDIA_HOST.getAttribute(),
mediaObject.attributes().getOdsOrganisationID());
attributes.put(LICENSE_NAME.getAttribute(), mediaObject.attributes().getDctermsLicense());
attributes.put(PRIMARY_MEDIA_ID.getAttribute(), mediaObject.attributes().getAcAccessURI());
attributes.put(REFERENT_NAME.getAttribute(),
mediaObject.type() + " for " + mediaObject.digitalSpecimenID());
attributes.put(LINKED_DO_PID.getAttribute(), mediaObject.digitalSpecimenID());
if (mediaObject.type() != null) {
attributes.put(MEDIA_FORMAT.getAttribute(), MEDIA_FORMAT.getDefaultValue());
}
// Optional values
attributes.put(MEDIA_FORMAT.getAttribute(), mediaObject.attributes().getDctermsFormat() == null
? MEDIA_FORMAT.getDefaultValue() : mediaObject.attributes().getDctermsFormat());
attributes.put(DCTERMS_TYPE.getAttribute(), mediaObject.attributes().getDctermsType() == null ?
DCTERMS_TYPE.getDefaultValue() : mediaObject.attributes().getDctermsType().value());

// Default values
attributes.put(ISSUED_FOR_AGENT.getAttribute(), fdoProperties.getIssuedForAgent());
attributes.put(PRIMARY_MO_TYPE.getAttribute(), PRIMARY_MO_TYPE.getDefaultValue());
attributes.put(PRIMARY_MO_ID_TYPE.getAttribute(), PRIMARY_MO_ID_TYPE.getDefaultValue());
attributes.put(PRIMARY_MO_ID_NAME.getAttribute(), PRIMARY_MO_ID_NAME.getDefaultValue());
attributes.put(RIGHTSHOLDER_PID_TYPE.getAttribute(), RIGHTSHOLDER_PID_TYPE.getDefaultValue());
attributes.put(IS_DERIVED_FROM_SPECIMEN.getAttribute(),
Boolean.valueOf(IS_DERIVED_FROM_SPECIMEN.getDefaultValue()));
attributes.put(LINKED_DO_TYPE.getAttribute(), LINKED_DO_TYPE.getDefaultValue());

return attributes;
}

public JsonNode buildRollbackCreationRequest(List<DigitalMediaRecord> digitalMedia) {
var handles = digitalMedia.stream().map(DigitalMediaRecord::id).toList();
var dataNode = handles.stream().map(handle -> mapper.createObjectNode().put("id", handle))
.toList();
ArrayNode dataArray = mapper.valueToTree(dataNode);
return mapper.createObjectNode().set("data", dataArray);
public List<String> buildRollbackCreationRequest(List<DigitalMediaRecord> digitalMedia) {
return digitalMedia.stream().map(DigitalMediaRecord::id).toList();
}

private void validateMandatoryAttributes(DigitalMedia mediaObject) throws PidCreationException {
if (mediaObject.getOdsOrganisationID() == null) {
log.error(String.format(MISSING_ELEMENT_MSG, "ods:organisationID"));
throw new PidCreationException(String.format(MISSING_ELEMENT_MSG, "ods:organisationID"));
}
if (mediaObject.getDctermsLicense() == null) {
log.error(String.format(MISSING_ELEMENT_MSG, "dcterms:license"));
throw new PidCreationException(String.format(MISSING_ELEMENT_MSG, "dcterms:license"));
}

}

public List<JsonNode> buildPatchDeleteRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public void updateHandle(List<JsonNode> request) throws PidCreationException {
validateResponse(response);
}

public void rollbackHandleCreation(JsonNode request)
public void rollbackHandleCreation(List<String> request)
throws PidCreationException {
log.info("Rolling back handle creation");
var requestBody = BodyInserters.fromValue(request);
var response = sendRequest(HttpMethod.DELETE, requestBody, "rollback");
var response = sendRequest(HttpMethod.DELETE, requestBody, "rollback/create");
validateResponse(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.ISSUED_FOR_AGENT;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.IS_DERIVED_FROM_SPECIMEN;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.LICENSE;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.LICENSE_NAME;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.LINKED_DO_PID;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.LINKED_DO_TYPE;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.MEDIA_FORMAT;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.MEDIA_HOST;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.PRIMARY_MEDIA_ID;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.PRIMARY_MO_ID_NAME;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.PRIMARY_MO_ID_TYPE;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.PRIMARY_MO_TYPE;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.DCTERMS_TYPE;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.REFERENT_NAME;
import static eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes.RIGHTSHOLDER_PID_TYPE;

Expand Down Expand Up @@ -217,13 +217,13 @@ public static JsonNode givenPostHandleRequest() {
public static JsonNode givenPostAttributes() {
var attributes = MAPPER.createObjectNode();
attributes.put(MEDIA_HOST.getAttribute(), MEDIA_HOST_TESTVAL);
attributes.put(LICENSE.getAttribute(), LICENSE_TESTVAL);
attributes.put(LICENSE_NAME.getAttribute(), LICENSE_TESTVAL);
attributes.put(PRIMARY_MEDIA_ID.getAttribute(), MEDIA_URL_1);
attributes.put(REFERENT_NAME.getAttribute(), TYPE + " for " + DIGITAL_SPECIMEN_ID);
attributes.put(LINKED_DO_PID.getAttribute(), DIGITAL_SPECIMEN_ID);
attributes.put(MEDIA_FORMAT.getAttribute(), "image");
attributes.put(MEDIA_FORMAT.getAttribute(), FORMAT);
attributes.put(ISSUED_FOR_AGENT.getAttribute(), "https://ror.org/0566bfb96");
attributes.put(PRIMARY_MO_TYPE.getAttribute(), PRIMARY_MO_TYPE.getDefaultValue());
attributes.put(DCTERMS_TYPE.getAttribute(), DCTERMS_TYPE.getDefaultValue());
attributes.put(PRIMARY_MO_ID_TYPE.getAttribute(), PRIMARY_MO_ID_TYPE.getDefaultValue());
attributes.put(PRIMARY_MO_ID_NAME.getAttribute(), PRIMARY_MO_ID_NAME.getDefaultValue());
attributes.put(RIGHTSHOLDER_PID_TYPE.getAttribute(), RIGHTSHOLDER_PID_TYPE.getDefaultValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
import static eu.dissco.core.digitalmediaprocessor.TestUtils.givenPostAttributes;
import static eu.dissco.core.digitalmediaprocessor.TestUtils.givenPostHandleRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mockStatic;

import com.fasterxml.jackson.databind.JsonNode;
import eu.dissco.core.digitalmediaprocessor.TestUtils;
import eu.dissco.core.digitalmediaprocessor.domain.DigitalMediaWrapper;
import eu.dissco.core.digitalmediaprocessor.domain.FdoProfileAttributes;
Expand All @@ -42,16 +41,6 @@ class FdoRecordServiceTest {
private MockedStatic<Instant> mockedStatic;
private MockedStatic<Clock> mockedClock;

private static JsonNode expectedRollbackCreationRequest() throws Exception {
return MAPPER.readTree("""
{
"data":[
{"id":"20.5000.1025/1BY-BHB-AVN"}
]
}
""");
}

@BeforeEach
void setup() {
Clock clock = Clock.fixed(CREATED, ZoneOffset.UTC);
Expand Down Expand Up @@ -101,7 +90,7 @@ void testBuildPostHandleRequest() throws Exception {
@Test
void testBuildRollbackCreationRequest() throws Exception {
// Given
var expected = expectedRollbackCreationRequest();
var expected = List.of(HANDLE);

// When
var result = fdoRecordService.buildRollbackCreationRequest(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package eu.dissco.core.digitalmediaprocessor.web;

import static eu.dissco.core.digitalmediaprocessor.TestUtils.CREATED;
import static eu.dissco.core.digitalmediaprocessor.TestUtils.HANDLE;
import static eu.dissco.core.digitalmediaprocessor.TestUtils.HANDLE_2;
import static eu.dissco.core.digitalmediaprocessor.TestUtils.MAPPER;
import static eu.dissco.core.digitalmediaprocessor.TestUtils.givenPidMap;
import static eu.dissco.core.digitalmediaprocessor.TestUtils.givenPostHandleRequest;
Expand Down Expand Up @@ -137,16 +139,9 @@ void testRetriesSuccess() throws Exception {
}

@Test
void testRollbackHandleCreation() throws Exception {
void testRollbackHandleCreation() {
// Given
var requestBody = MAPPER.readTree("""
{
"data": [
{"id": "20.5000.1025/AAA-111-AAA"},
{"id": "20.5000.1025/BBB-222-BBB"}
]
}
""");
var requestBody = List.of(HANDLE, HANDLE_2);
mockHandleServer.enqueue(new MockResponse().setResponseCode(HttpStatus.OK.value())
.addHeader("Content-Type", "application/json"));

Expand Down

0 comments on commit bb7805d

Please sign in to comment.