Skip to content

Commit

Permalink
[Bug #276] Allow removal of empty document vocabularies.
Browse files Browse the repository at this point in the history
Because all our vocabularies are document vocabularies.
  • Loading branch information
ledsoft committed Jul 23, 2024
1 parent c3a2ccb commit f62fdc8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
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

0 comments on commit f62fdc8

Please sign in to comment.