Changes from code review

This commit is contained in:
ianmarshall 2020-07-03 14:25:24 -04:00
parent 4e5ca2ee15
commit b1ff6cbe37
2 changed files with 41 additions and 5 deletions

View File

@ -50,7 +50,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.PostConstruct;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -146,10 +145,15 @@ public class TermDeferredStorageSvcImpl implements ITermDeferredStorageSvc {
ourLog.info("Saving {} deferred concepts...", count);
while (codeCount < count && myDeferredConcepts.size() > 0) {
TermConcept next = myDeferredConcepts.remove(0);
try {
codeCount += myCodeSystemStorageSvc.saveConcept(next);
} catch (Exception theE) {
ourLog.warn("Unable to save deferred TermConcept {}, possibly because Code System {} version PID {} may have since been replaced.",
if(myCodeSystemVersionDao.findById(next.getCodeSystemVersion().getPid()).isPresent()) {
try {
codeCount += myCodeSystemStorageSvc.saveConcept(next);
} catch (Exception theE) {
ourLog.error("Exception thrown when attempting to save TermConcept {} in Code System {}",
next.getCode(), next.getCodeSystemVersion().getCodeSystemDisplayName(), theE);
}
} else {
ourLog.warn("Unable to save deferred TermConcept {} because Code System {} version PID {} is no longer valid. Code system may have since been replaced.",
next.getCode(), next.getCodeSystemVersion().getCodeSystemDisplayName(), next.getCodeSystemVersion().getPid());
}
}

View File

@ -13,6 +13,8 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.transaction.PlatformTransactionManager;
import java.util.Optional;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.*;
@ -50,6 +52,7 @@ public class TermDeferredStorageSvcImplTest {
svc.setCodeSystemStorageSvcForUnitTest(myTermConceptStorageSvc);
svc.setDaoConfigForUnitTest(new DaoConfig());
when(myTermCodeSystemVersionDao.findById(anyLong())).thenReturn(Optional.of(myTermCodeSystemVersion));
svc.setCodeSystemVersionDaoForUnitTest(myTermCodeSystemVersionDao);
svc.setProcessDeferred(true);
svc.addConceptToStorageQueue(concept);
@ -65,6 +68,7 @@ public class TermDeferredStorageSvcImplTest {
concept.setCode("CODE_A");
TermCodeSystemVersion myTermCodeSystemVersion = new TermCodeSystemVersion();
myTermCodeSystemVersion.setId(1L);
concept.setCodeSystemVersion(myTermCodeSystemVersion);
TermDeferredStorageSvcImpl svc = new TermDeferredStorageSvcImpl();
@ -72,6 +76,34 @@ public class TermDeferredStorageSvcImplTest {
svc.setCodeSystemStorageSvcForUnitTest(myTermConceptStorageSvc);
svc.setDaoConfigForUnitTest(new DaoConfig());
when(myTermCodeSystemVersionDao.findById(anyLong())).thenReturn(Optional.empty());
svc.setCodeSystemVersionDaoForUnitTest(myTermCodeSystemVersionDao);
svc.setProcessDeferred(true);
svc.addConceptToStorageQueue(concept);
svc.saveDeferred();
verify(myTermConceptStorageSvc, times(0)).saveConcept(same(concept));
verifyNoMoreInteractions(myTermConceptStorageSvc);
}
@Test
public void testSaveDeferred_Concept_Exception() {
// There is a small
TermConcept concept = new TermConcept();
concept.setCode("CODE_A");
TermCodeSystemVersion myTermCodeSystemVersion = new TermCodeSystemVersion();
myTermCodeSystemVersion.setId(1L);
concept.setCodeSystemVersion(myTermCodeSystemVersion);
TermDeferredStorageSvcImpl svc = new TermDeferredStorageSvcImpl();
svc.setTransactionManagerForUnitTest(myTxManager);
svc.setCodeSystemStorageSvcForUnitTest(myTermConceptStorageSvc);
svc.setDaoConfigForUnitTest(new DaoConfig());
// Simulate the case where an exception is thrown despite a valid code system version.
when(myTermCodeSystemVersionDao.findById(anyLong())).thenReturn(Optional.of(myTermCodeSystemVersion));
when(myTermConceptStorageSvc.saveConcept(concept)).thenThrow(new RuntimeException("Foreign Constraint Violation"));
svc.setCodeSystemVersionDaoForUnitTest(myTermCodeSystemVersionDao);
svc.setProcessDeferred(true);