-
Notifications
You must be signed in to change notification settings - Fork 5
/
TestValueSetResourceProvider.java
979 lines (887 loc) · 55.7 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
package org.openconceptlab.fhir.provider;
import ca.uhn.fhir.rest.server.exceptions.AuthenticationException;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.r4.model.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.OngoingStubbing;
import org.openconceptlab.fhir.base.OclFhirTest;
import org.openconceptlab.fhir.model.*;
import org.openconceptlab.fhir.util.OclFhirUtil;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import static org.openconceptlab.fhir.util.OclFhirConstants.*;
public class TestValueSetResourceProvider extends OclFhirTest {
Collection collection1;
Collection collection2;
public static final String URL_COLLECTION_1 = "http://openconceptlab.org/collection1";
public static final String COLLECTION_1 = "collection1";
public static final String COLLECTION_1_NAME = "collection1 name";
public static final String COLLECTION_1_FULL_NAME = "collection1 full name";
public static final String COLLECTION_1_COPYRIGHT_TEXT = "collection1 copyright text";
public static final String TEST = "TEST";
public static final String URL_COLLECTION_2 = "http://openconceptlab.org/collection2";
public static final String COLLECTION_2 = "collection2";
public static final String COLLECTION_2_NAME = "collection2 name";
public static final String COLLECTION_2_FULL_NAME = "collection2 full name";
public static final String COLLECTION_2_COPYRIGHT_TEXT = "collection2 copyright text";
@Before
public void setUpBefore() {
MockitoAnnotations.initMocks(this);
when(requestDetails.getCompleteUrl()).thenReturn("http://test.org");
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());
populateSource1(source1);
populateSource2(source2);
collection1 = new Collection();
populateCollection1(collection1);
collection2 = new Collection();
populateCollection2(collection2);
}
@After
public void after() {
source1 = null;
source2 = null;
source3 = null;
collection1 = null;
collection2 = null;
cs11 = null;
cs21 = null;
cs22 = null;
cs23 = null;
cs24 = null;
cs31 = null;
cs32 = null;
cs33 = null;
cs34 = null;
}
private void populateCollection2(Collection collection2) {
collection2.setMnemonic(COLLECTION_2);
collection2.setUri(URI_COLLECTION_2);
collection2.setCanonicalUrl(URL_COLLECTION_2);
collection2.setName(COLLECTION_2_NAME);
collection2.setFullName(COLLECTION_2_FULL_NAME);
collection2.setDescription(COLLECTION_2_NAME);
collection2.setIsActive(false);
collection2.setPublisher(TEST);
collection2.setContact("[{\"name\": \"Jon Doe 2\", \"telecom\": [{\"use\": \"work\", \"rank\": 1, \"value\": \"jondoe2@gmail.com\", \"period\": {\"end\": \"2022-10-29T10:26:15-04:00\", \"start\": \"2021-10-29T10:26:15-04:00\"}, \"system\": \"email\"}]}]");
collection2.setJurisdiction("[{\"coding\": [{\"code\": \"ETH\", \"system\": \"http://unstats.un.org/unsd/methods/m49/m49.htm\", \"display\": \"Ethiopia\"}]}]");
collection2.setPurpose(TEST);
collection2.setCopyright(COLLECTION_2_COPYRIGHT_TEXT);
collection2.setImmutable(true);
collection2.setRevisionDate(Date.from(LocalDate.of(2020, 12, 2).atStartOfDay(ZoneId.systemDefault()).toInstant()));
}
private void populateCollection1(Collection collection1) {
collection1.setMnemonic(COLLECTION_1);
collection1.setUri(URI_COLLECTION_1);
collection1.setCanonicalUrl(URL_COLLECTION_1);
collection1.setName(COLLECTION_1_NAME);
collection1.setFullName(COLLECTION_1_FULL_NAME);
collection1.setDescription(COLLECTION_1_NAME);
collection1.setIsActive(true);
collection1.setPublisher(TEST);
collection1.setContact("[{\"name\": \"Jon Doe 1\", \"telecom\": [{\"use\": \"work\", \"rank\": 1, \"value\": \"jondoe1@gmail.com\", \"period\": {\"end\": \"2025-10-29T10:26:15-04:00\", \"start\": \"2020-10-29T10:26:15-04:00\"}, \"system\": \"email\"}]}]");
collection1.setJurisdiction("[{\"coding\": [{\"code\": \"USA\", \"system\": \"http://unstats.un.org/unsd/methods/m49/m49.htm\", \"display\": \"United States of America\"}]}]");
collection1.setPurpose(TEST);
collection1.setCopyright(COLLECTION_1_COPYRIGHT_TEXT);
collection1.setImmutable(false);
collection1.setRevisionDate(Date.from(LocalDate.of(2020, 12, 1).atStartOfDay(ZoneId.systemDefault()).toInstant()));
List<CollectionsReference> references = newReferences(
"/orgs/OCL/sources/source1/v1.0/concepts/"+AD+"/123/",
"/orgs/OCL/sources/source2/v2.0/concepts/"+TUMOR_DISORDER+"/123/"
);
collection1.setCollectionsReferences(references);
}
@Test
public void testSearchValueSet_return_1() {
collection1.setReleased(true);
when(collectionRepository.findAllMostRecentReleased(anyList())).thenReturn(Collections.singletonList(collection1));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSets(null, null, requestDetails);
assertEquals(1, bundle.getTotal());
ValueSet valueSet = (ValueSet) bundle.getEntryFirstRep().getResource();
assertEquals(0, valueSet.getCompose().getInclude().size());
assertBaseValueSet(valueSet, URL_COLLECTION_1, COLLECTION_1_NAME, COLLECTION_1_FULL_NAME, "Jon Doe 1", "jondoe1@gmail.com",
"USA", TEST, COLLECTION_1_COPYRIGHT_TEXT);
}
@Test
public void testSearchValueSet_return_2() {
collection1.setReleased(true);
collection2.setReleased(true);
when(collectionRepository.findAllMostRecentReleased(anyList())).thenReturn(Arrays.asList(collection1, collection2));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSets(null, null, requestDetails);
assertEquals(2, bundle.getTotal());
ValueSet valueSet1 = (ValueSet) bundle.getEntry().get(0).getResource();
assertEquals(0, valueSet1.getCompose().getInclude().size());
assertBaseValueSet(valueSet1, URL_COLLECTION_1, COLLECTION_1_NAME, COLLECTION_1_FULL_NAME, "Jon Doe 1", "jondoe1@gmail.com",
"USA", TEST, COLLECTION_1_COPYRIGHT_TEXT);
ValueSet valueSet2 = (ValueSet) bundle.getEntry().get(1).getResource();
assertEquals(0, valueSet2.getCompose().getInclude().size());
assertBaseValueSet(valueSet2, URL_COLLECTION_2, COLLECTION_2_NAME, COLLECTION_2_FULL_NAME, "Jon Doe 2", "jondoe2@gmail.com",
"ETH", TEST, COLLECTION_2_COPYRIGHT_TEXT);
}
@Test
public void testSearchValueSet_return_empty() {
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSets(null, null, requestDetails);
assertEquals(0, bundle.getTotal());
}
@Test
public void testSearchValueSet_head_return_empty() {
collection1.setIsLatestVersion(true);
collection2.setIsLatestVersion(true);
collection1.setVersion("HEAD");
collection2.setVersion("HEAD");
when(collectionRepository.findByPublicAccessIn(anyList())).thenReturn(Arrays.asList(collection1, collection2));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSets(null, null, requestDetails);
assertEquals(0, bundle.getTotal());
}
@Test
public void testSearchValueSetByUrl_version_empty_return_most_recent() {
when(collectionRepository.findFirstByCanonicalUrlAndReleasedAndPublicAccessInOrderByCreatedAtDesc(anyString(), anyBoolean(), anyList()))
.thenReturn(collection1);
when(sourceRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonicAndPublicAccessIn(anyString(), anyString(), anyString(), anyList()))
.thenReturn(source1).thenReturn(source2);
when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(anyLong(), anyList())).thenReturn(Collections.singletonList(cs22))
.thenReturn(Collections.singletonList(cs11));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSetByUrl(newString(URL_COLLECTION_1), null, null, null, requestDetails);
assertEquals(1, bundle.getTotal());
ValueSet valueSet = (ValueSet) bundle.getEntryFirstRep().getResource();
assertBaseValueSet(valueSet, URL_COLLECTION_1, COLLECTION_1_NAME, COLLECTION_1_FULL_NAME, "Jon Doe 1", "jondoe1@gmail.com",
"USA", TEST, COLLECTION_1_COPYRIGHT_TEXT);
assertConceptComponent(0, valueSet.getCompose().getInclude().get(0), URL_SOURCE_2, V_2_0, TM, TUMOR_DISORDER);
assertConceptComponent(0, valueSet.getCompose().getInclude().get(1), URL_SOURCE_1, V_1_0, AD, ALLERGIC_DISORDER);
}
@Test
public void testSearchValueSetByUrl_version() {
when(collectionRepository.findFirstByCanonicalUrlAndVersionAndPublicAccessIn(anyString(), anyString(), anyList()))
.thenReturn(collection1);
when(sourceRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonicAndPublicAccessIn(anyString(), anyString(), anyString(), anyList()))
.thenReturn(source1).thenReturn(source2);
when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(anyLong(), anyList())).thenReturn(Collections.singletonList(cs22))
.thenReturn(Collections.singletonList(cs11));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSetByUrl(newString(URL_COLLECTION_1), newString(V_1_0), null, null, requestDetails);
assertEquals(1, bundle.getTotal());
ValueSet valueSet = (ValueSet) bundle.getEntryFirstRep().getResource();
assertBaseValueSet(valueSet, URL_COLLECTION_1, COLLECTION_1_NAME, COLLECTION_1_FULL_NAME, "Jon Doe 1", "jondoe1@gmail.com",
"USA", TEST, COLLECTION_1_COPYRIGHT_TEXT);
assertConceptComponent(0, valueSet.getCompose().getInclude().get(0), URL_SOURCE_2, V_2_0, TM, TUMOR_DISORDER);
assertConceptComponent(0, valueSet.getCompose().getInclude().get(1), URL_SOURCE_1, V_1_0, AD, ALLERGIC_DISORDER);
}
@Test
public void testSearchValueSetByUrl_version_all() {
collection1.setVersion(V_1_0);
Collection collection1V2 = new Collection();
populateCollection1(collection1V2);
collection1V2.setVersion("v1.1");
when(collectionRepository.findByCanonicalUrlAndPublicAccessIn(anyString(), anyList()))
.thenReturn(Arrays.asList(collection1, collection1V2));
when(sourceRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonicAndPublicAccessIn(anyString(), anyString(), anyString(), anyList()))
.thenReturn(source1).thenReturn(source2);
when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(anyLong(), anyList())).thenReturn(Collections.singletonList(cs22))
.thenReturn(Collections.singletonList(cs11));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSetByUrl(newString(URL_COLLECTION_1), newString("*"), null, null, requestDetails);
assertEquals(2, bundle.getTotal());
ValueSet valueSet1 = (ValueSet) bundle.getEntry().get(0).getResource();
ValueSet valueSet2 = (ValueSet) bundle.getEntry().get(1).getResource();
assertEquals(V_1_0, valueSet1.getVersion());
assertEquals("v1.1", valueSet2.getVersion());
}
@Test
public void testSearchValueSetByUrl_version_head() {
when(collectionRepository.findFirstByCanonicalUrlAndVersionAndPublicAccessIn(anyString(), anyString(), anyList()))
.thenReturn(collection1);
when(sourceRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonicAndPublicAccessIn(anyString(), anyString(), anyString(), anyList()))
.thenReturn(source1).thenReturn(source2);
when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(anyLong(), anyList())).thenReturn(Collections.singletonList(cs22))
.thenReturn(Collections.singletonList(cs11));
collection1.setVersion("HEAD");
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSetByUrl(newString(URL_COLLECTION_1), newString(V_1_0), null, null, requestDetails);
assertEquals(0, bundle.getTotal());
}
@Test(expected = ResourceNotFoundException.class)
public void testSearchValueSetByUrl_not_found() {
ValueSetResourceProvider provider = valueSetProvider();
provider.searchValueSetByUrl(newString(URL_COLLECTION_1), null, null, null, requestDetails);
}
@Test
public void testSearchValueSetByOwner() {
collection1.setReleased(true);
when(collectionRepository.findByOrganizationMnemonicAndPublicAccessIn(anyString(), anyList()))
.thenReturn(Collections.singletonList(collection1));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSetByOwner(newString("org:OCL"), null, null, requestDetails);
assertEquals(1, bundle.getTotal());
assertBaseValueSet((ValueSet) bundle.getEntryFirstRep().getResource(), URL_COLLECTION_1, COLLECTION_1_NAME, COLLECTION_1_FULL_NAME, "Jon Doe 1", "jondoe1@gmail.com",
"USA", TEST, COLLECTION_1_COPYRIGHT_TEXT);
assertEquals(0, ((ValueSet) bundle.getEntryFirstRep().getResource()).getCompose().getInclude().size());
}
@Test
public void testSearchValueSetByOwner_user() {
collection1.setReleased(true);
when(collectionRepository.findByUserIdUsernameAndPublicAccessIn(anyString(), anyList()))
.thenReturn(Collections.singletonList(collection1));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSetByOwner(newString("user:test"), null, null, requestDetails);
assertEquals(1, bundle.getTotal());
assertBaseValueSet((ValueSet) bundle.getEntryFirstRep().getResource(), URL_COLLECTION_1, COLLECTION_1_NAME, COLLECTION_1_FULL_NAME, "Jon Doe 1", "jondoe1@gmail.com",
"USA", TEST, COLLECTION_1_COPYRIGHT_TEXT);
assertEquals(0, ((ValueSet) bundle.getEntryFirstRep().getResource()).getCompose().getInclude().size());
}
@Test
public void testSearchValueSetByOwnerAndId_version_empty() {
when(collectionRepository.findFirstByMnemonicAndReleasedAndPublicAccessInAndOrganizationMnemonicOrderByCreatedAtDesc(anyString(), anyBoolean(), anyList(), anyString()))
.thenReturn(collection1);
when(sourceRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonicAndPublicAccessIn(anyString(), anyString(), anyString(), anyList()))
.thenReturn(source1).thenReturn(source2);
when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(anyLong(), anyList())).thenReturn(Collections.singletonList(cs22))
.thenReturn(Collections.singletonList(cs11));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSetByOwnerAndId(newString("org:OCL"), newString("123"), null, null, null, requestDetails);
assertEquals(1, bundle.getTotal());
ValueSet valueSet = (ValueSet) bundle.getEntryFirstRep().getResource();
assertBaseValueSet(valueSet, URL_COLLECTION_1, COLLECTION_1_NAME, COLLECTION_1_FULL_NAME, "Jon Doe 1", "jondoe1@gmail.com",
"USA", TEST, COLLECTION_1_COPYRIGHT_TEXT);
assertConceptComponent(0, valueSet.getCompose().getInclude().get(0), URL_SOURCE_2, V_2_0, TM, TUMOR_DISORDER);
assertConceptComponent(0, valueSet.getCompose().getInclude().get(1), URL_SOURCE_1, V_1_0, AD, ALLERGIC_DISORDER);
}
@Test
public void testSearchValueSetByOwnerAndId_expression_owner_user() {
List<CollectionsReference> references = newReferences(
"/users/test/sources/source1/v1.0/concepts/"+AD+"/123/",
"/users/test/sources/source2/v2.0/concepts/"+TUMOR_DISORDER+"/123/"
);
collection1.setCollectionsReferences(references);
when(collectionRepository.findFirstByMnemonicAndReleasedAndPublicAccessInAndUserIdUsernameOrderByCreatedAtDesc(anyString(), anyBoolean(), anyList(), anyString()))
.thenReturn(collection1);
when(sourceRepository.findFirstByMnemonicAndVersionAndUserIdUsernameAndPublicAccessIn(anyString(), anyString(), anyString(), anyList()))
.thenReturn(source1).thenReturn(source2);
when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(anyLong(), anyList())).thenReturn(Collections.singletonList(cs22))
.thenReturn(Collections.singletonList(cs11));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSetByOwnerAndId(newString("user:test"), newString("123"), null, null, null, requestDetails);
assertEquals(1, bundle.getTotal());
ValueSet valueSet = (ValueSet) bundle.getEntryFirstRep().getResource();
assertBaseValueSet(valueSet, URL_COLLECTION_1, COLLECTION_1_NAME, COLLECTION_1_FULL_NAME, "Jon Doe 1", "jondoe1@gmail.com",
"USA", TEST, COLLECTION_1_COPYRIGHT_TEXT);
assertEquals(2, ((ValueSet) bundle.getEntryFirstRep().getResource()).getCompose().getInclude().size());
assertConceptComponent(0, valueSet.getCompose().getInclude().get(0), URL_SOURCE_2, V_2_0, TM, TUMOR_DISORDER);
assertConceptComponent(0, valueSet.getCompose().getInclude().get(1), URL_SOURCE_1, V_1_0, AD, ALLERGIC_DISORDER);
}
@Test
public void testSearchValueSetByOwnerAndId_expression_source_version_not_found() {
List<CollectionsReference> references = newReferences(
"/users/test/sources/source1/v1111.0/concepts/"+AD+"/123/",
"/users/test/sources/source2/v2222.0/concepts/"+TUMOR_DISORDER+"/123/"
);
collection1.setCollectionsReferences(references);
when(collectionRepository.findFirstByMnemonicAndReleasedAndPublicAccessInAndUserIdUsernameOrderByCreatedAtDesc(anyString(), anyBoolean(), anyList(), anyString()))
.thenReturn(collection1);
when(sourceRepository.findFirstByMnemonicAndVersionAndUserIdUsernameAndPublicAccessIn(anyString(), anyString(), anyString(), anyList()))
.thenReturn(source1).thenReturn(source2);
when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(anyLong(), anyList())).thenReturn(Collections.singletonList(cs22))
.thenReturn(Collections.singletonList(cs11));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSetByOwnerAndId(newString("user:test"), newString("123"), null, null, null, requestDetails);
assertEquals(1, bundle.getTotal());
ValueSet valueSet = (ValueSet) bundle.getEntryFirstRep().getResource();
assertBaseValueSet(valueSet, URL_COLLECTION_1, COLLECTION_1_NAME, COLLECTION_1_FULL_NAME, "Jon Doe 1", "jondoe1@gmail.com",
"USA", TEST, COLLECTION_1_COPYRIGHT_TEXT);
assertEquals(0, ((ValueSet) bundle.getEntryFirstRep().getResource()).getCompose().getInclude().size());
}
@Test
public void testSearchValueSetByOwnerAndId_expression_source_not_found() {
List<CollectionsReference> references = newReferences(
"/users/test/sources/source111/v1.0/concepts/"+AD+"/123/",
"/users/test/sources/source222/v2.0/concepts/"+TUMOR_DISORDER+"/123/"
);
collection1.setCollectionsReferences(references);
when(collectionRepository.findFirstByMnemonicAndReleasedAndPublicAccessInAndUserIdUsernameOrderByCreatedAtDesc(anyString(), anyBoolean(), anyList(), anyString()))
.thenReturn(collection1);
when(sourceRepository.findFirstByMnemonicAndVersionAndUserIdUsernameAndPublicAccessIn(anyString(), anyString(), anyString(), anyList()))
.thenReturn(source1).thenReturn(source2);
when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(anyLong(), anyList())).thenReturn(Collections.singletonList(cs22))
.thenReturn(Collections.singletonList(cs11));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSetByOwnerAndId(newString("user:test"), newString("123"), null, null, null, requestDetails);
assertEquals(1, bundle.getTotal());
ValueSet valueSet = (ValueSet) bundle.getEntryFirstRep().getResource();
assertBaseValueSet(valueSet, URL_COLLECTION_1, COLLECTION_1_NAME, COLLECTION_1_FULL_NAME, "Jon Doe 1", "jondoe1@gmail.com",
"USA", TEST, COLLECTION_1_COPYRIGHT_TEXT);
assertEquals(0, ((ValueSet) bundle.getEntryFirstRep().getResource()).getCompose().getInclude().size());
}
@Test
public void testSearchValueSetByOwnerAndId_expression_owner_not_found() {
List<CollectionsReference> references = newReferences(
"/users/OCL/sources/source111/v1.0/concepts/"+AD+"/123/",
"/users/OCL/sources/source222/v2.0/concepts/"+TUMOR_DISORDER+"/123/"
);
collection1.setCollectionsReferences(references);
when(collectionRepository.findFirstByMnemonicAndReleasedAndPublicAccessInAndUserIdUsernameOrderByCreatedAtDesc(anyString(), anyBoolean(), anyList(), anyString()))
.thenReturn(collection1);
when(sourceRepository.findFirstByMnemonicAndVersionAndUserIdUsernameAndPublicAccessIn(anyString(), anyString(), anyString(), anyList()))
.thenReturn(source1).thenReturn(source2);
when(conceptsSourceRepository.findBySourceIdAndConceptIdInOrderByConceptIdDesc(anyLong(), anyList())).thenReturn(Collections.singletonList(cs22))
.thenReturn(Collections.singletonList(cs11));
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSetByOwnerAndId(newString("user:test"), newString("123"), null, null, null, requestDetails);
assertEquals(1, bundle.getTotal());
ValueSet valueSet = (ValueSet) bundle.getEntryFirstRep().getResource();
assertBaseValueSet(valueSet, URL_COLLECTION_1, COLLECTION_1_NAME, COLLECTION_1_FULL_NAME, "Jon Doe 1", "jondoe1@gmail.com",
"USA", TEST, COLLECTION_1_COPYRIGHT_TEXT);
assertEquals(0, ((ValueSet) bundle.getEntryFirstRep().getResource()).getCompose().getInclude().size());
}
@Test
public void testSearchValueSetByOwnerAndId_expression_concept_not_found() {
List<CollectionsReference> references = newReferences(
"/users/OCL/sources/source1/v1.0/concepts/"+AD+"/123/",
"/users/OCL/sources/source2/v2.0/concepts/"+TUMOR_DISORDER+"/123/"
);
collection1.setCollectionsReferences(references);
when(collectionRepository.findFirstByMnemonicAndReleasedAndPublicAccessInAndUserIdUsernameOrderByCreatedAtDesc(anyString(), anyBoolean(), anyList(), anyString()))
.thenReturn(collection1);
when(sourceRepository.findFirstByMnemonicAndVersionAndUserIdUsernameAndPublicAccessIn(anyString(), anyString(), anyString(), anyList()))
.thenReturn(source1).thenReturn(source2);
ValueSetResourceProvider provider = valueSetProvider();
Bundle bundle = provider.searchValueSetByOwnerAndId(newString("user:test"), newString("123"), null, null, null, requestDetails);
assertEquals(1, bundle.getTotal());
ValueSet valueSet = (ValueSet) bundle.getEntryFirstRep().getResource();
assertBaseValueSet(valueSet, URL_COLLECTION_1, COLLECTION_1_NAME, COLLECTION_1_FULL_NAME, "Jon Doe 1", "jondoe1@gmail.com",
"USA", TEST, COLLECTION_1_COPYRIGHT_TEXT);
assertEquals(0, ((ValueSet) bundle.getEntryFirstRep().getResource()).getCompose().getInclude().size());
}
private void assertConceptComponent(int index, ValueSet.ConceptSetComponent component, String systemUrl, String systemVersion, String code, String display) {
assertEquals(systemUrl, component.getSystem());
assertEquals(systemVersion, component.getVersion());
assertEquals(code, component.getConcept().get(index).getCode());
assertEquals(display, component.getConcept().get(index).getDisplay());
}
private void assertBaseValueSet(ValueSet valueSet, String url, String name, String fullName,
String contactName, String contactEmail, String jurisdictionCode, String purpose,
String copyright) {
assertEquals(url, valueSet.getUrl());
assertEquals(name, valueSet.getName());
assertEquals(fullName, valueSet.getTitle());
assertEquals(contactName, valueSet.getContactFirstRep().getName());
assertEquals(contactEmail, valueSet.getContactFirstRep().getTelecomFirstRep().getValue());
assertEquals(jurisdictionCode, valueSet.getJurisdictionFirstRep().getCodingFirstRep().getCode());
assertEquals(purpose, valueSet.getPurpose());
assertEquals(copyright, valueSet.getCopyright());
}
@Test(expected = InvalidRequestException.class)
public void testValidateCode_url_null() {
validateCode(null, V_11_1, CS_URL, V_21_1, AD, null, null, null, OWNER_VAL);
}
@Test(expected = InvalidRequestException.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 = InvalidRequestException.class)
public void testValidateCode_system_null() {
validateCode(VS_URL, V_11_1, null, V_21_1, AD, null, null, null, OWNER_VAL);
}
@Test(expected = InvalidRequestException.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/"
);
source1.setCanonicalUrl(CS_URL);
source1.setMnemonic(CS);
source2.setCanonicalUrl(CS_URL);
source2.setMnemonic(CS);
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/"
);
source1.setCanonicalUrl(CS_URL);
source1.setMnemonic(CS);
source2.setCanonicalUrl(CS_URL);
source2.setMnemonic(CS);
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 = InvalidRequestException.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 = InvalidRequestException.class)
public void testExpand_missing_url() {
ValueSetResourceProvider provider = valueSetProvider();
provider.valueSetExpand(null, null, new IntegerType(0), new IntegerType(10),
null, null, null, null, null, null, null, newString(OWNER_VAL), requestDetails);
}
@Test(expected = InvalidRequestException.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());
}
@Test(expected = InvalidRequestException.class)
public void testCreateValueSet_null() {
ValueSetResourceProvider provider = valueSetProvider();
provider.createValueSet(null, requestDetails);
}
@Test(expected = InvalidRequestException.class)
public void testCreateValueSet_accessionId_null() {
ValueSetResourceProvider provider = valueSetProvider();
ValueSet valueSet = valueSet();
valueSet.setIdentifier(null);
provider.createValueSet(valueSet, requestDetails);
}
@Test(expected = InvalidRequestException.class)
public void testCreateValueSet_url_null() {
ValueSetResourceProvider provider = valueSetProvider();
ValueSet valueSet = valueSet();
valueSet.setUrl(null);
provider.createValueSet(valueSet, requestDetails);
}
@Test(expected = InvalidRequestException.class)
public void testCreateValueSet_invalid_org() {
ValueSetResourceProvider provider = valueSetProvider();
ValueSet valueSet = valueSet();
when(organizationRepository.findByMnemonic(anyString())).thenReturn(null);
provider.createValueSet(valueSet, requestDetails);
verify(organizationRepository, times(1)).findByMnemonic(anyString());
}
@Test(expected = InvalidRequestException.class)
public void testCreateValueSet_invalid_user() {
ValueSetResourceProvider provider = valueSetProvider();
ValueSet valueSet = valueSet();
valueSet.getIdentifierFirstRep().setValue("/users/testuser/ValueSet/testsource/2.0");
when(userRepository.findByUsername(anyString())).thenReturn(null);
provider.createValueSet(valueSet, requestDetails);
verify(userRepository, times(1)).findByUsername(anyString());
}
@Test(expected = AuthenticationException.class)
public void testCreateValueSet_token_null() {
ValueSetResourceProvider provider = valueSetProvider();
ValueSet valueSet = valueSet();
when(requestDetails.getHeader(anyString())).thenReturn(null);
when(organizationRepository.findByMnemonic(anyString())).thenReturn(newOrganization());
provider.createValueSet(valueSet, requestDetails);
verify(requestDetails, times(1)).getHeader(anyString());
verify(organizationRepository, times(1)).findByMnemonic(anyString());
}
@Test(expected = AuthenticationException.class)
public void testCreateValueSet_invalid_token() {
ValueSetResourceProvider provider = valueSetProvider();
ValueSet valueSet = valueSet();
when(requestDetails.getHeader(anyString())).thenReturn("Token 678423578911230985");
when(organizationRepository.findByMnemonic(anyString())).thenReturn(newOrganization());
when(authtokenRepository.findByKey(anyString())).thenReturn(null);
provider.createValueSet(valueSet, requestDetails);
verify(requestDetails, times(1)).getHeader(anyString());
verify(organizationRepository, times(1)).findByMnemonic(anyString());
verify(authtokenRepository, times(1)).findByKey(anyString());
}
@Test(expected = AuthenticationException.class)
public void testCreateValueSet_user_not_org_member_invalid_user() {
ValueSetResourceProvider provider = valueSetProvider();
ValueSet valueSet = valueSet();
when(requestDetails.getHeader(anyString())).thenReturn("Token 12345");
when(organizationRepository.findByMnemonic(anyString())).thenReturn(newOrganization());
when(authtokenRepository.findByKey(anyString())).thenReturn(newToken(test_user));
when(userProfilesOrganizationRepository.findByOrganizationMnemonic(anyString()))
.thenReturn(Collections.singletonList(newUserOrg("otheruser")));
provider.createValueSet(valueSet, requestDetails);
verify(requestDetails, times(1)).getHeader(anyString());
verify(organizationRepository, times(1)).findByMnemonic(anyString());
verify(authtokenRepository, times(1)).findByKey(anyString());
verify(userProfilesOrganizationRepository, times(1)).findByOrganizationMnemonic(anyString());
}
@Test(expected = ResourceVersionConflictException.class)
public void testCreateValueSet_valueset_id_exists() {
ValueSetResourceProvider provider = valueSetProvider();
ValueSet valueSet = valueSet();
when(requestDetails.getHeader(anyString())).thenReturn("Token 12345");
when(organizationRepository.findByMnemonic(anyString())).thenReturn(newOrganization());
when(authtokenRepository.findByKey(anyString())).thenReturn(newToken(test_user));
when(userProfilesOrganizationRepository.findByOrganizationMnemonic(anyString()))
.thenReturn(Collections.singletonList(newUserOrg(test_user)));
when(collectionRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonic(anyString(), anyString(), anyString()))
.thenReturn(new Collection());
provider.createValueSet(valueSet, requestDetails);
verify(requestDetails, times(1)).getHeader(anyString());
verify(organizationRepository, times(1)).findByMnemonic(anyString());
verify(authtokenRepository, times(1)).findByKey(anyString());
verify(userProfilesOrganizationRepository, times(1)).findByOrganizationMnemonic(anyString());
verify(collectionRepository, times(1)).findFirstByMnemonicAndVersionAndOrganizationMnemonic(anyString(), anyString(), anyString());
}
@Test(expected = ResourceVersionConflictException.class)
public void testCreateValueSet_valueset_url_exists() {
ValueSetResourceProvider provider = valueSetProvider();
ValueSet valueSet = valueSet();
when(requestDetails.getHeader(anyString())).thenReturn("Token 12345");
when(organizationRepository.findByMnemonic(anyString())).thenReturn(newOrganization());
when(authtokenRepository.findByKey(anyString())).thenReturn(newToken(test_user));
when(userProfilesOrganizationRepository.findByOrganizationMnemonic(anyString()))
.thenReturn(Collections.singletonList(newUserOrg(test_user)));
when(collectionRepository.findFirstByCanonicalUrlAndVersionAndOrganizationMnemonic(anyString(), anyString(), anyString()))
.thenReturn(new Collection());
provider.createValueSet(valueSet, requestDetails);
verify(requestDetails, times(1)).getHeader(anyString());
verify(organizationRepository, times(1)).findByMnemonic(anyString());
verify(authtokenRepository, times(1)).findByKey(anyString());
verify(userProfilesOrganizationRepository, times(1)).findByOrganizationMnemonic(anyString());
verify(collectionRepository, times(1)).findFirstByCanonicalUrlAndVersionAndOrganizationMnemonic(anyString(), anyString(), anyString());
}
@Test
public void testCreateValueSet() throws SQLException {
ValueSetResourceProvider provider = valueSetProvider();
ValueSet valueSet = valueSet();
when(requestDetails.getHeader(anyString())).thenReturn("Token 12345");
when(organizationRepository.findByMnemonic(anyString())).thenReturn(newOrganization());
when(authtokenRepository.findByKey(anyString())).thenReturn(newToken(test_user));
when(userProfilesOrganizationRepository.findByOrganizationMnemonic(anyString()))
.thenReturn(Collections.singletonList(newUserOrg(test_user)));
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
((Collection)args[0]).setId(123L);
return args[0];
}
}).when(collectionRepository).saveAndFlush(any(Collection.class));
when(insertCollectionReference.executeAndReturnKeyHolder(anyMap())).thenReturn(newKey());
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(jdbcTemplate).batchUpdate(anyString(), any(BatchPreparedStatementSetter.class));
populateSource1(source1);
source1.setOrganization(newOrganization());
when(sourceRepository.findFirstByCanonicalUrlAndReleasedAndPublicAccessInOrderByCreatedAtDesc(anyString(), anyBoolean(), anyList()))
.thenReturn(source1);
provider.createValueSet(valueSet, requestDetails);
verify(requestDetails, times(1)).getHeader(anyString());
verify(organizationRepository, times(1)).findByMnemonic(anyString());
verify(authtokenRepository, times(1)).findByKey(anyString());
verify(userProfilesOrganizationRepository, times(1)).findByOrganizationMnemonic(anyString());
verify(collectionRepository, times(1)).saveAndFlush(any(Collection.class));
verify(insertCollectionReference, times(1)).executeAndReturnKeyHolder(anyMap());
}
@Test
public void testUpdateValueSet() throws SQLException {
ValueSetResourceProvider provider = valueSetProvider();
ValueSet valueSet = valueSet();
valueSet.setName("Updated Name");
when(requestDetails.getHeader(AUTHORIZATION)).thenReturn("Token 12345");
when(requestDetails.getHeader(OWNER)).thenReturn("org:OCL");
when(organizationRepository.findByMnemonic(anyString())).thenReturn(newOrganization());
when(authtokenRepository.findByKey(anyString())).thenReturn(newToken(test_user));
when(userProfilesOrganizationRepository.findByOrganizationMnemonic(anyString()))
.thenReturn(Collections.singletonList(newUserOrg(test_user)));
when(collectionRepository.findFirstByMnemonicAndVersionAndOrganizationMnemonicAndPublicAccessIn(
anyString(), anyString(), anyString(), anyList()
)).thenReturn(collection1);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
((Collection)args[0]).setId(123L);
return args[0];
}
}).when(collectionRepository).saveAndFlush(any(Collection.class));
when(insertCollectionReference.executeAndReturnKeyHolder(anyMap())).thenReturn(newKey());
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(jdbcTemplate).batchUpdate(anyString(), any(BatchPreparedStatementSetter.class));
populateSource1(source1);
source1.setOrganization(newOrganization());
when(sourceRepository.findFirstByCanonicalUrlAndReleasedAndPublicAccessInOrderByCreatedAtDesc(anyString(), anyBoolean(), anyList()))
.thenReturn(source1);
IdType type = new IdType();
type.setParts("", "ValueSet", "testcollection", "2.0");
provider.updateValueSet(type, valueSet, requestDetails);
assertEquals("Updated Name", collection1.getName());
verify(requestDetails, times(1)).getHeader(AUTHORIZATION);
verify(requestDetails, times(1)).getHeader(OWNER);
verify(organizationRepository, times(1)).findByMnemonic(anyString());
verify(authtokenRepository, times(1)).findByKey(anyString());
verify(userProfilesOrganizationRepository, times(1)).findByOrganizationMnemonic(anyString());
verify(collectionRepository, times(1)).saveAndFlush(any(Collection.class));
verify(insertCollectionReference, times(1)).executeAndReturnKeyHolder(anyMap());
}
private ValueSet valueSet() {
ValueSet valueSet = new ValueSet();
valueSet.setUrl(URL_COLLECTION_1);
Identifier identifier = valueSet.getIdentifierFirstRep();
identifier.getType().getCodingFirstRep().setSystem(ACSN_SYSTEM).setCode(ACSN);
identifier.setSystem("http://test.org");
identifier.setValue("/orgs/OCL/ValueSet/testcollection/2.0");
valueSet.setName("Test Value Set");
valueSet.setStatus(Enumerations.PublicationStatus.DRAFT);
valueSet.setCopyright("Test copy right");
valueSet.setPurpose("Test purpose");
valueSet.setPublisher("Test publisher");
valueSet.setDescription("Test description");
valueSet.setTitle("Test title");
valueSet.setDate(date1);
valueSet.getContactFirstRep().setName("Jon Doe").getTelecomFirstRep().setSystem(ContactPoint.ContactPointSystem.EMAIL)
.setValue("jondoe@gmail.com").setUse(ContactPoint.ContactPointUse.WORK).setRank(1);
valueSet.getJurisdictionFirstRep().getCodingFirstRep().setSystem("http://unstats.un.org/unsd/methods/m49/m49.htm")
.setCode("USA").setDisplay("United States of America");
addCodes(valueSet, URL_SOURCE_1, "TEST");
return valueSet;
}
private void addCodes(ValueSet valueSet, String system, String code) {
ValueSet.ValueSetComposeComponent compose = valueSet.getCompose();
ValueSet.ConceptSetComponent conceptSetComponent = compose.getIncludeFirstRep();
conceptSetComponent.setSystem(system);
ValueSet.ConceptReferenceComponent referenceComponent = new ValueSet.ConceptReferenceComponent();
referenceComponent.setCode(code);
conceptSetComponent.getConcept().add(referenceComponent);
}
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 = valueSetProvider();
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), requestDetails);
}
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 = valueSetProvider();
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), requestDetails);
// 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;
}
@Test
public void testToOclUri() {
assertEquals("/orgs/OCL/sources/test/1.0/", OclFhirUtil.toOclUri("/orgs/OCL/CodeSystem/test/version/1.0"));
assertEquals("/orgs/OCL/sources/test/1.0/", OclFhirUtil.toOclUri("/orgs/OCL/CodeSystem/test/version/1.0/"));
assertEquals("/orgs/OCL/sources/test/1.0/", OclFhirUtil.toOclUri("orgs/OCL/CodeSystem/test/version/1.0"));
assertEquals("/orgs/OCL/sources/test/1.0/", OclFhirUtil.toOclUri("orgs/OCL/codesystem/test/version/1.0"));
assertEquals("/orgs/OCL/sources/test/1.0/", OclFhirUtil.toOclUri("orgs/OCL/cODesYsteM/test/version/1.0"));
assertEquals("/orgs/OCL/sources/test/1.0/", OclFhirUtil.toOclUri("/orgs/OCL/CodeSystem/test/1.0"));
assertEquals("/orgs/OCL/sources/test/1.0/", OclFhirUtil.toOclUri("/orgs/OCL/CodeSystem/test/1.0/"));
assertEquals("/orgs/OCL/sources/test/1.0/", OclFhirUtil.toOclUri("orgs/OCL/CodeSystem/test/1.0"));
assertEquals("/orgs/OCL/sources/test/1.0/", OclFhirUtil.toOclUri("orgs/OCL/codesystem/test/1.0"));
assertEquals("/orgs/OCL/sources/test/", OclFhirUtil.toOclUri("orgs/OCL/cODesYsteM/test/"));
assertEquals("/orgs/OCL/sources/test/", OclFhirUtil.toOclUri("orgs/OCL/CodeSystem/test/ "));
assertEquals("/orgs/OCL/sources/test/", OclFhirUtil.toOclUri(" orgs/OCL/CodeSystem/test/ "));
assertEquals("/orgs/OCL/sources/test/", OclFhirUtil.toOclUri(" /orgs/OCL/CodeSystem/test "));
assertEquals("/orgs/OCL/sources/test/", OclFhirUtil.toOclUri("/orgs/OCL/codesystem/test/ "));
assertEquals("/orgs/OCL/sources/test/", OclFhirUtil.toOclUri("/orgs/OCL/cODesYsteM/test/ "));
}
}