Skip to content

Commit

Permalink
Fix unit test failures due to lazy loading lack of session.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukedegruchy committed May 22, 2024
1 parent e4121b7 commit 09e45b4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
package ca.uhn.fhir.jpa.dao.data;

import ca.uhn.fhir.jpa.model.entity.ResourceSearchUrlEntity;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

public interface IResourceSearchUrlDao extends JpaRepository<ResourceSearchUrlEntity, Long>, IHapiFhirJpaRepository {

Expand All @@ -36,4 +38,8 @@ public interface IResourceSearchUrlDao extends JpaRepository<ResourceSearchUrlEn
@Modifying
@Query("DELETE FROM ResourceSearchUrlEntity s WHERE (s.myResourceTable.myId = :resID)")
int deleteByResId(@Param("resID") long resId);

@VisibleForTesting
@Query("SELECT u FROM ResourceSearchUrlEntity u JOIN FETCH u.myResourceTable r")
List<ResourceSearchUrlEntity> findAllWithFetchJoin();
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ public static ResourceSearchUrlEntity from(String theUrl, ResourceTable theResou
.setCreatedTime(new Date());
}

public Long getResourcePid() {
return myResourceTable.getResourceId();
}

public ResourceTable getResourceTable() {
return myResourceTable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -170,7 +169,7 @@ public void testMethodDeleteByResId_withEntries_willDeleteTheEntryIfExists(){
myResourceSearchUrlDao.saveAll(asList(entry1, entry2));

// when
myResourceSearchUrlSvc.deleteByResId(entry1.getResourcePid());
myResourceSearchUrlSvc.deleteByResId(entry1.getResourceTable().getResourceId());
myResourceSearchUrlSvc.deleteByResId(nonExistentResourceId);

// then
Expand All @@ -179,10 +178,6 @@ public void testMethodDeleteByResId_withEntries_willDeleteTheEntryIfExists(){

}

private List<Long> getStoredResourceSearchUrlEntitiesPids(){
List<ResourceSearchUrlEntity> remainingSearchUrlEntities = myResourceSearchUrlDao.findAll();
return remainingSearchUrlEntities.stream().map(ResourceSearchUrlEntity::getResourcePid).collect(Collectors.toList());
}

private Date cutOffTimePlus(long theAdjustment) {
long currentTimeMillis = System.currentTimeMillis();
Expand Down Expand Up @@ -225,6 +220,13 @@ private Callable<String> buildResourceAndCreateCallable() {

}

private List<Long> getStoredResourceSearchUrlEntitiesPids() {
return getStoredResourceSearchUrlEntities().stream()
.map(ResourceSearchUrlEntity::getResourceTable)
.map(ResourceTable::getResourceId)
.toList();
}

/**
* PointcutLatch that will force an executing thread to wait (block) until being notified.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,9 @@ public void testCreateResource_withConditionalCreate_willAddSearchUrlEntity(){
String expectedNormalizedMatchUrl = obs.fhirType() + "?" + matchUrl;

assertTrue(outcome.getCreated());
ResourceSearchUrlEntity searchUrlEntity = myResourceSearchUrlDao.findAll().get(0);
ResourceSearchUrlEntity searchUrlEntity = getStoredResourceSearchUrlEntities().get(0);
assertThat(searchUrlEntity, is(notNullValue()) );
assertThat(searchUrlEntity.getResourcePid(), equalTo(expectedResId));
assertThat(searchUrlEntity.getResourceTable().getResourceId(), equalTo(expectedResId));
assertThat(searchUrlEntity.getCreatedTime(), DateMatchers.within(1, SECONDS, new Date()));
assertThat(searchUrlEntity.getSearchUrl(), equalTo(expectedNormalizedMatchUrl));

Expand Down Expand Up @@ -1340,11 +1340,13 @@ public void testConditionalCreateDependsOnFirstEntryExisting(boolean theHasQuest
}

private void assertRemainingTasks(Task... theExpectedTasks) {
final List<ResourceSearchUrlEntity> searchUrlsPreDelete = myResourceSearchUrlDao.findAll();
final List<ResourceSearchUrlEntity> searchUrlsPreDelete = getStoredResourceSearchUrlEntities();

assertEquals(theExpectedTasks.length, searchUrlsPreDelete.size());
assertEquals(Arrays.stream(theExpectedTasks).map(Resource::getIdElement).map(IdType::getIdPartAsLong).toList(),
searchUrlsPreDelete.stream().map(ResourceSearchUrlEntity::getResourcePid).toList());
searchUrlsPreDelete.stream()
.map(ResourceSearchUrlEntity::getResourceTable)
.map(ResourceTable::getId).toList());
}

private void deleteExpunge(Task theTask) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import ca.uhn.fhir.jpa.model.config.PartitionSettings;
import ca.uhn.fhir.jpa.model.dao.JpaPid;
import ca.uhn.fhir.jpa.model.entity.ResourceHistoryTable;
import ca.uhn.fhir.jpa.model.entity.ResourceSearchUrlEntity;
import ca.uhn.fhir.jpa.packages.IPackageInstallerSvc;
import ca.uhn.fhir.jpa.partition.IPartitionLookupSvc;
import ca.uhn.fhir.jpa.provider.JpaSystemProvider;
Expand Down Expand Up @@ -208,6 +209,7 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.util.AopTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -854,6 +856,11 @@ protected void createLocalVs(CodeSystem codeSystem) {
myValueSetDao.update(valueSet, mySrd);
}

protected List<ResourceSearchUrlEntity> getStoredResourceSearchUrlEntities(){
final TransactionTemplate transactionTemplate = new TransactionTemplate(getTxManager());
return transactionTemplate.execute(x -> myResourceSearchUrlDao.findAllWithFetchJoin());
}

@AfterEach
public void afterEachClearCaches() {
myJpaValidationSupportChainR4.invalidateCaches();
Expand Down

0 comments on commit 09e45b4

Please sign in to comment.