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
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) 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
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: *
023: *
024: */
025: /*
026: * Copyright 2005-2006 The Apache Software Foundation.
027: *
028: * Licensed under the Apache License, Version 2.0 (the "License");
029: * you may not use this file except in compliance with the License.
030: * You may obtain a copy of the License at
031: *
032: * http://www.apache.org/licenses/LICENSE-2.0
033: *
034: * Unless required by applicable law or agreed to in writing, software
035: * distributed under the License is distributed on an "AS IS" BASIS,
036: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
037: * See the License for the specific language governing permissions and
038: * limitations under the License.
039: */
040: package com.bostechcorp.cbesb.runtime.component.sequencing;
041:
042: import java.io.File;
043: import java.net.URI;
044: import java.net.URL;
045:
046: import javax.jbi.messaging.ExchangeStatus;
047: import javax.jbi.messaging.InOut;
048: import javax.jbi.messaging.NormalizedMessage;
049: import javax.xml.namespace.QName;
050: import javax.xml.transform.Source;
051:
052: import junit.framework.TestCase;
053:
054: import org.apache.servicemix.client.DefaultServiceMixClient;
055: import org.apache.servicemix.jbi.container.JBIContainer;
056: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
057: import org.apache.servicemix.jbi.messaging.InOutImpl;
058:
059: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.NormalizedMessageHandler;
060: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringSource;
061: import com.bostechcorp.cbesb.runtime.component.parser.ParserComponent;
062: import com.bostechcorp.cbesb.runtime.component.xslt.XsltComponent;
063:
064: public class SequencerProviderTest extends TestCase {
065:
066: protected JBIContainer container;
067:
068: protected void setUp() throws Exception {
069: container = new JBIContainer();
070: container.setUseMBeanServer(false);
071: container.setCreateMBeanServer(false);
072: container.setEmbedded(true);
073: container.setCreateJmxConnector(false);
074: container.init();
075: }
076:
077: protected void tearDown() throws Exception {
078: if (container != null) {
079: container.shutDown();
080: }
081: }
082:
083: /*
084: * The component flow for this testing are as follow:
085: *
086: * SMClient creates -> InOnly -> route FileComponent's Provider side -> Write into the outbox directory
087: * -> picked up by FilePoller -> route to ReceiverComponent
088: *
089: */
090:
091: protected long testInOut(String msg, boolean streaming)
092: throws Exception {
093: // SequencingComponent Component
094: SequencingComponent sequencingComponent = new SequencingComponent();
095: container.activateComponent(sequencingComponent,
096: "sequencingService");
097:
098: ParserComponent parserComponent = new ParserComponent();
099: container.activateComponent(parserComponent, "parserService");
100:
101: XsltComponent xsltComponent = new XsltComponent();
102: container.activateComponent(xsltComponent, "xsltService");
103:
104: // // Add a receiver component
105: // Receiver receiver = new ReceiverComponent();
106: // ActivationSpec asReceiver = new ActivationSpec("receiver", receiver);
107: // asReceiver.setService(new QName("http://file.bostechcorp.com/Test", "FileInterface"));
108: // container.activateComponent(asReceiver);
109: //
110: // // Add the FilePoller
111: // FilePoller connector = new FilePoller();
112: // connector.setFile(new File("outbox"));
113: // ActivationSpec asConnector = new ActivationSpec("connector", connector);
114: // asConnector.setDestinationService(new QName("http://file.bostechcorp.com/Test", "FileInterface"));
115: // container.activateComponent(asConnector);
116:
117: // Start container
118: container.start();
119:
120: // Deploy SU
121: URL url = getClass().getClassLoader().getResource(
122: "sequencer/sequencer.wsdl");
123: File path = new File(new URI(url.toString()));
124: path = path.getParentFile();
125: sequencingComponent.getServiceUnitManager().deploy("sequencer",
126: path.getAbsolutePath());
127: sequencingComponent.getServiceUnitManager().start("sequencer");
128:
129: url = getClass().getClassLoader().getResource(
130: "parser/parser.wsdl");
131: path = new File(new URI(url.toString()));
132: path = path.getParentFile();
133: parserComponent.getServiceUnitManager().deploy("parser",
134: path.getAbsolutePath());
135: parserComponent.getServiceUnitManager().start("parser");
136:
137: url = getClass().getClassLoader().getResource("xslt/xslt.wsdl");
138: path = new File(new URI(url.toString()));
139: path = path.getParentFile();
140: xsltComponent.getServiceUnitManager().deploy("xslt",
141: path.getAbsolutePath());
142: xsltComponent.getServiceUnitManager().start("xslt");
143:
144: // Call it
145: DefaultServiceMixClient client = new DefaultServiceMixClient(
146: container);
147: // InOut inout = client.createInOutExchange();
148: // in.setInterfaceName(new QName("http://parser.bostechcorp.com/Test", "ParserInterface"));
149:
150: // in.getInMessage().setContent(new StreamSource(new ByteArrayInputStream(msg.getBytes())));
151:
152: InOut inout = new InOutImpl("exchangeId");
153:
154: inout.setInterfaceName(new QName(
155: "http://sequencer.bostechcorp.com/Test",
156: "SequencerInterface"));
157:
158: NormalizedMessage nmsg = inout.createMessage();
159:
160: //Create an instance of DataMessageUtil to wrap the NormalizedMessage
161: NormalizedMessageHandler dataMessage1 = new NormalizedMessageHandler(
162: nmsg);
163: //Add the Source as a record
164: dataMessage1.addRecord(new StringSource(msg));
165: dataMessage1.debugContents();
166:
167: nmsg = dataMessage1.generateMessageContent();
168: inout.setMessage(nmsg, "In");
169:
170: long t0 = System.currentTimeMillis();
171:
172: boolean result = client.sendSync(inout);
173: assertTrue(result);
174: assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
175: NormalizedMessage out = inout.getOutMessage();
176: assertNotNull(out);
177: Source src = out.getContent();
178: assertNotNull(src);
179:
180: String resultXML = "";
181: System.err.println(resultXML = new SourceTransformer()
182: .toString(src));
183: //assertTrue("ParserConfig result fails to match: ", resultXML.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>"));
184:
185: inout.setStatus(ExchangeStatus.DONE);
186: client.send(inout);
187: // assertTrue(inout.getStatus() == ExchangeStatus.DONE);
188: long t1 = System.currentTimeMillis();
189:
190: // Let us sleep for a while and give other side a chance to receive exchange
191: try {
192: Thread.sleep(5 * 1000);
193: } catch (InterruptedException e) {
194: // TODO Auto-generated catch block
195: e.printStackTrace();
196: }
197:
198: sequencingComponent.getServiceUnitManager().stop("sequencer");
199: sequencingComponent.getServiceUnitManager().shutDown(
200: "sequencer");
201: sequencingComponent.getServiceUnitManager().undeploy(
202: "sequencer", path.getAbsolutePath());
203: parserComponent.getServiceUnitManager().stop("parser");
204: parserComponent.getServiceUnitManager().shutDown("parser");
205: parserComponent.getServiceUnitManager().undeploy("parser",
206: path.getAbsolutePath());
207: xsltComponent.getServiceUnitManager().stop("xslt");
208: xsltComponent.getServiceUnitManager().shutDown("xslt");
209: xsltComponent.getServiceUnitManager().undeploy("xslt",
210: path.getAbsolutePath());
211:
212: return t1 - t0;
213: }
214:
215: public void testInOut() {
216: try {
217: testInOut(
218: "4267010,Dell,Inspiron 5150 Notebook,1545.00,1,1545.00",
219: false);
220: } catch (Exception e) {
221: // TODO Auto-generated catch block
222: e.printStackTrace();
223: fail();
224: }
225: }
226:
227: }
|