From e3e9992279a72ea7928b3d04456f19fefdc3907d Mon Sep 17 00:00:00 2001 From: mukul-tyagi08 Date: Fri, 11 Oct 2024 11:32:31 +0530 Subject: [PATCH] fix: delete page revisions when api is deleted --- .../api/PageRevisionRepository.java | 2 ++ .../JdbcPageRevisionRepository.java | 17 +++++++++++++ .../MongoPageRevisionRepository.java | 10 ++++++++ .../revision/PageRevisionMongoRepository.java | 3 +++ .../PageRevisionRepositoryTest.java | 24 +++++++++++++++++++ .../rest/api/service/PageRevisionService.java | 3 +++ .../service/impl/PageRevisionServiceImpl.java | 5 ++++ .../api/service/impl/PageServiceImpl.java | 3 +++ .../service/impl/PageRevisionServiceTest.java | 13 ++++++++++ 9 files changed, 80 insertions(+) diff --git a/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/PageRevisionRepository.java b/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/PageRevisionRepository.java index 1dc77bf4b63..546423a5063 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/PageRevisionRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/PageRevisionRepository.java @@ -48,4 +48,6 @@ public interface PageRevisionRepository extends FindAllRepository Optional findLastByPageId(String pageId) throws TechnicalException; List deleteByPageId(String pageId) throws TechnicalException; + + void deleteAllByPageId(String pageId) throws TechnicalException; } diff --git a/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcPageRevisionRepository.java b/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcPageRevisionRepository.java index 842c6d810c7..b8c7b1d61d3 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcPageRevisionRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcPageRevisionRepository.java @@ -156,4 +156,21 @@ public List deleteByPageId(String pageId) throws TechnicalException { throw new TechnicalException("Failed to delete page revision by page id", ex); } } + + @Override + public void deleteAllByPageId(String pageId) throws TechnicalException { + LOGGER.debug("JdbcPageRevisionRepository.deleteAllByPageId({})", pageId); + try { + String sql = "DELETE FROM " + this.tableName + " WHERE page_id = ?"; + int rowsAffected = jdbcTemplate.update(sql, pageId); + LOGGER.debug("JdbcPageRevisionRepository.deleteAllByPageId({}) = {} rows deleted", pageId, rowsAffected); + + if (rowsAffected == 0) { + LOGGER.warn("No revisions found for page id: {}", pageId); + } + } catch (final Exception ex) { + LOGGER.error("Failed to delete revisions by page id: {}", pageId, ex); + throw new TechnicalException("Failed to delete revisions by page id", ex); + } + } } diff --git a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoPageRevisionRepository.java b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoPageRevisionRepository.java index 7c12011a3c5..12c792c0ffa 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoPageRevisionRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoPageRevisionRepository.java @@ -103,6 +103,16 @@ public Optional findLastByPageId(String pageId) throws TechnicalEx } } + @Override + public void deleteAllByPageId(String pageId) throws TechnicalException { + try { + internalPageRevisionRepo.deleteAllByPageId(pageId); + } catch (Exception e) { + logger.error("An error occurred when deleting revision for page [{}]", pageId, e); + throw new TechnicalException("An error occurred when deleting the page revisions"); + } + } + @Override public Set findAll() throws TechnicalException { return internalPageRevisionRepo diff --git a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/page/revision/PageRevisionMongoRepository.java b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/page/revision/PageRevisionMongoRepository.java index 0eb993f1908..faa43c19b26 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/page/revision/PageRevisionMongoRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/page/revision/PageRevisionMongoRepository.java @@ -32,6 +32,9 @@ public interface PageRevisionMongoRepository @Query(value = "{ '_id.pageId' : ?0 }", sort = "{ 'revision': 1 }", delete = true) List deleteByPageId(String pageId); + @Query(value = "{ '_id.pageId' : ?0 }", delete = true) + void deleteAllByPageId(String pageId); + @Query(value = "{ '_id.pageId' : ?0 }", sort = "{ 'revision': 1 }") List findAllByPageId(String pageId); } diff --git a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/PageRevisionRepositoryTest.java b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/PageRevisionRepositoryTest.java index 8e8615e85ab..f714eac3a2f 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/PageRevisionRepositoryTest.java +++ b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/PageRevisionRepositoryTest.java @@ -16,6 +16,8 @@ package io.gravitee.repository.management; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import io.gravitee.common.data.domain.Page; import io.gravitee.repository.exceptions.TechnicalException; @@ -158,4 +160,26 @@ public void should_delete_by_page_id() throws Exception { assertThat(deleted.size()).isEqualTo(2); assertThat(nbAfterDeletion).isEqualTo(0); } + + @Test + public void shouldDeleteAllByPageId() throws TechnicalException { + List revisionsBefore = pageRevisionRepository.findAllByPageId("findByPageId"); + assertNotNull(revisionsBefore); + assertEquals(3, revisionsBefore.size()); + + pageRevisionRepository.deleteAllByPageId("findByPageId"); + + List revisionsAfter = pageRevisionRepository.findAllByPageId("findByPageId"); + assertNotNull(revisionsAfter); + assertEquals(0, revisionsAfter.size()); + } + + @Test + public void shouldDoNothingWhenNoRevisionsFoundWhileDeleting() throws TechnicalException { + pageRevisionRepository.deleteAllByPageId("nonExistingPageId"); + + List revisionsAfter = pageRevisionRepository.findAllByPageId("nonExistingPageId"); + assertNotNull(revisionsAfter); + assertEquals(0, revisionsAfter.size()); + } } diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/PageRevisionService.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/PageRevisionService.java index 4c4758d4d31..55535fc0f7d 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/PageRevisionService.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/PageRevisionService.java @@ -15,6 +15,7 @@ */ package io.gravitee.rest.api.service; +import io.gravitee.repository.exceptions.TechnicalException; import io.gravitee.repository.management.api.search.Pageable; import io.gravitee.repository.management.model.Page; import io.gravitee.rest.api.model.PageRevisionEntity; @@ -35,4 +36,6 @@ public interface PageRevisionService { List findAllByPageId(String pageId); PageRevisionEntity create(Page page); + + void deleteAllByPageId(String pageId) throws TechnicalException; } diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/PageRevisionServiceImpl.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/PageRevisionServiceImpl.java index 8745ecf4978..9f8f638fe2b 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/PageRevisionServiceImpl.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/PageRevisionServiceImpl.java @@ -115,6 +115,11 @@ public PageRevisionEntity create(Page page) { } } + @Override + public void deleteAllByPageId(String pageId) throws TechnicalException { + pageRevisionRepository.deleteAllByPageId(pageId); + } + private PageRevisionEntity convert(PageRevision revision) { PageRevisionEntity entity = new PageRevisionEntity(); entity.setPageId(revision.getPageId()); diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/PageServiceImpl.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/PageServiceImpl.java index fbcf8199ab7..326f78b57cb 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/PageServiceImpl.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/PageServiceImpl.java @@ -2116,6 +2116,9 @@ public void delete(ExecutionContext executionContext, String pageId) { pageRepository.delete(pageId); + // delete page revisions + pageRevisionService.deleteAllByPageId(pageId); + // delete links and translations related to the page if (!PageType.LINK.name().equalsIgnoreCase(page.getType()) && !PageType.TRANSLATION.name().equalsIgnoreCase(page.getType())) { this.deleteRelatedPages(page.getId()); diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/PageRevisionServiceTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/PageRevisionServiceTest.java index bb0a8d453bb..aa4f79d14ba 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/PageRevisionServiceTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/PageRevisionServiceTest.java @@ -26,6 +26,7 @@ import io.gravitee.rest.api.model.PageRevisionEntity; import io.gravitee.rest.api.model.PageType; import io.gravitee.rest.api.service.AuditService; +import io.gravitee.rest.api.service.common.GraviteeContext; import io.gravitee.rest.api.service.exceptions.TechnicalManagementException; import io.gravitee.rest.api.service.impl.PageRevisionServiceImpl; import java.util.Date; @@ -149,4 +150,16 @@ public void shouldNotCreate_Because_InvalidType() throws TechnicalException { when(page.getType()).thenReturn(PageType.FOLDER.name()); pageRevisionService.create(page); } + + @Test + public void shouldDeletePageRevision() throws TechnicalException { + pageRevisionService.deleteAllByPageId(PAGE_ID); + verify(pageRevisionRepository).deleteAllByPageId(PAGE_ID); + } + + @Test(expected = TechnicalException.class) + public void shouldNotDeletePageRevisionBecauseTechnicalException() throws TechnicalException { + doThrow(TechnicalException.class).when(pageRevisionRepository).deleteAllByPageId(PAGE_ID); + pageRevisionService.deleteAllByPageId(PAGE_ID); + } }