Created
May 20, 2013 08:31
-
-
Save lebinh/5611060 to your computer and use it in GitHub Desktop.
Mirth Connect channel test framework prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.mirth.connect.model.Channel; | |
import com.mirth.connect.model.Connector; | |
import com.mirth.connect.model.Filter; | |
import com.mirth.connect.model.Transformer; | |
import com.mirth.connect.model.converters.IXMLSerializer; | |
import com.mirth.connect.model.converters.ObjectXMLSerializer; | |
import com.mirth.connect.model.converters.SerializerFactory; | |
import com.mirth.connect.server.builders.JavaScriptBuilder; | |
import com.mirth.connect.server.util.JavaScriptUtil; | |
import junit.framework.TestCase; | |
import org.apache.log4j.Logger; | |
import org.mozilla.javascript.Context; | |
import org.mozilla.javascript.Scriptable; | |
import org.springframework.core.io.ClassPathResource; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.List; | |
public abstract class BaseChannelTestCase extends TestCase { | |
private static final Logger LOGGER = Logger.getLogger(BaseChannelTestCase.class); | |
private Channel channel; | |
private JavaScriptUtil jsUtil = JavaScriptUtil.getInstance(); | |
private JavaScriptBuilder jsBuilder = new JavaScriptBuilder(); | |
private Context jsContext = jsUtil.getContext(); | |
private Scriptable jsScope = jsUtil.getScope(); | |
private IXMLSerializer<String> defaultHl7Serializer = SerializerFactory.getHL7Serializer(); | |
/** | |
* Setup this BaseChannelTestCase to use given channel configuration. | |
* | |
* @param filePath path to channel configuration file | |
*/ | |
public void useChannelConfiguration(String filePath) throws IOException { | |
ClassPathResource res = new ClassPathResource(filePath); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(res.getInputStream())); | |
ObjectXMLSerializer serializer = new ObjectXMLSerializer(); | |
channel = (Channel) serializer.fromXML(reader); | |
} | |
protected String hl7ToXml(String hl7Msg) throws Exception { | |
return defaultHl7Serializer.toXML(hl7Msg); | |
} | |
protected String xmlToHl7(String xmlMsg) throws Exception { | |
return defaultHl7Serializer.fromXML(xmlMsg); | |
} | |
protected Connector getSourceConnector() { | |
return channel.getSourceConnector(); | |
} | |
protected List<Connector> getDestinationConnectors() { | |
return channel.getDestinationConnectors(); | |
} | |
private Connector getDestinationConnector(int index) { | |
List<Connector> all = channel.getDestinationConnectors(); | |
return all.get(index); | |
} | |
/** | |
* Return the filter of source connector. | |
* | |
* @return the source filter instance | |
*/ | |
protected Filter getSourceFilter() { | |
return channel.getSourceConnector().getFilter(); | |
} | |
/** | |
* Return the transformer of source connector. | |
* | |
* @return the source transformer instance | |
*/ | |
protected Transformer getSourceTransformer() { | |
return channel.getSourceConnector().getTransformer(); | |
} | |
/** | |
* Return the filter of specific destination. | |
* | |
* @param index the destination index | |
* @return the filter | |
*/ | |
protected Filter getDestinationFilter(int index) { | |
return getDestinationConnector(index).getFilter(); | |
} | |
/** | |
* Return the transformer of specific destination. | |
* | |
* @param index the destination index | |
* @return the transformer | |
*/ | |
protected Transformer getDestinationTransformer(int index) { | |
return getDestinationConnector(index).getTransformer(); | |
} | |
/** | |
* Process message on current channel. | |
* | |
* @param msg the message to test | |
* @return a list of output message for every destination of current channel | |
*/ | |
protected List<String> processMessage(String msg) throws Exception { | |
Filter sourceFilter = getSourceFilter(); | |
if (!applyFilter(sourceFilter, msg)) { | |
// filtered message | |
return null; | |
} | |
Transformer sourceTransformer = getSourceTransformer(); | |
msg = applyTransformer(sourceTransformer, msg); | |
List<Connector> destinations = getDestinationConnectors(); | |
List<String> result = new ArrayList<String>(); | |
for (Connector conn : destinations) { | |
Filter filter = conn.getFilter(); | |
Transformer trans = conn.getTransformer(); | |
if (applyFilter(filter, msg)) { | |
result.add(applyTransformer(trans, msg)); | |
} else { | |
result.add(null); | |
} | |
} | |
return result; | |
} | |
/** | |
* Apply a specific filter on given message. | |
* | |
* @param filter the filter to use | |
* @param msg the test message | |
* @return true if this msg is accepted by given filter, false otherwise | |
*/ | |
protected boolean applyFilter(Filter filter, String msg) throws Exception { | |
if (filter.getRules().isEmpty()) { | |
return true; | |
} | |
StringBuilder script = new StringBuilder(); | |
script.append("var msg = ").append(msg).append(";"); | |
String filterScript = jsBuilder.generateTransformerScript(filter, new Transformer()); | |
script.append(filterScript); | |
script.append("doFilter();"); | |
LOGGER.debug(script); | |
Object result = jsContext.evaluateString(jsScope, script.toString(), "source-filter", 0, null); | |
return (Boolean) result; | |
} | |
/** | |
* Apply a specific transformer on given message. | |
* | |
* @param transformer the transformer to use | |
* @param msg the test message | |
* @return the transformed message by given transformer | |
*/ | |
protected String applyTransformer(Transformer transformer, String msg) throws Exception { | |
if (transformer.getSteps().isEmpty()) { | |
return msg; | |
} | |
StringBuilder script = new StringBuilder(); | |
script.append("var msg = ").append(msg).append(";\n"); | |
String filterScript = jsBuilder.generateTransformerScript(new Filter(), transformer); | |
script.append(filterScript); | |
script.append("doTransform(); msg"); | |
LOGGER.debug(script); | |
Object result = jsContext.evaluateString(jsScope, script.toString(), "source-filter", 0, null); | |
return (String) result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you resolve this?