From 01c8be45b17364043a92094273093367c0c089cf Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Mon, 26 Aug 2024 08:35:51 +0200 Subject: [PATCH] [kbss-cvut/termit-ui#449] Throw exception when sheet contains duplicate term identifiers. --- .../excel/LocalizedSheetImporter.java | 8 +++++++ .../importer/excel/ExcelImporterTest.java | 24 ++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/cz/cvut/kbss/termit/service/importer/excel/LocalizedSheetImporter.java b/src/main/java/cz/cvut/kbss/termit/service/importer/excel/LocalizedSheetImporter.java index ecc4d49c2..30822660f 100644 --- a/src/main/java/cz/cvut/kbss/termit/service/importer/excel/LocalizedSheetImporter.java +++ b/src/main/java/cz/cvut/kbss/termit/service/importer/excel/LocalizedSheetImporter.java @@ -64,6 +64,8 @@ class LocalizedSheetImporter { private final Map labelToTerm = new LinkedHashMap<>(); private final Map idToTerm = new HashMap<>(); + // Identifiers discovered in this sheet + private final Set sheetIdentifiers = new HashSet<>(); private List rawDataToInsert; LocalizedSheetImporter(Services services, PrefixMap prefixMap, List existingTerms, @@ -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 label = getAttributeValue(termRow, SKOS.PREF_LABEL); if (label.isPresent()) { diff --git a/src/test/java/cz/cvut/kbss/termit/service/importer/excel/ExcelImporterTest.java b/src/test/java/cz/cvut/kbss/termit/service/importer/excel/ExcelImporterTest.java index dca95ce42..81a4adc35 100644 --- a/src/test/java/cz/cvut/kbss/termit/service/importer/excel/ExcelImporterTest.java +++ b/src/test/java/cz/cvut/kbss/termit/service/importer/excel/ExcelImporterTest.java @@ -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)); + } }