Skip to content

Commit

Permalink
Fix bug in MetadataImport where it could call addMetadata() with empt…
Browse files Browse the repository at this point in the history
…y values. Minor refactors to MetadataImportIT to make findItemByName more efficient.
  • Loading branch information
tdonohue committed May 24, 2024
1 parent 25f722e commit f8ac8ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,10 @@ protected void compareAndUpdate(Context c, Item item, String[] fromCSV, boolean
addRelationships(c, item, element, values);
} else {
itemService.clearMetadata(c, item, schema, element, qualifier, language);
itemService.addMetadata(c, item, schema, element, qualifier,
language, values, authorities, confidences);
if (!values.isEmpty()) {
itemService.addMetadata(c, item, schema, element, qualifier,
language, values, authorities, confidences);
}
itemService.update(c, item);
}
}
Expand Down Expand Up @@ -1121,8 +1123,8 @@ protected void add(Context c, String[] fromCSV, String md, BulkEditChange change
.getAuthoritySeparator() + dcv.getConfidence();
}

// Add it
if ((value != null) && (!"".equals(value))) {
// Add it, if value is not blank
if (value != null && StringUtils.isNotBlank(value)) {
changes.registerAdd(dcv);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.cli.ParseException;
Expand Down Expand Up @@ -218,9 +218,10 @@ public void personMetadataImportTest() throws Exception {

@Test
public void metadataImportRemovingValueTest() throws Exception {

context.turnOffAuthorisationSystem();
Item item = ItemBuilder.createItem(context,personCollection).withAuthor("TestAuthorToRemove").withTitle("title")
String itemTitle = "Testing removing author";
Item item = ItemBuilder.createItem(context,personCollection).withAuthor("TestAuthorToRemove")
.withTitle(itemTitle)
.build();
context.restoreAuthSystemState();

Expand All @@ -232,19 +233,21 @@ public void metadataImportRemovingValueTest() throws Exception {
String[] csv = {"id,collection,dc.title,dc.contributor.author[*]",
item.getID().toString() + "," + personCollection.getHandle() + "," + item.getName() + ","};
performImportScript(csv);
item = findItemByName("title");
item = findItemByName(itemTitle);
assertEquals(0, itemService.getMetadata(item, "dc", "contributor", "author", Item.ANY).size());
}

private Item findItemByName(String name) throws SQLException {
Item importedItem = null;
List<Item> allItems = IteratorUtils.toList(itemService.findAll(context));
for (Item item : allItems) {
if (item.getName().equals(name)) {
importedItem = item;
}
private Item findItemByName(String name) throws Exception {
List<Item> items =
IteratorUtils.toList(itemService.findByMetadataField(context, "dc", "title", null, name));

if (items != null && !items.isEmpty()) {
// Just return first matching Item. Tests should ensure name/title is unique.
return items.get(0);
} else {
fail("Could not find expected Item with dc.title = '" + name + "'");
return null;
}
return importedItem;
}

public void performImportScript(String[] csv) throws Exception {
Expand Down

0 comments on commit f8ac8ed

Please sign in to comment.