11package com .divudi .ejb ;
22
3- import org .junit .jupiter .api .Test ;
4- import static org .junit .jupiter .api .Assertions .assertEquals ;
5- import java .util .ArrayList ;
6- import java .util .List ;
7-
83import com .divudi .core .entity .lab .PatientReport ;
9- import com .divudi .core .entity .lab .Investigation ;
4+ import com .divudi .core .entity .lab .PatientReportItemValue ;
105import com .divudi .core .entity .lab .ReportItem ;
6+ import com .divudi .core .entity .lab .Investigation ;
117import com .divudi .core .entity .lab .PatientInvestigation ;
128import com .divudi .core .entity .Patient ;
139import com .divudi .core .entity .PatientEncounter ;
10+ import com .divudi .core .entity .lab .InvestigationItem ;
1411import com .divudi .core .data .InvestigationItemType ;
1512import com .divudi .core .data .InvestigationItemValueType ;
1613import com .divudi .core .facade .PatientReportItemValueFacade ;
17- import com .divudi .core .entity .lab .PatientReportItemValue ;
18- import com .divudi .core .entity .lab .InvestigationItem ;
14+ import com .divudi .core .facade .AntibioticFacade ;
15+
16+ import java .util .ArrayList ;
17+ import java .util .HashMap ;
18+ import java .util .List ;
19+ import java .util .Map ;
20+
21+ import org .junit .jupiter .api .Test ;
22+ import static org .junit .jupiter .api .Assertions .*;
1923
2024public class PatientReportBeanTest {
2125
22- // Test the performance of adding report item values
26+ private static class MockPatientReportItemValueFacade extends PatientReportItemValueFacade {
27+ private int createCount = 0 ;
28+ private int batchCreateCount = 0 ;
29+ private List <PatientReportItemValue > allCreated = new ArrayList <>();
30+
31+ @ Override
32+ public void create (PatientReportItemValue entity ) {
33+ createCount ++;
34+ allCreated .add (entity );
35+ }
36+
37+ @ Override
38+ public void batchCreate (List <PatientReportItemValue > entities ) {
39+ batchCreateCount ++;
40+ allCreated .addAll (entities );
41+ }
42+
43+ @ Override
44+ public PatientReportItemValue findFirstByJpql (String jpql , Map m ) {
45+ return null ;
46+ }
47+
48+ @ Override
49+ public PatientReportItemValue findFirstByJpql (String jpql ) {
50+ return null ;
51+ }
52+ }
53+
54+ private static class MockAntibioticFacade extends AntibioticFacade {
55+ @ Override
56+ public List findByJpql (String jpql ) {
57+ return new ArrayList ();
58+ }
59+ }
60+
61+ private static class TestInvestigation extends Investigation {
62+ private List <InvestigationItem > customReportItems = new ArrayList <>();
63+
64+ @ Override
65+ public List <InvestigationItem > getReportItems () {
66+ return customReportItems ;
67+ }
68+
69+ public void setCustomReportItems (List <InvestigationItem > reportItems ) {
70+ this .customReportItems = reportItems ;
71+ }
72+ }
73+
74+ @ Test
75+ public void testAddMicrobiologyReportItemValuesForReport_createsValues () {
76+ PatientReportBean bean = new PatientReportBean ();
77+ MockPatientReportItemValueFacade facade = new MockPatientReportItemValueFacade ();
78+ MockAntibioticFacade antibioticFacade = new MockAntibioticFacade ();
79+ bean .setPtRivFacade (facade );
80+ bean .antibioticFacade = antibioticFacade ;
81+
82+ PatientReport report = new PatientReport ();
83+ TestInvestigation inv = new TestInvestigation ();
84+ report .setItem (inv );
85+
86+ PatientInvestigation pi = new PatientInvestigation ();
87+ pi .setPatient (new Patient ());
88+ pi .setEncounter (new PatientEncounter ());
89+ report .setPatientInvestigation (pi );
90+
91+ List <InvestigationItem > items = new ArrayList <>();
92+ for (int i =0 ; i <1000 ; i ++) {
93+ InvestigationItem item = new InvestigationItem ();
94+ item .setIxItemType (InvestigationItemType .Value );
95+ item .setIxItemValueType (InvestigationItemValueType .Memo );
96+ item .setRetired (false );
97+ items .add (item );
98+ }
99+ inv .setCustomReportItems (items );
100+
101+ long startTime = System .currentTimeMillis ();
102+ bean .addMicrobiologyReportItemValuesForReport (report );
103+ long endTime = System .currentTimeMillis ();
104+
105+ System .out .println ("Time taken: " + (endTime - startTime ) + "ms" );
106+ System .out .println ("createCount: " + facade .createCount );
107+ System .out .println ("batchCreateCount: " + facade .batchCreateCount );
108+
109+ assertEquals (0 , facade .createCount );
110+ assertEquals (1 , facade .batchCreateCount );
111+ assertEquals (1000 , facade .allCreated .size ());
112+ }
113+
23114 @ Test
24115 public void testPerformance () {
25116 PatientReportBean bean = new PatientReportBean () {
@@ -41,13 +132,12 @@ public void batchCreate(List<PatientReportItemValue> entities) {
41132 }
42133 @ Override
43134 public PatientReportItemValue findFirstByJpql (String jpql ) {
44- return null ; // simulate no existing values
135+ return null ;
45136 }
46137 };
47138 }
48139 };
49140
50- // Setup data
51141 PatientReport ptReport = new PatientReport ();
52142 ptReport .setId (0L );
53143
@@ -68,7 +158,6 @@ public PatientReportItemValue findFirstByJpql(String jpql) {
68158 items .add (item );
69159 }
70160
71- // using reflection to set report items on Investigation as there might not be a setter
72161 try {
73162 java .lang .reflect .Field field = com .divudi .core .entity .lab .Investigation .class .getSuperclass ().getDeclaredField ("reportItems" );
74163 field .setAccessible (true );
@@ -80,7 +169,6 @@ public PatientReportItemValue findFirstByJpql(String jpql) {
80169 ptReport .setItem (inv );
81170 ptReport .setPatientReportItemValues (new ArrayList <>());
82171
83- // Measure
84172 long start = System .currentTimeMillis ();
85173 bean .addPatientReportItemValuesForTemplateReport (ptReport );
86174 long end = System .currentTimeMillis ();
0 commit comments