Skip to content

Commit

Permalink
Started implementing tests for the sword examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
janvanmansum committed Dec 9, 2024
1 parent 28d6484 commit 0efb264
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 6 deletions.
22 changes: 21 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
<main-class>nl.knaw.dans.dvingest.DdDataverseIngestApplication</main-class>
<dd-dataverse-ingest-api.version>0.2.0</dd-dataverse-ingest-api.version>
<dd-validate-dans-bag-api.version>1.0.0</dd-validate-dans-bag-api.version>
<dd-dans-sword2-examples.version>1.0.0</dd-dans-sword2-examples.version>
</properties>

<scm>
<developerConnection>scm:git:ssh://github.com/DANS-KNAW/dd-dataverse-ingest</developerConnection>
<developerConnection>scm:git:ssh://github.com/DANS-KNAW/${project.artifactId}</developerConnection>
<tag>HEAD</tag>
</scm>

Expand Down Expand Up @@ -216,6 +217,7 @@
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>compile-dependencies</id>
<phase>initialize</phase>
<goals>
<goal>unpack</goal>
Expand All @@ -237,6 +239,24 @@
</artifactItems>
</configuration>
</execution>
<execution>
<id>test-dependencies</id>
<phase>generate-test-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>nl.knaw.dans</groupId>
<artifactId>dd-dans-sword2-examples</artifactId>
<version>${dd-dans-sword2-examples.version}</version>
<outputDirectory>${project.build.directory}/test</outputDirectory>
<includes>example-bags/**</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,37 @@
import nl.knaw.dans.dvingest.core.dansbag.xml.XmlReader;
import nl.knaw.dans.dvingest.core.dansbag.xml.XmlReaderImpl;
import nl.knaw.dans.dvingest.core.service.DataverseService;
import nl.knaw.dans.lib.dataverse.model.dataset.CompoundMultiValueField;
import nl.knaw.dans.lib.dataverse.model.dataset.ControlledMultiValueField;
import nl.knaw.dans.lib.dataverse.model.dataset.ControlledSingleValueField;
import nl.knaw.dans.lib.dataverse.model.dataset.License;
import nl.knaw.dans.lib.dataverse.model.dataset.MetadataField;
import nl.knaw.dans.lib.dataverse.model.dataset.PrimitiveMultiValueField;
import nl.knaw.dans.lib.dataverse.model.dataset.PrimitiveSingleValueField;
import nl.knaw.dans.lib.util.MappingLoader;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mockito;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

public abstract class DansConversionFixture extends TestDirFixture {
protected final DataverseService dataverseServiceMock = Mockito.mock(DataverseService.class);
Expand Down Expand Up @@ -81,4 +97,99 @@ private Map<URI, License> licenses(String... uri) {
}
return licenses;
}

protected Path createValidDeposit(String validExample, String depositDir) throws Exception {
var deposit = testDir.resolve(depositDir);
Files.createDirectory(deposit);
// Create deposit.properties
var props = new Properties();
props.setProperty("state.label", "SUBMITTED");
props.setProperty("state.description", "Deposit is submitted");
props.setProperty("deposit.origin", "SWORD");
props.setProperty("creation.timestamp", DateTimeFormatter.ISO_INSTANT
.withZone(ZoneId.of("UTC"))
.format(Instant.now()));
props.setProperty("depositor.userId", "jdoe");
try (var out = Files.newBufferedWriter(deposit.resolve("deposit.properties"))) {
props.store(out, null);
}
FileUtils.copyDirectoryToDirectory(Paths.get("target/test/example-bags/valid").resolve(validExample).toFile(), deposit.toFile());
return deposit;
}

protected void assertPrimitiveSinglevalueFieldContainsValue(List<MetadataField> fields, String typeName, String value) {
assertThat(fields)
.filteredOn(f -> typeName.equals(f.getTypeName()))
.map(f -> (PrimitiveSingleValueField) f).extracting(PrimitiveSingleValueField::getValue)
.containsExactly(value);
}

protected void assertPrimitiveMultiValueFieldContainsValues(List<MetadataField> fields, String typeName, String... values) {
assertThat(fields)
.filteredOn(f -> typeName.equals(f.getTypeName()))
.map(f -> (PrimitiveMultiValueField) f).extracting(PrimitiveMultiValueField::getValue)
.containsExactly(List.of(values));
}

protected void assertControlledSingleValueFieldContainsValue(List<MetadataField> fields, String typeName, String value) {
assertThat(fields)
.filteredOn(f -> typeName.equals(f.getTypeName()))
.map(f -> (ControlledSingleValueField) f).extracting(ControlledSingleValueField::getValue)
.containsExactly(value);
}

protected void assertControlledMultiValueFieldContainsValues(List<MetadataField> fields, String typeName, String... values) {
assertThat(fields)
.filteredOn(f -> typeName.equals(f.getTypeName()))
.map(f -> (ControlledMultiValueField) f).extracting(ControlledMultiValueField::getValue)
.containsExactly(List.of(values));
}

protected void assertCompoundMultiValueFieldContainsValues(List<MetadataField> fields, String typeName, List<Map<String, String>> expectedValues) {
var filteredFields = fields.stream()
.filter(f -> typeName.equals(f.getTypeName()))
.map(f -> (CompoundMultiValueField) f)
.toList();

assertThat(filteredFields).as("Field not found: " + typeName).isNotEmpty();
assertThat(filteredFields).as("Field appearing more than once: " + typeName).hasSize(1);

var actualValues = filteredFields.get(0).getValue();
assertThat(actualValues).as("Different number of actual and expected values: " + actualValues.size() + " vs " + expectedValues.size()).hasSize(expectedValues.size());

List<Map<String, String>> actualValuesList = new ArrayList<>();
for (var actualValue : actualValues) {
var actualValueMap = actualValue.entrySet().stream()
.map(e -> Map.entry(e.getKey(), e.getValue().getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
actualValuesList.add(actualValueMap);
}

assertThat(actualValuesList).containsExactlyInAnyOrderElementsOf(expectedValues);

}

@SafeVarargs
protected final void assertCompoundMultiValueFieldContainsValues2(List<MetadataField> fields, String typeName, Map<String, String>... expectedValues) {
var filteredFields = fields.stream()
.filter(f -> typeName.equals(f.getTypeName()))
.map(f -> (CompoundMultiValueField) f)
.toList();

assertThat(filteredFields).as("Field not found: " + typeName).isNotEmpty();
assertThat(filteredFields).as("Field appearing more than once: " + typeName).hasSize(1);

var actualValues = filteredFields.get(0).getValue();
assertThat(actualValues).as("Different number of actual and expected values: " + actualValues.size() + " vs " + expectedValues.length).hasSize(expectedValues.length);

List<Map<String, String>> actualValuesList = new ArrayList<>();
for (var actualValue : actualValues) {
var actualValueMap = actualValue.entrySet().stream()
.map(e -> Map.entry(e.getKey(), e.getValue().getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
actualValuesList.add(actualValueMap);
}

assertThat(actualValuesList).containsExactlyInAnyOrderElementsOf(List.of(expectedValues));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

import nl.knaw.dans.dvingest.core.service.YamlService;
import nl.knaw.dans.dvingest.core.service.YamlServiceImpl;
import nl.knaw.dans.lib.dataverse.model.dataset.Dataset;
import nl.knaw.dans.lib.dataverse.model.user.AuthenticatedUser;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -34,20 +35,36 @@ public class DansDepositConverterTest extends DansConversionFixture {
@Test
public void run_converts_dans_sword_all_mappings_example_to_dataverse_ingest_deposit() throws Exception {
// Given
FileUtils.copyDirectoryToDirectory(Paths.get("src/test/resources/unit-test/d0919038-9866-49e8-986a-bcef54ae7566").toFile(), testDir.toFile());
var depositDir = testDir.resolve("d0919038-9866-49e8-986a-bcef54ae7566");
var deposit = dansBagDepositReader.readDeposit(depositDir);
var depositDir = createValidDeposit("all-mappings", "00000000-0000-0000-0000-000000000001");
var authenticatedUser = new AuthenticatedUser();
authenticatedUser.setFirstName("John");
authenticatedUser.setLastName("Doe");
authenticatedUser.setEmail("[email protected]");
authenticatedUser.setDisplayName("John Doe");
Mockito.when(dataverseServiceMock.getUserById(Mockito.anyString())).thenReturn(Optional.of(authenticatedUser));
var deposit = dansBagDepositReader.readDeposit(depositDir);

// When
new DansDepositConverter(deposit, null, mappingService, yamlService).run();

// Then
assertThat(deposit.getBagDir().resolve("dataset.yml")).exists();
var datasetYml = yamlService.readYaml(deposit.getBagDir().resolve("dataset.yml"), Dataset.class);
var citationBlockFields = datasetYml.getDatasetVersion().getMetadataBlocks().get("citation").getFields();
// Find the metadata field with property typeName = "title"
assertPrimitiveSinglevalueFieldContainsValue(citationBlockFields, "title", "A bag containing examples for each mapping rule");
assertPrimitiveMultiValueFieldContainsValues(citationBlockFields, "alternativeTitle", "DCTERMS title 1");
assertCompoundMultiValueFieldContainsValues2(citationBlockFields, "datasetContact", Map.of(
"datasetContactName", "John Doe",
"datasetContactEmail", "[email protected]"
));
assertCompoundMultiValueFieldContainsValues2(citationBlockFields, "otherId",
Map.of("otherIdAgency", "", "otherIdValue", "DCTERMS_ID001"),
Map.of("otherIdAgency", "", "otherIdValue", "DC_ID002"),
Map.of("otherIdAgency", "", "otherIdValue", "DCTERMS_ID003"),
Map.of("otherIdAgency", "TESTPREFIX", "otherIdValue", "1234"));


}

}

0 comments on commit 0efb264

Please sign in to comment.