Skip to content

Commit 6008d6f

Browse files
committed
resolve Copilot comments
1 parent b2c1f41 commit 6008d6f

3 files changed

Lines changed: 91 additions & 97 deletions

File tree

dspace-api/src/main/java/org/dspace/health/MetadataCheck.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,19 @@ private void addMessage(String validationType,
385385
* comparing to the frequency of the new message
386386
*/
387387
private void replaceMessage(Message message, StoredMessagesInfo storedMessagesInfo, int dispersionQuota) {
388-
int highestMessageFrequency = storedMessagesInfo.getHighestFrequency();
388+
Map<String, List<String>> storedMessages = storedMessagesInfo.getStoredMessages();
389+
390+
// calculate the highest frequency of messages for any short message in stored messages,
391+
String messageKeyWithHighestFrequency = Objects.requireNonNull(getMessageWithHighestCount(storedMessages));
392+
int highestMessageFrequency = storedMessages.get(messageKeyWithHighestFrequency).size();
393+
394+
// int highestMessageFrequency = storedMessagesInfo.getHighestFrequency();
389395
if (highestMessageFrequency <= 1) {
390396
// no replacement, as there are no messages with the frequency higher than 1, so the replacement
391397
// of any message would not increase the diversity of messages in stored messages
392398
return;
393399
}
394400
String messageKey = message.getMessageKey();
395-
Map<String, List<String>> storedMessages = storedMessagesInfo.getStoredMessages();
396401
List<String> storedMessagesForMessageKey = storedMessages.get(messageKey);
397402

398403
if (storedMessagesForMessageKey != null &&
@@ -402,11 +407,10 @@ private void replaceMessage(Message message, StoredMessagesInfo storedMessagesIn
402407
return;
403408
}
404409

405-
// recalculate the highest frequency of messages for any short message in storedmessages,
410+
// recalculate the highest frequency of messages for any short message in stored messages,
406411
// because it can be changed after each replacement
407-
String messageKeyWithHighestFrequency = Objects.requireNonNull(getMessageWithHighestCount(storedMessages));
412+
messageKeyWithHighestFrequency = Objects.requireNonNull(getMessageWithHighestCount(storedMessages));
408413
highestMessageFrequency = storedMessages.get(messageKeyWithHighestFrequency).size();
409-
storedMessagesInfo.setHighestFrequency(highestMessageFrequency);
410414

411415
if (highestMessageFrequency <= 1) {
412416
// no replacement, as there are no messages with the frequency higher than 1 anymore
@@ -455,27 +459,17 @@ private static String getMessageWithHighestCount(Map<String, List<String>> store
455459
*/
456460
private static class StoredMessagesInfo {
457461
private int count;
458-
private int highestFrequency;
459462
private final Map<String, List<String>> storedMessages;
460463

461464
StoredMessagesInfo() {
462465
this.count = 0;
463-
this.highestFrequency = Integer.MAX_VALUE;
464466
storedMessages = new TreeMap<>();
465467
}
466468

467469
public int getCount() {
468470
return count;
469471
}
470472

471-
public int getHighestFrequency() {
472-
return highestFrequency;
473-
}
474-
475-
public void setHighestFrequency(int highestFrequency) {
476-
this.highestFrequency = highestFrequency;
477-
}
478-
479473
public Map<String, List<String>> getStoredMessages() {
480474
return storedMessages;
481475
}

dspace-api/src/main/resources/metadata-check-patterns.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"^There are bitstreams but incomplete rights metadata."
2626
],
2727
"dc.description": [
28-
"^contains suspicious dc.description.uri metadata"
28+
"^contains suspicious [dc.description.uri] metadata"
2929
],
3030
"local.branding": [
3131
"^local.branding "

dspace-api/src/test/java/org/dspace/scripts/HealthReportIT.java

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.dspace.content.Collection;
4141
import org.dspace.content.Community;
4242
import org.dspace.content.Item;
43-
import org.dspace.content.MetadataValue;
4443
import org.dspace.content.ReportResult;
4544
import org.dspace.content.clarin.ClarinLicense;
4645
import org.dspace.content.clarin.ClarinLicenseLabel;
@@ -399,108 +398,109 @@ public void testMetadataCheckWithRestrictedReportSize() throws Exception {
399398
configurationService.setProperty("healthcheck.metadata.max-errors-to-show", 8);
400399
configurationService.setProperty("healthcheck.metadata.error-dispersion-quota", 1);
401400

402-
Community community = CommunityBuilder.createCommunity(context)
403-
.withName("Community")
404-
.build();
405-
406-
Collection collection = CollectionBuilder.createCollection(context, community)
407-
.withName("Collection")
408-
.withSubmitterGroup(eperson)
409-
.build();
410-
411-
Item item1 = ItemBuilder.createItem(context, collection)
412-
.withTitle("Test item 1")
413-
.withType("corpus")
414-
.withSubject("Test subject")
415-
.withMetadata("local", "branding", null, "Community")
416-
.build();
417-
418-
Item item2 = ItemBuilder.createItem(context, collection)
419-
.withTitle("Test item 2")
420-
.withType("toolService")
421-
.withSubject("Test subject")
422-
.withMetadata("local", "branding", null, "Community")
423-
.withMetadata("dc", "relation", "replaces", findItemUri(item1))
424-
.build();
401+
try {
402+
Community community = CommunityBuilder.createCommunity(context)
403+
.withName("Community")
404+
.build();
425405

426-
ItemBuilder.createItem(context, collection)
427-
.withTitle("Test item 3")
428-
.withType("toolService")
429-
.withSubject("Test subject")
430-
.withMetadata("local", "branding", null, "Community")
431-
.withMetadata("dc", "relation", "isreplacedby", findItemUri(item2))
432-
.build();
406+
Collection collection = CollectionBuilder.createCollection(context, community)
407+
.withName("Collection")
408+
.withSubmitterGroup(eperson)
409+
.build();
433410

434-
// create 4 items with missing title
435-
for (int i = 0; i < 4; i++) {
436-
ItemBuilder.createItem(context, collection)
437-
.withType("toolService")
411+
Item item1 = ItemBuilder.createItem(context, collection)
412+
.withTitle("Test item 1")
413+
.withType("corpus")
438414
.withSubject("Test subject")
439415
.withMetadata("local", "branding", null, "Community")
440416
.build();
441-
}
442417

443-
// create 4 items with missing type
444-
for (int i = 4; i < 8; i++) {
445-
ItemBuilder.createItem(context, collection)
446-
.withTitle("Test Item " + i)
418+
Item item2 = ItemBuilder.createItem(context, collection)
419+
.withTitle("Test item 2")
420+
.withType("toolService")
447421
.withSubject("Test subject")
448422
.withMetadata("local", "branding", null, "Community")
423+
.withMetadata("dc", "relation", "replaces", findItemUri(item1))
449424
.build();
450-
}
451425

452-
// create 4 items with duplicate type
453-
for (int i = 8; i < 12; i++) {
454426
ItemBuilder.createItem(context, collection)
455-
.withTitle("Test Item " + i)
427+
.withTitle("Test item 3")
456428
.withType("toolService")
457-
.withType("corpus")
458429
.withSubject("Test subject")
459430
.withMetadata("local", "branding", null, "Community")
431+
.withMetadata("dc", "relation", "isreplacedby", findItemUri(item2))
460432
.build();
461-
}
462-
463-
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
464433

465-
// with "health-report -c 5", only Metadata check is running
466-
String[] args = new String[]{"health-report", "-c", "5"};
467-
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
468-
469-
assertThat(testDSpaceRunnableHandler.getErrorMessages(), empty());
470-
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
434+
// create 4 items with missing title
435+
for (int i = 0; i < 4; i++) {
436+
ItemBuilder.createItem(context, collection)
437+
.withType("toolService")
438+
.withSubject("Test subject")
439+
.withMetadata("local", "branding", null, "Community")
440+
.build();
441+
}
471442

472-
assertThat(messages, hasSize(1));
473-
assertThat(messages.get(0), containsString("dc.relation issues: " + " ".repeat(15) + "2"));
474-
assertThat(messages.get(0), containsString("dc.title issues: " + " ".repeat(15) + "4"));
475-
assertThat(messages.get(0), containsString("dc.type issues: " + " ".repeat(15) + "4"));
476-
assertThat(messages.get(0), containsString("duplicate value issues:" + " ".repeat(13) + "4"));
477-
assertThat(messages.get(0), containsString("Error count total: " + " ".repeat(14) + "14"));
443+
// create 4 items with missing type
444+
for (int i = 4; i < 8; i++) {
445+
ItemBuilder.createItem(context, collection)
446+
.withTitle("Test Item " + i)
447+
.withSubject("Test subject")
448+
.withMetadata("local", "branding", null, "Community")
449+
.build();
450+
}
478451

479-
assertThat(messages.get(0), containsString("Errors:"));
452+
// create 4 items with duplicate type
453+
for (int i = 8; i < 12; i++) {
454+
ItemBuilder.createItem(context, collection)
455+
.withTitle("Test Item " + i)
456+
.withType("toolService")
457+
.withType("corpus")
458+
.withSubject("Test subject")
459+
.withMetadata("local", "branding", null, "Community")
460+
.build();
461+
}
480462

481-
// check if dc.type error is present exactly 2 times
482-
assertThat(StringUtils.countMatches(messages.get(0), "Does not have dc.type metadata"), is(2));
483-
// check if dc.title error is present exactly 2 times
484-
assertThat(StringUtils.countMatches(messages.get(0), "Item has no dc.title metadata"), is(2));
485-
// check id duplicate value error is present exactly 2 times
486-
assertThat(StringUtils.countMatches(messages.get(0), "value [dc.type] is present multiple times"), is(2));
487-
488-
// check if all dc.relation errors are present
489-
assertThat(StringUtils.countMatches(messages.get(0), "does not refer back via dc.relation.replaces"), is(1));
490-
assertThat(
491-
StringUtils.countMatches(messages.get(0), "does not refer back via dc.relation.isreplacedby"), is(1));
492-
assertThat(messages.get(0), containsString("and more..."));
493-
494-
configurationService.setProperty("healthcheck.metadata.max-errors-to-show", null);
495-
configurationService.setProperty("healthcheck.metadata.error-dispersion-quota", null);
463+
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
464+
465+
// with "health-report -c 5", only Metadata check is running
466+
String[] args = new String[]{"health-report", "-c", "5"};
467+
ScriptLauncher.handleScript(args,
468+
ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
469+
470+
assertThat(testDSpaceRunnableHandler.getErrorMessages(), empty());
471+
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
472+
473+
assertThat(messages, hasSize(1));
474+
assertThat(messages.get(0), containsString("dc.relation issues: " + " ".repeat(15) + "2"));
475+
assertThat(messages.get(0), containsString("dc.title issues: " + " ".repeat(15) + "4"));
476+
assertThat(messages.get(0), containsString("dc.type issues: " + " ".repeat(15) + "4"));
477+
assertThat(messages.get(0), containsString("duplicate value issues:" + " ".repeat(13) + "4"));
478+
assertThat(messages.get(0), containsString("Error count total: " + " ".repeat(14) + "14"));
479+
480+
assertThat(messages.get(0), containsString("Errors:"));
481+
482+
// check if dc.type error is present exactly 2 times
483+
assertThat(StringUtils.countMatches(messages.get(0), "Does not have dc.type metadata"), is(2));
484+
// check if dc.title error is present exactly 2 times
485+
assertThat(StringUtils.countMatches(messages.get(0), "Item has no dc.title metadata"), is(2));
486+
// check if duplicate value error is present exactly 2 times
487+
assertThat(StringUtils.countMatches(messages.get(0), "value [dc.type] is present multiple times"), is(2));
488+
489+
// check if all dc.relation errors are present
490+
assertThat(StringUtils.countMatches(
491+
messages.get(0), "does not refer back via dc.relation.replaces"), is(1));
492+
assertThat(StringUtils.countMatches(
493+
messages.get(0), "does not refer back via dc.relation.isreplacedby"), is(1));
494+
assertThat(messages.get(0), containsString("and more..."));
495+
} finally {
496+
configurationService.setProperty("healthcheck.metadata.max-errors-to-show", null);
497+
configurationService.setProperty("healthcheck.metadata.error-dispersion-quota", null);
498+
}
496499
}
497500

498501
private String findItemUri(Item item) {
499-
return item.getMetadata().stream()
500-
.filter(metadataValue -> "dc_identifier_uri".equals(metadataValue.getMetadataField().toString()))
501-
.findFirst()
502-
.map(MetadataValue::getValue)
503-
.orElse(null);
502+
return ContentServiceFactory.getInstance().getItemService()
503+
.getMetadataFirstValue(item, "dc", "identifier", "uri", Item.ANY);
504504
}
505505

506506
ReportResult findLastReportResult(List<ReportResult> reportResults) {

0 commit comments

Comments
 (0)