Search in sources :

Example 1 with DriverTestsuiteException

use of org.apache.plc4x.test.driver.exceptions.DriverTestsuiteException in project plc4x by apache.

the class DriverTestsuite method parseTestsuite.

public static DriverTestsuite parseTestsuite(URI suiteUri, InputStream testsuiteDocumentXml, boolean autoMigrate) throws DriverTestsuiteException {
    try {
        SAXReader reader = new LocationAwareSAXReader();
        reader.setDocumentFactory(new LocationAwareDocumentFactory());
        Document document = reader.read(testsuiteDocumentXml);
        Element testsuiteXml = document.getRootElement();
        // Force the driver to not wait for the connection before returning the connection.
        System.setProperty(GeneratedDriverBase.PROPERTY_PLC4X_FORCE_AWAIT_SETUP_COMPLETE, "false");
        // Force the driver to not wait for the disconnection before returning closing the channel.
        System.setProperty(GeneratedDriverBase.PROPERTY_PLC4X_FORCE_AWAIT_DISCONNECT_COMPLETE, "false");
        // TODO: replace with signal
        Delay.delay(200);
        // Shared instance for synchronizing
        Synchronizer synchronizer = new Synchronizer();
        DriverTestsuiteBuilder driverTestsuiteBuilder = new DriverTestsuiteBuilder(suiteUri, testsuiteXml, synchronizer, autoMigrate);
        return driverTestsuiteBuilder.build();
    } catch (DocumentException e) {
        throw new DriverTestsuiteException("Error parsing testsuite xml", e);
    }
}
Also used : Synchronizer(org.apache.plc4x.test.driver.internal.utils.Synchronizer) LocationAwareSAXReader(org.apache.plc4x.test.dom4j.LocationAwareSAXReader) SAXReader(org.dom4j.io.SAXReader) LocationAwareElement(org.apache.plc4x.test.dom4j.LocationAwareElement) Element(org.dom4j.Element) DocumentException(org.dom4j.DocumentException) DriverTestsuiteException(org.apache.plc4x.test.driver.exceptions.DriverTestsuiteException) LocationAwareSAXReader(org.apache.plc4x.test.dom4j.LocationAwareSAXReader) LocationAwareDocumentFactory(org.apache.plc4x.test.dom4j.LocationAwareDocumentFactory) Document(org.dom4j.Document)

Example 2 with DriverTestsuiteException

use of org.apache.plc4x.test.driver.exceptions.DriverTestsuiteException in project plc4x by apache.

the class TestStep method parseTestStep.

public static TestStep parseTestStep(Element curElement, Synchronizer synchronizer, DriverTestsuiteConfiguration driverTestsuiteConfiguration) throws DriverTestsuiteException {
    final String elementName = curElement.getName();
    final StepType stepType = StepType.valueOf(elementName.toUpperCase().replace("-", "_"));
    final String stepName = curElement.attributeValue(new QName("name"));
    Element parserArgumentsNode = null;
    Element definitionNode = null;
    for (Element element : curElement.elements()) {
        if (element.getName().equals("parser-arguments")) {
            parserArgumentsNode = element;
        } else if (definitionNode == null) {
            definitionNode = element;
        } else {
            throw new DriverTestsuiteException("Error processing the xml. Only one content node allowed.");
        }
    }
    final List<String> parserArguments = new ArrayList<>();
    if (parserArgumentsNode != null) {
        for (Element parserArgumentNode : parserArgumentsNode.elements()) {
            parserArguments.add(parserArgumentNode.getTextTrim());
        }
    }
    Location location = null;
    if (curElement instanceof LocationAwareElement) {
        location = ((LocationAwareElement) curElement).getLocation();
    }
    return new TestStep(stepType, stepName, location, parserArguments, definitionNode, synchronizer, driverTestsuiteConfiguration);
}
Also used : QName(org.dom4j.QName) LocationAwareElement(org.apache.plc4x.test.dom4j.LocationAwareElement) Element(org.dom4j.Element) DriverTestsuiteException(org.apache.plc4x.test.driver.exceptions.DriverTestsuiteException) ArrayList(java.util.ArrayList) LocationAwareElement(org.apache.plc4x.test.dom4j.LocationAwareElement) Location(org.apache.plc4x.test.model.Location)

Example 3 with DriverTestsuiteException

use of org.apache.plc4x.test.driver.exceptions.DriverTestsuiteException in project plc4x by apache.

the class IncomingPlcMessageHandler method getBytesFromXml.

@SuppressWarnings({ "rawtypes", "unchecked" })
public byte[] getBytesFromXml(Element referenceXml, ByteOrder byteOrder) throws DriverTestsuiteException {
    final WriteBufferByteBased writeBuffer = new WriteBufferByteBased(1024, byteOrder);
    MessageInput messageInput = MessageResolver.getMessageInput(driverTestsuiteConfiguration.getOptions(), referenceXml.getName());
    // Get Message and Validate
    Message message = MessageValidatorAndMigrator.validateInboundMessageAndGet(messageInput, referenceXml, parserArguments);
    // Get Bytes
    try {
        message.serialize(writeBuffer);
        final byte[] data = new byte[message.getLengthInBytes()];
        System.arraycopy(writeBuffer.getData(), 0, data, 0, writeBuffer.getPos());
        return data;
    } catch (SerializationException e) {
        throw new DriverTestsuiteException("Error serializing message", e);
    }
}
Also used : DriverTestsuiteException(org.apache.plc4x.test.driver.exceptions.DriverTestsuiteException)

Example 4 with DriverTestsuiteException

use of org.apache.plc4x.test.driver.exceptions.DriverTestsuiteException in project plc4x by apache.

the class MessageResolver method getMessageIOType.

@SuppressWarnings("unchecked")
private static TypeMessageInput getMessageIOType(Map<String, String> options, String typeName) throws DriverTestsuiteException, ClassNotFoundException {
    String extraMessage = "";
    if (options.containsKey("package")) {
        try {
            return lookup(options.get("package"), typeName);
        } catch (NoSuchMethodException e) {
            extraMessage = "custom package '" + options.get("package") + "' and ";
        }
    }
    String protocolName = options.get("protocolName");
    String outputFlavor = options.get("outputFlavor");
    String classPackage = String.format("org.apache.plc4x.java.%s.%s", protocolName, StringUtils.replace(outputFlavor, "-", ""));
    try {
        return lookup(classPackage, typeName);
    } catch (NoSuchMethodException e) {
        throw new DriverTestsuiteException("Could not find " + typeName + " in " + extraMessage + "standard package '" + classPackage + "'");
    }
}
Also used : DriverTestsuiteException(org.apache.plc4x.test.driver.exceptions.DriverTestsuiteException)

Example 5 with DriverTestsuiteException

use of org.apache.plc4x.test.driver.exceptions.DriverTestsuiteException in project plc4x by apache.

the class ChannelUtil method getOutboundBytes.

public static byte[] getOutboundBytes(Plc4xEmbeddedChannel embeddedChannel) throws DriverTestsuiteException {
    ByteBuf byteBuf = null;
    for (int i = 0; i < MAX_TRIES; i++) {
        byteBuf = embeddedChannel.readOutbound();
        if (byteBuf != null) {
            break;
        }
        Delay.delay(10);
    }
    if (byteBuf == null) {
        throw new DriverTestsuiteException(String.format("No outbound message available within %dms", 10 * MAX_TRIES));
    }
    final byte[] data = new byte[byteBuf.readableBytes()];
    byteBuf.readBytes(data);
    return data;
}
Also used : DriverTestsuiteException(org.apache.plc4x.test.driver.exceptions.DriverTestsuiteException) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

DriverTestsuiteException (org.apache.plc4x.test.driver.exceptions.DriverTestsuiteException)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 LocationAwareElement (org.apache.plc4x.test.dom4j.LocationAwareElement)2 Element (org.dom4j.Element)2 Diff (org.xmlunit.diff.Diff)2 ByteBuf (io.netty.buffer.ByteBuf)1 Plc4xEmbeddedChannel (io.netty.channel.embedded.Plc4xEmbeddedChannel)1 IOException (java.io.IOException)1 Modifier (java.lang.reflect.Modifier)1 Charset (java.nio.charset.Charset)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 StringUtils (org.apache.commons.lang3.StringUtils)1 PlcConnection (org.apache.plc4x.java.api.PlcConnection)1 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)1