/** * The contents of this file are subject to the OpenMRS Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://license.openmrs.org * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * Copyright (C) OpenMRS, LLC. All Rights Reserved. */ package org.openmrs.module.odamocklogicws; import java.io.IOException; import junit.framework.TestCase; import com.meterware.httpunit.*; import org.apache.xml.serialize.XMLSerializer; import org.apache.xml.serialize.OutputFormat; import org.w3c.dom.Document; import java.io.OutputStream; import org.xml.sax.SAXException; public class TestMockWebService extends TestCase { private String realm = "OpenMRS Rest API"; private String user = "admin"; private String pass = "test"; private WebConversation wc; public void setUp() { wc = new WebConversation(); wc.setAuthentication(realm, user, pass); } public void testGetFilters() { WebRequest req = new GetMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getFilters"); try { WebResponse resp = wc.getResponse(req); assertTrue(resp.getText().indexOf("filterList") > 0); System.out.println("\n"); System.out.println("Filter Response:\n"); prettyPrint(resp.getDOM(), System.out); System.out.println("\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void testGetTokens() { WebRequest req = new GetMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getTokens"); try { WebResponse resp = wc.getResponse(req); assertTrue(resp.getText().indexOf("tokenList") > 0); System.out.println("\n"); System.out.println("Token Response:\n"); prettyPrint(resp.getDOM(), System.out); System.out.println("\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void testGetTokenTags() { WebRequest req = new GetMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getTokenTags"); try { WebResponse resp = wc.getResponse(req); assertTrue(resp.getText().indexOf("tokenTagList") > 0); System.out.println("\n"); System.out.println("Token Tag Response:\n"); prettyPrint(resp.getDOM(), System.out); System.out.println("\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void testGetData() { WebRequest req = new GetMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?filter=2&filterType=static&token=token1&token=token2"); try { WebResponse resp = wc.getResponse(req); assertTrue(resp.getText().indexOf("dataset") > 0); System.out.println("\n"); System.out.println("Data Response:\n"); prettyPrint(resp.getDOM(), System.out); System.out.println("\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void testGetDataWithModifiers() { WebRequest req = new GetMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?filter=0&filterType=static&token=AGE|Greater Than|30&token=DEAD|Last&token=DEATH%20DATE|Exists"); try { WebResponse resp = wc.getResponse(req); assertTrue(resp.getText().indexOf("dataset") > 0); System.out.println("\n"); System.out.println("Data Response:\n"); prettyPrint(resp.getDOM(), System.out); System.out.println("\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void prettyPrint(Document doc, OutputStream out) throws Exception { OutputFormat format = new OutputFormat(doc); format.setLineWidth(65); format.setIndenting(true); format.setIndent(2); format.setOmitDocumentType(true); XMLSerializer serializer = new XMLSerializer(out, format); serializer.serialize(doc); } }