Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
08/03/08 02:50:22 (5 months ago)
Author:
kevjay
Message:

logicws: Added javadocs, formatted, and reorganized code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs-modules/logicws/test/org/openmrs/module/logicws/test/LogicWsHTTPtest.java

    r5059 r5144  
    1515 
    1616import java.io.IOException; 
     17import java.io.OutputStream; 
     18 
    1719import junit.framework.TestCase; 
    18 import com.meterware.httpunit.*; 
    19  
     20 
     21import org.apache.xml.serialize.OutputFormat; 
    2022import org.apache.xml.serialize.XMLSerializer; 
    21 import org.apache.xml.serialize.OutputFormat; 
    22 import org.openmrs.Cohort; 
    23 import org.openmrs.api.context.Context; 
    24 import org.openmrs.api.impl.CohortServiceImpl; 
    25 import org.openmrs.cohort.CohortDefinition; 
    26 import org.openmrs.logic.LogicException; 
    27 import org.openmrs.logic.LogicService; 
    28 import org.openmrs.reporting.PatientFilter; 
    29 import org.openmrs.test.BaseModuleContextSensitiveTest; 
    3023import org.w3c.dom.Document; 
    31 import java.io.OutputStream; 
    32 import java.util.Set; 
    33  
    3424import org.xml.sax.SAXException; 
    3525 
     26import com.meterware.httpunit.PostMethodWebRequest; 
     27import com.meterware.httpunit.WebConversation; 
     28import com.meterware.httpunit.WebRequest; 
     29import com.meterware.httpunit.WebResponse; 
    3630 
    3731public class LogicWsHTTPtest extends TestCase { 
    3832 
    39          
    40         private String realm = "OpenMRS Rest API"; 
    41         private String user = "admin"; 
    42         private String pass = "test"; 
    43         private WebConversation wc; 
    44          
    45          
    46         protected void setUp() throws Exception  
    47         { 
    48                 wc = new WebConversation(); 
    49                 wc.setAuthentication(realm, user, pass); 
    50                  
    51         }        
    52          
    53         public void testGetFilters()  
    54         {                
    55             WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getFilters"); 
    56              
    57             try  
    58             { 
    59                         WebResponse resp = wc.getResponse(req);                  
    60                         assertTrue(resp.getText().indexOf("filterList") > 0);                    
    61                          
    62                         System.out.println("\n"); 
    63                         System.out.println("Filter Response:\n"); 
    64                         prettyPrint(resp.getDOM(), System.out); 
    65                         System.out.println("\n"); 
    66                          
    67                 } catch (IOException e) { 
    68                         // TODO Auto-generated catch block 
    69                         e.printStackTrace(); 
    70                 } catch (SAXException e) { 
    71                         // TODO Auto-generated catch block 
    72                         e.printStackTrace(); 
    73                 } catch (Exception e) { 
    74                         // TODO Auto-generated catch block 
    75                         e.printStackTrace(); 
    76                 }                
    77         } 
    78          
    79         public void testGetTokens()  
    80         {                
    81             WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getTokens"); 
    82              
    83             try  
    84             { 
    85                         WebResponse resp = wc.getResponse(req);                  
    86                         assertTrue(resp.getText().indexOf("tokenList") > 0);                     
    87                          
    88                         System.out.println("\n"); 
    89                         System.out.println("Token Response:\n"); 
    90                         prettyPrint(resp.getDOM(), System.out); 
    91                         System.out.println("\n"); 
    92                          
    93                 } catch (IOException e) { 
    94                         // TODO Auto-generated catch block 
    95                         e.printStackTrace(); 
    96                 } catch (SAXException e) { 
    97                         // TODO Auto-generated catch block 
    98                         e.printStackTrace(); 
    99                 } catch (Exception e) { 
    100                         // TODO Auto-generated catch block 
    101                         e.printStackTrace(); 
    102                 }                
    103         } 
    104          
    105         public void testGetTokenTags()  
    106         {                
    107             WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getTokenTags"); 
    108              
    109             try  
    110             { 
    111                         WebResponse resp = wc.getResponse(req);                  
    112                         assertTrue(resp.getText().indexOf("tokenTagList") > 0);                  
    113                          
    114                         System.out.println("\n"); 
    115                         System.out.println("Token Tag Response:\n"); 
    116                         prettyPrint(resp.getDOM(), System.out); 
    117                         System.out.println("\n"); 
    118                          
    119                 } catch (IOException e) { 
    120                         // TODO Auto-generated catch block 
    121                         e.printStackTrace(); 
    122                 } catch (SAXException e) { 
    123                         // TODO Auto-generated catch block 
    124                         e.printStackTrace(); 
    125                 } catch (Exception e) { 
    126                         // TODO Auto-generated catch block 
    127                         e.printStackTrace(); 
    128                 }                
    129         } 
    130          
    131         public void testGetDataWhereCohortIsEmpty()  
    132         {                
    133             WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?filter=3:org.openmrs.reporting.PatientSearch&token=AGE&token=GENDER"); 
    134              
    135             try  
    136             { 
    137                         WebResponse resp = wc.getResponse(req);                  
    138                         assertTrue(resp.getText().indexOf("dataset") > 0);                       
    139                          
    140                         System.out.println("\n"); 
    141                         System.out.println("Data Response:\n"); 
    142                         prettyPrint(resp.getDOM(), System.out); 
    143                         System.out.println("\n"); 
    144                          
    145                 } catch (IOException e) { 
    146                         // TODO Auto-generated catch block 
    147                         e.printStackTrace(); 
    148                 } catch (SAXException e) { 
    149                         // TODO Auto-generated catch block 
    150                         e.printStackTrace(); 
    151                 }  
    152                 catch (Exception e) { 
    153                         // TODO Auto-generated catch block 
    154                         e.printStackTrace(); 
    155                 }                
    156         } 
    157          
    158         public void testGetDataWhenNoArguments()  
    159         {                
    160             WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData"); 
    161              
    162             try  
    163             { 
    164                         WebResponse resp = wc.getResponse(req);                  
    165                         assertTrue(resp.getText().indexOf("dataset") > 0);                       
    166                          
    167                         System.out.println("\n"); 
    168                         System.out.println("Data Response:\n"); 
    169                         prettyPrint(resp.getDOM(), System.out); 
    170                         System.out.println("\n"); 
    171                          
    172                 } catch (IOException e) { 
    173                         // TODO Auto-generated catch block 
    174                         e.printStackTrace(); 
    175                 } catch (SAXException e) { 
    176                         // TODO Auto-generated catch block 
    177                         e.printStackTrace(); 
    178                 }  
    179                 catch (Exception e) { 
    180                         // TODO Auto-generated catch block 
    181                         e.printStackTrace(); 
    182                 }                
    183         } 
    184  
    185         public void testGetData()  
    186         {                
    187            // WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{GENDER}%20FROM%203:org.openmrs.cohort.StaticCohortDefinition"); 
    188                 WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{WEIGHT%20(KG)}%20FROM%205:org.openmrs.cohort.StaticCohortDefinition&datastyle=stacked"); 
    189             try  
    190             { 
    191                         WebResponse resp = wc.getResponse(req);                  
    192                         assertTrue(resp.getText().indexOf("dataset") > 0);                       
    193                          
    194                         System.out.println("\n"); 
    195                         System.out.println("Data Response:\n"); 
    196                         prettyPrint(resp.getDOM(), System.out); 
    197                         System.out.println("\n"); 
    198                          
    199                 } catch (IOException e) { 
    200                         // TODO Auto-generated catch block 
    201                         e.printStackTrace(); 
    202                 } catch (SAXException e) { 
    203                         // TODO Auto-generated catch block 
    204                         e.printStackTrace(); 
    205                 }  
    206                 catch (Exception e) { 
    207                         // TODO Auto-generated catch block 
    208                         e.printStackTrace(); 
    209                 }                
    210         } 
    211          
    212         public void testGetFlatData()  
    213         {                
    214            // WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{GENDER}%20FROM%203:org.openmrs.cohort.StaticCohortDefinition"); 
    215                 WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{AGE}:OBSERVATIONDATE|%20{GENDER}:OBSERVATIONDATE|%20{WEIGHT%20(KG)}%20LT%2022:OBSERVATIONDATE%20FROM%205:org.openmrs.cohort.StaticCohortDefinition&datastyle=flat"); 
    216             try  
    217             { 
    218                         WebResponse resp = wc.getResponse(req);                  
    219                         assertTrue(resp.getText().indexOf("dataset") > 0);                       
    220                          
    221                         System.out.println("\n"); 
    222                         System.out.println("Data Response:\n"); 
    223                         prettyPrint(resp.getDOM(), System.out); 
    224                         System.out.println("\n"); 
    225                          
    226                 } catch (IOException e) { 
    227                         // TODO Auto-generated catch block 
    228                         e.printStackTrace(); 
    229                 } catch (SAXException e) { 
    230                         // TODO Auto-generated catch block 
    231                         e.printStackTrace(); 
    232                 }  
    233                 catch (Exception e) { 
    234                         // TODO Auto-generated catch block 
    235                         e.printStackTrace(); 
    236                 }                
    237         } 
    238          
    239         public void testGetStackedData()  
    240         {                
    241            // WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{GENDER}%20FROM%203:org.openmrs.cohort.StaticCohortDefinition"); 
    242                 WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{AGE}:OBSERVATIONDATE|%20{GENDER}:OBSERVATIONDATE|%20{WEIGHT%20(KG)}%20LT%2022:OBSERVATIONDATE%20FROM%203:org.openmrs.cohort.StaticCohortDefinition&datastyle=stacked"); 
    243             try  
    244             { 
    245                         WebResponse resp = wc.getResponse(req);                  
    246                         assertTrue(resp.getText().indexOf("dataset") > 0);                       
    247                          
    248                         System.out.println("\n"); 
    249                         System.out.println("Data Response:\n"); 
    250                         prettyPrint(resp.getDOM(), System.out); 
    251                         System.out.println("\n"); 
    252                          
    253                 } catch (IOException e) { 
    254                         // TODO Auto-generated catch block 
    255                         e.printStackTrace(); 
    256                 } catch (SAXException e) { 
    257                         // TODO Auto-generated catch block 
    258                         e.printStackTrace(); 
    259                 }  
    260                 catch (Exception e) { 
    261                         // TODO Auto-generated catch block 
    262                         e.printStackTrace(); 
    263                 }                
    264         } 
    265  
    266          
    267         public void prettyPrint(Document doc, OutputStream out) throws Exception { 
     33    private String realm = "OpenMRS Rest API"; 
     34 
     35    private String user = "admin"; 
     36 
     37    private String pass = "test"; 
     38 
     39    private WebConversation wc; 
     40 
     41    protected void setUp() throws Exception { 
     42        wc = new WebConversation(); 
     43        wc.setAuthentication(realm, user, pass); 
     44 
     45    } 
     46 
     47    public void testGetFilters() { 
     48        WebRequest req = new PostMethodWebRequest( 
     49                "http://localhost:8080/openmrs/moduleServlet/logicws/api/getFilters"); 
     50 
     51        try { 
     52            WebResponse resp = wc.getResponse(req); 
     53            assertTrue(resp.getText().indexOf("filterList") > 0); 
     54 
     55            System.out.println("\n"); 
     56            System.out.println("Filter Response:\n"); 
     57            prettyPrint(resp.getDOM(), System.out); 
     58            System.out.println("\n"); 
     59 
     60        } catch (IOException e) { 
     61            // TODO Auto-generated catch block 
     62            e.printStackTrace(); 
     63        } catch (SAXException e) { 
     64            // TODO Auto-generated catch block 
     65            e.printStackTrace(); 
     66        } catch (Exception e) { 
     67            // TODO Auto-generated catch block 
     68            e.printStackTrace(); 
     69        } 
     70    } 
     71 
     72    public void testGetTokens() { 
     73        WebRequest req = new PostMethodWebRequest( 
     74                "http://localhost:8080/openmrs/moduleServlet/logicws/api/getTokens"); 
     75 
     76        try { 
     77            WebResponse resp = wc.getResponse(req); 
     78            assertTrue(resp.getText().indexOf("tokenList") > 0); 
     79 
     80            System.out.println("\n"); 
     81            System.out.println("Token Response:\n"); 
     82            prettyPrint(resp.getDOM(), System.out); 
     83            System.out.println("\n"); 
     84 
     85        } catch (IOException e) { 
     86            // TODO Auto-generated catch block 
     87            e.printStackTrace(); 
     88        } catch (SAXException e) { 
     89            // TODO Auto-generated catch block 
     90            e.printStackTrace(); 
     91        } catch (Exception e) { 
     92            // TODO Auto-generated catch block 
     93            e.printStackTrace(); 
     94        } 
     95    } 
     96 
     97    public void testGetTokenTags() { 
     98        WebRequest req = new PostMethodWebRequest( 
     99                "http://localhost:8080/openmrs/moduleServlet/logicws/api/getTokenTags"); 
     100 
     101        try { 
     102            WebResponse resp = wc.getResponse(req); 
     103            assertTrue(resp.getText().indexOf("tokenTagList") > 0); 
     104 
     105            System.out.println("\n"); 
     106            System.out.println("Token Tag Response:\n"); 
     107            prettyPrint(resp.getDOM(), System.out); 
     108            System.out.println("\n"); 
     109 
     110        } catch (IOException e) { 
     111            // TODO Auto-generated catch block 
     112            e.printStackTrace(); 
     113        } catch (SAXException e) { 
     114            // TODO Auto-generated catch block 
     115            e.printStackTrace(); 
     116        } catch (Exception e) { 
     117            // TODO Auto-generated catch block 
     118            e.printStackTrace(); 
     119        } 
     120    } 
     121 
     122    /* 
     123    public void testGetDataWhereCohortIsEmpty() { 
     124        WebRequest req = new PostMethodWebRequest( 
     125                "http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?filter=3:org.openmrs.reporting.PatientSearch&token=AGE&token=GENDER"); 
     126 
     127        try { 
     128            WebResponse resp = wc.getResponse(req); 
     129            assertTrue(resp.getText().indexOf("dataset") > 0); 
     130 
     131            System.out.println("\n"); 
     132            System.out.println("Data Response:\n"); 
     133            prettyPrint(resp.getDOM(), System.out); 
     134            System.out.println("\n"); 
     135 
     136        } catch (IOException e) { 
     137            // TODO Auto-generated catch block 
     138            e.printStackTrace(); 
     139        } catch (SAXException e) { 
     140            // TODO Auto-generated catch block 
     141            e.printStackTrace(); 
     142        } catch (Exception e) { 
     143            // TODO Auto-generated catch block 
     144            e.printStackTrace(); 
     145        } 
     146    } 
     147*/ 
     148    public void testGetDataWhenNoArguments() { 
     149        WebRequest req = new PostMethodWebRequest( 
     150                "http://localhost:8080/openmrs/moduleServlet/logicws/api/getData"); 
     151 
     152        try { 
     153            WebResponse resp = wc.getResponse(req); 
     154            assertTrue(resp.getText().indexOf("dataset") > 0); 
     155 
     156            System.out.println("\n"); 
     157            System.out.println("Data Response:\n"); 
     158            prettyPrint(resp.getDOM(), System.out); 
     159            System.out.println("\n"); 
     160 
     161        } catch (IOException e) { 
     162            // TODO Auto-generated catch block 
     163            e.printStackTrace(); 
     164        } catch (SAXException e) { 
     165            // TODO Auto-generated catch block 
     166            e.printStackTrace(); 
     167        } catch (Exception e) { 
     168            // TODO Auto-generated catch block 
     169            e.printStackTrace(); 
     170        } 
     171    } 
     172 
     173    public void testGetData() { 
     174        // WebRequest req = new 
     175        // PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{GENDER}%20FROM%203:org.openmrs.cohort.StaticCohortDefinition"); 
     176        WebRequest req = new PostMethodWebRequest( 
     177                "http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{WEIGHT%20(KG)}%20FROM%205:org.openmrs.cohort.StaticCohortDefinition&datastyle=stacked"); 
     178        try { 
     179            WebResponse resp = wc.getResponse(req); 
     180            assertTrue(resp.getText().indexOf("dataset") > 0); 
     181 
     182            System.out.println("\n"); 
     183            System.out.println("Data Response:\n"); 
     184            prettyPrint(resp.getDOM(), System.out); 
     185            System.out.println("\n"); 
     186 
     187        } catch (IOException e) { 
     188            // TODO Auto-generated catch block 
     189            e.printStackTrace(); 
     190        } catch (SAXException e) { 
     191            // TODO Auto-generated catch block 
     192            e.printStackTrace(); 
     193        } catch (Exception e) { 
     194            // TODO Auto-generated catch block 
     195            e.printStackTrace(); 
     196        } 
     197    } 
     198 
     199    public void testGetFlatData() { 
     200        // WebRequest req = new 
     201        // PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{GENDER}%20FROM%203:org.openmrs.cohort.StaticCohortDefinition"); 
     202        WebRequest req = new PostMethodWebRequest( 
     203                "http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{AGE}:OBSERVATIONDATE|%20{GENDER}:OBSERVATIONDATE|%20{WEIGHT%20(KG)}%20LT%2022:OBSERVATIONDATE%20FROM%205:org.openmrs.cohort.StaticCohortDefinition&datastyle=flat"); 
     204        try { 
     205            WebResponse resp = wc.getResponse(req); 
     206            assertTrue(resp.getText().indexOf("dataset") > 0); 
     207 
     208            System.out.println("\n"); 
     209            System.out.println("Data Response:\n"); 
     210            prettyPrint(resp.getDOM(), System.out); 
     211            System.out.println("\n"); 
     212 
     213        } catch (IOException e) { 
     214            // TODO Auto-generated catch block 
     215            e.printStackTrace(); 
     216        } catch (SAXException e) { 
     217            // TODO Auto-generated catch block 
     218            e.printStackTrace(); 
     219        } catch (Exception e) { 
     220            // TODO Auto-generated catch block 
     221            e.printStackTrace(); 
     222        } 
     223    } 
     224 
     225    public void testGetStackedData() { 
     226        // WebRequest req = new 
     227        // PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{GENDER}%20FROM%203:org.openmrs.cohort.StaticCohortDefinition"); 
     228        WebRequest req = new PostMethodWebRequest( 
     229                "http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{AGE}:OBSERVATIONDATE|%20{GENDER}:OBSERVATIONDATE|%20{WEIGHT%20(KG)}%20LT%2022:OBSERVATIONDATE%20FROM%203:org.openmrs.cohort.StaticCohortDefinition&datastyle=stacked"); 
     230        try { 
     231            WebResponse resp = wc.getResponse(req); 
     232            assertTrue(resp.getText().indexOf("dataset") > 0); 
     233 
     234            System.out.println("\n"); 
     235            System.out.println("Data Response:\n"); 
     236            prettyPrint(resp.getDOM(), System.out); 
     237            System.out.println("\n"); 
     238 
     239        } catch (IOException e) { 
     240            // TODO Auto-generated catch block 
     241            e.printStackTrace(); 
     242        } catch (SAXException e) { 
     243            // TODO Auto-generated catch block 
     244            e.printStackTrace(); 
     245        } catch (Exception e) { 
     246            // TODO Auto-generated catch block 
     247            e.printStackTrace(); 
     248        } 
     249    } 
     250     
     251    public void testGetMostRecentData() { 
     252        // WebRequest req = new 
     253        // PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{GENDER}%20FROM%203:org.openmrs.cohort.StaticCohortDefinition"); 
     254        WebRequest req = new PostMethodWebRequest( 
     255                "http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{AGE}:OBSERVATIONDATE|%20{GENDER}:OBSERVATIONDATE|%20{WEIGHT%20(KG)}%20LT%2022:OBSERVATIONDATE%20FROM%203:org.openmrs.cohort.StaticCohortDefinition&datastyle=mostrecent"); 
     256        try { 
     257            WebResponse resp = wc.getResponse(req); 
     258            assertTrue(resp.getText().indexOf("dataset") > 0); 
     259 
     260            System.out.println("\n"); 
     261            System.out.println("Data Response:\n"); 
     262            prettyPrint(resp.getDOM(), System.out); 
     263            System.out.println("\n"); 
     264 
     265        } catch (IOException e) { 
     266            // TODO Auto-generated catch block 
     267            e.printStackTrace(); 
     268        } catch (SAXException e) { 
     269            // TODO Auto-generated catch block 
     270            e.printStackTrace(); 
     271        } catch (Exception e) { 
     272            // TODO Auto-generated catch block 
     273            e.printStackTrace(); 
     274        } 
     275    } 
     276 
     277    public void prettyPrint(Document doc, OutputStream out) throws Exception { 
    268278        OutputFormat format = new OutputFormat(doc); 
    269279        format.setLineWidth(65);