Skip to content

Commit

Permalink
Start adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samleeflang committed Sep 18, 2023
1 parent 1f48123 commit 1131089
Show file tree
Hide file tree
Showing 43 changed files with 1,191 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import eu.dissco.core.translator.terms.specimen.identification.taxonomy.SpecimenName;
import eu.dissco.core.translator.terms.specimen.identification.taxonomy.TaxonId;
import eu.dissco.core.translator.terms.specimen.identification.taxonomy.TaxonRank;
import eu.dissco.core.translator.terms.specimen.identification.taxonomy.TaxonRemarks;
import eu.dissco.core.translator.terms.specimen.identification.taxonomy.TaxonomicStatus;
import eu.dissco.core.translator.terms.specimen.identification.taxonomy.VernacularName;
import eu.dissco.core.translator.terms.specimen.location.Continent;
Expand Down Expand Up @@ -380,6 +381,7 @@ private Identifications createIdentification(JsonNode data, boolean dwc) {
.withDwcSpecificEpithet(termMapper.retrieveTerm(new SpecificEpithet(), data, dwc))
.withDwcTaxonomicStatus(termMapper.retrieveTerm(new TaxonomicStatus(), data, dwc))
.withDwcNomenclaturalCode(termMapper.retrieveTerm(new NomenclaturalCode(), data, dwc))
.withDwcTaxonRemarks(termMapper.retrieveTerm(new TaxonRemarks(), data, dwc))
.withDwcVernacularName(termMapper.retrieveTerm(new VernacularName(), data, dwc));
return new Identifications()
.withDwcIdentificationID(termMapper.retrieveTerm(new IdentificationId(), data, dwc))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class MediaAssertions {

List<Pair<Term, String>> abcdMediaAssertions = List.of(Pair.of(new FileSize(), "kb"),
private final List<Pair<Term, String>> abcdMediaAssertions = List.of(Pair.of(new FileSize(), "kb"),
Pair.of(new PixelXDimension(), "pixel"), Pair.of(new PixelYDimension(), "pixel"));

public List<eu.dissco.core.translator.schema.Assertions> gatherAssertions(JsonNode data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class Source extends Term {

public static final String TERM = "dcterms:source";
private final List<String> dwcaTerms = List.of(TERM);
private final List<String> dwcaTerms = List.of(TERM, "dc:source");
private final List<String> abcdTerms = List.of("abcd:productURI");

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,11 @@ public class TypeStatus extends AbstractTaxonomy {

private final List<String> dwcaTerms = List.of(TERM);

// Pick the first TypeStatus from ABCD
private final List<String> abcdTerms =
List.of(
"abcd:specimenUnit/nomenclaturalTypeDesignations/nomenclaturalTypeDesignation/0/typeStatus",
"abcd:specimenUnit/nomenclaturalTypeDesignations/nomenclaturalTypeDesignation/0/typifiedName/fullScientificNameString",
"abcd:specimenUnit/nomenclaturalTypeDesignations/nomenclaturalTypeDesignation/0/nomenclaturalReference/titleCitation"
);

@Override
public String retrieveFromDWCA(JsonNode unit) {
return super.searchJsonForTerm(unit, dwcaTerms);
}

@Override
public String retrieveFromABCD(JsonNode unit) {
return super.combineABCDTerms(unit, abcdTerms);
}

@Override
public String getTerm() {
return TERM;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,128 +1,23 @@
package eu.dissco.core.translator.terms.specimen.identification.taxonomy;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import eu.dissco.core.translator.terms.Term;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.tuple.Pair;

public abstract class AbstractTaxonomy extends Term {

protected static final String IDENTIFICATION = "abcd:identifications/identification/";
protected static final String EXTENSIONS = "extensions";
protected static final String IDENTIFICATION_EXTENSION = "dwc:Identification";
protected static final String IDENTIFICATION_VERIFICATION_STATUS = "dwc:identificationVerificationStatus";
private static final String IDENTIFICATION_INDEX = "ods:taxonIdentificationIndex";
private static final Pair<String, String> ABCD_TAXON_RANK =
Pair.of("result/taxonIdentified/higherTaxa/higherTaxon/",
"/higherTaxonRank");
private static final Pair<String, String> ABCD_VALUE =
Pair.of("result/taxonIdentified/higherTaxa/higherTaxon/",
"/higherTaxonName");
private static final Pair<String, String> abcdPreferredFlag = Pair.of(IDENTIFICATION,
"/preferredFlag");

protected String getTaxonFromDWCA(JsonNode unit, List<String> dwcaTerms) {
var result = super.searchJsonForTerm(unit, dwcaTerms);
if (result != null) {
return result;
} else if (unit.get(EXTENSIONS) != null
&& unit.get(EXTENSIONS).get(IDENTIFICATION_EXTENSION) != null) {
var identificationIndex = getIdentificationIndexDWCA(unit);
if (identificationIndex != null) {
return searchJsonForTerm(
unit.get(EXTENSIONS).get(IDENTIFICATION_EXTENSION)
.get(Integer.parseInt(identificationIndex)), dwcaTerms);
}
}
return null;
}

private Optional<String> determineIdentificationIndexDWCA(JsonNode unit) {
if (unit.get(EXTENSIONS) != null
&& unit.get(EXTENSIONS).get(IDENTIFICATION_EXTENSION) != null) {
var identifications = unit.get(EXTENSIONS).get(IDENTIFICATION_EXTENSION);
for (int i = 0; i < identifications.size(); i++) {
var identification = identifications.get(i);
if (identification.get(IDENTIFICATION_VERIFICATION_STATUS) == null) {
return Optional.of(String.valueOf(i));
} else {
var verificationStatus = identification.get(IDENTIFICATION_VERIFICATION_STATUS)
.asText();
if (verificationStatus.equals("1")) {
return Optional.of(String.valueOf(i));
}
}
}
}
return Optional.empty();
}

protected String getIdentificationIndexDWCA(JsonNode unit) {
if (unit.get(IDENTIFICATION_INDEX) != null) {
return unit.get(IDENTIFICATION_INDEX).asText();
} else {
var optionalIndex = determineIdentificationIndexDWCA(unit);
if (optionalIndex.isPresent()) {
((ObjectNode) unit).put(IDENTIFICATION_INDEX, optionalIndex.get());
return optionalIndex.get();
} else {
return null;
}
}
}

protected String getIdentificationIndexABCD(JsonNode unit) {
if (unit.get(IDENTIFICATION_INDEX) != null) {
return unit.get(IDENTIFICATION_INDEX).asText();
} else {
var identificationIndex = determineIdentificationIndexABCD(unit);
((ObjectNode) unit).put(IDENTIFICATION_INDEX, identificationIndex);
return identificationIndex;
}
}

private String determineIdentificationIndexABCD(JsonNode unit) {
var numberFound = 0;
while (true) {
if (unit.get(getStringAtIndex(abcdPreferredFlag, numberFound)) != null) {
var optionalResult = checkPreferredFlag(unit, numberFound);
if (optionalResult.isPresent()) {
return optionalResult.get();
} else {
numberFound++;
}
} else {
return "0";
}
}
}

protected String searchABCDSplitTerms(JsonNode unit, List<String> searchTerms) {
return searchABCDSplitTerms(unit, searchTerms,
Pair.of(ABCD_TAXON_RANK.getLeft(), ABCD_TAXON_RANK.getRight()),
Pair.of(ABCD_VALUE.getLeft(), ABCD_VALUE.getRight()));
}

protected String searchABCDTerms(JsonNode unit, List<String> searchTerms) {
var identificationIndex = getIdentificationIndexABCD(unit);
var terms = searchTerms.stream().map(
term -> IDENTIFICATION + identificationIndex + "/result/taxonIdentified" + term).toList();
return searchJsonForTerm(unit, terms);
}

protected String getStringAtIndex(Pair<String, String> string, int numberFound) {
return string.getLeft() + numberFound + string.getRight();
}

private Optional<String> checkPreferredFlag(JsonNode unit, int numberFound) {
var preferred = unit.get(getStringAtIndex(abcdPreferredFlag, numberFound)).asBoolean();
if (preferred) {
return Optional.of(String.valueOf(numberFound));
}
return Optional.empty();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package eu.dissco.core.translator.terms.media;

import static eu.dissco.core.translator.TestUtils.MAPPER;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class AccessRightsTest {

private final AccessRights accessRights = new AccessRights();

@Test
void testRetrieveFromDWCA() {
// Given
var accessUriString = "https://accessuri.eu/image_1";
var unit = MAPPER.createObjectNode();
unit.put("ac:accessRights", accessUriString);

// When
var result = accessRights.retrieveFromDWCA(unit);

// Then
assertThat(result).isEqualTo(accessUriString);
}

@Test
void testGetTerm() {
// When
var result = accessRights.getTerm();

// Then
assertThat(result).isEqualTo(AccessRights.TERM);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package eu.dissco.core.translator.terms.media;

import static eu.dissco.core.translator.TestUtils.MAPPER;
import static org.assertj.core.api.Assertions.assertThat;

import org.checkerframework.checker.units.qual.C;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class Created {

private final Created created = new Created();

@Test
void testRetrieveFromDWCA() {
// Given
var accessUriString = "https://accessuri.eu/image_1";
var unit = MAPPER.createObjectNode();
unit.put("ac:accessRights", accessUriString);

// When
var result = accessRights.retrieveFromDWCA(unit);

// Then
assertThat(result).isEqualTo(accessUriString);
}

@Test
void testGetTerm() {
// When
var result = created.getTerm();

// Then
assertThat(result).isEqualTo(AccessRights.TERM);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package eu.dissco.core.translator.terms.media;

import static eu.dissco.core.translator.TestUtils.MAPPER;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class CreatorTest {

private final Created created = new Created();

@Test
void testRetrieveFromDWCA() {
// Given
var createdString = "18-09-2023";
var unit = MAPPER.createObjectNode();
unit.put("dcterms:created", createdString);

// When
var result = created.retrieveFromDWCA(unit);

// Then
assertThat(result).isEqualTo(createdString);
}

@Test
void testRetrieveFromAbcd() {
// Given
var createdString = "18-09-2023";
var unit = MAPPER.createObjectNode();
unit.put("abcd:createdDate", createdString);

// When
var result = created.retrieveFromABCD(unit);

// Then
assertThat(result).isEqualTo(createdString);
}


@Test
void testGetTerm() {
// When
var result = created.getTerm();

// Then
assertThat(result).isEqualTo(Created.TERM);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package eu.dissco.core.translator.terms.media;

import static eu.dissco.core.translator.TestUtils.MAPPER;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class DescriptionTest {

private final Creator creator = new Creator();

@Test
void testRetrieveFromDWCA() {
// Given
var creatorString = "Sam Leeflang";
var unit = MAPPER.createObjectNode();
unit.put("dcterms:creator", creatorString);

// When
var result = creator.retrieveFromDWCA(unit);

// Then
assertThat(result).isEqualTo(creatorString);
}

@Test
void testRetrieveFromAbcd() {
// Given
var createdString = "Sam Leeflang";
var unit = MAPPER.createObjectNode();
unit.put("abcd:creator", createdString);

// When
var result = creator.retrieveFromABCD(unit);

// Then
assertThat(result).isEqualTo(createdString);
}


@Test
void testGetTerm() {
// When
var result = creator.getTerm();

// Then
assertThat(result).isEqualTo(Creator.TERM);
}

}
Loading

0 comments on commit 1131089

Please sign in to comment.