diff --git a/src/main/java/cz/cvut/kbss/termit/rest/PasswordChangeController.java b/src/main/java/cz/cvut/kbss/termit/rest/PasswordChangeController.java index 36d6b9f25..8c800e732 100644 --- a/src/main/java/cz/cvut/kbss/termit/rest/PasswordChangeController.java +++ b/src/main/java/cz/cvut/kbss/termit/rest/PasswordChangeController.java @@ -35,7 +35,6 @@ public class PasswordChangeController { @Autowired public PasswordChangeController(PasswordChangeService passwordChangeService) { this.passwordChangeService = passwordChangeService; - LOG.debug("Instantiating password change controller."); } @Operation(description = "Requests a password reset for the specified username.") diff --git a/src/main/java/cz/cvut/kbss/termit/service/business/ResourceService.java b/src/main/java/cz/cvut/kbss/termit/service/business/ResourceService.java index 824892e4c..084004a3c 100644 --- a/src/main/java/cz/cvut/kbss/termit/service/business/ResourceService.java +++ b/src/main/java/cz/cvut/kbss/termit/service/business/ResourceService.java @@ -20,7 +20,6 @@ import cz.cvut.kbss.termit.asset.provenance.SupportsLastModification; import cz.cvut.kbss.termit.event.DocumentRenameEvent; import cz.cvut.kbss.termit.event.FileRenameEvent; -import cz.cvut.kbss.termit.exception.AssetRemovalException; import cz.cvut.kbss.termit.exception.InvalidParameterException; import cz.cvut.kbss.termit.exception.NotFoundException; import cz.cvut.kbss.termit.exception.UnsupportedAssetOperationException; @@ -104,13 +103,16 @@ public ResourceService(ResourceRepositoryService repositoryService, DocumentMana @PreAuthorize("@resourceAuthorizationService.canRemove(#toRemove)") public void remove(Resource toRemove) { Objects.requireNonNull(toRemove); - if (toRemove instanceof Document && !((Document) toRemove).getFiles().isEmpty()) { - throw new AssetRemovalException("Cannot remove non-empty document " + toRemove.getLabel() + "!"); + final Resource managed = findRequired(toRemove.getUri()); + if (managed instanceof Document doc) { + doc.getFiles().forEach(f -> { + documentManager.remove(f); + repositoryService.remove(f); + }); } // We need the reference managed, so that its name is available to document manager - final Resource actualToRemove = getReference(toRemove.getUri()); - documentManager.remove(actualToRemove); - repositoryService.remove(actualToRemove); + documentManager.remove(managed); + repositoryService.remove(managed); } /** diff --git a/src/main/java/cz/cvut/kbss/termit/service/repository/VocabularyRepositoryService.java b/src/main/java/cz/cvut/kbss/termit/service/repository/VocabularyRepositoryService.java index 2b5512d84..1c23c5b4e 100644 --- a/src/main/java/cz/cvut/kbss/termit/service/repository/VocabularyRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/termit/service/repository/VocabularyRepositoryService.java @@ -268,10 +268,6 @@ public long getLastModified() { @Transactional @Override public void remove(Vocabulary instance) { - if (instance.getDocument() != null) { - throw new AssetRemovalException("Removal of document vocabularies is not supported yet."); - } - final List vocabularies = vocabularyDao.getImportingVocabularies(instance); if (!vocabularies.isEmpty()) { throw new AssetRemovalException( diff --git a/src/test/java/cz/cvut/kbss/termit/service/business/ResourceServiceTest.java b/src/test/java/cz/cvut/kbss/termit/service/business/ResourceServiceTest.java index b98434ce4..91347cffe 100644 --- a/src/test/java/cz/cvut/kbss/termit/service/business/ResourceServiceTest.java +++ b/src/test/java/cz/cvut/kbss/termit/service/business/ResourceServiceTest.java @@ -21,7 +21,6 @@ import cz.cvut.kbss.termit.environment.Generator; import cz.cvut.kbss.termit.event.DocumentRenameEvent; import cz.cvut.kbss.termit.event.FileRenameEvent; -import cz.cvut.kbss.termit.exception.AssetRemovalException; import cz.cvut.kbss.termit.exception.NotFoundException; import cz.cvut.kbss.termit.exception.TermItException; import cz.cvut.kbss.termit.exception.UnsupportedAssetOperationException; @@ -43,7 +42,11 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.*; +import org.mockito.ArgumentCaptor; +import org.mockito.InOrder; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.context.ApplicationEventPublisher; import org.springframework.http.MediaType; @@ -54,10 +57,23 @@ import java.net.URI; import java.nio.charset.StandardCharsets; import java.time.Instant; -import java.util.*; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.anySet; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class ResourceServiceTest { @@ -105,19 +121,19 @@ void updateUpdatesResourceViaRepositoryService() { @Test void removeRemovesResourceViaRepositoryService() { final Resource resource = Generator.generateResourceWithId(); - when(resourceRepositoryService.getReference(resource.getUri())).thenReturn(resource); + when(resourceRepositoryService.findRequired(resource.getUri())).thenReturn(resource); sut.remove(resource); verify(resourceRepositoryService).remove(resource); } - // Bug #1356 + // Bug R#1356 @Test void removeEnsuresAttributesForDocumentManagerArePresent() { final Resource toRemove = new Resource(); toRemove.setUri(Generator.generateUri()); final Resource resource = Generator.generateResource(); resource.setUri(toRemove.getUri()); - when(resourceRepositoryService.getReference(resource.getUri())).thenReturn(resource); + when(resourceRepositoryService.findRequired(resource.getUri())).thenReturn(resource); sut.remove(resource); verify(resourceRepositoryService).remove(resource); verify(documentManager).remove(resource); @@ -393,7 +409,7 @@ void hasContentReturnsFalseForNonFile() { @Test void removeRemovesAssociatedDiskContent() { final Resource resource = Generator.generateResourceWithId(); - when(resourceRepositoryService.getReference(resource.getUri())).thenReturn(resource); + when(resourceRepositoryService.findRequired(resource.getUri())).thenReturn(resource); sut.remove(resource); verify(documentManager).remove(resource); } @@ -463,13 +479,17 @@ void updateDoesNotPublishFileRenameEventWhenRepositoryServiceThrowsException() { } @Test - void removeThrowsAssetRemovalExceptionWhenNonEmptyDocumentIsRemoved() { + void removeNonEmptyDocumentRemovesAllAssociatedFiles() { final File file = Generator.generateFileWithId("test.html"); final Document document = Generator.generateDocumentWithId(); document.addFile(file); + when(resourceRepositoryService.findRequired(document.getUri())).thenReturn(document); - assertThrows(AssetRemovalException.class, () -> sut.remove(document)); - verify(resourceRepositoryService, never()).remove(any()); + sut.remove(document); + verify(documentManager).remove(file); + verify(documentManager).remove(document); + verify(resourceRepositoryService).remove(file); + verify(resourceRepositoryService).remove(document); } @Test