Skip to content

Commit

Permalink
Throw IllegalArgumentException if addMetadata() called without values…
Browse files Browse the repository at this point in the history
…. Add unit tests to prove it works
  • Loading branch information
tdonohue committed May 24, 2024
1 parent 068bcdf commit ed918a8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,31 @@ public List<MetadataValue> addMetadata(Context context, T dso, MetadataField met

}

/**
* Add metadata value(s) to a MetadataField of a DSpace Object
* @param context current DSpace context
* @param dso DSpaceObject to modify
* @param metadataField MetadataField to add values to
* @param lang Language code to add
* @param values One or more metadata values to add
* @param authorities One or more authorities to add
* @param confidences One or more confidences to add (for authorities)
* @param placeSupplier Supplier of "place" for new metadata values
* @return List of newly added metadata values
* @throws SQLException if database error occurs
* @throws IllegalArgumentException for an empty list of values
*/
public List<MetadataValue> addMetadata(Context context, T dso, MetadataField metadataField, String lang,
List<String> values, List<String> authorities, List<Integer> confidences, Supplier<Integer> placeSupplier)
throws SQLException {

// Throw an error if we are attempting to add empty values
if (values == null || values.isEmpty()) {
throw new IllegalArgumentException("Cannot add empty values to a new metadata field " +
metadataField.toString() + " on object with uuid = " +
dso.getID().toString() + " and type = " + getTypeText(dso));
}

boolean authorityControlled = metadataAuthorityService.isAuthorityControlled(metadataField);
boolean authorityRequired = metadataAuthorityService.isAuthorityRequired(metadataField);
List<MetadataValue> newMetadata = new ArrayList<>();
Expand Down
24 changes: 24 additions & 0 deletions dspace-api/src/test/java/org/dspace/content/ItemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,17 @@ public void testAddMetadata_5args_1() throws SQLException {
assertThat("testAddMetadata_5args_1 11", dc.get(1).getValue(), equalTo(values[1]));
}

@Test(expected = IllegalArgumentException.class)
public void testAddMetadata_5args_no_values() throws Exception {
String schema = "dc";
String element = "contributor";
String qualifier = "author";
String lang = Item.ANY;
String[] values = {};
itemService.addMetadata(context, it, schema, element, qualifier, lang, Arrays.asList(values));
fail("IllegalArgumentException expected");
}

/**
* Test of addMetadata method, of class Item.
*/
Expand Down Expand Up @@ -614,6 +625,19 @@ public void testAddMetadata_7args_1_noauthority() throws SQLException {
assertThat("testAddMetadata_7args_1 15", dc.get(1).getConfidence(), equalTo(-1));
}

@Test(expected = IllegalArgumentException.class)
public void testAddMetadata_7args_no_values() throws Exception {
String schema = "dc";
String element = "contributor";
String qualifier = "author";
String lang = Item.ANY;
List<String> values = new ArrayList();
List<String> authorities = new ArrayList();
List<Integer> confidences = new ArrayList();
itemService.addMetadata(context, it, schema, element, qualifier, lang, values, authorities, confidences);
fail("IllegalArgumentException expected");
}

/**
* Test of addMetadata method, of class Item.
*/
Expand Down

0 comments on commit ed918a8

Please sign in to comment.