Skip to content

Commit

Permalink
[Enhancement kbss-cvut/termit-ui#520] ChangeRecordDaoTest cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaskabc committed Nov 14, 2024
1 parent a657a98 commit 9c04678
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
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.Constants;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.ValueFactory;
Expand Down Expand Up @@ -118,9 +117,6 @@ class VocabularyDaoTest extends BaseDaoTestRunner {
@SpyBean
private ChangeRecordDao changeRecordDao;

@SpyBean
private ChangeTrackingContextResolver changeTrackingContextResolver;

private User author;

@BeforeEach
Expand Down Expand Up @@ -954,12 +950,10 @@ void getDetailedHistoryOfContentCallsChangeRecordDaoWithFilter() {
final ChangeRecordFilterDto filterDto = new ChangeRecordFilterDto();
filterDto.setAuthorName("Name of the author");

doReturn(vocabulary.getUri()).when(changeTrackingContextResolver).resolveChangeTrackingContext(vocabulary);
doReturn(records).when(changeRecordDao).findAllRelatedToType(vocabulary, filterDto, skosConcept, unpaged);

sut.getDetailedHistoryOfContent(vocabulary, filterDto, unpaged);

verify(changeTrackingContextResolver).resolveChangeTrackingContext(vocabulary);
verify(changeRecordDao).findAllRelatedToType(vocabulary, filterDto, skosConcept, unpaged);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,133 +288,114 @@ void getAuthorsRetrievesUsersAssociatedWithPersistChangeRecordsOfSpecifiedAsset(
}

@Test
void voidFindAllReturnsChangeRecordsWithoutVocabularyChanges() {

}

@Test
void findAllFilteredReturnsRecordsOfExistingTermFilteredByTermName() {
void findAllRelatedToTypeReturnsChangeRecordsWithoutVocabularyChanges() {
enableRdfsInference(em);

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

// Two terms with needle in the label, one term without needle in the label
final Term firstTerm = Generator.generateTermWithId(vocabulary.getUri());
firstTerm.getLabel().set(Environment.LANGUAGE, haystack);
final Term secondTerm = Generator.generateTermWithId(vocabulary.getUri());
secondTerm.getLabel().set(mud + needle);
final Term thirdTerm = Generator.generateTermWithId(vocabulary.getUri());
thirdTerm.getLabel().set(Environment.LANGUAGE, mud);

final List<AbstractChangeRecord> firstChanges = Generator.generateChangeRecords(firstTerm, author);
final List<AbstractChangeRecord> secondChanges = Generator.generateChangeRecords(secondTerm, author);
final List<AbstractChangeRecord> thirdChanges = Generator.generateChangeRecords(thirdTerm, author);

final List<AbstractChangeRecord> vocabularyChanges = Generator.generateChangeRecords(vocabulary, author);

final Descriptor changeContextDescriptor = persistDescriptor(contextResolver.resolveChangeTrackingContext(vocabulary));
final Descriptor vocabularyDescriptor = persistDescriptor(vocabulary.getUri());

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

em.persist(firstTerm, vocabularyDescriptor);
em.persist(secondTerm, vocabularyDescriptor);
em.persist(thirdTerm, vocabularyDescriptor);

Stream.of(firstChanges, secondChanges, thirdChanges)
Stream.of(firstChanges, secondChanges, vocabularyChanges)
.flatMap(Collection::stream)
.forEach(r -> em.persist(r, changeContextDescriptor));
});

final ChangeRecordFilterDto filter = new ChangeRecordFilterDto();
filter.setAssetLabel(needle);

final int recordsCount = firstChanges.size() + secondChanges.size();
final Pageable pageable = Pageable.ofSize(recordsCount * 2);
final Pageable pageable = Pageable.unpaged();

final List<AbstractChangeRecord> contentChanges = sut.findAllRelatedToType(vocabulary, filter, SKOS_CONCEPT, 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();
// check that all changes are related to the first or the second term
assertTrue(contentChanges.stream()
.allMatch(ch -> firstTerm.getUri().equals(ch.getChangedEntity()) ||
secondTerm.getUri().equals(ch.getChangedEntity())));
assertEquals(2, persistCount);
assertEquals(recordsCount - 2, updatesCount); // -2 persist records
assertEquals(0, deleteCount);
}


@Test
void findAllFilteredReturnsRecordsOfDeletedTermFilteredByTermName() {
void findAllRelatedToTypeReturnsRecordsOfExistingTermFilteredByTermName() {
enableRdfsInference(em);

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


// needle is inside the label of first and the second term
final Term firstTerm = Generator.generateTermWithId(vocabulary.getUri());
// the needle is placed in the term which will be removed
firstTerm.getLabel().set(Environment.LANGUAGE, mud);
firstTerm.setVocabulary(vocabulary.getUri());
final Term termToRemove = Generator.generateTermWithId(vocabulary.getUri());
termToRemove.getLabel().set(Environment.LANGUAGE, haystack);
termToRemove.setVocabulary(vocabulary.getUri());
firstTerm.getLabel().set(Environment.LANGUAGE, haystack);
final Term secondTerm = Generator.generateTermWithId(vocabulary.getUri());
secondTerm.getLabel().set(mud + needle);
final Term thirdTerm = Generator.generateTermWithId(vocabulary.getUri());
thirdTerm.getLabel().set(Environment.LANGUAGE, 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());
final List<AbstractChangeRecord> secondChanges = Generator.generateChangeRecords(secondTerm, author);
final List<AbstractChangeRecord> thirdChanges = Generator.generateChangeRecords(thirdTerm, author);

final Descriptor changeContextDescriptor = persistDescriptor(contextResolver.resolveChangeTrackingContext(vocabulary));
final Descriptor vocabularyDescriptor = persistDescriptor(vocabulary.getUri());

transactional(() -> {
em.persist(vocabulary);
em.persist(vocabulary, vocabularyDescriptor);

em.persist(firstTerm, vocabularyDescriptor);
em.persist(termToRemove, vocabularyDescriptor);
em.persist(secondTerm, vocabularyDescriptor);
em.persist(thirdTerm, vocabularyDescriptor);

Stream.of(firstChanges, termToRemoveChanges, List.of(deleteChangeRecord))
Stream.of(firstChanges, secondChanges, thirdChanges)
.flatMap(Collection::stream)
.forEach(r -> em.persist(r, changeContextDescriptor));
});

final ChangeRecordFilterDto filter = new ChangeRecordFilterDto();
filter.setAssetLabel(needle);

final int recordsCount = termToRemoveChanges.size() + 1; // +1 for the delete record
final Pageable pageable = Pageable.unpaged();
// needle is inside the label of first and the second term
final int recordsCount = firstChanges.size() + secondChanges.size();
final Pageable pageable = Pageable.ofSize(recordsCount * 2);

final List<AbstractChangeRecord> contentChanges = sut.findAllRelatedToType(vocabulary, filter, SKOS_CONCEPT, 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); // -1 persist record -1 delete record
assertEquals(1, deleteCount);
assertEquals(2, persistCount);
assertEquals(recordsCount - 2, updatesCount); // -2 persist records
assertEquals(0, deleteCount);
}


@Test
void findAllFilteredReturnsRecordsOfExistingTermFilteredByChangedAttributeName() {
void findAllRelatedToTypeReturnsRecordsOfExistingTermFilteredByChangedAttributeName() {
enableRdfsInference(em);

// Two terms with needle in the label, one term without needle in the label
final Term firstTerm = Generator.generateTermWithId(vocabulary.getUri());
final Term secondTerm = Generator.generateTermWithId(vocabulary.getUri());

final List<AbstractChangeRecord> firstChanges = Generator.generateChangeRecords(firstTerm, author);
final List<AbstractChangeRecord> secondChanges = Generator.generateChangeRecords(secondTerm, author);

// randomize changed attributes
final Random random = new Random();
final AtomicInteger recordCount = new AtomicInteger(0);
final URI changedAttribute = URI.create(SKOS.DEFINITION);
Expand All @@ -424,10 +405,12 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByChangedAttributeName()
final Descriptor changeContextDescriptor = persistDescriptor(contextResolver.resolveChangeTrackingContext(vocabulary));
final Descriptor vocabularyDescriptor = persistDescriptor(vocabulary.getUri());

// randomize changed attributes
Stream.of(firstChanges, secondChanges).flatMap(Collection::stream)
.filter(r -> r instanceof UpdateChangeRecord)
.map(r -> (UpdateChangeRecord) r)
.forEach(r -> {
// ensuring at least one has the "changedAttribute"
if(random.nextBoolean() || recordCount.get() == 0) {
r.setChangedAttribute(changedAttribute);
recordCount.incrementAndGet();
Expand Down Expand Up @@ -464,10 +447,9 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByChangedAttributeName()
}

@Test
void findAllFilteredReturnsRecordsOfExistingTermFilteredByAuthorName() {
void findAllRelatedToTypeReturnsRecordsOfExistingTermFilteredByAuthorName() {
enableRdfsInference(em);

// Two terms with needle in the label, one term without needle in the label
final Term firstTerm = Generator.generateTermWithId(vocabulary.getUri());
final Term secondTerm = Generator.generateTermWithId(vocabulary.getUri());

Expand Down Expand Up @@ -523,11 +505,10 @@ void findAllFilteredReturnsRecordsOfExistingTermFilteredByAuthorName() {
PersistChangeRecord.class,
DeleteChangeRecord.class
})
void findAllFilteredReturnsRecordsOfExistingTermFilteredByChangeType(Class<? extends AbstractChangeRecord> typeClass) {
void findAllRelatedToTypeReturnsRecordsOfExistingTermFilteredByChangeType(Class<? extends AbstractChangeRecord> typeClass) {
enableRdfsInference(em);
final URI typeUri = URI.create(typeClass.getAnnotation(OWLClass.class).iri());

// Two terms with needle in the label, one term without needle in the label
final Term firstTerm = Generator.generateTermWithId(vocabulary.getUri());
final Term secondTerm = Generator.generateTermWithId(vocabulary.getUri());

Expand Down

0 comments on commit 9c04678

Please sign in to comment.