Commit b6819891 authored by Matija Obreza's avatar Matija Obreza
Browse files

AccessionTriggers: updated tests

parent 14c439e0
......@@ -18,6 +18,7 @@ package org.gringlobal.service.triggers;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import org.apache.commons.collections4.IterableUtils;
......@@ -25,6 +26,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
......@@ -101,14 +103,22 @@ public class AccessionTriggers implements InitializingBean {
accessionClassPk = classPKService.getClassPk(Accession.class);
}
@Before(value = "execution(* org.genesys.blocks.auditlog.persistence.AuditLogRepository.saveAll(Iterable)) && args(auditLogs)")
public void beforeSaveAuditLogs(final JoinPoint joinPoint, final Collection<AuditLog> auditLogs) throws Throwable {
@After(value = "(execution(* org.genesys.blocks.auditlog.persistence.AuditLogRepository.save(..)) || execution(* org.genesys.blocks.auditlog.persistence.AuditLogRepository.saveAndFlush(..))) && args(auditLog)")
public void afterSaveAuditLogs(final JoinPoint joinPoint, final AuditLog auditLog) throws Throwable {
afterSaveAuditLogs(null, List.of(auditLog));
}
@After(value = "execution(* org.genesys.blocks.auditlog.persistence.AuditLogRepository.saveAll(Iterable)) && args(auditLogs)")
public void afterSaveAuditLogs(final JoinPoint joinPoint, final Collection<AuditLog> auditLogs) throws Throwable {
LOG.trace("Checking {} saved auditLogs, cpk={}", auditLogs.size(), this.accessionClassPk);
var changedAccessions = new HashMap<Long, HashMap<String, Pair<Object, Object>>>();
auditLogs.stream().filter(this::isAccessionChangeLog).forEach(auditLog -> {
var changes = changedAccessions.get(auditLog.getEntityId());
if (changes == null) {
changedAccessions.put(auditLog.getEntityId(), changes = new HashMap<String, Pair<Object, Object>>());
}
LOG.trace("{}#{} {} -> {}", auditLog.getEntityId(), auditLog.getPropertyName(), auditLog.getPreviousState(), auditLog.getNewState());
LOG.trace("{}#{} {} -> {}", auditLog.getEntityId(), auditLog.getPropertyName(), auditLog.getPreviousEntity(), auditLog.getNewEntity());
changes.put(auditLog.getPropertyName(), Pair.of(auditLog.getPreviousEntity(), auditLog.getNewEntity()));
});
changedAccessions.forEach((accessionId, changes) -> {
......@@ -204,7 +214,7 @@ public class AccessionTriggers implements InitializingBean {
private void recordAccessionNumberIfNeeded(long accessionId, String oldNumber, String newNumber, String oldNumberPart1, String newNumberPart1) {
LOG.trace("Recording name change aid={} {}/{} -> {}/{}", accessionId, oldNumberPart1, oldNumber, newNumberPart1, newNumber);
if (oldNumber != null && !newNumber.equals(oldNumber)) {
if (newNumber != null && !newNumber.equals(oldNumber)) {
LOG.debug("Recording name change aid={} {}/{} -> {}/{}", accessionId, oldNumberPart1, oldNumber, newNumberPart1, newNumber);
var accession = new Accession(accessionId);
......@@ -231,6 +241,7 @@ public class AccessionTriggers implements InitializingBean {
action.setStartedDateCode(CommunityCodeValues.DATE_FORMAT_DATETIME.value);
action.setCompletedDateCode(CommunityCodeValues.DATE_FORMAT_DATETIME.value);
action = actionService.create(action);
LOG.debug("Added action {}: {}", action.getActionNameCode(), action.getNote());
assert (action != null);
} else {
LOG.trace("Not recording name change {} {}", oldNumber, newNumber);
......@@ -256,7 +267,7 @@ public class AccessionTriggers implements InitializingBean {
}
private void recordAccessionSpeciesChangesIfNeeded(long accessionId, TaxonomySpecies oldTs, TaxonomySpecies newTs) {
LOG.debug("recordAccessionSpeciesChangesIfNeeded: orig.id={} curr=", oldTs, newTs);
LOG.debug("recordAccessionSpeciesChangesIfNeeded: orig.id={} curr=", oldTs.getId(), newTs.getId());
if (oldTs != null && !Objects.equals(oldTs.getId(), newTs.getId())) {
AccessionInvAnnotation annotation = new AccessionInvAnnotation();
annotation.setAnnotationTypeCode(CommunityCodeValues.ANNOTATION_TYPE_RE_IDENT.value);
......
......@@ -205,8 +205,8 @@ public class AccessionServiceTest extends AbstractServicesTest {
assertThat(accessionInvNameRepository.count(), is(2L));
List<AccessionInvName> allNames = accessionInvNameRepository.findAll();
assertThat(allNames.stream().anyMatch(a -> a.getPlantName().equals(accession.getAccessionNumber())), is(true));
assertThat(allNames.stream().anyMatch(a -> a.getPlantName().equals(updated.getAccessionNumber())), is(true));
assertThat(allNames.stream().anyMatch(a -> a.getPlantName().equals("AccessionNumberPart1")), is(true));
assertThat(allNames.stream().anyMatch(a -> a.getPlantName().equals("AccessionNumberPart1 part3")), is(true));
// assert that name is registered with name group
var nameGroup = new NameGroup();
......@@ -215,12 +215,15 @@ public class AccessionServiceTest extends AbstractServicesTest {
updated.setAccessionNumberPart1("TMe");
updated.setAccessionNumberPart2(-1l);
updated.setAccessionNumberPart3("A");
var newUpdated = accessionService.update(updated);
assertThat(accessionInvNameRepository.count(), is(3L));
allNames = accessionInvNameRepository.findAll();
// A name with NameGroup exists!
assertThat(allNames.stream().anyMatch(a -> a.getPlantName().equals(newUpdated.getAccessionNumber()) && a.getNameGroup() != null && savedNameGroup.getId().equals(a.getNameGroup().getId())), is(true));
assertThat(allNames.stream().anyMatch(a -> a.getPlantName().equals("AccessionNumberPart1")), is(true));
assertThat(allNames.stream().anyMatch(a -> a.getPlantName().equals("AccessionNumberPart1 part3")), is(true));
assertThat(allNames.stream().anyMatch(a -> a.getPlantName().equals("TMe 1 A") && a.getNameGroup() != null && savedNameGroup.getGroupName().equals("TMe")), is(true));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment