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: package com.bostechcorp.cbesb.runtime.cbr.test;
026:
027: import java.io.File;
028:
029: import javax.xml.parsers.DocumentBuilder;
030: import javax.xml.parsers.DocumentBuilderFactory;
031: import javax.xml.xpath.XPath;
032: import javax.xml.xpath.XPathExpressionException;
033: import javax.xml.xpath.XPathFactory;
034:
035: import junit.framework.TestCase;
036:
037: import org.w3c.dom.Document;
038: import org.xml.sax.InputSource;
039:
040: import com.bostechcorp.cbesb.common.util.Dom;
041: import com.bostechcorp.cbesb.runtime.cbr.CBRException;
042: import com.bostechcorp.cbesb.runtime.cbr.ExpressionHandler;
043: import com.bostechcorp.cbesb.runtime.cbr.RoutingRuleElement;
044: import com.bostechcorp.cbesb.runtime.cbr.RoutingRulesHandler;
045: import com.bostechcorp.cbesb.runtime.cbr.TargetElement;
046: import com.bostechcorp.cbesb.runtime.cbr.TrxIDHandler;
047:
048: public class TestCBR extends TestCase {
049:
050: public RoutingRulesHandler routingRuleHandler;
051:
052: public void testFixed() throws Throwable {
053:
054: loadRoutingRules("src/resources/fixed.xml");
055:
056: String inMessage = "4267010,Dell,Inspiron 5150 Notebook,1545.00,1,1545.00";
057:
058: String trxId = processTrxId(inMessage, "fixed", 0, 3, null, 0,
059: null, null, null, null);
060:
061: printTargetService(inMessage, trxId);
062:
063: }
064:
065: public void testCVS() throws Throwable {
066:
067: loadRoutingRules("src/resources/cvs.xml");
068:
069: String inMessage = "4267010,Dell,Inspiron 5150 Notebook,1545.00,1,1545.00";
070:
071: String trxId = processTrxId(inMessage, "cvs", 0, 0, ",", 2,
072: null, null, null, null);
073:
074: printTargetService(inMessage, trxId);
075:
076: }
077:
078: public void testX12() throws Throwable {
079:
080: loadRoutingRules("src/resources/x12.xml");
081:
082: String inMessage = "ST*824*00917001~BGN*34*0202221*20030323~N1*FR*SAFEWAY INC*9*"
083: + "0091372091700~N1*TO*XYZ SUPPLIER*9*1234567890000~N1*PE*XYZ SUPPLIER*9*"
084: + "1234567890000~PER*AA*ECR/EDI DEPARTMENT*TE*925-467-3197*EM*ECREDI@SAFEWAY.COM~OTI*"
085: + "TR*TN*0202221*7778889999*8019721193*20030321*0930*5520*055200010*810~DTM*003*"
086: + "20030319~AMT*TP*22234.8~QTY*53*60~TED*012*"
087: + "INVALID SHIP-TO OR INVALID PO NUMBER*****SHIP-TO DUNS SUFFIX/PO # (1922/912938)~NTE*GEN*CONTACT BROKER OR ORDER ENTRY DEPARTMENT~NTE*GEN*PLEASE CORRECT INVOICE(S) & RETRANSMIT EDI. PAPER INVOICES WILL NOT BE ACCEPTED.~SE*14*00917001~";
088:
089: String trxId = processTrxId(inMessage, "x12", 0, 0, null, 0,
090: null, null, null, null);
091:
092: printTargetService(inMessage, trxId);
093:
094: }
095:
096: public void testHL7() throws Throwable {
097:
098: loadRoutingRules("src/resources/hl7.xml");
099:
100: String inMessage = "MSH|^~\\&|REGADT|MCM|IFENG||199112311501||ADT^A04|000001|P|2.3.1|||"
101: + "\r"
102: + "EVN|A04|199601101500|199601101400|01||199601101410"
103: + "\n";
104:
105: String trxId = processTrxId(inMessage, "hl7", 0, 0, null, 0,
106: null, null, null, null);
107:
108: printTargetService(inMessage, trxId);
109:
110: }
111:
112: public void testXPATH() throws Throwable {
113:
114: loadRoutingRules("src/resources/xpath.xml");
115:
116: DocumentBuilderFactory dbf = DocumentBuilderFactory
117: .newInstance();
118: dbf.setNamespaceAware(true);
119: DocumentBuilder db = dbf.newDocumentBuilder();
120: Document doc = db.parse("src/resources/books.xml");
121:
122: String inMessage = Dom.createStringFromDOMDocument(doc, true);
123:
124: String trxId = processTrxId(inMessage, "xpath", 0, 0, null, 0,
125: "/bookstore/book/price/text()", null, null, null);
126:
127: printTargetService(inMessage, trxId);
128:
129: }
130:
131: public void testScript() throws Throwable {
132: loadRoutingRules("src/resources/script.xml");
133:
134: String inMessage = "Hello World";
135:
136: String trxId = processTrxId(inMessage, "script", 0, 0, null, 0,
137: null, "groovy", "test.groovy", "execute");
138:
139: printTargetService(inMessage, trxId);
140: }
141:
142: public void testRegExp() throws Throwable {
143: loadRoutingRules("src/resources/expression.xml");
144:
145: String inMessage = "4267010,Dell.haha,Inspiron 5150 Notebook,1545.00,1,1545.00";
146:
147: String trxId = processTrxId(inMessage, "cvs", 0, 0, ",", 2,
148: null, null, null, null);
149:
150: printTargetService(inMessage, trxId);
151: }
152:
153: private void printTargetService(String inMessage, String trxId) {
154: System.out.println("trxid : " + trxId);
155: for (int i = 0; i < routingRuleHandler.size(); i++) {
156: RoutingRuleElement routingRule = routingRuleHandler
157: .getRoutingRuleAtIndex(i);
158: String expression = routingRule.getExpression();
159: String expressionType = routingRule.getExpressionType();
160: boolean result = true;
161: if (expression != null && expressionType != null) {
162: try {
163: result = ExpressionHandler.processExpression(
164: inMessage, expressionType, expression,
165: trxId);
166: } catch (Exception e) {
167: // TODO: handle exception
168: }
169: }
170:
171: if (result) {
172: for (int j = 0; j < routingRule.size(); j++) {
173: TargetElement target = routingRule
174: .getTargetServiceAtIndex(j);
175: System.err.println(target.getService());
176: }
177: }
178: }
179: }
180:
181: private void loadRoutingRules(String fileName) throws Exception {
182: File routingRulesFile = new File(fileName);
183: routingRuleHandler = RoutingRulesHandler.load(routingRulesFile);
184: }
185:
186: private String processTrxId(String inMessage, String type,
187: int offset, int length, String delimiter, int index,
188: String expression, String scriptEngine, String scriptClass,
189: String scriptMethod) throws Exception {
190:
191: String result = null;
192: if ("fixed".equalsIgnoreCase(type)) {
193: result = TrxIDHandler.instance().processFixed(inMessage,
194: offset, length);
195: } else if ("cvs".equalsIgnoreCase(type)) {
196: result = TrxIDHandler.instance().processCSV(inMessage,
197: delimiter, index);
198: } else if ("x12".equalsIgnoreCase(type)) {
199: // String expression = ((CBREndpoint) getEndpoint())
200: result = TrxIDHandler.instance().processX12(inMessage);
201: } else if ("hl7".equalsIgnoreCase(type)) {
202: result = TrxIDHandler.instance().processHL7(inMessage);
203: } else if ("xpath".equalsIgnoreCase(type)) {
204: result = TrxIDHandler.instance().processXPATH(expression,
205: inMessage);
206: }
207: /*
208:
209: else if ("script".equalsIgnoreCase(type)) {
210: result = TrxIDHandler.instance().processScript(inMessage,
211: scriptEngine, scriptClass, scriptMethod,null);
212: } else {
213: throw new CBRException("Wrong trxId type");
214: }
215: */
216:
217: return result;
218: }
219: }
|