Skip to content

Commit

Permalink
[Enhancement kbss-cvut/termit-ui#520] Add tests for delete records an…
Browse files Browse the repository at this point in the history
…d vocabulary content detailed history endpoint
  • Loading branch information
lukaskabc committed Nov 5, 2024
1 parent 5c51ae0 commit 780936e
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public List<AbstractChangeRecord> getDetailedHistoryOfContent(Vocabulary vocabul

private TypedQuery<AbstractChangeRecord> createDetailedContentChangesQuery(Vocabulary vocabulary, VocabularyContentChangeFilterDto filter, Pageable pageReq) {
TypedQuery<AbstractChangeRecord> query = em.createNativeQuery("""
SELECT ?record WHERE {
SELECT DISTINCT ?record WHERE {
GRAPH ?changeContext {
?record a ?changeRecord .
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import cz.cvut.kbss.termit.dto.PrefixDeclaration;
import cz.cvut.kbss.termit.dto.RdfsStatement;
import cz.cvut.kbss.termit.dto.Snapshot;
import cz.cvut.kbss.termit.dto.filter.VocabularyContentChangeFilterDto;
import cz.cvut.kbss.termit.environment.Environment;
import cz.cvut.kbss.termit.environment.Generator;
import cz.cvut.kbss.termit.event.AssetPersistEvent;
Expand All @@ -37,13 +38,18 @@
import cz.cvut.kbss.termit.model.User;
import cz.cvut.kbss.termit.model.Vocabulary;
import cz.cvut.kbss.termit.model.changetracking.AbstractChangeRecord;
import cz.cvut.kbss.termit.model.changetracking.DeleteChangeRecord;
import cz.cvut.kbss.termit.model.changetracking.PersistChangeRecord;
import cz.cvut.kbss.termit.model.changetracking.UpdateChangeRecord;
import cz.cvut.kbss.termit.model.resource.Document;
import cz.cvut.kbss.termit.model.resource.File;
import cz.cvut.kbss.termit.model.util.EntityToOwlClassMapper;
import cz.cvut.kbss.termit.persistence.context.DescriptorFactory;
import cz.cvut.kbss.termit.persistence.dao.changetracking.ChangeRecordDao;
import cz.cvut.kbss.termit.persistence.dao.changetracking.ChangeTrackingContextResolver;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Constants;
import cz.cvut.kbss.termit.util.Utils;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.vocabulary.RDF;
Expand All @@ -59,6 +65,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Pageable;
import org.springframework.test.annotation.DirtiesContext;

import java.net.URI;
Expand Down Expand Up @@ -108,6 +115,9 @@ class VocabularyDaoTest extends BaseDaoTestRunner {
@Autowired
private VocabularyDao sut;

@Autowired
private TermDao termDao;

private User author;

@BeforeEach
Expand Down Expand Up @@ -927,4 +937,106 @@ void getAnyExternalRelationsReturnsTermsWithBothRelations(URI termRelation) {
}
});
}

@Test
void getDetailedHistoryOfContentReturnsRecordsForAllChangeTypes() {
enableRdfsInference(em);
final Configuration config = new Configuration();
config.getChangetracking().getContext().setExtension("/zmeny");
final ChangeTrackingContextResolver resolver = new ChangeTrackingContextResolver(em, config);
final ChangeRecordDao changeRecordDao = new ChangeRecordDao(resolver, em);

final Vocabulary vocabulary = Generator.generateVocabularyWithId();
final Term firstTerm = Generator.generateTermWithId(vocabulary.getUri());
final Term termToRemove = Generator.generateTermWithId(vocabulary.getUri());

final List<AbstractChangeRecord> firstChanges = Generator.generateChangeRecords(firstTerm, author);
final List<AbstractChangeRecord> termToRemoveChanges = Generator.generateChangeRecords(termToRemove, author);
final DeleteChangeRecord deleteChangeRecord = new DeleteChangeRecord();
deleteChangeRecord.setChangedEntity(termToRemove.getUri());
deleteChangeRecord.setTimestamp(Utils.timestamp());
deleteChangeRecord.setAuthor(author);
deleteChangeRecord.setLabel(termToRemove.getLabel());

transactional(() -> {
vocabulary.getGlossary().addRootTerm(firstTerm);
sut.persist(vocabulary);
Environment.addRelation(vocabulary.getUri(), URI.create(cz.cvut.kbss.termit.util.Vocabulary.s_p_ma_glosar), vocabulary.getGlossary().getUri(), em);

termDao.persist(firstTerm, vocabulary);
termDao.persist(termToRemove, vocabulary);

firstChanges.forEach(r -> changeRecordDao.persist(r, firstTerm));
termToRemoveChanges.forEach(r -> changeRecordDao.persist(r, termToRemove));
changeRecordDao.persist(deleteChangeRecord, termToRemove);
});

final VocabularyContentChangeFilterDto filter = new VocabularyContentChangeFilterDto();
final int recordsCount = firstChanges.size() + termToRemoveChanges.size() + 1;
final Pageable pageable = Pageable.ofSize(recordsCount * 3);
final List<AbstractChangeRecord> contentChanges = sut.getDetailedHistoryOfContent(vocabulary, filter, pageable);

assertEquals(recordsCount, contentChanges.size());
final long persistCount = contentChanges.stream().filter(ch -> ch instanceof PersistChangeRecord).count();
final long updatesCount = contentChanges.stream().filter(ch -> ch instanceof UpdateChangeRecord).count();
final long deleteCount = contentChanges.stream().filter(ch -> ch instanceof DeleteChangeRecord).count();
assertEquals(2, persistCount);
assertEquals(recordsCount - 3, updatesCount);
assertEquals(1, deleteCount);
}

@Test
void getDetailedHistoryOfContentReturnsRecordsOfExistingTermFilteredByTermName() {
enableRdfsInference(em);
final Configuration config = new Configuration();
config.getChangetracking().getContext().setExtension("/zmeny");
final ChangeTrackingContextResolver resolver = new ChangeTrackingContextResolver(em, config);
final ChangeRecordDao changeRecordDao = new ChangeRecordDao(resolver, em);

final String needle = "needle";
final String haystack = "A label that contains needle somewhere";
final String mud = "The n3edle is not here";

final Vocabulary vocabulary = Generator.generateVocabularyWithId();
final Term firstTerm = Generator.generateTermWithId(vocabulary.getUri());
firstTerm.setLabel(MultilingualString.create(haystack, null));
final Term termToRemove = Generator.generateTermWithId(vocabulary.getUri());
termToRemove.getLabel().set(mud);

final List<AbstractChangeRecord> firstChanges = Generator.generateChangeRecords(firstTerm, author);
final List<AbstractChangeRecord> termToRemoveChanges = Generator.generateChangeRecords(termToRemove, author);
final DeleteChangeRecord deleteChangeRecord = new DeleteChangeRecord();
deleteChangeRecord.setChangedEntity(termToRemove.getUri());
deleteChangeRecord.setTimestamp(Utils.timestamp());
deleteChangeRecord.setAuthor(author);
deleteChangeRecord.setLabel(termToRemove.getLabel());

transactional(() -> {
vocabulary.getGlossary().addRootTerm(firstTerm);
sut.persist(vocabulary);
Environment.addRelation(vocabulary.getUri(), URI.create(cz.cvut.kbss.termit.util.Vocabulary.s_p_ma_glosar), vocabulary.getGlossary().getUri(), em);

termDao.persist(firstTerm, vocabulary);
termDao.persist(termToRemove, vocabulary);

firstChanges.forEach(r -> changeRecordDao.persist(r, firstTerm));
termToRemoveChanges.forEach(r -> changeRecordDao.persist(r, termToRemove));
changeRecordDao.persist(deleteChangeRecord, termToRemove);
});

final VocabularyContentChangeFilterDto filter = new VocabularyContentChangeFilterDto();
filter.setTermName(needle);

final int recordsCount = termToRemoveChanges.size() + 1;
final Pageable pageable = Pageable.ofSize(recordsCount * 2); // page bigger than number of records
final List<AbstractChangeRecord> contentChanges = sut.getDetailedHistoryOfContent(vocabulary, filter, pageable);

assertEquals(recordsCount, contentChanges.size());
final long persistCount = contentChanges.stream().filter(ch -> ch instanceof PersistChangeRecord).count();
final long updatesCount = contentChanges.stream().filter(ch -> ch instanceof UpdateChangeRecord).count();
final long deleteCount = contentChanges.stream().filter(ch -> ch instanceof DeleteChangeRecord).count();
assertEquals(1, persistCount);
assertEquals(recordsCount - 2, updatesCount);
assertEquals(1, deleteCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import cz.cvut.kbss.termit.model.User;
import cz.cvut.kbss.termit.model.Vocabulary;
import cz.cvut.kbss.termit.model.changetracking.AbstractChangeRecord;
import cz.cvut.kbss.termit.model.changetracking.DeleteChangeRecord;
import cz.cvut.kbss.termit.model.changetracking.PersistChangeRecord;
import cz.cvut.kbss.termit.model.changetracking.UpdateChangeRecord;
import cz.cvut.kbss.termit.model.resource.File;
Expand All @@ -52,6 +53,8 @@
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class ChangeTrackingTest extends BaseServiceTestRunner {

Expand Down Expand Up @@ -141,7 +144,7 @@ void updatingVocabularyReferenceAndLiteralAttributesCreatesTwoUpdateRecords() {
assertEquals(vocabulary.getUri(), chr.getChangedEntity());
assertThat(result.get(0), instanceOf(UpdateChangeRecord.class));
assertThat(((UpdateChangeRecord) chr).getChangedAttribute().toString(), anyOf(equalTo(DC.Terms.TITLE),
equalTo(cz.cvut.kbss.termit.util.Vocabulary.s_p_importuje_slovnik)));
equalTo(cz.cvut.kbss.termit.util.Vocabulary.s_p_importuje_slovnik)));
});
}

Expand Down Expand Up @@ -214,7 +217,7 @@ void updatingTermLiteralAttributesCreatesChangeRecordWithOriginalAndNewValue() {
final List<AbstractChangeRecord> result = changeRecordDao.findAll(term);
assertEquals(1, result.size());
assertEquals(Collections.singleton(originalDefinition),
((UpdateChangeRecord) result.get(0)).getOriginalValue());
((UpdateChangeRecord) result.get(0)).getOriginalValue());
assertEquals(Collections.singleton(newDefinition), ((UpdateChangeRecord) result.get(0)).getNewValue());
}

Expand Down Expand Up @@ -271,4 +274,24 @@ void updatingTermStateCreatesUpdateChangeRecord() {
assertEquals(URI.create(cz.cvut.kbss.termit.util.Vocabulary.s_p_ma_stav_pojmu),
((UpdateChangeRecord) result.get(0)).getChangedAttribute());
}

@Test
void deletingTermCreatesDeleteChangeRecord() {
enableRdfsInference(em);
final Term term = Generator.generateTermWithId(vocabulary.getUri());
transactional(()-> {
em.persist(vocabulary, descriptorFactory.vocabularyDescriptor(vocabulary));
term.setGlossary(vocabulary.getGlossary().getUri());
em.persist(term, descriptorFactory.termDescriptor(vocabulary));
Generator.addTermInVocabularyRelationship(term, vocabulary.getUri(), em);
});

termService.remove(term);
final List<AbstractChangeRecord> result = changeRecordDao.findAll(term);
assertEquals(1, result.size());
final DeleteChangeRecord record = assertInstanceOf(DeleteChangeRecord.class, result.get(0));
assertEquals(term.getUri(), record.getChangedEntity());
assertNotNull(record.getLabel());
assertEquals(term.getLabel(), record.getLabel());
}
}

0 comments on commit 780936e

Please sign in to comment.