Skip to content

Commit

Permalink
[Enhancement kbss-cvut/termit-ui#520] Fix tests not saving change rec…
Browse files Browse the repository at this point in the history
…ords to change context
  • Loading branch information
lukaskabc committed Nov 14, 2024
1 parent 6c5797b commit e572355
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
package cz.cvut.kbss.termit.persistence.dao.changetracking;

import cz.cvut.kbss.jopa.exceptions.NoResultException;
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.model.query.TypedQuery;
import cz.cvut.kbss.jopa.vocabulary.RDFS;
Expand Down Expand Up @@ -73,12 +73,30 @@ public void persist(AbstractChangeRecord record, Asset<?> changedAsset) {
}
}

/**
* Finds all change records related to the specified asset.
*
* @param asset the asset
* @return list of change records
*/
public List<AbstractChangeRecord> findAll(Asset<?> asset) {
return findAll(asset, new ChangeRecordFilterDto());
}

/**
*
* @param asset
* @param filterDto
* @return
*/
public List<AbstractChangeRecord> findAll(Asset<?> asset, ChangeRecordFilterDto filterDto) {
if (filterDto.isEmpty()) {
// there is nothing to filter, simple query can be used
return findAll(asset);
URI changeTrackingContext = null;
try {
changeTrackingContext = contextResolver.resolveChangeTrackingContext(asset);
} catch (NoResultException e) {
return List.of();
}
return findAllFiltered(contextResolver.resolveChangeTrackingContext(asset), filterDto, Optional.of(asset), Optional.empty(), Pageable.unpaged());
return findAllFiltered(changeTrackingContext, filterDto, Optional.of(asset), Optional.empty(), Pageable.unpaged());
}

/**
Expand All @@ -100,6 +118,7 @@ public List<AbstractChangeRecord> findAllFiltered(URI changeContext, ChangeRecor
?hasTime ?timestamp ;
?hasAuthor ?author .
""" + /* Find an asset type if it is known (deleted assets does not have a type */ """
BIND(?assetTypeValue as ?assetTypeVar)
OPTIONAL {
?asset a ?assetType .
OPTIONAL {
Expand All @@ -108,7 +127,7 @@ public List<AbstractChangeRecord> findAllFiltered(URI changeContext, ChangeRecor
}
}
""" + /* filter assets without a type (deleted) or with a matching type */ """
FILTER(!BOUND(?assetType) || ?isAssetType)
FILTER(!BOUND(?assetTypeVar) || !BOUND(?assetType) || BOUND(?isAssetType))
""" + /* Get author's name */ """
?author ?hasFirstName ?firstName ;
?hasLastName ?lastName .
Expand Down Expand Up @@ -149,7 +168,7 @@ public List<AbstractChangeRecord> findAllFiltered(URI changeContext, ChangeRecor
""", AbstractChangeRecord.class)
.setParameter("changeContext", changeContext)
.setParameter("subClassOf", URI.create(RDFS.SUB_CLASS_OF))
.setParameter("changeType", URI.create(cz.cvut.kbss.termit.util.Vocabulary.s_c_zmena))
.setParameter("changeType", URI.create(cz.cvut.kbss.termit.util.Vocabulary.s_c_zmena))
.setParameter("hasChangedEntity", URI.create(cz.cvut.kbss.termit.util.Vocabulary.s_p_ma_zmenenou_entitu))
.setParameter("hasTime", URI.create(cz.cvut.kbss.termit.util.Vocabulary.s_p_ma_datum_a_cas_modifikace))
.setParameter("hasAuthor", URI.create(cz.cvut.kbss.termit.util.Vocabulary.s_p_ma_editora)) // record has author
Expand Down Expand Up @@ -184,6 +203,8 @@ public List<AbstractChangeRecord> findAllFiltered(URI changeContext, ChangeRecor
query = query.setParameter("attributeNameValue", filter.getChangedAttributeName().trim());
}

query = query.setDescriptor(new EntityDescriptor().anyLanguage());

if(pageable.isUnpaged()) {
return query.getResultList();
}
Expand All @@ -192,33 +213,6 @@ public List<AbstractChangeRecord> findAllFiltered(URI changeContext, ChangeRecor
.setMaxResults(pageable.getPageSize()).getResultList();
}

/**
* Finds all change records to the specified asset.
*
* @param asset The changed asset
* @return List of change records ordered by timestamp (descending)
*/
public List<AbstractChangeRecord> findAll(Asset<?> asset) {
Objects.requireNonNull(asset);
try {
final Descriptor descriptor = new EntityDescriptor();
descriptor.setLanguage(null);
return em.createNativeQuery("SELECT ?r WHERE {" +
"?r a ?changeRecord ;" +
"?relatesTo ?asset ;" +
"?hasTime ?timestamp ." +
"OPTIONAL { ?r ?hasChangedAttribute ?attribute . }" +
"} ORDER BY DESC(?timestamp) ?attribute", AbstractChangeRecord.class)
.setParameter("changeRecord", URI.create(Vocabulary.s_c_zmena))
.setParameter("relatesTo", URI.create(Vocabulary.s_p_ma_zmenenou_entitu))
.setParameter("hasChangedAttribute", URI.create(Vocabulary.s_p_ma_zmeneny_atribut))
.setParameter("hasTime", URI.create(Vocabulary.s_p_ma_datum_a_cas_modifikace))
.setParameter("asset", asset.getUri()).setDescriptor(descriptor).getResultList();
} catch (RuntimeException e) {
throw new PersistenceException(e);
}
}

/**
* Gets a set of authors of the specified asset. That is, this method retrieves authors of persist change records
* associated with the specified asset.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,16 @@ private UpdateChangeRecord generateUpdateRecord(Instant timestamp, URI changedOb
@Test
void findAllRetrievesChangeRecordsRelatedToSpecifiedAsset() {
enableRdfsInference(em);
final Term asset = Generator.generateTermWithId();
final Term asset = Generator.generateTermWithId(vocabulary.getUri());
transactional(() -> {
em.persist(vocabulary);
em.persist(asset, persistDescriptor(vocabulary.getUri()));
});
final List<AbstractChangeRecord> records = IntStream.range(0, 5).mapToObj(
i -> generateUpdateRecord(Instant.ofEpochMilli(System.currentTimeMillis() - i * 10000L),
asset.getUri())).collect(Collectors.toList());
transactional(() -> records.forEach(r -> em.persist(r, persistDescriptor(vocabulary.getUri()))));
final URI changeContext = contextResolver.resolveChangeTrackingContext(vocabulary);
transactional(() -> records.forEach(r -> em.persist(r, persistDescriptor(changeContext))));

final List<AbstractChangeRecord> result = sut.findAll(asset);
assertEquals(records.size(), result.size());
Expand All @@ -146,11 +151,16 @@ private Descriptor persistDescriptor(URI context) {
@Test
void findAllReturnsChangeRecordsOrderedByTimestampDescending() {
enableRdfsInference(em);
final Term asset = Generator.generateTermWithId();
final Term asset = Generator.generateTermWithId(vocabulary.getUri());
transactional(() -> {
em.persist(vocabulary);
em.persist(asset, persistDescriptor(vocabulary.getUri()));
});
final List<AbstractChangeRecord> records = IntStream.range(0, 5).mapToObj(
i -> generateUpdateRecord(Instant.ofEpochMilli(System.currentTimeMillis() + i * 10000L),
asset.getUri())).collect(Collectors.toList());
transactional(() -> records.forEach(r -> em.persist(r, persistDescriptor(vocabulary.getUri()))));
final URI changeContext = contextResolver.resolveChangeTrackingContext(vocabulary);
transactional(() -> records.forEach(r -> em.persist(r, persistDescriptor(changeContext))));

final List<AbstractChangeRecord> result = sut.findAll(asset);
records.sort(Comparator.comparing(AbstractChangeRecord::getTimestamp).reversed());
Expand All @@ -160,15 +170,18 @@ void findAllReturnsChangeRecordsOrderedByTimestampDescending() {
@Test
void findAllReturnsChangeRecordsOrderedByTimestampDescendingAndChangedAttributeId() {
enableRdfsInference(em);
final Term asset = Generator.generateTermWithId();
final Term asset = Generator.generateTermWithId(vocabulary.getUri());
final Instant now = Utils.timestamp();
final UpdateChangeRecord rOne = generateUpdateRecord(now, asset.getUri());
rOne.setChangedAttribute(URI.create(SKOS.PREF_LABEL));
final UpdateChangeRecord rTwo = generateUpdateRecord(now, asset.getUri());
rTwo.setChangedAttribute(URI.create(SKOS.DEFINITION));
final Descriptor changeContextDescriptor = persistDescriptor(contextResolver.resolveChangeTrackingContext(vocabulary));
transactional(() -> {
em.persist(rOne, persistDescriptor(vocabulary.getUri()));
em.persist(rTwo, persistDescriptor(vocabulary.getUri()));
em.persist(vocabulary);
em.persist(asset, persistDescriptor(vocabulary.getUri()));
em.persist(rOne, changeContextDescriptor);
em.persist(rTwo, changeContextDescriptor);
});

final List<AbstractChangeRecord> result = sut.findAll(asset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import cz.cvut.kbss.termit.model.changetracking.AbstractChangeRecord;
import cz.cvut.kbss.termit.model.changetracking.UpdateChangeRecord;
import cz.cvut.kbss.termit.persistence.context.DescriptorFactory;
import cz.cvut.kbss.termit.persistence.dao.changetracking.ChangeRecordDao;
import cz.cvut.kbss.termit.service.BaseServiceTestRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -51,6 +52,9 @@ class ChangeRecordServiceTest extends BaseServiceTestRunner {
@Autowired
private ChangeRecordService sut;

@Autowired
private ChangeRecordDao dao;

private User author;

private Vocabulary asset;
Expand Down Expand Up @@ -84,7 +88,9 @@ private List<AbstractChangeRecord> generateChanges() {
r.setTimestamp(Instant.ofEpochMilli(System.currentTimeMillis() - i * 10000L));
return r;
}).collect(Collectors.toList());
transactional(() -> records.forEach(em::persist));
transactional(() -> {
records.forEach(r -> dao.persist(r, asset));
});
return records;
}
}

0 comments on commit e572355

Please sign in to comment.