Skip to content

Commit

Permalink
[CST-18016] Improved OpenAlex Publication mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamF42 committed Feb 23, 2025
1 parent c1b27eb commit 1503eaf
Show file tree
Hide file tree
Showing 4 changed files with 1,471 additions and 1,167 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.importer.external.metadatamapping.contributor;

import java.util.Collection;
import java.util.Collections;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.importer.external.metadatamapping.MetadatumDTO;

/**
* A metadata contributor that extracts a value from JSON using a {@link JsonPathMetadataProcessor}
* and compares it against a predefined right operand. If the extracted value matches the right operand,
* the specified {@link SimpleJsonPathMetadataContributor} is used to extract additional metadata.
* Otherwise, an empty collection is returned.
*
* This class extends {@link SimpleJsonPathMetadataContributor} and allows conditional metadata extraction
* based on JSON path processing.
*
* @author Adamo Fapohunda (adamo.fapohunda at 4science.com)
*/
public class ConditionEqualJsonPathMetadataContributor extends SimpleJsonPathMetadataContributor {

private static final Logger log = LogManager.getLogger(ConditionEqualJsonPathMetadataContributor.class);


private JsonPathMetadataProcessor leftOperandProcessor;

private String rightOperand;

private SimpleJsonPathMetadataContributor metadatumContributor;

/**
* Extracts metadata from the provided JSON string. The extraction process follows these steps:
* <ol>
* <li>Uses {@link JsonPathMetadataProcessor} to extract a value from the JSON.</li>
* <li>Compares the extracted value with the predefined right operand.</li>
* <li>If they match, the {@link SimpleJsonPathMetadataContributor} is used to extract and return metadata.</li>
* <li>If they do not match, an empty collection is returned.</li>
* </ol>
*
* @param json The JSON string to process.
* @return A collection of {@link MetadatumDTO} if the condition is met; otherwise, an empty collection.
*/
@Override
public Collection<MetadatumDTO> contributeMetadata(String json) {
Collection<String> leftOperands = leftOperandProcessor.processMetadata(json);
if (leftOperands == null || leftOperands.size() != 1) {
log.warn("No left operands found in json path: " + json);
return Collections.emptyList();
}

if (rightOperand.equals(leftOperands.iterator().next())) {
return metadatumContributor.contributeMetadata(json);
}

return Collections.emptyList();
}

/**
* Sets the {@link JsonPathMetadataProcessor} responsible for extracting the left operand value from the JSON.
*
* @param leftOperandProcessor The JSON path processor used for extraction.
*/
public void setLeftOperandProcessor(
JsonPathMetadataProcessor leftOperandProcessor) {
this.leftOperandProcessor = leftOperandProcessor;
}

/**
* Sets the right operand value that the extracted JSON value must match for metadata extraction to proceed.
*
* @param rightOperand The expected value to compare against.
*/
public void setRightOperand(String rightOperand) {
this.rightOperand = rightOperand;
}

/**
* Sets the {@link SimpleJsonPathMetadataContributor} responsible for extracting metadata if the condition is met.
*
* @param metadatumContributor The metadata contributor to use upon a successful match.
*/
public void setMetadatumContributor(
SimpleJsonPathMetadataContributor metadatumContributor) {
this.metadatumContributor = metadatumContributor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,82 @@ public void findOpenalexPublicationExternalSourceEntriesEmptyWithQueryTest() thr
}
}

@Test
public void findOneOpenalexPublicationExternalSourceEntriesWithQueryTest() throws Exception {

try (InputStream file = getClass().getResourceAsStream("openalex-publication-single.json")) {
String jsonResponse = IOUtils.toString(file, Charset.defaultCharset());

when(liveImportClient.executeHttpGetRequest(anyInt(), anyString(), anyMap()))
.thenReturn(jsonResponse);

getClient().perform(get("/api/integration/externalsources/openalexPublication/entries")
.param("query", "protein"))
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.externalSourceEntries", Matchers.hasSize(1)))
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].id").value("W1775749144"))
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].display")
.value("PROTEIN MEASUREMENT WITH THE FOLIN PHENOL REAGENT"))
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].value")
.value("PROTEIN MEASUREMENT WITH THE FOLIN PHENOL REAGENT"))
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].externalSource")
.value("openalexPublication"))
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.contributor.author']",
Matchers.hasSize(4)))
.andExpect(
jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.contributor.author'][0].value")
.value("OliverH. Lowry"))
.andExpect(
jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.contributor.author'][1].value")
.value("NiraJ. Rosebrough"))
.andExpect(
jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.contributor.author'][2].value")
.value("A. Farr"))
.andExpect(
jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.contributor.author'][3].value")
.value("RoseJ. Randall"))
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.identifier.orcid']",
Matchers.hasSize(2)))
.andExpect(
jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.identifier.orcid'][0].value")
.value("https://orcid.org/0000-0001-6187-6610"))
.andExpect(
jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.identifier.orcid'][1].value")
.value("https://orcid.org/0000-0001-6187-6611"))
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.identifier.uri']",
Matchers.hasSize(2)))
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.identifier.uri'][0]" +
".value")
.value("https://doi.org/10.1016/s0021-9258(19)52451-6"))
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.identifier.uri'][1]" +
".value")
.value("https://doi.org/10.1016/s0021-9258(19)52451-6/pdf"))
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.date.issued'][0].value")
.value("1951-11-01"))
.andExpect(
jsonPath("$._embedded.externalSourceEntries[0].metadata['oaire.version.volume'][0].value")
.value("http://purl.org/coar/version/c_970fb48d4fbd8a85"))
.andExpect(
jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.identifier.doi'][0].value")
.value("10.1016/s0021-9258(19)52451-6"))
.andExpect(
jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.identifier.pmid'][0].value")
.value("14907713"))
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.language.iso'][0].value")
.value("en"))
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].metadata['dc.title'][0].value")
.value("PROTEIN MEASUREMENT WITH THE FOLIN PHENOL REAGENT"))
.andExpect(jsonPath("$.page.totalElements").value(1))
.andExpect(jsonPath("$.page.totalPages").value(1))
.andExpect(jsonPath("$.page.size").value(20))
.andExpect(jsonPath("$.page.number").value(0));

verify(liveImportClient, times(2)).executeHttpGetRequest(anyInt(), anyString(), anyMap());
}
}

@Test
public void findOneOpenaireFundingExternalSourceEntriesWithQueryTest() throws Exception {
public void findAllOpenalexPublicationExternalSourceEntriesWithQueryTest() throws Exception {

try (InputStream file = getClass().getResourceAsStream("openalex-publication-multiple.json")) {
String jsonResponse = IOUtils.toString(file, Charset.defaultCharset());
Expand Down Expand Up @@ -129,5 +202,4 @@ public void findOneOpenaireFundingExternalSourceEntriesWithQueryTest() throws Ex
}
}


}
Loading

0 comments on commit 1503eaf

Please sign in to comment.