001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: ParserProviderTest.java 3842 2006-12-14 21:11:32Z elu $
023: */
024: package com.bostechcorp.cbesb.runtime.component.parser;
025:
026: import java.io.File;
027: import java.net.URI;
028: import java.net.URL;
029:
030: import javax.jbi.messaging.ExchangeStatus;
031: import javax.jbi.messaging.InOut;
032: import javax.jbi.messaging.NormalizedMessage;
033: import javax.jbi.servicedesc.ServiceEndpoint;
034: import javax.xml.namespace.QName;
035: import javax.xml.transform.Source;
036:
037: import junit.framework.TestCase;
038:
039: import org.apache.servicemix.client.DefaultServiceMixClient;
040: import org.apache.servicemix.jbi.container.JBIContainer;
041: import org.apache.servicemix.jbi.framework.Registry;
042: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
043: import org.apache.servicemix.jbi.messaging.InOutImpl;
044:
045: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.NormalizedMessageHandler;
046: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringSource;
047:
048: public class ParserProviderTest extends TestCase {
049:
050: protected JBIContainer container;
051:
052: protected void setUp() throws Exception {
053: container = new JBIContainer();
054: container.setUseMBeanServer(false);
055: container.setCreateMBeanServer(false);
056: container.setEmbedded(true);
057: container.setCreateJmxConnector(false);
058: container.init();
059: }
060:
061: protected void tearDown() throws Exception {
062: if (container != null) {
063: container.shutDown();
064: }
065: }
066:
067: /*
068: * The component flow for this testing are as follow:
069: *
070: * SMClient creates -> InOnly -> route FileComponent's Provider side ->
071: * Write into the outbox directory -> picked up by FilePoller -> route to
072: * ReceiverComponent
073: *
074: */
075: protected long testInOut(String msg, boolean streaming)
076: throws Exception {
077:
078: // ParserConfig Component
079: ParserComponent component = new ParserComponent();
080: container.activateComponent(component, "ParserProviderTest");
081:
082: // Start container
083: container.start();
084:
085: // Deploy SU
086: URL url = getClass().getClassLoader().getResource(
087: "provider/parser.wsdl");
088:
089: File path = new File(new URI(url.toString()));
090: path = path.getParentFile();
091: component.getServiceUnitManager().deploy("provider",
092: path.getAbsolutePath());
093: component.getServiceUnitManager().start("provider");
094:
095: // Call it
096: DefaultServiceMixClient client = new DefaultServiceMixClient(
097: container);
098: InOut inout = new InOutImpl("exchangeId");
099: inout
100: .setInterfaceName(new QName(
101: "http://parser.bostechcorp.com/Test",
102: "ParserInterface"));
103: System.err.println(inout.getInterfaceName());
104: Registry registry = container.getRegistry();
105: ServiceEndpoint[] endpoints = registry
106: .getEndpointsForInterface(inout.getInterfaceName());
107: for (int i = 0; i < endpoints.length; i++) {
108: System.err
109: .println(endpoints[i].getEndpointName() + " " + i);
110: }
111:
112: NormalizedMessage nmsg = inout.createMessage();
113:
114: // Create an instance of DataMessageUtil to wrap the NormalizedMessage
115: NormalizedMessageHandler dataMessage1 = new NormalizedMessageHandler(
116: nmsg);
117: // Add the Source as a record
118: dataMessage1.addRecord(new StringSource(msg));
119: dataMessage1.debugContents();
120:
121: nmsg = dataMessage1.generateMessageContent();
122: inout.setMessage(nmsg, "In");
123:
124: long t0 = System.currentTimeMillis();
125:
126: boolean result = client.sendSync(inout);
127: assertTrue(result);
128: assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
129: NormalizedMessage out = inout.getOutMessage();
130: assertNotNull(out);
131: Source src = out.getContent();
132: assertNotNull(src);
133:
134: String resultXML = "";
135: System.err.println(resultXML = new SourceTransformer()
136: .toString(src));
137: assertTrue(
138: "ParserConfig result fails to match: ",
139: resultXML
140: .equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><DataEnvelope><XMLRecord><OrderRecord xmlns=\"http://www.bostechcorp.com/variable1\"><SKU>4267010</SKU><Manufacturer>Dell</Manufacturer><ItemDescription>Inspiron 5150 Notebook</ItemDescription><UnitPrice>1545.00</UnitPrice><Quantity>1</Quantity><Total>1545.00</Total></OrderRecord></XMLRecord></DataEnvelope>"));
141:
142: inout.setStatus(ExchangeStatus.DONE);
143: client.send(inout);
144: long t1 = System.currentTimeMillis();
145:
146: // Let us sleep for a while and give other side a chance to receive
147: // exchange
148: try {
149: Thread.sleep(5 * 1000);
150: } catch (InterruptedException e) {
151: // TODO Auto-generated catch block
152: e.printStackTrace();
153: }
154:
155: component.getServiceUnitManager().stop("provider");
156: component.getServiceUnitManager().shutDown("provider");
157: component.getServiceUnitManager().undeploy("provider",
158: path.getAbsolutePath());
159:
160: return t1 - t0;
161: }
162:
163: public void testInOut() {
164: try {
165: testInOut(
166: "4267010,Dell,Inspiron 5150 Notebook,1545.00,1,1545.00",
167: false);
168: // testInOut("ST*824*00917001\r\nBGN*34*0202221*20030323\r\nN1*FR*SAFEWAY INC*9*0091372091700\r\n" +
169: // "LX*12\r\nN1*TO*XYZ SUPPLIER*9*1234567890000\r\nLIN*12\r\nN1*PE*XYZ SUPPLIER*9*1234567890000\r\n"+
170: // "PER*AA*ECR/EDI DEPARTMENT*TE*925-467-3197*EM*ECREDI@SAFEWAY.COM*11*12*13*14\r\nSE*14*00917001",
171: // false);
172: } catch (Exception e) {
173: // TODO Auto-generated catch block
174: e.printStackTrace();
175: fail();
176: }
177: }
178: }
|