-
Notifications
You must be signed in to change notification settings - Fork 5
/
TestValueSetResourceProvider.java
387 lines (338 loc) · 18.3 KB
/
TestValueSetResourceProvider.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package org.openconceptlab.fhir.provider;
import static org.mockito.Mockito.*;
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.r4.model.*;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import org.mockito.stubbing.OngoingStubbing;
import org.openconceptlab.fhir.base.OclFhirTest;
import org.openconceptlab.fhir.model.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class TestValueSetResourceProvider extends OclFhirTest {
public static final String VEIN_PROCEDURE = "Vein Procedure";
public static final String LUNG_PROCEDURE = "Lung Procedure";
public static final String NECK_PROCEDURE = "Neck Procedure";
public static final String VEIN_PROCEDURE_1 = "Vein Procedure1";
public static final String VEIN_PROCEDURE_2 = "Vein Procedure2";
public static final String VEIN_PROCEDURE_3 = "Vein Procedure3";
public static final String LUNG_PROCEDURE_1 = "Lung Procedure1";
public static final String NECK_PROCEDURE_1 = "Neck Procedure1";
Source source1;
Source source2;
Source source3;
ConceptsSource cs11;
ConceptsSource cs21;
ConceptsSource cs22;
ConceptsSource cs23;
ConceptsSource cs24;
ConceptsSource cs31;
ConceptsSource cs32;
ConceptsSource cs33;
ConceptsSource cs34;
@Before
public void setUpBefore() {
MockitoAnnotations.initMocks(this);
source1 = source(123L, "v1.0", concept1(), concept2(), concept3());
source2 = source(234L, "v2.0", concept1(), concept2(), concept3(), concept4());
source3 = source(345L, "v3.0", concept1(), concept2(), concept3(), concept4());
cs11 = conceptsSource(concept1(), source1);
cs21 = conceptsSource(concept1(), source2);
cs22 = conceptsSource(concept2(), source2);
cs23 = conceptsSource(concept3(), source2);
cs24 = conceptsSource(concept4(), source2);
cs31 = conceptsSource(concept1(), source3);
cs32 = conceptsSource(concept2(), source3);
cs33 = conceptsSource(concept3(), source3);
cs34 = conceptsSource(concept4(), source3);
}
@Test(expected = UnprocessableEntityException.class)
public void testValidateCode_url_null() {
validateCode(null, V_11_1, CS_URL, V_21_1, AD, null, null, null, OWNER_VAL);
}
@Test(expected = UnprocessableEntityException.class)
public void testValidateCode_code_null() {
validateCode(VS_URL, V_11_1, CS_URL, V_21_1, null, null, null, null, OWNER_VAL);
}
@Test(expected = UnprocessableEntityException.class)
public void testValidateCode_system_null() {
validateCode(VS_URL, V_11_1, null, V_21_1, AD, null, null, null, OWNER_VAL);
}
@Test(expected = UnprocessableEntityException.class)
public void testValidateCode_code_coding() {
validateCode(VS_URL, V_11_1, CS_URL, null, AD, null, null,
new Coding("ABC", "ABC", "ABC"), OWNER_VAL);
}
@Test
public void testValidateCode_systemversion_unmatch() {
assertFalse(validateCode(VS_URL, V_11_1, CS_URL, V_21_1, AD, null, null, null, OWNER_VAL));
}
@Test
public void testValidateCode_systemversion_match1() {
assertTrue(validateCode(VS_URL, V_11_1, CS_URL, null, AD, null, null, null, OWNER_VAL));
}
@Test
public void testValidateCode_systemversion_match2() {
assertTrue(validateCode(VS_URL, V_11_1, CS_URL, V_21_2, AD, null, null, null, OWNER_VAL));
}
@Test
public void testValidateCode_display_match1() {
assertTrue(validateCode(VS_URL, V_11_1, CS_URL, V_21_2, AD, ALLERGIC_DISORDER, null, null, OWNER_VAL));
}
@Test
public void testValidateCode_display_match2() {
assertTrue(validateCode(VS_URL, V_11_1, CS_URL, V_21_2, AD, TRASTORNO_ALERGICO, null, null, OWNER_VAL));
}
@Test
public void testValidateCode_display_dl_match1() {
assertTrue(validateCode(VS_URL, V_11_1, CS_URL, V_21_2, AD, ALLERGIC_DISORDER, EN, null, OWNER_VAL));
}
@Test
public void testValidateCode_display_dl_match2() {
assertTrue(validateCode(VS_URL, V_11_1, CS_URL, V_21_2, AD, TRASTORNO_ALERGICO, ES, null, OWNER_VAL));
}
@Test
public void testValidateCode_display_dl_unmatch1() {
assertFalse(validateCode(VS_URL, V_11_1, CS_URL, V_21_2, AD, ALLERGIC_DISORDER, ES, null, OWNER_VAL));
}
@Test
public void testValidateCode_display_dl_unmatch2() {
assertFalse(validateCode(VS_URL, V_11_1, CS_URL, V_21_2, AD, TRASTORNO_ALERGICO, EN, null, OWNER_VAL));
}
@Test
public void testExpand() {
// all match
List<CollectionsReference> references = newReferences(
"/orgs/OCL/sources/"+CS+"/v1.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+TM+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+VEIN_PROCEDURE+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+LUNG_PROCEDURE+"/123/"
);
ValueSet vs = runExpand(references, Collections.singletonList(cs11), Arrays.asList(cs21, cs22, cs23, cs24), null, 0, 50, "");
assertEquals(5, vs.getExpansion().getContains().size());
assertContains(vs, 0, CS_URL, "v2.0", AD, ALLERGIC_DISORDER);
assertContains(vs, 1, CS_URL, "v2.0", LUNG_PROCEDURE, LUNG_PROCEDURE_1);
assertContains(vs, 2, CS_URL, "v2.0", TM, TUMOR_DISORDER);
assertContains(vs, 3, CS_URL, "v2.0", VEIN_PROCEDURE, VEIN_PROCEDURE_1);
assertContains(vs, 4, CS_URL, "v1.0", AD, ALLERGIC_DISORDER);
}
@Test
public void testExpand_partial() {
List<CollectionsReference> references = newReferences(
"/orgs/OCL/sources/"+CS+"/v1.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+TM+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+VEIN_PROCEDURE+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+LUNG_PROCEDURE+"/123/"
);
ValueSet vs = runExpand(references, Collections.singletonList(cs11), Arrays.asList(cs24, cs22, cs23), null, 2, 50, "");
assertEquals(3, vs.getExpansion().getContains().size());
assertContains(vs, 0, CS_URL, "v2.0", LUNG_PROCEDURE, LUNG_PROCEDURE_1);
assertContains(vs, 1, CS_URL, "v2.0", TM, TUMOR_DISORDER);
assertContains(vs, 2, CS_URL, "v2.0", VEIN_PROCEDURE, VEIN_PROCEDURE_1);
}
@Test
public void testExpand_count_0() {
List<CollectionsReference> references = newReferences(
"/orgs/OCL/sources/"+CS+"/v1.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+TM+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+VEIN_PROCEDURE+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+LUNG_PROCEDURE+"/123/"
);
ValueSet vs = runExpand(references, Collections.singletonList(cs11), Collections.singletonList(cs21), null,0, 0, "");
assertEquals(5, vs.getExpansion().getTotal());
assertEquals(0, vs.getExpansion().getContains().size());
}
@Test(expected = UnprocessableEntityException.class)
public void testExpand_count_negative() {
List<CollectionsReference> references = newReferences(
"/orgs/OCL/sources/"+CS+"/v1.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+TM+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+VEIN_PROCEDURE+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+LUNG_PROCEDURE+"/123/"
);
runExpand(references, Collections.singletonList(cs11), Collections.singletonList(cs21), null, 0, -2, "");
}
@Test(expected = UnprocessableEntityException.class)
public void testExpand_missing_url() {
ValueSetResourceProvider provider = provider();
provider.valueSetExpand(null, null, new IntegerType(0), new IntegerType(10),
null, null, null, null, null, null, null, newString(OWNER_VAL));
}
@Test(expected = UnprocessableEntityException.class)
public void testExpand_unknown_systemversion() {
// all match
List<CollectionsReference> references = newReferences(
"/orgs/OCL/sources/"+CS+"/v1.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+TM+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+VEIN_PROCEDURE+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+LUNG_PROCEDURE+"/123/"
);
runExpand(references, Collections.singletonList(cs11), Arrays.asList(cs21, cs22, cs23, cs24),
null, 0, 50, CS_URL+"|unk");
}
@Test
public void testExpand_known_systemversion() {
// all match
List<CollectionsReference> references = newReferences(
"/orgs/OCL/sources/"+CS+"/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/concepts/"+TM+"/123/",
"/orgs/OCL/sources/"+CS+"/concepts/"+VEIN_PROCEDURE+"/123/",
"/orgs/OCL/sources/"+CS+"/concepts/"+LUNG_PROCEDURE+"/123/"
);
ValueSet vs = runExpand(references, Collections.singletonList(cs11), Arrays.asList(cs21, cs22, cs23, cs24),
Arrays.asList(cs31, cs32, cs33, cs34), 0, 50, CS_URL + "|v3.0");
assertEquals(4, vs.getExpansion().getContains().size());
assertContains(vs, 0, CS_URL, "v3.0", AD, ALLERGIC_DISORDER);
assertContains(vs, 1, CS_URL, "v3.0", LUNG_PROCEDURE, LUNG_PROCEDURE_1);
assertContains(vs, 2, CS_URL, "v3.0", TM, TUMOR_DISORDER);
assertContains(vs, 3, CS_URL, "v3.0", VEIN_PROCEDURE, VEIN_PROCEDURE_1);
}
@Test
public void testExpand_known_systemversion_no_head_in_expression() {
// all match
List<CollectionsReference> references = newReferences(
"/orgs/OCL/sources/"+CS+"/v1.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+TM+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+VEIN_PROCEDURE+"/123/",
"/orgs/OCL/sources/"+CS+"/v2.0/concepts/"+LUNG_PROCEDURE+"/123/"
);
ValueSet vs = runExpand(references, Collections.singletonList(cs11), Arrays.asList(cs21, cs22, cs23, cs24),
Arrays.asList(cs31, cs32, cs33, cs34), 0, 50, CS_URL + "|v3.0");
assertEquals(0, vs.getExpansion().getContains().size());
}
private void assertContains(ValueSet valueSet, int index, String system, String version, String code, String display) {
assertEquals(system, valueSet.getExpansion().getContains().get(index).getSystem());
assertEquals(version, valueSet.getExpansion().getContains().get(index).getVersion());
assertEquals(code, valueSet.getExpansion().getContains().get(index).getCode());
assertEquals(display, valueSet.getExpansion().getContains().get(index).getDisplay());
}
public ValueSet runExpand(List<CollectionsReference> references, List<ConceptsSource> list1, List<ConceptsSource> list2,
List<ConceptsSource> list3, Integer offset, Integer count, String systemVersion) {
// set up
ValueSetResourceProvider provider = provider();
Collection collection = collection(references);
when(sourceRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonicAndPublicAccessIn(anyString(), anyString(), anyString(), anyList()))
.thenReturn(source2).thenReturn(source1);
OngoingStubbing<List<ConceptsSource>> stub1 = when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(eq(123L), anyList()));
for (ConceptsSource cs1 : list1) {
stub1 = stub1.thenReturn(Collections.singletonList(cs1));
}
OngoingStubbing<List<ConceptsSource>> stub2 = when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(eq(234L), anyList()));
for (ConceptsSource cs2 : list2) {
stub2 = stub2.thenReturn(Collections.singletonList(cs2));
}
if (isValid(systemVersion) & !systemVersion.contains("unk")) {
when(sourceRepository.findFirstByCanonicalUrlAndVersionAndPublicAccessIn(anyString(), anyString(), anyList())).thenReturn(source3);
OngoingStubbing<List<ConceptsSource>> stub3 = when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(eq(345L), anyList()));
if (list3 != null) {
for (ConceptsSource cs3 : list3) {
stub3 = stub3.thenReturn(Collections.singletonList(cs3));
}
}
}
when(collectionRepository.findFirstByCanonicalUrlAndReleasedAndOrganizationMnemonicAndPublicAccessInOrderByCreatedAtDesc(
anyString(), anyBoolean(), anyString(), anyList())).thenReturn(collection);
return provider.valueSetExpand(newUrl(VS_URL), null, new IntegerType(offset), new IntegerType(count),
null, null, null, null, null, Sets.newHashSet(new CanonicalType(systemVersion)), null, newString(OWNER_VAL));
}
public Parameters validateCode(String url, String version, String system, String systemVersion, String code,
String display, String language, Coding coding, String owner) {
// set up
ValueSetResourceProvider provider = provider();
Concept concept1 = concept1();
Concept concept2 = concept2();
Source source = source(123L, systemVersion, concept1, concept2);
Collection collection = collection(newReferences(
"/orgs/OCL/sources/"+CS+"/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/"+V_21_2+"/concepts/"+AD+"/123/",
"/orgs/OCL/sources/"+CS+"/"+V_21_1+"/concepts/"+TM+"/123/"
));
ConceptsSource cs1 = conceptsSource(concept1, source);
ConceptsSource cs2 = conceptsSource(concept2, source);
// mocks
if (StringUtils.isNotBlank(version)) {
when(collectionRepository.findFirstByCanonicalUrlAndVersionAndOrganizationMnemonicAndPublicAccessIn(anyString(),
anyString(), anyString(), anyList())).thenReturn(collection);
} else {
when(collectionRepository.findFirstByCanonicalUrlAndReleasedAndOrganizationMnemonicAndPublicAccessInOrderByCreatedAtDesc(
anyString(), anyBoolean(), anyString(), anyList())).thenReturn(collection);
}
if (StringUtils.isNotBlank(systemVersion)) {
when(sourceRepository.findFirstByCanonicalUrlAndVersionAndOrganizationMnemonicAndPublicAccessIn(anyString(),
anyString(), anyString(), anyList())).thenReturn(source);
} else {
when(sourceRepository.findFirstByCanonicalUrlAndReleasedAndOrganizationMnemonicAndPublicAccessInOrderByCreatedAtDesc(
anyString(), anyBoolean(), anyString(), anyList())).thenReturn(source);
}
when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(anyLong(), anyList()))
.thenReturn(Arrays.asList(cs1, cs2));
when(conceptRepository.findByMnemonic(anyString())).thenReturn(Arrays.asList(concept1, concept2));
// call to test method
Parameters output = provider.valueSetValidateCode(newUrl(url), newString(version), newCode(code), newUrl(system), newString(systemVersion),
newString(display), newCode(language), coding, newString(owner));
// verify
if (StringUtils.isNotBlank(version)) {
verify(collectionRepository, times(1))
.findFirstByCanonicalUrlAndVersionAndOrganizationMnemonicAndPublicAccessIn(anyString(), anyString(), anyString(), anyList());
} else {
verify(collectionRepository, times(1))
.findFirstByCanonicalUrlAndReleasedAndOrganizationMnemonicAndPublicAccessInOrderByCreatedAtDesc(anyString(), anyBoolean(), anyString(), anyList());
}
if (StringUtils.isNotBlank(systemVersion)) {
verify(sourceRepository, times(1))
.findFirstByCanonicalUrlAndVersionAndOrganizationMnemonicAndPublicAccessIn(anyString(), anyString(), anyString(), anyList());
} else {
verify(sourceRepository, times(1))
.findFirstByCanonicalUrlAndReleasedAndOrganizationMnemonicAndPublicAccessInOrderByCreatedAtDesc(anyString(), anyBoolean(), anyString(), anyList());
}
return output;
}
private ConceptsSource conceptsSource(Concept concept, Source source) {
ConceptsSource cs = new ConceptsSource();
cs.setConcept(concept);
cs.setSource(source);
return cs;
}
private Concept concept1() {
return newConcept(1L, "123", AD, newName(ALLERGIC_DISORDER, "", EN, false),
newName(TRASTORNO_ALERGICO, "", ES, false));
}
private Concept concept2() {
return newConcept(2L,"123", TM, newName(TUMOR_DISORDER, "", EN, false),
newName(TUMOR_TRASTORNO, "", ES, false));
}
private Concept concept3() {
return newConcept(3L, "123", VEIN_PROCEDURE, newName(VEIN_PROCEDURE_1, "", EN, true),
newName(VEIN_PROCEDURE_2, "", ES, false),
newName(VEIN_PROCEDURE_3, "", "fr", false));
}
private Concept concept4() {
return newConcept(4L, "123", LUNG_PROCEDURE, newName(LUNG_PROCEDURE_1, "", EN, true));
}
private Concept concept5() {
return newConcept(5L, "123", NECK_PROCEDURE, newName(NECK_PROCEDURE_1, "", EN, true));
}
private Concept newConcept(Long id, String version, String code, ConceptsName... names) {
Concept concept = concept(id, code);
concept.setVersion(version);
for (ConceptsName name: names) {
concept.getConceptsNames().add(name);
}
return concept;
}
private List<Concept> concepts(Concept... concept) {
return new ArrayList<>(Arrays.asList(concept));
}
}