forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schema): enable ControlledVocabularyValue data-binding from TSV I…
…QSS#8085 - Add headers enum like with MetadataBlock and DatasetFieldType - Add a placeholder for alternate values that need references to the dataset field this CVV is a part of - Make the alternatives come from a column with a header (This is undocumented behaviour in the docs currently!) - Proper validation as with the other data bindings - Includes lot's of tests to make sure we notice when it breaks.
- Loading branch information
1 parent
16bf03c
commit 336825d
Showing
3 changed files
with
250 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
...est/java/edu/harvard/iq/dataverse/util/metadata/ControlledVocabularyValueParsingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package edu.harvard.iq.dataverse.util.metadata; | ||
|
||
import com.univocity.parsers.common.DataValidationException; | ||
import com.univocity.parsers.common.processor.BeanListProcessor; | ||
import com.univocity.parsers.tsv.TsvParser; | ||
import com.univocity.parsers.tsv.TsvParserSettings; | ||
import edu.harvard.iq.dataverse.ControlledVocabularyValue; | ||
import edu.harvard.iq.dataverse.DatasetFieldType; | ||
import edu.harvard.iq.dataverse.MetadataBlock; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EmptySource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import java.io.StringReader; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class ControlledVocabularyValueTest { | ||
|
||
static BeanListProcessor<ControlledVocabularyValue> controlledVocabularyValueProcessor = new BeanListProcessor<>(ControlledVocabularyValue.class); | ||
static TsvParser parser; | ||
static TsvParserSettings settings = new TsvParserSettings(); | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
settings.setProcessor(controlledVocabularyValueProcessor); | ||
settings.setHeaderExtractionEnabled(true); | ||
// TODO: replace this char with a global constant (introduced when creating the parsing bean) | ||
settings.getFormat().setComment('\''); | ||
parser = new TsvParser(settings); | ||
} | ||
|
||
@ParameterizedTest | ||
@EmptySource | ||
@ValueSource(strings = {" "}) | ||
public void parseInvalidValue(String value) { | ||
// given | ||
StringReader subjectUnderTest = new StringReader(generateCvvTSV(Map.of( | ||
ControlledVocabularyValue.Headers.DATASET_FIELD, "test", | ||
ControlledVocabularyValue.Headers.VALUE, value, | ||
ControlledVocabularyValue.Headers.DISPLAY_ORDER, "0"))); | ||
// when & then | ||
assertThrows(DataValidationException.class, () -> parser.parse(subjectUnderTest)); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"https://www^^", "doi:1234^", "hello my name", "hello!", "hello#"}) | ||
public void parseInvalidIdentifier(String identifier) { | ||
// given | ||
StringReader subjectUnderTest = new StringReader(generateCvvTSV(Map.of( | ||
ControlledVocabularyValue.Headers.DATASET_FIELD, "test", | ||
ControlledVocabularyValue.Headers.VALUE, "test", | ||
ControlledVocabularyValue.Headers.DISPLAY_ORDER, "0", | ||
ControlledVocabularyValue.Headers.IDENTIFIER, identifier))); | ||
// when & then | ||
assertThrows(DataValidationException.class, () -> parser.parse(subjectUnderTest)); | ||
} | ||
|
||
@ParameterizedTest | ||
@EmptySource | ||
@ValueSource(strings = {"https://skosmos/foo#bar", "doi:1234", "hello_my-name", "foo+bar"}) | ||
public void parseValidIdentifier(String identifier) { | ||
// given | ||
StringReader subjectUnderTest = new StringReader(generateCvvTSV(Map.of( | ||
ControlledVocabularyValue.Headers.DATASET_FIELD, "test", | ||
ControlledVocabularyValue.Headers.VALUE, "test", | ||
ControlledVocabularyValue.Headers.DISPLAY_ORDER, "0", | ||
ControlledVocabularyValue.Headers.IDENTIFIER, identifier))); | ||
// when | ||
parser.parse(subjectUnderTest); | ||
// then | ||
assertEquals(1, controlledVocabularyValueProcessor.getBeans().size()); | ||
if (!identifier.isEmpty()) { | ||
assertEquals(identifier, controlledVocabularyValueProcessor.getBeans().get(0).getIdentifier()); | ||
} else { | ||
assertNull(controlledVocabularyValueProcessor.getBeans().get(0).getIdentifier()); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"-1", "-100", "0.00", "abc", "_foobar!"}) | ||
public void parseInvalidDisplayOrder(String displayOrder) { | ||
// given | ||
StringReader subjectUnderTest = new StringReader(generateCvvTSV(Map.of( | ||
ControlledVocabularyValue.Headers.DATASET_FIELD, "test", | ||
ControlledVocabularyValue.Headers.VALUE, "test", | ||
ControlledVocabularyValue.Headers.DISPLAY_ORDER, displayOrder))); | ||
// when & then | ||
assertThrows(DataValidationException.class, () -> parser.parse(subjectUnderTest)); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {".foobar", "!foo", "foo!", "_foo_", "-foo-foo", "foo.", "_foo.foo_", "1foo", ".bar"}) | ||
public void parseInvalidDatasetFieldTypeName(String fieldName) { | ||
// given | ||
StringReader subjectUnderTest = new StringReader(generateCvvTSV(Map.of( | ||
ControlledVocabularyValue.Headers.DATASET_FIELD, fieldName, | ||
ControlledVocabularyValue.Headers.VALUE, "test", | ||
ControlledVocabularyValue.Headers.DISPLAY_ORDER, "0"))); | ||
// when & then | ||
assertThrows(DataValidationException.class, () -> parser.parse(subjectUnderTest)); | ||
} | ||
|
||
@Test | ||
public void parseAlternateValues() { | ||
// given | ||
String t1 = "test1"; | ||
String t2 = "test2"; | ||
String tsv = generateCvvTSV(Map.of( | ||
ControlledVocabularyValue.Headers.DATASET_FIELD, "test", | ||
ControlledVocabularyValue.Headers.VALUE, "test", | ||
ControlledVocabularyValue.Headers.DISPLAY_ORDER, "0"), | ||
List.of(t1, t2)); | ||
StringReader subjectUnderTest = new StringReader(tsv); | ||
// when | ||
parser.parse(subjectUnderTest); | ||
// then | ||
assertEquals(1, controlledVocabularyValueProcessor.getBeans().size()); | ||
|
||
ControlledVocabularyValue result = controlledVocabularyValueProcessor.getBeans().get(0); | ||
|
||
assertFalse(result.getControlledVocabAlternates().isEmpty()); | ||
assertTrue(result.getControlledVocabAlternates().stream().allMatch(a -> a instanceof Placeholder.ControlledVocabAlternate)); | ||
|
||
assertEquals(2, result.getControlledVocabAlternates().size()); | ||
assertTrue(result.getControlledVocabAlternates().stream().anyMatch(a -> t1.equals(a.getStrValue()))); | ||
assertTrue(result.getControlledVocabAlternates().stream().anyMatch(a -> t2.equals(a.getStrValue()))); | ||
} | ||
|
||
private static final String header = "#controlledVocabulary\t" + String.join("\t", ControlledVocabularyValue.Headers.keys()); | ||
|
||
/** | ||
* This method simply inserts all the values from the map into a line, combined by \t and adds a "header" line before it. | ||
* It does this based on the {@link MetadataBlock.Headers} enum value order, which is the same as in the TSV definition. | ||
* Nonpresent values will be inserted as blank strings. | ||
* | ||
* @param values | ||
* @return | ||
*/ | ||
public static String generateCvvTSV(Map<ControlledVocabularyValue.Headers, String> values) { | ||
List<String> fieldValues = Arrays.stream(ControlledVocabularyValue.Headers.values()) | ||
.map(k -> values.getOrDefault(k, "")) | ||
.collect(Collectors.toList()); | ||
return header + settings.getFormat().getLineSeparatorString() + "\t" + String.join("\t", fieldValues); | ||
} | ||
|
||
public static String generateCvvTSV(Map<ControlledVocabularyValue.Headers, String> values, List<String> altValues) { | ||
List<String> fieldValues = Arrays.stream(ControlledVocabularyValue.Headers.values()) | ||
.map(k -> values.getOrDefault(k, "")) | ||
.collect(Collectors.toList()); | ||
|
||
String headerWithAlts = header + ("\t"+ControlledVocabularyValue.Headers.Constants.ALT_VALUES).repeat(altValues.size()-1); | ||
|
||
return headerWithAlts + settings.getFormat().getLineSeparatorString() + | ||
"\t" + String.join("\t", fieldValues) + String.join("\t", altValues); | ||
} | ||
} |