Skip to content

Commit

Permalink
[kbss-cvut/termit-ui#449] Throw exception when sheet contains duplica…
Browse files Browse the repository at this point in the history
…te term identifiers.
  • Loading branch information
ledsoft committed Aug 26, 2024
1 parent 8b1a07d commit 01c8be4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class LocalizedSheetImporter {

private final Map<String, Term> labelToTerm = new LinkedHashMap<>();
private final Map<URI, Term> idToTerm = new HashMap<>();
// Identifiers discovered in this sheet
private final Set<URI> sheetIdentifiers = new HashSet<>();
private List<ExcelImporter.TermRelationship> rawDataToInsert;

LocalizedSheetImporter(Services services, PrefixMap prefixMap, List<Term> existingTerms,
Expand Down Expand Up @@ -140,7 +142,13 @@ private void findTerms(Sheet sheet) {
Term term = existingTerms.size() >= i ? existingTerms.get(i - 1) : new Term();
getAttributeValue(termRow, JsonLd.ID).ifPresent(id -> {
term.setUri(resolveTermUri(id));
if (sheetIdentifiers.contains(term.getUri())) {
throw new VocabularyImportException(
"Sheet " + sheet.getSheetName() + " contains multiple terms with the same identifier: " + id,
"error.vocabulary.import.excel.duplicateIdentifier");
}
idToTerm.put(term.getUri(), term);
sheetIdentifiers.add(term.getUri());
});
final Optional<String> label = getAttributeValue(termRow, SKOS.PREF_LABEL);
if (label.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,5 +596,27 @@ void importThrowsVocabularyImportExceptionWhenSheetContainsDuplicateLabels() thr
verify(termService, never()).addRootTermToVocabulary(any(), eq(vocabulary));
}

// TODO
@Test
void importThrowsVocabularyImportExceptionWhenSheetContainsDuplicateIdentifiers() throws Exception {
vocabulary.setUri(URI.create("http://example.com"));
when(vocabularyDao.exists(vocabulary.getUri())).thenReturn(true);
when(vocabularyDao.find(vocabulary.getUri())).thenReturn(Optional.of(vocabulary));
final Workbook input = new XSSFWorkbook(Environment.loadFile("template/termit-import.xlsx"));
final Sheet sheet = input.getSheet("English");
sheet.shiftColumns(0, 12, 1);
sheet.getRow(0).createCell(0).setCellValue("Identifier");
sheet.getRow(1).createCell(0).setCellValue("http://example.com/terms/Construction");
sheet.getRow(1).getCell(1).setCellValue("Construction");
sheet.getRow(2).createCell(0).setCellValue("http://example.com/terms/Construction");
sheet.getRow(2).getCell(1).setCellValue("Another Construction");
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
input.write(bos);

final VocabularyImportException ex = assertThrows(VocabularyImportException.class,
() -> sut.importVocabulary(
new VocabularyImporter.ImportConfiguration(false, vocabulary.getUri(), prePersist),
new VocabularyImporter.ImportInput(Constants.MediaType.EXCEL, new ByteArrayInputStream(bos.toByteArray()))));
assertEquals("error.vocabulary.import.excel.duplicateIdentifier", ex.getMessageId());
verify(termService, never()).addRootTermToVocabulary(any(), eq(vocabulary));
}
}

0 comments on commit 01c8be4

Please sign in to comment.