Skip to content

Commit

Permalink
Test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Oct 3, 2024
1 parent 8e06416 commit 380c004
Showing 1 changed file with 13 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
package ca.uhn.fhir.jpa.cache;

import ca.uhn.fhir.interceptor.model.RequestPartitionId;
import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
import ca.uhn.fhir.jpa.api.svc.IIdHelperService;
import ca.uhn.fhir.jpa.dao.data.IResourceTableDao;
import ca.uhn.fhir.jpa.dao.index.IdHelperService;
import ca.uhn.fhir.jpa.model.config.PartitionSettings;
import ca.uhn.fhir.jpa.model.dao.JpaPid;
import ca.uhn.fhir.jpa.model.entity.ResourceTable;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.api.server.storage.IResourcePersistentId;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Root;
import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.ArrayList;
Expand All @@ -39,12 +31,10 @@
@ExtendWith(MockitoExtension.class)
public class ResourceVersionSvcTest {

@Mock
DaoRegistry myDaoRegistry;
@Mock
IResourceTableDao myResourceTableDao;
@Mock
IIdHelperService myIdHelperService;
IIdHelperService<JpaPid> myIdHelperService;

// TODO KHS move the methods that use this out to a separate test class
@InjectMocks
Expand All @@ -58,66 +48,46 @@ public void before() {
/**
* Gets a ResourceTable record for getResourceVersionsForPid
* Order matters!
*
* @param resourceType
* @param pid
* @param version
* @return
*/
private Object[] getResourceTableRecordForResourceTypeAndPid(String resourceType, long pid, long version) {
private Object[] getResourceTableRecordForResourceTypeAndPid(JpaPid thePid, String theResourceType, String theFhirId, long theVersion) {
return new Object[]{
pid, // long
resourceType, // string
version // long
thePid, // JpaPid
theResourceType, // String
theFhirId, // String
theVersion // Long
};
}

/**
* Helper function to mock out resolveResourcePersistentIdsWithCache
* to return empty lists (as if no resources were found).
*/
private void mock_resolveResourcePersistentIdsWithCache_toReturnNothing() {
CriteriaBuilder cb = Mockito.mock(CriteriaBuilder.class);
CriteriaQuery<ResourceTable> criteriaQuery = Mockito.mock(CriteriaQuery.class);
Root<ResourceTable> from = Mockito.mock(Root.class);
Path path = Mockito.mock(Path.class);
}

/**
* Helper function to mock out getIdsOfExistingResources
* to return the matches and resources matching those provided
* by parameters.
*
* @param theResourcePacks
*/
private void mockReturnsFor_getIdsOfExistingResources(ResourceIdPackage... theResourcePacks) {
List<IResourcePersistentId> resourcePersistentIds = new ArrayList<>();
List<JpaPid> resourcePersistentIds = new ArrayList<>();
List<Object[]> matches = new ArrayList<>();

for (ResourceIdPackage pack : theResourcePacks) {
resourcePersistentIds.add(pack.myPid);

matches.add(getResourceTableRecordForResourceTypeAndPid(
pack.myPid,
pack.MyResourceId.getResourceType(),
pack.myPid.getId(),
pack.MyResourceId.getIdPart(),
pack.MyVersion
));
}

IResourcePersistentId first = resourcePersistentIds.remove(0);
if (resourcePersistentIds.isEmpty()) {
when(myIdHelperService.resolveResourcePersistentIdsWithCache(any(), any())).thenReturn(Collections.singletonList(first));
} else {
when(myIdHelperService.resolveResourcePersistentIdsWithCache(any(), any())).thenReturn(resourcePersistentIds);
}
when(myIdHelperService.resolveResourcePersistentIdsWithCache(any(), any())).thenReturn(resourcePersistentIds);
when(myResourceTableDao.getResourceVersionsForPid(any())).thenReturn(matches);
}

@Test
public void getLatestVersionIdsForResourceIds_whenResourceExists_returnsMapWithPIDAndVersion() {
IIdType type = new IdDt("Patient/RED");
JpaPid jpaPid = JpaPid.fromId(1L);
jpaPid.setAssociatedResourceId(type);
HashMap<IIdType, IResourcePersistentId> map = new HashMap<>();
HashMap<IIdType, JpaPid> map = new HashMap<>();
map.put(type, jpaPid);
ResourceIdPackage pack = new ResourceIdPackage(type, jpaPid, 2L);

Expand All @@ -136,9 +106,6 @@ public void getLatestVersionIdsForResourceIds_whenResourceExists_returnsMapWithP
public void getLatestVersionIdsForResourceIds_whenResourceDoesNotExist_returnsEmptyMap() {
IIdType type = new IdDt("Patient/RED");

// when
mock_resolveResourcePersistentIdsWithCache_toReturnNothing();

// test
ResourcePersistentIdMap retMap = myResourceVersionSvc.getLatestVersionIdsForResourceIds(RequestPartitionId.allPartitions(),
Collections.singletonList(type));
Expand All @@ -158,7 +125,6 @@ public void getLatestVersionIdsForResourceIds_whenSomeResourcesDoNotExist_return
IIdType type2 = new IdDt("Patient/BLUE");

// when
mock_resolveResourcePersistentIdsWithCache_toReturnNothing();
mockReturnsFor_getIdsOfExistingResources(pack);

// test
Expand Down Expand Up @@ -198,7 +164,7 @@ public void testReplaceDefault_DefaultPartition() {
}

// helper class to package up data for helper methods
private class ResourceIdPackage {
private static class ResourceIdPackage {
public IIdType MyResourceId;
public JpaPid myPid;
public Long MyVersion;
Expand Down

0 comments on commit 380c004

Please sign in to comment.