1 17 package org.apache.servicemix.bpe; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.net.HttpURLConnection ; 24 import java.net.URI ; 25 import java.net.URL ; 26 27 import javax.jbi.messaging.ExchangeStatus; 28 import javax.jbi.messaging.MessageExchange; 29 import javax.xml.namespace.QName ; 30 31 import junit.framework.TestCase; 32 import loanbroker.Bank; 33 import loanbroker.CreditAgency; 34 35 import org.apache.servicemix.client.DefaultServiceMixClient; 36 import org.apache.servicemix.client.ServiceMixClient; 37 import org.apache.servicemix.http.HttpEndpoint; 38 import org.apache.servicemix.http.HttpSpringComponent; 39 import org.apache.servicemix.jbi.container.ActivationSpec; 40 import org.apache.servicemix.jbi.container.JBIContainer; 41 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 42 import org.apache.servicemix.jbi.jaxp.StringSource; 43 import org.apache.servicemix.jbi.messaging.MessageExchangeSupport; 44 45 public class BPEComponentTest extends TestCase { 46 47 private JBIContainer jbi; 48 private BPEComponent bpe; 49 private ServiceMixClient client; 50 51 protected void setUp() throws Exception { 52 jbi = new JBIContainer(); 53 jbi.setEmbedded(true); 54 jbi.init(); 55 client = new DefaultServiceMixClient(jbi); 56 bpe = new BPEComponent(); 57 jbi.activateComponent(bpe, "bpe"); 58 } 59 60 protected void tearDown() throws Exception { 61 if (jbi != null) { 62 jbi.shutDown(); 63 } 64 } 65 66 protected void registerCreditAgency() throws Exception { 67 ActivationSpec creditAgency = new ActivationSpec(); 68 creditAgency.setInterfaceName(new QName ("urn:logicblaze:soa:creditagency", "CreditAgency")); 69 creditAgency.setComponent(new CreditAgency()); 70 jbi.activateComponent(creditAgency); 71 } 72 73 protected void registerBanks() throws Exception { 74 for (int i = 1; i <= 5; i++) { 75 ActivationSpec bank = new ActivationSpec(); 76 bank.setInterfaceName(new QName ("urn:logicblaze:soa:bank", "Bank")); 77 bank.setComponent(new Bank(i)); 78 jbi.activateComponent(bank); 79 } 80 } 81 82 83 protected void registerHttp() throws Exception { 84 HttpSpringComponent http = new HttpSpringComponent(); 85 HttpEndpoint ep = new HttpEndpoint(); 86 ep.setSoap(true); 87 ep.setDefaultMep(MessageExchangeSupport.IN_OUT); 88 ep.setRoleAsString("consumer"); 89 ep.setService(new QName ("urn:logicblaze:soa:loanbroker", "LoanBrokerService")); 90 ep.setEndpoint("loanbroker"); 91 ep.setLocationURI("http://localhost:8192"); 92 http.setEndpoints(new HttpEndpoint[] { ep }); 93 jbi.activateComponent(http, "http"); 94 } 95 96 public static void copyInputStream(InputStream in, OutputStream out) throws IOException { 97 byte[] buffer = new byte[1024]; 98 int len; 99 while ((len = in.read(buffer)) >= 0) { 100 out.write(buffer, 0, len); 101 } 102 } 103 104 public void testWithHttp() throws Exception { 105 registerCreditAgency(); 106 registerBanks(); 107 registerHttp(); 108 jbi.start(); 109 110 URL url = getClass().getClassLoader().getResource("loanbroker/loanbroker.bpel"); 111 File path = new File (new URI (url.toString())); 112 path = path.getParentFile(); 113 bpe.getServiceUnitManager().deploy("loanbroker", path.getAbsolutePath()); 114 bpe.getServiceUnitManager().start("loanbroker"); 115 116 HttpURLConnection con = (HttpURLConnection ) new URL ("http://localhost:8192").openConnection(); 117 con.setDoOutput(true); 118 con.setDoInput(true); 119 InputStream is = getClass().getClassLoader().getResourceAsStream("request.xml"); 120 OutputStream os = con.getOutputStream(); 121 copyInputStream(is, os); 122 copyInputStream(con.getInputStream(), System.out); 123 } 124 125 public void testBPEOk() throws Exception { 126 registerCreditAgency(); 127 registerBanks(); 128 jbi.start(); 129 130 URL url = getClass().getClassLoader().getResource("loanbroker/loanbroker.bpel"); 131 File path = new File (new URI (url.toString())); 132 path = path.getParentFile(); 133 bpe.getServiceUnitManager().deploy("loanbroker", path.getAbsolutePath()); 134 bpe.getServiceUnitManager().start("loanbroker"); 135 136 MessageExchange me = client.createInOutExchange(); 140 me.setService(new QName ("urn:logicblaze:soa:loanbroker", "LoanBrokerService")); 141 me.setOperation(new QName ("getLoanQuote")); 142 me.getMessage("in").setContent(new StringSource("<getLoanQuoteRequest xmlns=\"urn:logicblaze:soa:loanbroker\"><ssn>1234341</ssn><amount>100000.0</amount><duration>12</duration></getLoanQuoteRequest>")); 143 long t0 = System.currentTimeMillis(); 144 client.sendSync(me); 145 long t1 = System.currentTimeMillis(); 146 if (me.getError() != null) { 147 throw me.getError(); 148 } 149 assertEquals(ExchangeStatus.ACTIVE, me.getStatus()); 150 String out = new SourceTransformer().contentToString(me.getMessage("out")); 151 System.err.println(out); 152 System.err.println("Time: " + (t1 - t0)); 153 client.done(me); 154 155 me = client.createInOutExchange(); 159 me.setService(new QName ("urn:logicblaze:soa:loanbroker", "LoanBrokerService")); 160 me.setOperation(new QName ("getLoanQuote")); 161 me.getMessage("in").setContent(new StringSource("<getLoanQuoteRequest xmlns=\"urn:logicblaze:soa:loanbroker\"><ssn>1234341</ssn><amount>50000.0</amount><duration>12</duration></getLoanQuoteRequest>")); 162 t0 = System.currentTimeMillis(); 163 client.sendSync(me); 164 t1 = System.currentTimeMillis(); 165 if (me.getError() != null) { 166 throw me.getError(); 167 } 168 assertEquals(ExchangeStatus.ACTIVE, me.getStatus()); 169 out = new SourceTransformer().contentToString(me.getMessage("out")); 170 System.err.println(out); 171 System.err.println("Time: " + (t1 - t0)); 172 client.done(me); 173 174 me = client.createInOutExchange(); 178 me.setService(new QName ("urn:logicblaze:soa:loanbroker", "LoanBrokerService")); 179 me.setOperation(new QName ("getLoanQuote")); 180 me.getMessage("in").setContent(new StringSource("<getLoanQuoteRequest xmlns=\"urn:logicblaze:soa:loanbroker\"><ssn>1234341</ssn><amount>1200.0</amount><duration>12</duration></getLoanQuoteRequest>")); 181 t0 = System.currentTimeMillis(); 182 client.sendSync(me); 183 t1 = System.currentTimeMillis(); 184 if (me.getError() != null) { 185 throw me.getError(); 186 } 187 assertEquals(ExchangeStatus.ACTIVE, me.getStatus()); 188 out = new SourceTransformer().contentToString(me.getMessage("out")); 189 System.err.println(out); 190 System.err.println("Time: " + (t1 - t0)); 191 client.done(me); 192 } 193 194 public void testBPEWithFault() throws Exception { 195 registerCreditAgency(); 196 jbi.start(); 197 198 URL url = getClass().getClassLoader().getResource("loanbroker/loanbroker.bpel"); 199 File path = new File (new URI (url.toString())); 200 path = path.getParentFile(); 201 bpe.getServiceUnitManager().deploy("loanbroker", path.getAbsolutePath()); 202 bpe.getServiceUnitManager().start("loanbroker"); 203 204 MessageExchange me = client.createInOutExchange(); 205 me.setService(new QName ("urn:logicblaze:soa:loanbroker", "LoanBrokerService")); 206 me.setOperation(new QName ("getLoanQuote")); 207 me.getMessage("in").setContent(new StringSource("<getLoanQuoteRequest xmlns=\"urn:logicblaze:soa:loanbroker\"><ssn>234341</ssn></getLoanQuoteRequest>")); 208 client.sendSync(me); 209 if (me.getError() != null) { 210 throw me.getError(); 211 } 212 assertEquals(ExchangeStatus.ACTIVE, me.getStatus()); 213 assertNotNull(me.getFault()); 214 client.done(me); 215 } 216 217 public void testBPEWithException() throws Exception { 218 registerCreditAgency(); 219 jbi.start(); 220 221 URL url = getClass().getClassLoader().getResource("loanbroker/loanbroker.bpel"); 222 File path = new File (new URI (url.toString())); 223 path = path.getParentFile(); 224 bpe.getServiceUnitManager().deploy("loanbroker", path.getAbsolutePath()); 225 bpe.getServiceUnitManager().start("loanbroker"); 226 227 MessageExchange me = client.createInOutExchange(); 228 me.setService(new QName ("urn:logicblaze:soa:loanbroker", "LoanBrokerService")); 229 me.setOperation(new QName ("getLoanQuote")); 230 me.getMessage("in").setContent(new StringSource("<getLoanQuoteRequest xmlns=\"urn:logicblaze:soa:loanbroker\"><ssn></ssn></getLoanQuoteRequest>")); 231 client.sendSync(me); 232 assertEquals(ExchangeStatus.ERROR, me.getStatus()); 233 assertNotNull(me.getError()); 234 } 235 236 } 237 | Popular Tags |