Skip to content

Commit

Permalink
PR fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaskabc authored and ledsoft committed Aug 12, 2024
1 parent 3aaccd6 commit fa79893
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package cz.cvut.kbss.termit.event;

import cz.cvut.kbss.termit.model.Vocabulary;
import org.springframework.context.ApplicationEvent;

import java.net.URI;

/**
* Indicates that a Vocabulary will be removed
* <p>
* Fired in {@link cz.cvut.kbss.termit.persistence.dao.VocabularyDao#remove(Vocabulary)} right before the vocabulary removal.
*/
public class VocabularyRemovalEvent extends ApplicationEvent {
public class VocabularyWillBeRemovedEvent extends ApplicationEvent {
private final URI vocabulary;
public VocabularyRemovalEvent(Object source, URI vocabulary) {

public VocabularyWillBeRemovedEvent(Object source, URI vocabulary) {
super(source);
this.vocabulary = vocabulary;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import cz.cvut.kbss.termit.event.AssetPersistEvent;
import cz.cvut.kbss.termit.event.AssetUpdateEvent;
import cz.cvut.kbss.termit.event.RefreshLastModifiedEvent;
import cz.cvut.kbss.termit.event.VocabularyRemovalEvent;
import cz.cvut.kbss.termit.event.VocabularyWillBeRemovedEvent;
import cz.cvut.kbss.termit.exception.PersistenceException;
import cz.cvut.kbss.termit.model.Glossary;
import cz.cvut.kbss.termit.model.Term;
Expand Down Expand Up @@ -217,19 +217,34 @@ public Vocabulary update(Vocabulary entity) {
* This deletes the whole graph of the vocabulary, all terms in the vocabulary's glossary and then removes the vocabulary itself. Extreme caution
* should be exercised when using this method. All relevant data, including documents and files, will be dropped.
* <p>
* Publishes {@link VocabularyRemovalEvent} before the actual removal to allow other services to clean up related resources (e.g., delete the document).
* Publishes {@link VocabularyWillBeRemovedEvent} before the actual removal to allow other services to clean up related resources (e.g., delete the document).
* @param entity The vocabulary to delete
*/
@ModifiesData
@Override
public void remove(Vocabulary entity) {
eventPublisher.publishEvent(new VocabularyRemovalEvent(this, entity.getUri()));
eventPublisher.publishEvent(new VocabularyWillBeRemovedEvent(this, entity.getUri()));
this.removeVocabulary(entity, true);
}

/**
* Does not publish the {@link VocabularyWillBeRemovedEvent}.
* <p>
* Does not publishes the {@link VocabularyRemovalEvent}.<br>
* Forcefully removes the specified vocabulary.
* <p>
* This deletes all terms in the vocabulary's glossary and then removes the vocabulary itself.
* Extreme caution should be exercised when using this method,
* as it does not check for any references or usage and just drops all the relevant data.
* <p>
* The document is not removed.
*/
public void removeVocabularyKeepDocument(Vocabulary entity) {
this.removeVocabulary(entity, false);
}

/**
* <p>
* Does not publish the {@link VocabularyWillBeRemovedEvent}.<br>
* You should use {@link #remove(Vocabulary)} instead.
* <p>
* Forcefully removes the specified vocabulary.
Expand All @@ -243,7 +258,7 @@ public void remove(Vocabulary entity) {
* their relations, model, glossary and vocabulary itself, keeps the document.
* When true, the whole vocabulary graph is dropped.
*/
public void removeVocabulary(Vocabulary entity, boolean dropGraph) {
private void removeVocabulary(Vocabulary entity, boolean dropGraph) {
Objects.requireNonNull(entity);
LOG.debug("Forcefully removing vocabulary {} and all its contents.", entity);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void clearVocabulary(Vocabulary newVocabulary) {
possibleVocabulary.ifPresent(toRemove -> {
newVocabulary.setDocument(toRemove.getDocument());
newVocabulary.setAcl(toRemove.getAcl());
vocabularyDao.removeVocabulary(toRemove, false);
vocabularyDao.removeVocabularyKeepDocument(toRemove);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
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.event.VocabularyRemovalEvent;
import cz.cvut.kbss.termit.event.VocabularyWillBeRemovedEvent;
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 @@ -97,7 +97,7 @@ public ResourceService(ResourceRepositoryService repositoryService, DocumentMana
* Ensures that document gets removed during Vocabulary removal
*/
@EventListener
public void onVocabularyRemoval(VocabularyRemovalEvent event) {
public void onVocabularyRemoval(VocabularyWillBeRemovedEvent event) {
vocabularyService.find(event.getVocabulary()).ifPresent(vocabulary -> {
if(vocabulary.getDocument() != null) {
remove(vocabulary.getDocument());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@

import java.net.URI;
import java.time.Instant;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static cz.cvut.kbss.termit.util.Constants.VOCABULARY_REMOVAL_IGNORED_RELATIONS;

Expand Down Expand Up @@ -188,12 +195,12 @@ public Set<URI> getRelatedVocabularies(Vocabulary entity) {
return repositoryService.getRelatedVocabularies(entity);
}

@PreAuthorize("@vocabularyAuthorizationService.canRead(#entity)")
@PreAuthorize("@vocabularyAuthorizationService.canRead(#vocabulary)")
public List<RdfsStatement> getTermRelations(Vocabulary vocabulary) {
return repositoryService.getTermRelations(vocabulary);
}

@PreAuthorize("@vocabularyAuthorizationService.canRead(#entity)")
@PreAuthorize("@vocabularyAuthorizationService.canRead(#vocabulary)")
public List<RdfsStatement> getVocabularyRelations(Vocabulary vocabulary) {
return repositoryService.getVocabularyRelations(vocabulary, VOCABULARY_REMOVAL_IGNORED_RELATIONS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public void remove(Vocabulary instance) {
* @param instance The instance to be removed, not {@code null}
*/
@Override
protected void preRemove(Vocabulary instance) {
protected void preRemove(@NotNull Vocabulary instance) {
ensureNotImported(instance);
ensureNoTermRelationsExists(instance);
super.preRemove(instance);
Expand Down
20 changes: 19 additions & 1 deletion src/test/java/cz/cvut/kbss/termit/environment/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import cz.cvut.kbss.termit.model.UserAccount;
import cz.cvut.kbss.termit.security.model.AuthenticationToken;
import cz.cvut.kbss.termit.security.model.TermItUserDetails;
import cz.cvut.kbss.termit.service.security.SecurityUtils;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Constants;
import cz.cvut.kbss.termit.util.Vocabulary;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.rio.RDFFormat;
Expand All @@ -49,6 +49,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -219,4 +220,21 @@ public static void injectConfiguration(cz.cvut.kbss.termit.model.Vocabulary voca
throw new RuntimeException("Unable to inject configuration into Vocabulary instance.", e);
}
}

/**
* Adds relation into entity manager connection
*
* @implNote call in transactional
*/
public static void addRelation(URI subject, URI predicate, URI object, EntityManager em) {
final Repository repo = em.unwrap(Repository.class);
try (RepositoryConnection conn = repo.getConnection()) {
final ValueFactory vf = conn.getValueFactory();
conn.begin();
conn.add(vf.createIRI(subject.toString()),
vf.createIRI(predicate.toString()),
vf.createIRI(object.toString()));
conn.commit();
}
}
}
16 changes: 0 additions & 16 deletions src/test/java/cz/cvut/kbss/termit/environment/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,20 +527,4 @@ public static List<AccessControlRecord<?>> generateAccessControlRecords() {
result.add(rr);
return result;
}

/**
* Adds relation into entity manager connection
* @implNote call in transactional
*/
public static void addRelation(URI subject, URI predicate, URI object, EntityManager em) {
final Repository repo = em.unwrap(Repository.class);
try (RepositoryConnection conn = repo.getConnection()) {
final ValueFactory vf = conn.getValueFactory();
conn.begin();
conn.add(vf.createIRI(subject.toString()),
vf.createIRI(predicate.toString()),
vf.createIRI(object.toString()));
conn.commit();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import cz.cvut.kbss.termit.event.AssetPersistEvent;
import cz.cvut.kbss.termit.event.AssetUpdateEvent;
import cz.cvut.kbss.termit.event.RefreshLastModifiedEvent;
import cz.cvut.kbss.termit.event.VocabularyRemovalEvent;
import cz.cvut.kbss.termit.event.VocabularyWillBeRemovedEvent;
import cz.cvut.kbss.termit.model.Glossary;
import cz.cvut.kbss.termit.model.Model;
import cz.cvut.kbss.termit.model.Term;
Expand All @@ -54,7 +54,6 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Spy;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -710,7 +709,7 @@ void removeVocabularyRemovesVocabularyGlossaryModelAndAllTermsWithoutDocument()
});
});

transactional(() -> sut.removeVocabulary(vocabulary, false));
transactional(() -> sut.removeVocabularyKeepDocument(vocabulary));
final String query = "ASK { ?x a ?type }";
// vocabulary removed
assertFalse(em.createNativeQuery(query, Boolean.class)
Expand Down Expand Up @@ -762,10 +761,10 @@ void removePublishesEventAndDropsGraph() {

transactional(() -> sut.remove(vocabulary));

ArgumentCaptor<VocabularyRemovalEvent> eventCaptor = ArgumentCaptor.forClass(VocabularyRemovalEvent.class);
ArgumentCaptor<VocabularyWillBeRemovedEvent> eventCaptor = ArgumentCaptor.forClass(VocabularyWillBeRemovedEvent.class);
verify(eventPublisher).publishEvent(eventCaptor.capture());

VocabularyRemovalEvent event = eventCaptor.getValue();
VocabularyWillBeRemovedEvent event = eventCaptor.getValue();
assertNotNull(event);

assertEquals(event.getVocabulary(), vocabulary.getUri());
Expand Down Expand Up @@ -800,7 +799,7 @@ void internalAddRealtionCreatesRelation() {
sut.persist(vocabulary);
sut.persist(secondVocabulary);

Generator.addRelation(vocabulary.getUri(), relation, secondVocabulary.getUri(), em);
Environment.addRelation(vocabulary.getUri(), relation, secondVocabulary.getUri(), em);
});

Boolean result = em.createNativeQuery("""
Expand Down Expand Up @@ -843,7 +842,7 @@ void getAnyExternalRelationsReturnsTermsWithOutgoingRelations(URI termRelation)
em.persist(secondTerm, descriptorFactory.termDescriptor(secondTerm));
Generator.addTermInVocabularyRelationship(secondTerm, secondVocabulary.getUri(), em);

Generator.addRelation(term.getUri(), termRelation, secondTerm.getUri(), em);
Environment.addRelation(term.getUri(), termRelation, secondTerm.getUri(), em);
});

final List<RdfsStatement> relations = sut.getTermRelations(vocabulary);
Expand Down Expand Up @@ -876,7 +875,7 @@ void getAnyExternalRelationsReturnsTermsWithIncommingRelations(URI termRelation)
em.persist(secondTerm, descriptorFactory.termDescriptor(secondTerm));
Generator.addTermInVocabularyRelationship(secondTerm, secondVocabulary.getUri(), em);

Generator.addRelation(secondTerm.getUri(), termRelation, term.getUri(), em);
Environment.addRelation(secondTerm.getUri(), termRelation, term.getUri(), em);
});

final List<RdfsStatement> relations = sut.getTermRelations(vocabulary);
Expand Down Expand Up @@ -910,8 +909,8 @@ void getAnyExternalRelationsReturnsTermsWithBothRelations(URI termRelation) {
em.persist(secondTerm, descriptorFactory.termDescriptor(secondTerm));
Generator.addTermInVocabularyRelationship(secondTerm, secondVocabulary.getUri(), em);

Generator.addRelation(secondTerm.getUri(), termRelation, term.getUri(), em);
Generator.addRelation(term.getUri(), termRelation, secondTerm.getUri(), em);
Environment.addRelation(secondTerm.getUri(), termRelation, term.getUri(), em);
Environment.addRelation(term.getUri(), termRelation, secondTerm.getUri(), em);
});

final List<RdfsStatement> relations = sut.getTermRelations(vocabulary);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.model.MultilingualString;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.vocabulary.SKOS;
import cz.cvut.kbss.termit.environment.Environment;
import cz.cvut.kbss.termit.environment.Generator;
import cz.cvut.kbss.termit.exception.*;
import cz.cvut.kbss.termit.exception.AssetRemovalException;
import cz.cvut.kbss.termit.exception.NotFoundException;
import cz.cvut.kbss.termit.exception.ResourceExistsException;
import cz.cvut.kbss.termit.exception.SnapshotNotEditableException;
import cz.cvut.kbss.termit.exception.TermItException;
import cz.cvut.kbss.termit.exception.ValidationException;
import cz.cvut.kbss.termit.exception.importing.VocabularyImportException;
import cz.cvut.kbss.termit.model.Term;
import cz.cvut.kbss.termit.model.UserAccount;
Expand All @@ -41,10 +45,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Spy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -57,10 +58,16 @@
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class VocabularyRepositoryServiceTest extends BaseServiceTestRunner {

Expand All @@ -73,7 +80,7 @@ class VocabularyRepositoryServiceTest extends BaseServiceTestRunner {
@Autowired
private EntityManager em;

@SpyBean
@Autowired
private VocabularyRepositoryService sut;

private UserAccount user;
Expand Down Expand Up @@ -229,7 +236,7 @@ void removeThrowsWhenTermRelationExists(URI relation) {
em.persist(objectTerm, descriptorFactory.termDescriptor(objectTerm));
em.persist(subjectTerm, descriptorFactory.termDescriptor(subjectTerm));

Generator.addRelation(subjectTerm.getUri(), relation, objectTerm.getUri(), em);
Environment.addRelation(subjectTerm.getUri(), relation, objectTerm.getUri(), em);
});

assertThrows(AssetRemovalException.class, ()->sut.remove(vocabulary));
Expand Down

0 comments on commit fa79893

Please sign in to comment.