Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug #276] Allow removal of empty document vocabularies. #278

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vocabulary> vocabularies = vocabularyDao.getImportingVocabularies(instance);
if (!vocabularies.isEmpty()) {
throw new AssetRemovalException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down