-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
MdmSurvivorshipSvcImplTest.java
219 lines (194 loc) · 7.5 KB
/
MdmSurvivorshipSvcImplTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package ca.uhn.fhir.jpa.mdm.svc;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.interceptor.model.RequestPartitionId;
import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
import ca.uhn.fhir.jpa.dao.index.IdHelperService;
import ca.uhn.fhir.jpa.dao.tx.HapiTransactionService;
import ca.uhn.fhir.jpa.dao.tx.IHapiTransactionService;
import ca.uhn.fhir.jpa.model.dao.JpaPid;
import ca.uhn.fhir.mdm.api.IMdmLinkQuerySvc;
import ca.uhn.fhir.mdm.api.IMdmSettings;
import ca.uhn.fhir.mdm.api.MdmLinkSourceEnum;
import ca.uhn.fhir.mdm.api.MdmMatchResultEnum;
import ca.uhn.fhir.mdm.model.CanonicalEID;
import ca.uhn.fhir.mdm.model.MdmTransactionContext;
import ca.uhn.fhir.mdm.model.mdmevents.MdmLinkJson;
import ca.uhn.fhir.mdm.rules.json.MdmRulesJson;
import ca.uhn.fhir.mdm.svc.MdmSurvivorshipSvcImpl;
import ca.uhn.fhir.mdm.util.EIDHelper;
import ca.uhn.fhir.mdm.util.GoldenResourceHelper;
import ca.uhn.fhir.mdm.util.MdmPartitionHelper;
import ca.uhn.fhir.mdm.util.MdmResourceUtil;
import ca.uhn.fhir.rest.api.server.RequestDetails;
import ca.uhn.fhir.rest.api.server.storage.IResourcePersistentId;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.Patient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class MdmSurvivorshipSvcImplTest {
@Spy
private FhirContext myFhirContext = FhirContext.forR4Cached();
@Mock
private DaoRegistry myDaoRegistry;
private GoldenResourceHelper myGoldenResourceHelper;
// mocks for our GoldenResourceHelper
@Mock
private IMdmSettings myMdmSettings;
@Mock
private EIDHelper myEIDHelper;
@Mock
private MdmPartitionHelper myMdmPartitionHelper;
@Mock
private IdHelperService myIIdHelperService;
@Mock
private IMdmLinkQuerySvc myMdmLinkQuerySvc;
@Mock
private HapiTransactionService myTransactionService;
private MdmSurvivorshipSvcImpl mySvc;
@BeforeEach
public void before() {
myGoldenResourceHelper = spy(new GoldenResourceHelper(
myFhirContext,
myMdmSettings,
myEIDHelper,
myMdmPartitionHelper
));
mySvc = new MdmSurvivorshipSvcImpl(
myFhirContext,
myGoldenResourceHelper,
myDaoRegistry,
myMdmLinkQuerySvc,
myIIdHelperService,
myTransactionService
);
}
@SuppressWarnings({"rawtypes", "unchecked"})
@ParameterizedTest
@ValueSource(booleans = {true,false})
public void rebuildGoldenResourceCurrentLinksUsingSurvivorshipRules_withManyLinks_rebuildsInUpdateOrder(boolean theIsUseNonNumericId) {
// setup
// create resources
int goldenId = 777;
Patient goldenPatient = new Patient();
goldenPatient.addAddress()
.setCity("Toronto")
.addLine("200 fake st");
goldenPatient.addName()
.setFamily("Doe")
.addGiven("Jane");
goldenPatient.setId("Patient/" + goldenId);
MdmResourceUtil.setMdmManaged(goldenPatient);
MdmResourceUtil.setGoldenResource(goldenPatient);
List<IBaseResource> resources = new ArrayList<>();
List<MdmLinkJson> links = new ArrayList<>();
Map<String, IResourcePersistentId> sourceIdToPid = new HashMap<>();
for (int i = 0; i < 10; i++) {
// we want our resources to be slightly different
Patient patient = new Patient();
patient.addName()
.setFamily("Doe")
.addGiven("John" + i);
patient.addAddress()
.setCity("Toronto")
.addLine(String.format("11%d fake st", i));
patient.addIdentifier()
.setSystem("http:https://example.com")
.setValue("Value" + i);
patient.setId("Patient/" + (theIsUseNonNumericId ? "pat" + i : Integer.toString(i)));
resources.add(patient);
MdmLinkJson link = createLinkJson(
patient,
goldenPatient
);
link.setSourcePid(JpaPid.fromId((long)i));
link.setGoldenPid(JpaPid.fromId((long)goldenId));
link.setSourceId(patient.getId());
link.setGoldenResourceId(goldenPatient.getId());
links.add(link);
sourceIdToPid.put(patient.getId(), link.getSourcePid());
}
IFhirResourceDao resourceDao = mock(IFhirResourceDao.class);
// when
IHapiTransactionService.IExecutionBuilder executionBuilder = mock(IHapiTransactionService.IExecutionBuilder.class);
when(myTransactionService.withRequest(any())).thenReturn(executionBuilder);
doAnswer(a -> {
Runnable callback = a.getArgument(0);
callback.run();
return 0;
}).when(executionBuilder).execute(any(Runnable.class));
when(myDaoRegistry.getResourceDao(eq("Patient")))
.thenReturn(resourceDao);
AtomicInteger counter = new AtomicInteger();
when(resourceDao.readByPid(any()))
.thenAnswer(params -> resources.get(counter.getAndIncrement()));
Page<MdmLinkJson> linkPage = new PageImpl<>(links);
when(myMdmLinkQuerySvc.queryLinks(any(), any()))
.thenReturn(linkPage);
when(myMdmSettings.getMdmRules())
.thenReturn(new MdmRulesJson());
doReturn(sourceIdToPid).when(myIIdHelperService)
.resolveResourcePersistentIds(any(RequestPartitionId.class), anyString(), any(List.class));
// we will return a non-empty list to reduce mocking
when(myEIDHelper.getExternalEid(any()))
.thenReturn(Collections.singletonList(new CanonicalEID("example", "value", "use")));
// test
Patient goldenPatientRebuilt = mySvc.rebuildGoldenResourceWithSurvivorshipRules(
goldenPatient,
createTransactionContext()
);
// verify
assertNotNull(goldenPatientRebuilt);
// make sure it doesn't match the previous golden resource
assertThat(goldenPatientRebuilt).isNotEqualTo(goldenPatient);
assertThat(goldenPatientRebuilt.getName().get(0).getGiven()).isNotEqualTo(goldenPatient.getName().get(0).getGiven());
assertThat(goldenPatientRebuilt.getAddress().get(0).getLine().get(0)).isNotEqualTo(goldenPatient.getAddress().get(0).getLine().get(0));
// make sure it's still a golden resource
assertTrue(MdmResourceUtil.isGoldenRecord(goldenPatientRebuilt));
verify(resourceDao)
.update(eq(goldenPatientRebuilt), any(RequestDetails.class));
}
private MdmTransactionContext createTransactionContext() {
MdmTransactionContext context = new MdmTransactionContext();
context.setRestOperation(MdmTransactionContext.OperationType.UPDATE_LINK);
context.setResourceType("Patient");
return context;
}
private MdmLinkJson createLinkJson(
IBaseResource theSource,
IBaseResource theGolden
) {
MdmLinkJson linkJson = new MdmLinkJson();
linkJson.setLinkSource(MdmLinkSourceEnum.AUTO);
linkJson.setGoldenResourceId(theGolden.getIdElement().getValueAsString());
linkJson.setSourceId(theSource.getIdElement().getValueAsString());
linkJson.setMatchResult(MdmMatchResultEnum.MATCH);
return linkJson;
}
}