Merge pull request #1960 from jamesagnew/im_20200630_code_system_load

Accidentally merged pull request before pushing changes from code review
This commit is contained in:
IanMMarshall 2020-07-03 16:29:34 -04:00 committed by GitHub
commit e23e222f80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 17 deletions

View File

@ -146,7 +146,12 @@ public class TermDeferredStorageSvcImpl implements ITermDeferredStorageSvc {
while (codeCount < count && myDeferredConcepts.size() > 0) {
TermConcept next = myDeferredConcepts.remove(0);
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

@ -12,6 +12,28 @@ public class TermCodeSystemStorageSvcTest extends BaseJpaR4Test {
public static final String URL_MY_CODE_SYSTEM = "http://example.com/my_code_system";
@Test
public void testStoreNewCodeSystemVersionForExistingCodeSystem() {
CodeSystem upload = createCodeSystemWithMoreThan100Concepts();
// Create CodeSystem resource
ResourceTable codeSystemResourceEntity = (ResourceTable)myCodeSystemDao.create(upload, mySrd).getEntity();
// Update the CodeSystem resource
runInTransaction(() -> myTermCodeSystemStorageSvc.storeNewCodeSystemVersionIfNeeded(upload, codeSystemResourceEntity));
/*
Because there are more than 100 concepts in the code system, the first 100 will be persisted immediately and
the remaining 25 concepts will be queued up for "deferred save".
As the CodeSystem was persisted twice, the extra 25 term concepts will be queued twice, each with a different
CodeSystem version PID. Only one set of the term concepts should be persisted (i.e. 125 term concepts in total).
*/
myTerminologyDeferredStorageSvc.setProcessDeferred(true);
myTerminologyDeferredStorageSvc.saveDeferred();
assertEquals(125, myTermConceptDao.count());
}
private CodeSystem createCodeSystemWithMoreThan100Concepts() {
CodeSystem codeSystem = new CodeSystem();
codeSystem.setUrl(URL_MY_CODE_SYSTEM);
@ -25,20 +47,5 @@ public class TermCodeSystemStorageSvcTest extends BaseJpaR4Test {
}
@Test
public void testStoreNewCodeSystemVersionForExistingCodeSystem() {
CodeSystem upload = createCodeSystemWithMoreThan100Concepts();
ResourceTable codeSystemResourceEntity = (ResourceTable)myCodeSystemDao.create(upload, mySrd).getEntity();
runInTransaction(() -> myTermCodeSystemStorageSvc.storeNewCodeSystemVersionIfNeeded(upload, codeSystemResourceEntity));
myTerminologyDeferredStorageSvc.setProcessDeferred(true);
myTerminologyDeferredStorageSvc.saveDeferred();
myTerminologyDeferredStorageSvc.saveDeferred();
assertEquals(125, myTermConceptDao.count());
}
}

View File

@ -21,7 +21,6 @@ import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class TermDeferredStorageSvcImplTest {
@Mock
private PlatformTransactionManager myTxManager;
@Mock
@ -69,6 +68,7 @@ public class TermDeferredStorageSvcImplTest {
concept.setCode("CODE_A");
TermCodeSystemVersion myTermCodeSystemVersion = new TermCodeSystemVersion();
myTermCodeSystemVersion.setId(1L);
concept.setCodeSystemVersion(myTermCodeSystemVersion);
TermDeferredStorageSvcImpl svc = new TermDeferredStorageSvcImpl();
@ -76,15 +76,45 @@ 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);
svc.addConceptToStorageQueue(concept);
svc.saveDeferred();
verify(myTermConceptStorageSvc, times(1)).saveConcept(same(concept));
verifyNoMoreInteractions(myTermConceptStorageSvc);
}
@Test
public void testSaveDeferred_ConceptParentChildLink_ConceptsMissing() {
TermConceptParentChildLink conceptLink = new TermConceptParentChildLink();