Java tutorial
/* * Copyright Ekagra and SemanticBits, LLC * * Distributed under the OSI-approved BSD 3-Clause License. * See http://ncip.github.com/ihub/LICENSE.txt for details. */ package gov.nih.nci.caxchange.messaging; import gov.nih.nci.integration.catissue.CaTissueParticipantClientIntegrationTest; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * This is an integration test class for Adverse event flows. * * @author Rohit Gupta */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext-mirth-deploy-test.xml") public class AdverseEventIntegrationTest { @Value("${transcend.caxchange.service.url}") private String transcendCaxchangeServiceUrl; private final HttpClient httpclient = new DefaultHttpClient(); private static final String XMLTEXT = "text/xml"; private static final String ERRORCODE1053 = "<errorCode>1053</errorCode>"; private static final String ERRORCODE1101 = "<errorCode>1101</errorCode>"; private static final Logger LOG = LoggerFactory.getLogger(AdverseEventIntegrationTest.class); /** * TestCase for Creating Adverse Event in caAERS */ @Test public void createAdverseEvent() { try { final HttpPost httppost = new HttpPost(transcendCaxchangeServiceUrl); final StringEntity reqentity = new StringEntity(getCreateAdverseEventXMLStr()); httppost.setEntity(reqentity); httppost.setHeader(HttpHeaders.CONTENT_TYPE, XMLTEXT); final HttpResponse response = httpclient.execute(httppost); final HttpEntity entity = response.getEntity(); String createdXML = null; if (entity != null) { createdXML = EntityUtils.toString(entity); Assert.assertEquals(true, createdXML.contains("<responseStatus>SUCCESS</responseStatus>")); } } catch (ClientProtocolException e) { Assert.fail(e.getMessage()); } catch (IllegalStateException e) { Assert.fail(e.getMessage()); } catch (IOException e) { Assert.fail(e.getMessage()); } } /** * TestCase for Creating Adverse Event in caAERS when Study does not exist in caAERS. */ @Test public void createAEStudyNotExist() { try { final HttpPost httppost = new HttpPost(transcendCaxchangeServiceUrl); final StringEntity reqentity = new StringEntity(getCreateAEStudyNotExist()); httppost.setEntity(reqentity); httppost.setHeader(HttpHeaders.CONTENT_TYPE, XMLTEXT); final HttpResponse response = httpclient.execute(httppost); final HttpEntity entity = response.getEntity(); String createdXML = null; if (entity != null) { createdXML = EntityUtils.toString(entity); Assert.assertEquals(true, createdXML.contains(ERRORCODE1101)); } } catch (ClientProtocolException e) { Assert.fail(e.getMessage()); } catch (IllegalStateException e) { Assert.fail(e.getMessage()); } catch (IOException e) { Assert.fail(e.getMessage()); } } /** * TestCase for Creating Adverse Event in caAERS when Participant does not exist in caAERS. */ @Test public void createAEParticipantNotExist() { try { final HttpPost httppost = new HttpPost(transcendCaxchangeServiceUrl); final StringEntity reqentity = new StringEntity(getCreateAEParticipantNotExist()); httppost.setEntity(reqentity); httppost.setHeader(HttpHeaders.CONTENT_TYPE, XMLTEXT); final HttpResponse response = httpclient.execute(httppost); final HttpEntity entity = response.getEntity(); String createdXML = null; if (entity != null) { createdXML = EntityUtils.toString(entity); Assert.assertEquals(true, createdXML.contains(ERRORCODE1053)); } } catch (ClientProtocolException e) { Assert.fail(e.getMessage()); } catch (IllegalStateException e) { Assert.fail(e.getMessage()); } catch (IOException e) { Assert.fail(e.getMessage()); } } private String getCreateAdverseEventXMLStr() { return getXMLString("CreateAdverseEvent_inbound.xml"); } private String getCreateAEStudyNotExist() { return getXMLString("CreateAEStudyNotExist_inbound.xml"); } private String getCreateAEParticipantNotExist() { return getXMLString("CreateAEParticipantNotExist_inbound.xml"); } private String getXMLString(String fileName) { String contents = null; final InputStream is = CaTissueParticipantClientIntegrationTest.class.getClassLoader() .getResourceAsStream("payloads/adverseevent/" + fileName); try { contents = org.apache.cxf.helpers.IOUtils.toString(is); } catch (IOException e) { LOG.error("Error while reading contents of file : " + fileName + ". " + e); } return contents; } }