-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
EncounterTest.java
1392 lines (1142 loc) · 43.5 KB
/
EncounterTest.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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mock;
import org.openmrs.api.EncounterService;
import org.openmrs.api.ProviderService;
import org.openmrs.test.BaseContextMockTest;
import org.openmrs.test.Verifies;
/**
* This class tests the all of the {@link Encounter} non-trivial object methods.
*
* @see Encounter
*/
public class EncounterTest extends BaseContextMockTest {
@Mock
EncounterService encounterService;
@Mock
ProviderService providerService;
/**
* @see Encounter#toString()
*/
@Test
@Verifies(value = "should not fail with empty object", method = "toString()")
public void toString_shouldNotFailWithEmptyObject() throws Exception {
Encounter encounter = new Encounter();
@SuppressWarnings("unused")
String toStringOutput = encounter.toString();
}
/**
* @see Encounter#removeObs(Obs)
*/
@Test
@Verifies(value = "should remove obs successfully", method = "removeObs(Obs)")
public void removeObs_shouldRemoveObsSuccessfully() throws Exception {
Obs obsToRemove = new Obs();
Set<Obs> obsSet = new HashSet<Obs>();
obsSet.add(obsToRemove);
// add the set of obs to the encounter and make sure its there
Encounter encounter = new Encounter();
encounter.setObs(obsSet);
Assert.assertEquals(1, encounter.getAllObs(true).size());
Assert.assertTrue(encounter.getAllObs(true).contains(obsToRemove));
// remove the obs and make sure its gone from the encounter
encounter.removeObs(obsToRemove);
Assert.assertEquals(0, encounter.getAllObs(true).size());
}
/**
* @see Encounter#removeObs(Obs)
*/
@Test
@Verifies(value = "should not throw error when removing null obs from empty set", method = "removeObs(Obs)")
public void removeObs_shouldNotThrowErrorWhenRemovingNullObsFromEmptySet() throws Exception {
Encounter encounterWithoutObsSet = new Encounter();
encounterWithoutObsSet.removeObs(null);
}
/**
* @see Encounter#removeObs(Obs)
*/
@Test
@Verifies(value = "should not throw error when removing null obs from non empty set", method = "removeObs(Obs)")
public void removeObs_shouldNotThrowErrorWhenRemovingNullObsFromNonEmptySet() throws Exception {
Encounter encounterWithObsSet = new Encounter();
Set<Obs> obsSet = new HashSet<Obs>();
obsSet.add(new Obs());
encounterWithObsSet.setObs(obsSet);
// make sure the encounter got the obs
Assert.assertEquals(1, encounterWithObsSet.getAllObs(true).size());
encounterWithObsSet.removeObs(null);
}
/**
* @see Encounter#getObs()
*/
@Test
@Verifies(value = "should not return null with null obs set", method = "getObs()")
public void getObs_shouldNotReturnNullWithNullObsSet() throws Exception {
Encounter encounter = new Encounter();
assertNotNull(encounter.getObs());
assertEquals(encounter.getObs().size(), 0);
}
/**
* @see Encounter#getAllObs(null)
*/
@Test
@Verifies(value = "should not return null with null obs set", method = "getAllObs(null)")
public void getAllObs_shouldNotReturnNullWithNullObsSet() throws Exception {
Encounter encounter = new Encounter();
assertNotNull(encounter.getAllObs(true));
assertEquals(encounter.getAllObs(true).size(), 0);
assertNotNull(encounter.getAllObs(false));
assertEquals(encounter.getAllObs(false).size(), 0);
}
/**
* @see Encounter#getObsAtTopLevel(null)
*/
@Test
@Verifies(value = "should not return null with null obs set", method = "getObsAtTopLevel(null)")
public void getObsAtTopLevel_shouldNotReturnNullWithNullObsSet() throws Exception {
Encounter encounter = new Encounter();
assertNotNull(encounter.getObsAtTopLevel(true));
assertEquals(encounter.getObsAtTopLevel(true).size(), 0);
assertNotNull(encounter.getObsAtTopLevel(false));
assertEquals(encounter.getObsAtTopLevel(false).size(), 0);
}
/**
* @see Encounter#getObs()
*/
@Test
@Verifies(value = "should get obs", method = "getObs()")
public void getObs_shouldGetObs() throws Exception {
Encounter encounter = new Encounter();
//create and add an Obs
Obs o = new Obs();
encounter.addObs(o);
assertNotNull(encounter.getObs());
assertEquals(1, encounter.getObs().size());
}
/**
* @see Encounter#getObsAtTopLevel(null)
*/
@Test
@Verifies(value = "should get obs", method = "getObsAtTopLevel(null)")
public void getObsAtTopLevel_shouldGetObs() throws Exception {
Encounter encounter = new Encounter();
//create and add an Obs
Obs o = new Obs();
encounter.addObs(o);
assertNotNull(encounter.getObsAtTopLevel(true));
assertEquals(1, encounter.getObsAtTopLevel(true).size());
assertNotNull(encounter.getObsAtTopLevel(false));
assertEquals(1, encounter.getObsAtTopLevel(false).size());
}
/**
* @see Encounter#getAllObs(null)
*/
@Test
@Verifies(value = "should get obs", method = "getAllObs(null)")
public void getAllObs_shouldGetObs() throws Exception {
Encounter encounter = new Encounter();
//create and add an Obs
Obs o = new Obs();
encounter.addObs(o);
assertNotNull(encounter.getAllObs(true));
assertEquals(1, encounter.getAllObs(true).size());
assertNotNull(encounter.getAllObs(false));
assertEquals(1, encounter.getAllObs(false).size());
}
/**
* @see Encounter#getObs()
*/
@Test
@Verifies(value = "should not get voided obs", method = "getObs()")
public void getObs_shouldNotGetVoidedObs() throws Exception {
Encounter enc = new Encounter();
//create and add an Obs
Obs o = new Obs();
o.setVoided(true);
enc.addObs(o);
assertEquals(0, enc.getObs().size());
}
/**
* @see Encounter#getObsAtTopLevel(null)
*/
@Test
@Verifies(value = "should not get voided obs", method = "getObsAtTopLevel(null)")
public void getObsAtTopLevel_shouldNotGetVoidedObs() throws Exception {
Encounter enc = new Encounter();
//create and add an Obs
Obs o = new Obs();
o.setVoided(true);
enc.addObs(o);
assertNotNull(enc.getObsAtTopLevel(true));
assertEquals(1, enc.getObsAtTopLevel(true).size());
assertNotNull(enc.getObsAtTopLevel(false));
assertEquals(0, enc.getObsAtTopLevel(false).size());
}
/**
* @see Encounter#getAllObs(null)
*/
@Test
@Verifies(value = "should not get voided obs", method = "getAllObs(null)")
public void getAllObs_shouldNotGetVoidedObs() throws Exception {
Encounter enc = new Encounter();
//create and add an Obs
Obs o = new Obs();
o.setVoided(true);
enc.addObs(o);
assertNotNull(enc.getAllObs(true));
assertEquals(1, enc.getAllObs(true).size());
assertNotNull(enc.getAllObs(false));
assertEquals(0, enc.getAllObs(false).size());
}
/**
* @see Encounter#getObs()
*/
@Test
@Verifies(value = "should only get child obs", method = "getObs()")
public void getObs_shouldOnlyGetChildObs() throws Exception {
Encounter encounter = new Encounter();
//create and add an Obs
Obs parentObs = new Obs();
encounter.addObs(parentObs);
//add a child to the obs and make sure that the Obs is an ObsGroup with one child:
Obs childObs = new Obs();
parentObs.addGroupMember(childObs);
//obsGroup should recurse and ONLY the child obs should be picked up:
assertEquals(1, encounter.getObs().size());
// make sure that the obs is the oChild
Obs obsInEncounter = (Obs) encounter.getObs().toArray()[0];
assertTrue(childObs.equals(obsInEncounter));
assertFalse(obsInEncounter.isObsGrouping());
}
/**
* @see Encounter#getObsAtTopLevel(null)
*/
@Test
@Verifies(value = "should only get parents obs", method = "getObsAtTopLevel(null)")
public void getObsAtTopLevel_shouldOnlyGetParentsObs() throws Exception {
Encounter encounter = new Encounter();
//create and add an Obs
Obs parentObs = new Obs();
encounter.addObs(parentObs);
//add a child to the obs and make sure that the Obs is an ObsGroup with one child:
Obs childObs = new Obs();
parentObs.addGroupMember(childObs);
//assert that the parent obs is returned by getObsAtTopLevel()
assertNotNull(encounter.getObsAtTopLevel(true));
assertEquals(1, encounter.getObsAtTopLevel(true).size());
assertNotNull(encounter.getObsAtTopLevel(false));
assertEquals(1, encounter.getObsAtTopLevel(false).size());
// make sure that the obs is the parent obs
Obs obsInEncounter = (Obs) encounter.getObsAtTopLevel(false).toArray()[0];
assertTrue(obsInEncounter.isObsGrouping());
}
/**
* @see Encounter#getAllObs(null)
*/
@Test
@Verifies(value = "should get both parent and child obs", method = "getAllObs(null)")
public void getAllObs_shouldGetBothParentAndChildObs() throws Exception {
Encounter encounter = new Encounter();
//create and add an Obs
Obs parentObs = new Obs();
encounter.addObs(parentObs);
//add a child to the obs and make sure that the Obs is an ObsGroup with one child:
Obs childObs = new Obs();
parentObs.addGroupMember(childObs);
//assert that the parent obs is returned
assertNotNull(encounter.getAllObs(true));
assertEquals(1, encounter.getAllObs(true).size());
assertNotNull(encounter.getAllObs(false));
assertEquals(1, encounter.getAllObs(false).size());
Obs obsInEncounter = (Obs) encounter.getAllObs(false).toArray()[0];
assertTrue(obsInEncounter.isObsGrouping());
}
/**
* @see Encounter#getObs()
*/
@Test
@Verifies(value = "should not get child obs if child also on encounter", method = "getObs()")
public void getObs_shouldNotGetChildObsIfChildAlsoOnEncounter() throws Exception {
Encounter encounter = new Encounter();
//create and add an Obs
Obs parentObs = new Obs();
encounter.addObs(parentObs);
//add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
Obs childObs = new Obs();
parentObs.addGroupMember(childObs);
// add the child obs directly to the encounter as well
childObs.setEncounter(encounter);
encounter.addObs(childObs);
// do the check
assertEquals(1, encounter.getObs().size());
Obs obsInEncounter = (Obs) encounter.getObs().toArray()[0];
assertFalse(obsInEncounter.isObsGrouping());
}
/**
* @see Encounter#getObsAtTopLevel(null)
*/
@Test
@Verifies(value = "should only return the grouped top level obs", method = "getObsAtTopLevel(null)")
public void getObsAtTopLevel_shouldOnlyReturnTheGroupedTopLevelObs() throws Exception {
Encounter encounter = new Encounter();
//create and add an Obs
Obs parentObs = new Obs();
encounter.addObs(parentObs);
//add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
Obs childObs = new Obs();
parentObs.addGroupMember(childObs);
// add the child obs directly to the encounter as well
childObs.setEncounter(encounter);
encounter.addObs(childObs);
// do the check
assertEquals(1, encounter.getObsAtTopLevel(false).size());
Obs obsInEncounter = (Obs) encounter.getObsAtTopLevel(false).toArray()[0];
assertTrue(obsInEncounter.isObsGrouping());
}
/**
* @see Encounter#getAllObs(null)
*/
@Test
@Verifies(value = "should get both parent and child with child directly on encounter", method = "getAllObs(null)")
public void getAllObs_shouldGetBothParentAndChildWithChildDirectlyOnEncounter() throws Exception {
Encounter enc = new Encounter();
//create and add an Obs
Obs parentObs = new Obs();
enc.addObs(parentObs);
//add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
Obs childObs = new Obs();
parentObs.addGroupMember(childObs);
// add the child obs directly to the encounter as well
childObs.setEncounter(enc);
enc.addObs(childObs);
// do the check
assertEquals(2, enc.getAllObs(true).size());
//this should return one of each -- the obsGroup and the child (because encounter_id is populated in the child Obs):
int numberOfChildObs = 0;
int numberofParentObs = 0;
for (Obs oTmp : enc.getAllObs(false)) {
if (oTmp.isObsGrouping())
numberofParentObs++;
else
numberOfChildObs++;
}
assertEquals(1, numberOfChildObs);
assertEquals(1, numberofParentObs);
}
/**
* @see Encounter#getAllObs(null)
*/
@Test
@Verifies(value = "should get both child and parent obs after removing child from parent grouping", method = "getAllObs(null)")
public void getAllObs_shouldGetBothChildAndParentObsAfterRemovingChildFromParentGrouping() throws Exception {
Encounter enc = new Encounter();
//create and add an Obs
Obs parentObs = new Obs();
enc.addObs(parentObs);
//add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
Obs childObs = new Obs();
parentObs.addGroupMember(childObs);
// add the child obs directly to the encounter as well
childObs.setEncounter(enc);
enc.addObs(childObs);
//remove the obsGrouping, so that both obs are now just children of the Encounter
parentObs.removeGroupMember(childObs);
assertEquals(2, enc.getAllObs(true).size());
}
/**
* @see Encounter#getObs()
*/
@Test
@Verifies(value = "should get both child and parent obs after removing child from parent grouping", method = "getObs()")
public void getObs_shouldGetBothChildAndParentObsAfterRemovingChildFromParentGrouping() throws Exception {
Encounter enc = new Encounter();
//create and add an Obs
Obs parentObs = new Obs();
enc.addObs(parentObs);
//add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
Obs childObs = new Obs();
parentObs.addGroupMember(childObs);
// add the child obs directly to the encounter as well
childObs.setEncounter(enc);
enc.addObs(childObs);
//remove the obsGrouping, so that both obs are now just children of the Encounter
parentObs.removeGroupMember(childObs);
// do the check
assertEquals(2, enc.getObs().size());
}
/**
* @see Encounter#getObsAtTopLevel(null)
*/
@Test
@Verifies(value = "should get both child and parent obs after removing child from parent grouping", method = "getObsAtTopLevel(null)")
public void getObsAtTopLevel_shouldGetBothChildAndParentObsAfterRemovingChildFromParentGrouping() throws Exception {
Encounter enc = new Encounter();
//create and add an Obs
Obs parentObs = new Obs();
enc.addObs(parentObs);
//add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
Obs childObs = new Obs();
parentObs.addGroupMember(childObs);
// add the child obs directly to the encounter as well
childObs.setEncounter(enc);
enc.addObs(childObs);
//remove the obsGrouping, so that both obs are now just children of the Encounter
parentObs.removeGroupMember(childObs);
assertEquals(2, enc.getObsAtTopLevel(false).size());
}
/**
* @see Encounter#getObs()
*/
@Test
@Verifies(value = "should get obs with two levels of hierarchy", method = "getObs()")
public void getObs_shouldGetObsWithTwoLevelsOfHierarchy() throws Exception {
Encounter enc = new Encounter();
//create and add an Obs
Obs parentObs = new Obs();
enc.addObs(parentObs);
//add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
Obs childObs = new Obs();
parentObs.addGroupMember(childObs);
// add the child obs directly to the encounter as well
childObs.setEncounter(enc);
enc.addObs(childObs);
// make the obs two levels deep
Obs grandChildObsOne = new Obs();
Obs grandChildObsTwo = new Obs();
childObs.addGroupMember(grandChildObsOne);
childObs.addGroupMember(grandChildObsTwo);
/// now getObs() should return the two leaf obs 2 levels down:
assertEquals(2, enc.getObs().size());
assertTrue(enc.getObs().contains(grandChildObsOne));
assertTrue(enc.getObs().contains(grandChildObsTwo));
// now grandChildObsOne isn't a leaf, it holds greatGrandChildObsOne, but the size of the set returned shouldn't change:
Obs greatGrandChildObsOne = new Obs();
grandChildObsOne.addGroupMember(greatGrandChildObsOne);
assertEquals(2, enc.getObs().size());
assertTrue(enc.getObs().contains(greatGrandChildObsOne));
assertTrue(enc.getObs().contains(grandChildObsTwo));
//add a sibling to one of the leaves. Add leaf to grandChildObsOne:
Obs greatGrandChildObsTwo = new Obs();
grandChildObsOne.addGroupMember(greatGrandChildObsTwo);
//now getObs() should return a set of size 3:
assertEquals(3, enc.getObs().size());
// make sure none of them are grouping obs
for (Obs oTmp : enc.getObs()) {
assertFalse(oTmp.isObsGrouping());
}
assertTrue(enc.getObs().contains(greatGrandChildObsOne));
assertTrue(enc.getObs().contains(greatGrandChildObsTwo));
assertTrue(enc.getObs().contains(grandChildObsTwo));
}
/**
* @see Encounter#getObs()
*/
@Test
@Verifies(value = "should get obs with three levels of hierarchy", method = "getObs()")
public void getObs_shouldGetObsWithThreeLevelsOfHierarchy() throws Exception {
Encounter enc = new Encounter();
//create and add an Obs
Obs parentObs = new Obs();
enc.addObs(parentObs);
//add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
Obs childObs = new Obs();
parentObs.addGroupMember(childObs);
// add the child obs directly to the encounter as well
childObs.setEncounter(enc);
enc.addObs(childObs);
// make the obs two levels deep
Obs grandChildObsOne = new Obs();
Obs grandChildObsTwo = new Obs();
Obs greatGrandChildObsOne = new Obs();
Obs greatGrandChildObsTwo = new Obs();
childObs.addGroupMember(grandChildObsOne);
childObs.addGroupMember(grandChildObsTwo);
grandChildObsOne.addGroupMember(greatGrandChildObsOne);
grandChildObsOne.addGroupMember(greatGrandChildObsTwo);
// test the third level
Obs greatGreatGrandChildObsOne = new Obs();
Obs greatGreatGrandChildObsTwo = new Obs();
greatGrandChildObsTwo.addGroupMember(greatGreatGrandChildObsOne);
greatGrandChildObsTwo.addGroupMember(greatGreatGrandChildObsTwo);
assertEquals(4, enc.getObs().size());
assertTrue(enc.getObs().contains(greatGrandChildObsOne));
assertTrue(enc.getObs().contains(greatGreatGrandChildObsOne));
assertTrue(enc.getObs().contains(greatGreatGrandChildObsTwo));
assertTrue(enc.getObs().contains(grandChildObsTwo));
}
/**
* @see Encounter#getObs()
*/
@Test
@Verifies(value = "should not get voided obs with three layers of hierarchy", method = "getObs()")
public void getObs_shouldNotGetVoidedObsWithThreeLayersOfHierarchy() throws Exception {
Encounter enc = new Encounter();
//create and add an Obs
Obs parentObs = new Obs();
enc.addObs(parentObs);
//add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
Obs childObs = new Obs();
parentObs.addGroupMember(childObs);
// add the child obs directly to the encounter as well
childObs.setEncounter(enc);
enc.addObs(childObs);
// make the obs two levels deep
Obs grandChildObsOne = new Obs();
Obs grandChildObsTwo = new Obs();
Obs greatGrandChildObsOne = new Obs();
Obs greatGrandChildObsTwo = new Obs();
childObs.addGroupMember(grandChildObsOne);
childObs.addGroupMember(grandChildObsTwo);
grandChildObsOne.addGroupMember(greatGrandChildObsOne);
grandChildObsOne.addGroupMember(greatGrandChildObsTwo);
// set up the third level
Obs greatGreatGrandChildObsOne = new Obs();
Obs greatGreatGrandChildObsTwo = new Obs();
greatGrandChildObsTwo.addGroupMember(greatGreatGrandChildObsOne);
greatGrandChildObsTwo.addGroupMember(greatGreatGrandChildObsTwo);
// make sure voided objects in the obsGroup hierarchy aren't getting returned:
greatGrandChildObsTwo.setVoided(true);
//now the set size should drop down to 2 because the obs we voided had two child leaves:
assertEquals(2, enc.getObs().size());
assertTrue(enc.getObs().contains(greatGrandChildObsOne));
assertTrue(enc.getObs().contains(grandChildObsTwo));
assertFalse(enc.getObs().contains(greatGreatGrandChildObsOne));
assertFalse(enc.getObs().contains(greatGreatGrandChildObsTwo));
}
/**
* @see Encounter#Encounter(Integer)
*/
@Test
@Verifies(value = "should set encounter id", method = "Encounter(Integer)")
public void Encounter_shouldSetEncounterId() throws Exception {
Encounter encounter = new Encounter(123);
Assert.assertEquals(123, encounter.getEncounterId().intValue());
}
/**
* @see Encounter#addObs(Obs)
*/
@Test
@Verifies(value = "should add obs with null values", method = "addObs(Obs)")
public void addObs_shouldAddObsWithNullValues() throws Exception {
Encounter encounter = new Encounter();
encounter.addObs(new Obs());
assertEquals(1, encounter.getAllObs(true).size());
}
/**
* @see Encounter#addObs(Obs)
*/
@Test
@Verifies(value = "should not fail with null obs", method = "addObs(Obs)")
public void addObs_shouldNotFailWithNullObs() throws Exception {
Encounter encounter = new Encounter();
encounter.addObs(null);
assertEquals(0, encounter.getAllObs(true).size());
}
/**
* @see Encounter#addObs(Obs)
*/
@Test
@Verifies(value = "should set encounter attribute on obs", method = "addObs(Obs)")
public void addObs_shouldSetEncounterAttributeOnObs() throws Exception {
Encounter encounter = new Encounter();
Obs obs = new Obs();
encounter.addObs(obs);
assertTrue(obs.getEncounter().equals(encounter));
}
/**
* @see Encounter#addObs(Obs)
*/
@Test
@Verifies(value = "should add obs to non null initial obs set", method = "addObs(Obs)")
public void addObs_shouldAddObsToNonNullInitialObsSet() throws Exception {
Encounter encounter = new Encounter();
Set<Obs> obsSet = new HashSet<Obs>();
obsSet.add(new Obs(1));
encounter.setObs(obsSet);
encounter.addObs(new Obs(2));
assertEquals(2, encounter.getAllObs(true).size());
}
/**
* @see Encounter#addObs(Obs)
*/
@Test
@Verifies(value = "should add encounter attrs to obs if attributes are null", method = "addObs(Obs)")
public void addObs_shouldAddEncounterAttrsToObsIfAttributesAreNull() throws Exception {
/// an encounter that will hav the date/location/patient on it
Encounter encounter = new Encounter();
Date date = new Date();
encounter.setEncounterDatetime(date);
Location location = new Location(1);
encounter.setLocation(location);
Patient patient = new Patient(1);
encounter.setPatient(patient);
// add an obs that doesn't have date/location/patient set on it.
Obs obs = new Obs(123);
encounter.addObs(obs);
// make sure it was added
assertEquals(1, encounter.getAllObs(true).size());
// check the values of the obs attrs to see if they were added
assertTrue(obs.getObsDatetime().equals(date));
assertTrue(obs.getLocation().equals(location));
assertTrue(obs.getPerson().equals(patient));
}
/**
* @see Encounter#addObs(Obs)
*/
@Test
@Verifies(value = "should add encounter attrs to obs if attributes are null", method = "addObs(Obs)")
public void addObs_shouldAddEncounterAttrsToObsGroupMembersIfAttributesAreNull() throws Exception {
/// an encounter that will hav the date/location/patient on it
Encounter encounter = new Encounter();
Date date = new Date();
encounter.setEncounterDatetime(date);
Location location = new Location(1);
encounter.setLocation(location);
Patient patient = new Patient(1);
encounter.setPatient(patient);
// add an obs that doesn't have date/location/patient set on it.
Obs obs = new Obs(123);
Obs childObs = new Obs(456);
obs.addGroupMember(childObs);
//check for infinite recursion
// childObs-->childObs2 and childObs2-->childObs
Obs childObs2 = new Obs(456);
childObs.addGroupMember(childObs2);
childObs2.addGroupMember(childObs);
assertTrue(obs.getGroupMembers() != null && obs.getGroupMembers().size() == 1);
encounter.addObs(obs);
// check the values of the obs attrs to see if they were added
assertTrue(childObs.getObsDatetime().equals(date));
assertTrue(childObs.getLocation().equals(location));
assertTrue(childObs.getPerson().equals(patient));
assertTrue(childObs2.getObsDatetime().equals(date));
assertTrue(childObs2.getLocation().equals(location));
assertTrue(childObs2.getPerson().equals(patient));
}
/**
* @see Encounter#addOrder(Order)
*/
@Test
@Verifies(value = "should add order with null values", method = "addOrder(Order)")
public void addOrder_shouldAddOrderWithNullValues() throws Exception {
Encounter encounter = new Encounter();
encounter.addOrder(new Order());
assertEquals(1, encounter.getOrders().size());
}
/**
* @see Encounter#addOrder(Order)
*/
@Test
@Verifies(value = "should not fail with null obs passed to add order", method = "addOrder(Order)")
public void addOrder_shouldNotFailWithNullObsPassedToAddOrder() throws Exception {
Encounter encounter = new Encounter();
encounter.addOrder(null);
assertEquals(0, encounter.getOrders().size());
}
/**
* @see Encounter#addOrder(Order)
*/
@Test
@Verifies(value = "should set encounter attribute", method = "addOrder(Order)")
public void addOrder_shouldSetEncounterAttribute() throws Exception {
Encounter encounter = new Encounter();
Order order = new Order();
encounter.addOrder(order);
assertTrue(order.getEncounter().equals(encounter));
}
/**
* @see Encounter#addOrder(Order)
*/
@Test
@Verifies(value = "should add order to non null initial order set", method = "addOrder(Order)")
public void addOrder_shouldAddOrderToNonNullInitialOrderSet() throws Exception {
Encounter encounter = new Encounter();
Set<Order> orderSet = new HashSet<Order>();
orderSet.add(new Order(1));
encounter.setOrders(orderSet);
encounter.addOrder(new Order(2));
assertEquals(2, encounter.getOrders().size());
}
/**
* @see Encounter#getOrders()
*/
@Test
@Verifies(value = "should add order to encounter when adding order to set returned from getOrders", method = "getOrders()")
public void addOrders_shouldAddOrderToEncounterWhenAddingOrderToSetReturnedFromGetOrders() throws Exception {
Encounter encounter = new Encounter();
Order order = new Order();
encounter.getOrders().add(order);
assertEquals(1, encounter.getOrders().size());
}
/**
* @see Encounter#removeOrder(Order)
*/
@Test
@Verifies(value = "should remove order from encounter", method = "removeOrder(Order)")
public void removeOrder_shouldRemoveOrderFromEncounter() throws Exception {
Encounter encounter = new Encounter();
Order order = new Order(1);
encounter.addOrder(order);
assertEquals(1, encounter.getOrders().size());
encounter.removeOrder(order);
assertEquals(0, encounter.getOrders().size());
}
/**
* @see Encounter#removeOrder(Order)
*/
@Test
@Verifies(value = "should not fail when removing null order", method = "removeOrder(Order)")
public void removeOrder_shouldNotFailWhenRemovingNullOrder() throws Exception {
Encounter encounter = new Encounter();
encounter.removeOrder(null);
}
/**
* @see Encounter#removeOrder(Order)
*/
@Test
@Verifies(value = "should not fail when removing non existent order", method = "removeOrder(Order)")
public void removeOrder_shouldNotFailWhenRemovingNonExistentOrder() throws Exception {
Encounter encounter = new Encounter();
encounter.removeOrder(new Order(123));
}
/**
* @see Encounter#addProvider(EncounterRole,Provider)
* @verifies add provider for new role
*/
@Test
public void addProvider_shouldAddProviderForNewRole() throws Exception {
//given
Encounter encounter = new Encounter();
EncounterRole encounterRole = new EncounterRole();
Provider provider = new Provider();
//when
encounter.addProvider(encounterRole, provider);
//then
Assert.assertTrue(encounter.getProvidersByRole(encounterRole).contains(provider));
}
/**
* @see Encounter#addProvider(EncounterRole,Provider)
* @verifies add second provider for role
*/
@Test
public void addProvider_shouldAddSecondProviderForRole() throws Exception {
//given
Encounter encounter = new Encounter();
EncounterRole role = new EncounterRole();
Provider provider1 = new Provider();
Provider provider2 = new Provider();
//when
encounter.addProvider(role, provider1);
encounter.addProvider(role, provider2);
//then
List<Provider> providers = Arrays.asList(provider1, provider2);
Assert.assertTrue(encounter.getProvidersByRole(role).containsAll(providers));
}
/**
* @see Encounter#addProvider(EncounterRole,Provider)
* @verifies not add same provider twice for role
*/
@Test
public void addProvider_shouldNotAddSameProviderTwiceForRole() throws Exception {
//given
Encounter encounter = new Encounter();
EncounterRole role = new EncounterRole();
Provider provider1 = new Provider();
//when
encounter.addProvider(role, provider1);
encounter.addProvider(role, provider1);
//then
// we need to cheat and use reflection to look at the private encounterProviders property; we don't want the getProvidersByRole method hiding duplicates from us
Collection<EncounterProvider> providers = (Collection<EncounterProvider>) FieldUtils.readField(encounter,
"encounterProviders", true);
Assert.assertEquals(1, providers.size());
Assert.assertTrue(encounter.getProvidersByRole(role).contains(provider1));
}
/**
* @see Encounter#getProvider()
* @verifies return null if there is no provider for person
*/
@Test
public void getProvider_shouldReturnNullIfThereIsNoProviderForPerson() throws Exception {
//given
Encounter encounter = new Encounter();
EncounterRole role = new EncounterRole();
Provider provider = new Provider();
encounter.addProvider(role, provider);
//when
Person result = encounter.getProvider();
//then
Assert.assertNull(result);
}
/**
* @see Encounter#getProvider()
* @verifies return null if there is no providers
*/
@Test
public void getProvider_shouldReturnNullIfThereIsNoProviders() throws Exception {
//given
Encounter encounter = new Encounter();
//when
Person result = encounter.getProvider();
//then
Assert.assertNull(result);
}
/**
* @see Encounter#getProvider()
* @verifies return provider for person
*/
@Test
public void getProvider_shouldReturnProviderForPerson() throws Exception {
//given
Encounter encounter = new Encounter();
EncounterRole role = new EncounterRole();
Provider provider = new Provider();
Person person = new Person();
provider.setPerson(person);
encounter.addProvider(role, provider);
//when
Person result = encounter.getProvider();
//then
Assert.assertEquals(person, result);
}
/**
* @see Encounter#getProvider()
* @verifies should exclude voided providers
*/
@Test
public void getProvider_shouldExcludeVoidedProviders() throws Exception {
//given
Encounter encounter = new Encounter();
EncounterRole role = new EncounterRole();
Provider provider = new Provider();
Provider anotherProvider = new Provider();