Skip to content

Commit

Permalink
Added validation to prevent page duplication when adding
Browse files Browse the repository at this point in the history
  • Loading branch information
lumber1000 committed Sep 30, 2024
1 parent a794500 commit fab3f51
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,19 @@ protected void doAddBook(BookToAdd book, BookId bookId) throws IOException

@Override
protected void doAddPages(BookId bookId, List<PageInfo> pages, PageInfo lastPage) throws IOException {
String bookName = bookId.getName();
PageNameOperator pageNameOperator = operators.getPageNameOperator();

List<PageEntity> pagesToAdd = pages.stream()
.map(page -> new PageEntity(bookId.getName(), page.getName(), page.getStarted(), page.getComment(), page.getEnded(), page.getUpdated()))
.map(page -> new PageEntity(bookName, page.getName(), page.getStarted(), page.getComment(), page.getEnded(), page.getUpdated()))
.collect(Collectors.toList());

for (PageEntity page: pagesToAdd) {
if (pageNameOperator.get(bookName, page.getName()).one() != null) {
throw new IOException("Query to insert page '" + page.getName() + "' to book '" + bookName + "' failed. Page already exists");
}
}

PageEntity lastPageEntity = lastPage != null ? new PageEntity(lastPage) : null;

try {
Expand All @@ -308,8 +317,7 @@ protected void doAddPages(BookId bookId, List<PageInfo> pages, PageInfo lastPage
}

@Override
protected Collection<PageInfo> doLoadPages(BookId bookId) throws CradleStorageException
{
protected Collection<PageInfo> doLoadPages(BookId bookId) throws CradleStorageException {
return bookCache.loadPageInfo(bookId, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,34 @@ public void testAddTheSecondPageBeforeThreshold() throws CradleStorageException,
}
}

@Test(
expectedExceptions = IOException.class,
expectedExceptionsMessageRegExp = "Query to insert page 'page1' to book 'testaddexistedpage' failed. Page already exists"
)
public void testAddExistedPage() throws CradleStorageException, IOException {
try {
// Preparation
BookId testBookId = new BookId("testAddExistedPage");
Instant testBookStart = Instant.now();
// Adding page before book start time looks strange but the current behavior doesn't affect anybody
PageId pageId1 = new PageId(testBookId, testBookStart, "page1");
storage.addBook(new BookToAdd(testBookId.getName(), testBookStart));
storage.addPage(testBookId, pageId1.getName(), pageId1.getStart(), null);
assertEquals(storage.getAllPages(testBookId).size(), 1);

// Test (adding page with the same name)
PageId pageId2 = new PageId(
testBookId,
Instant.now().plus(BOOK_REFRESH_INTERVAL_MILLIS * 5, MILLIS),
"page1"
);
storage.addPage(testBookId, pageId2.getName(), pageId2.getStart(), null);
} catch (IOException | CradleStorageException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testRenamePage() throws CradleStorageException, IOException {
// Preparation
Expand Down

0 comments on commit fab3f51

Please sign in to comment.