|
| 1 | +package com.cybersource.ws.client; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import org.w3c.dom.Document; |
| 7 | +import org.w3c.dom.Element; |
| 8 | +import org.xml.sax.InputSource; |
| 9 | +import org.xml.sax.SAXException; |
| 10 | + |
| 11 | +import javax.xml.parsers.DocumentBuilder; |
| 12 | +import javax.xml.parsers.ParserConfigurationException; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.StringReader; |
| 16 | +import java.util.Properties; |
| 17 | + |
| 18 | +/** |
| 19 | + * Test case to validate the PoolingHttpClientConnection instance |
| 20 | + * @author Mayuri K. |
| 21 | + * |
| 22 | + */ |
| 23 | +public class PoolingHttpClientConnectionIT { |
| 24 | + private String requestFilename = "src/test/resources/auth.xml"; |
| 25 | + PoolingHttpClientConnection con = null; |
| 26 | + Document signedDoc; |
| 27 | + DocumentBuilder builder; |
| 28 | + MerchantConfig mc; |
| 29 | + LoggerWrapper logger; |
| 30 | + Document request; |
| 31 | + private static Document soapEnvelope; |
| 32 | + private static final String SOAP_ENVELOPE = |
| 33 | + "<soap:Envelope xmlns:soap=\"" + |
| 34 | + "http://schemas.xmlsoap.org/soap/envelope/" + |
| 35 | + "\"><soap:Body></soap:Body></soap:Envelope>"; |
| 36 | + private long requestSentTime = System.currentTimeMillis(); |
| 37 | + |
| 38 | + static { |
| 39 | + try { |
| 40 | + // load the SOAP envelope document. |
| 41 | + DocumentBuilder builder = Utility.newDocumentBuilder(); |
| 42 | + StringReader sr = new StringReader(SOAP_ENVELOPE); |
| 43 | + soapEnvelope = builder.parse(new InputSource(sr)); |
| 44 | + sr.close(); |
| 45 | + } catch (ParserConfigurationException e) { |
| 46 | + e.printStackTrace(); |
| 47 | + } catch (SAXException e) { |
| 48 | + e.printStackTrace(); |
| 49 | + } catch (IOException e) { |
| 50 | + e.printStackTrace(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Before |
| 55 | + public void start(){ |
| 56 | + Properties merchantProperties = new Properties(); |
| 57 | + //Loading the properties file from src/test/resources |
| 58 | + InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("test_cybs.properties"); |
| 59 | + if (in == null) { |
| 60 | + throw new RuntimeException("Unable to load test_cybs.properties file"); |
| 61 | + } |
| 62 | + try { |
| 63 | + merchantProperties.load(in); |
| 64 | + } catch (IOException e1) { |
| 65 | + e1.printStackTrace(); |
| 66 | + } |
| 67 | + try { |
| 68 | + builder = Utility.newDocumentBuilder(); |
| 69 | + request = Utility.readRequest(merchantProperties, requestFilename); |
| 70 | + mc = new MerchantConfig(merchantProperties, null); |
| 71 | + String merchantID = mc.getMerchantID(); |
| 72 | + String nsURI = mc.getEffectiveNamespaceURI(); |
| 73 | + setMerchantID(request, merchantID, nsURI); |
| 74 | + logger = new LoggerWrapper(null, true, true, mc); |
| 75 | + |
| 76 | + con = new PoolingHttpClientConnection(mc, builder, logger); |
| 77 | + } catch (ParserConfigurationException e) { |
| 78 | + e.printStackTrace(); |
| 79 | + } catch (ConfigException e) { |
| 80 | + e.printStackTrace(); |
| 81 | + } catch (ClientException e) { |
| 82 | + e.printStackTrace(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Method returns the Connection instance of PoolingHttpURLConnection |
| 88 | + */ |
| 89 | + @Test |
| 90 | + public void testGetInstance(){ |
| 91 | + Assert.assertNotNull(con); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Method test the post request |
| 96 | + */ |
| 97 | + @Test |
| 98 | + public void testPostRequest(){ |
| 99 | + try { |
| 100 | + Element requestMessage |
| 101 | + = Utility.getElement( |
| 102 | + request, "requestMessage", mc.getEffectiveNamespaceURI()); |
| 103 | + Document wrappedDoc = builder.newDocument(); |
| 104 | + |
| 105 | + wrappedDoc.appendChild( |
| 106 | + wrappedDoc.importNode(soapEnvelope.getFirstChild(), true)); |
| 107 | + |
| 108 | + if (requestMessage != null) { |
| 109 | + wrappedDoc.getFirstChild().getFirstChild().appendChild( |
| 110 | + wrappedDoc.importNode(requestMessage, true)); |
| 111 | + } |
| 112 | + SecurityUtil.loadMerchantP12File(mc,logger); |
| 113 | + signedDoc = SecurityUtil.createSignedDoc(wrappedDoc, mc.getKeyAlias(), mc.getKeyPassword(), logger); |
| 114 | + Document wrappedReply = con.post(signedDoc, requestSentTime); |
| 115 | + Assert.assertNotNull(wrappedReply); |
| 116 | + //test the http response code |
| 117 | + Assert.assertEquals(200, con.getHttpResponseCode()); |
| 118 | + //test the http request sent or not |
| 119 | + Assert.assertEquals(true, con.isRequestSent()); |
| 120 | + |
| 121 | + } catch (ClientException e) { |
| 122 | + e.printStackTrace(); |
| 123 | + } catch (FaultException e) { |
| 124 | + e.printStackTrace(); |
| 125 | + } catch (SignException e) { |
| 126 | + e.printStackTrace(); |
| 127 | + } catch (SignEncryptException e) { |
| 128 | + e.printStackTrace(); |
| 129 | + } catch (ConfigException e) { |
| 130 | + e.printStackTrace(); |
| 131 | + } |
| 132 | + } |
| 133 | + /** |
| 134 | + * Sets the merchantID in the request. |
| 135 | + * |
| 136 | + * @param request request to add the merchantID to. |
| 137 | + * @param merchantID merchantID to add to request. |
| 138 | + * @param nsURI namespace URI to use. |
| 139 | + */ |
| 140 | + private static void setMerchantID(Document request, String merchantID, String nsURI) { |
| 141 | + // create merchantID node |
| 142 | + Element merchantIDElem = Utility.createElement(request, nsURI, "merchantID", merchantID); |
| 143 | + |
| 144 | + // add it as the first child of the requestMessage element. |
| 145 | + Element requestMessage = Utility.getElement(request, "requestMessage", nsURI); |
| 146 | + requestMessage.insertBefore(merchantIDElem, requestMessage.getFirstChild()); |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments