Search in sources :

Example 1 with RbvException

use of com.axway.ats.rbv.model.RbvException in project ats-framework by Axway.

the class StringInMimePartRule method performMatch.

@Override
public boolean performMatch(MetaData metaData) throws RbvException {
    boolean actualResult = false;
    //get the emailMessage
    //the meta data type check already passed, so it is safe to cast
    MimePackage emailMessage = getNeededMimePackage(metaData);
    InputStream actualPartDataStream = null;
    BufferedInputStream bufferedStream = null;
    try {
        List<MimePart> partsToCheck = new ArrayList<MimePart>();
        if (partIndex == PART_MAIN_MESSAGE) {
            partsToCheck = emailMessage.getMimeParts();
        } else {
            partsToCheck.add(emailMessage.getPart(partIndex, isPartAttachment));
        }
        for (MimePart partToCheck : partsToCheck) {
            Object partContent = partToCheck.getContent();
            ContentType partContentType = new ContentType(partToCheck.getContentType());
            //skip if no content
            if (partContent == null) {
                log.debug("MIME part does not have any content");
                continue;
            }
            String partContentAsString;
            if (partContent instanceof String) {
                //directly read the content of the part
                partContentAsString = (String) partContent;
            } else if (partContent instanceof InputStream && partContentType.getBaseType().toLowerCase().startsWith(TEXT_MIME_TYPE_LC)) {
                // to be closed in finally
                actualPartDataStream = (InputStream) partContent;
                //get the charset of the part - default to us-ascii
                String charset = partContentType.getParameter("charset");
                if (charset == null) {
                    charset = "us-ascii";
                }
                //read stream by large chunks to minimize memory fragmentation
                int bufLen = 4096;
                byte[] buffer = new byte[bufLen];
                StringBuffer dataStringBuffer = new StringBuffer();
                bufferedStream = new BufferedInputStream(actualPartDataStream);
                int numBytesRead = bufLen;
                while (numBytesRead == bufLen) {
                    numBytesRead = bufferedStream.read(buffer, 0, bufLen);
                    if (numBytesRead != -1) {
                        dataStringBuffer.append(new String(buffer, 0, numBytesRead, charset));
                    } else {
                        //we've reached end of stream
                        break;
                    }
                }
                partContentAsString = dataStringBuffer.toString();
            } else {
                log.debug("Skipping MIME part as it is binary");
                continue;
            }
            if (isValueRegularExpression) {
                actualResult = Pattern.compile(expectedValue).matcher(partContentAsString).find();
            } else {
                actualResult = partContentAsString.indexOf(expectedValue) >= 0;
            }
            //continue anymore
            if (actualResult) {
                break;
            }
        }
        return actualResult;
    } catch (MessagingException me) {
        throw new RbvException(me);
    } catch (PackageException pe) {
        throw new RbvException(pe);
    } catch (IOException ioe) {
        throw new RbvException(ioe);
    } finally {
        IoUtils.closeStream(actualPartDataStream);
        IoUtils.closeStream(bufferedStream);
    }
}
Also used : ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) RbvException(com.axway.ats.rbv.model.RbvException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MimePackage(com.axway.ats.action.objects.MimePackage) BufferedInputStream(java.io.BufferedInputStream) MimePart(javax.mail.internet.MimePart) PackageException(com.axway.ats.action.objects.model.PackageException)

Example 2 with RbvException

use of com.axway.ats.rbv.model.RbvException in project ats-framework by Axway.

the class SubjectRule method performMatch.

@Override
protected boolean performMatch(MetaData metaData) throws RbvException {
    //get the emailMessage
    //the meta data type check already passed, so it is safe to cast
    MimePackage emailMessage = getNeededMimePackage(metaData);
    //get the actual subject value
    String subjectValue;
    try {
        subjectValue = emailMessage.getSubject();
    } catch (PackageException pe) {
        throw new RbvException(pe);
    }
    //if there is no such header return false 
    boolean actualResult = false;
    if (subjectValue != null) {
        switch(matchMode) {
            case LEFT:
                actualResult = subjectValue.startsWith(expectedValue);
                break;
            case RIGHT:
                actualResult = subjectValue.endsWith(expectedValue);
                break;
            case EQUALS:
                actualResult = subjectValue.equals(expectedValue);
                break;
            case FIND:
                actualResult = subjectValue.indexOf(expectedValue) >= 0;
                break;
            case REGEX:
                actualResult = Pattern.compile(expectedValue).matcher(subjectValue).find();
                break;
        }
        log.info("Actual subject is '" + subjectValue + "'");
    } else {
        log.info("No subject was found");
    }
    return actualResult;
}
Also used : MimePackage(com.axway.ats.action.objects.MimePackage) RbvException(com.axway.ats.rbv.model.RbvException) PackageException(com.axway.ats.action.objects.model.PackageException)

Example 3 with RbvException

use of com.axway.ats.rbv.model.RbvException in project ats-framework by Axway.

the class MimePartRule method performMatch.

@Override
protected boolean performMatch(MetaData metaData) throws RbvException {
    boolean actualResult = false;
    //get the emailMessage
    //the meta data type check already passed, so it is safe to cast
    MimePackage emailMessage = ((ImapMetaData) metaData).getMimePackage();
    try {
        InputStream actualPartDataStream = null;
        try {
            actualPartDataStream = emailMessage.getPartData(partIndex, isPartAttachment);
        } catch (NoSuchMimePartException e) {
            //if there is no such mime part then the parts do not match
            log.debug("No MIME part at position '" + partIndex + "'");
            return false;
        }
        if (actualPartDataStream != null) {
            long actualChecksum = emailMessage.getPartChecksum(partIndex, isPartAttachment);
            actualResult = (expectedChecksum == actualChecksum);
        } else {
            log.debug("MIME part at position '" + partIndex + "' does not have any content");
            return false;
        }
    } catch (PackageException pe) {
        throw new RbvException(pe);
    }
    return actualResult;
}
Also used : MimePackage(com.axway.ats.action.objects.MimePackage) ImapMetaData(com.axway.ats.rbv.imap.ImapMetaData) InputStream(java.io.InputStream) RbvException(com.axway.ats.rbv.model.RbvException) NoSuchMimePartException(com.axway.ats.action.objects.model.NoSuchMimePartException) PackageException(com.axway.ats.action.objects.model.PackageException)

Example 4 with RbvException

use of com.axway.ats.rbv.model.RbvException in project ats-framework by Axway.

the class Test_DbVerification method clearRulesTest.

@Test
public void clearRulesTest() throws RbvException {
    DbVerification dbVerification = new DbVerification(new DbSearchTerm(""), provider);
    try {
        //adding rule with wrong value and expecting RBVException exception 
        dbVerification.checkFieldValueEquals("", "key1", "wrongValue");
        dbVerification.verifyDbDataExists();
        Assert.fail();
    } catch (RbvException e) {
    //this is the expected behavior
    }
    //clear all rules
    dbVerification.clearRules();
    //adding rule with existing value
    dbVerification.checkFieldValueEquals("", "key1", "value00");
    dbVerification.verifyDbDataExists();
}
Also used : RbvException(com.axway.ats.rbv.model.RbvException) DbSearchTerm(com.axway.ats.rbv.db.DbSearchTerm) DbVerification(com.axway.ats.rbv.clients.DbVerification) BaseTest(com.axway.ats.rbv.BaseTest) Test(org.junit.Test)

Example 5 with RbvException

use of com.axway.ats.rbv.model.RbvException in project ats-framework by Axway.

the class VerificationSkeleton method verify.

/**
     * Verify that all rules match using the given expected result
     * 
     * @param expectedResult the expected result
     * @param endOnFirstMatch end on first match or keep testing until all polling attempts are exhausted
     * @param endOnFirstFailure end or first failure or keep testing until all polling attempts are exhausted
     * 
     * @return the matched meta data
     * @throws RbvException on error doing the verifications
     */
protected List<MetaData> verify(boolean expectedResult, boolean endOnFirstMatch, boolean endOnFirstFailure) throws RbvException {
    try {
        this.executor.setRootRule(this.rootRule);
        applyConfigurationSettings();
        Monitor monitor = new Monitor(getMonitorName(), this.folder, this.executor, new PollingParameters(pollingInitialDelay, pollingInterval, pollingAttempts), expectedResult, endOnFirstMatch, endOnFirstFailure);
        ArrayList<Monitor> monitors = new ArrayList<Monitor>();
        monitors.add(monitor);
        SimpleMonitorListener monitorListener = new SimpleMonitorListener(monitors);
        boolean evaluationPassed = monitorListener.evaluateMonitors(pollingTimeout);
        String lastRuleName = monitor.getLastRuleName();
        String classSimpleName = getClass().getSimpleName();
        if ("FileSystemVerification".equals(classSimpleName) && !expectedResult) {
            lastRuleName = "File do exist!";
        } else if (lastRuleName != null) {
            lastRuleName = "Rule failed '" + lastRuleName + "'!";
        } else {
            if ("DbVerification".equals(classSimpleName)) {
                lastRuleName = "Database connection problem!";
            } else if ("FileSystemVerification".equals(classSimpleName) && expectedResult) {
                lastRuleName = "File does not exist!";
            } else {
                lastRuleName = "Server connection problem!";
            }
        }
        if (evaluationPassed == false) {
            throw new RbvVerificationException("Verification failed. " + lastRuleName);
        }
        return monitor.getAllMatchedMetaData();
    } catch (ConfigurationException ce) {
        throw new RbvException("RBV configuration error", ce);
    }
}
Also used : Monitor(com.axway.ats.rbv.Monitor) RbvVerificationException(com.axway.ats.rbv.model.RbvVerificationException) SimpleMonitorListener(com.axway.ats.rbv.SimpleMonitorListener) ConfigurationException(com.axway.ats.config.exceptions.ConfigurationException) RbvException(com.axway.ats.rbv.model.RbvException) ArrayList(java.util.ArrayList) PollingParameters(com.axway.ats.rbv.PollingParameters)

Aggregations

RbvException (com.axway.ats.rbv.model.RbvException)15 PackageException (com.axway.ats.action.objects.model.PackageException)6 MimePackage (com.axway.ats.action.objects.MimePackage)5 BaseTest (com.axway.ats.rbv.BaseTest)3 MetaData (com.axway.ats.rbv.MetaData)3 MetaDataIncorrectException (com.axway.ats.rbv.model.MetaDataIncorrectException)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 Date (java.util.Date)2 FileSystemOperations (com.axway.ats.action.filesystem.FileSystemOperations)1 FilePackage (com.axway.ats.action.objects.FilePackage)1 NoSuchHeaderException (com.axway.ats.action.objects.model.NoSuchHeaderException)1 NoSuchMimePartException (com.axway.ats.action.objects.model.NoSuchMimePartException)1 ConfigurationException (com.axway.ats.config.exceptions.ConfigurationException)1 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)1 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)1 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)1 Monitor (com.axway.ats.rbv.Monitor)1 PollingParameters (com.axway.ats.rbv.PollingParameters)1