Search in sources :

Example 1 with MetaDataIncorrectException

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

the class Test_MetaDataIncorrectException method constructors.

@Test
public void constructors() {
    MetaDataIncorrectException exception;
    exception = new MetaDataIncorrectException("test");
    assertEquals("test", exception.getMessage());
    assertNull(exception.getCause());
    Exception helperException = new Exception();
    exception = new MetaDataIncorrectException("test", helperException);
    assertEquals("test", exception.getMessage());
    assertEquals(helperException, exception.getCause());
    exception = new MetaDataIncorrectException(helperException);
    assertEquals("java.lang.Exception", exception.getMessage());
    assertEquals(helperException, exception.getCause());
}
Also used : MetaDataIncorrectException(com.axway.ats.rbv.model.MetaDataIncorrectException) MetaDataIncorrectException(com.axway.ats.rbv.model.MetaDataIncorrectException) BaseTest(com.axway.ats.rbv.BaseTest) Test(org.junit.Test)

Example 2 with MetaDataIncorrectException

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

the class DbBooleanFieldRule method performMatch.

@Override
protected boolean performMatch(MetaData metaData) throws RbvException {
    // this cast is safe, as isMatch has already checked the type of meta data
    DbMetaData dbMetaData = (DbMetaData) metaData;
    Boolean actual = null;
    Object actualValue = dbMetaData.getProperty(this.expectedMetaDataKey.toString());
    // check for null
    if (this.expectedValue == null) {
        if (actualValue == null) {
            return true;
        }
        return false;
    }
    if (actualValue == null) {
        return false;
    }
    // check if the value is of type String
    if (actualValue instanceof String) {
        if ("0".equals(actualValue)) {
            actual = false;
        } else if ("1".equals(actualValue)) {
            actual = true;
        } else {
            throw new MetaDataIncorrectException("Meta data is incorrect. Received a String containing : " + actualValue);
        }
    }
    // check if value is of type Number
    if (actualValue instanceof Number) {
        Number actualNumber = (Number) actualValue;
        if (actualNumber.byteValue() != actualNumber.doubleValue()) {
            // this would mean that the value has floating point digits which is wrong
            throw new MetaDataIncorrectException("Meta data is incorrect. Received a Number containing floating point digits: " + actualValue);
        }
        if (actualNumber.byteValue() == 0) {
            actual = false;
        } else if (actualNumber.byteValue() == 1) {
            actual = true;
        } else {
            throw new MetaDataIncorrectException("Meta data is incorrect. Received a Number containing : " + actualValue);
        }
    }
    // check if value is of type Number
    if (actualValue instanceof Boolean) {
        actual = (Boolean) actualValue;
    }
    boolean expected = ((Boolean) this.expectedValue).booleanValue();
    if (actual == null) {
        throw new MetaDataIncorrectException("Meta data is incorrect. Unexpected type of the value in the database : " + actualValue.getClass().getCanonicalName());
    }
    if (expected == actual) {
        return true;
    }
    return false;
}
Also used : DbMetaData(com.axway.ats.rbv.db.DbMetaData) MetaDataIncorrectException(com.axway.ats.rbv.model.MetaDataIncorrectException)

Example 3 with MetaDataIncorrectException

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

the class DbDateFieldRule method checkString.

private boolean checkString(DbMetaData dbMetaData) throws RbvException {
    String actualValue;
    try {
        actualValue = (String) dbMetaData.getProperty(this.expectedMetaDataKey.toString());
    } catch (ClassCastException cce) {
        throw new MetaDataIncorrectException("Meta data is incorrect - expected String");
    }
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(this.actualValuePattern);
        sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
        Date expectedDate = new Date(Long.parseLong((String) this.expectedValue) * 1000);
        Date actualDate = sdf.parse(actualValue);
        switch(this.relation) {
            case BEFORE_DATE:
                return expectedDate.before(actualDate);
            case AFTER_DATE:
                return expectedDate.after(actualDate);
            case EXACT:
                return expectedDate.compareTo(actualDate) == 0;
            default:
                throw new RbvException("No implementation for MatchRelation '" + this.relation.toString() + "' in DbDateFieldMatchingTerm");
        }
    } catch (NumberFormatException nfe) {
        throw new RbvException("Expected value '" + this.expectedValue + "' cannot be converted to UNIX timestamp");
    } catch (ParseException pe) {
        // only if the actual value is incorrect
        throw new RbvException("Actual value '" + actualValue + "' cannot be converted to Date");
    }
}
Also used : MetaDataIncorrectException(com.axway.ats.rbv.model.MetaDataIncorrectException) SimpleTimeZone(java.util.SimpleTimeZone) RbvException(com.axway.ats.rbv.model.RbvException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 4 with MetaDataIncorrectException

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

the class DbDateFieldRule method checkDate.

private boolean checkDate(DbMetaData dbMetaData) throws RbvException {
    Timestamp actual;
    try {
        actual = (Timestamp) dbMetaData.getProperty(this.expectedMetaDataKey);
    } catch (ClassCastException cce) {
        throw new MetaDataIncorrectException("Meta data is incorrect - expected Timestamp");
    }
    long timestamp = ((Date) this.expectedValue).getTime();
    Timestamp expected = new Timestamp(timestamp);
    switch(this.relation) {
        case BEFORE_DATE:
            return actual.compareTo(expected) <= 0;
        case AFTER_DATE:
            return actual.compareTo(expected) >= 0;
        case EXACT:
            return actual.compareTo(expected) == 0;
        default:
            throw new RbvException("No implementation for MatchRelation '" + this.relation.toString() + "' in DbDateFieldMatchingTerm");
    }
}
Also used : MetaDataIncorrectException(com.axway.ats.rbv.model.MetaDataIncorrectException) RbvException(com.axway.ats.rbv.model.RbvException) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 5 with MetaDataIncorrectException

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

the class DbStringFieldRule method performMatch.

@Override
protected boolean performMatch(MetaData metaData) throws RbvException {
    boolean actualResult = false;
    // this cast is safe, as isMatch has already checked the type of meta data
    DbMetaData dbMetaData = (DbMetaData) metaData;
    String actualValue;
    try {
        actualValue = (String) dbMetaData.getProperty(expectedMetaDataKey.toString());
        if (dbEncryptor != null) {
            actualValue = dbEncryptor.decrypt(actualValue);
        }
        log.info("Actual value is '" + actualValue + "'");
    } catch (NoSuchMetaDataKeyException nsmdke) {
        log.warn(nsmdke.getMessage());
        return false;
    } catch (ClassCastException cce) {
        throw new MetaDataIncorrectException("Meta data is incorrect - expected String");
    }
    if (expectedValue == null) {
        return actualValue == null;
    } else {
        if (actualValue == null) {
            return false;
        }
    }
    if (relation == MatchRelation.EQUALS) {
        actualResult = expectedValue.equals(actualValue);
    } else if (relation == MatchRelation.CONTAINS) {
        actualResult = actualValue.contains((String) expectedValue);
    } else if (relation == MatchRelation.REGEX_MATCH) {
        Pattern regexPattern = Pattern.compile((String) expectedValue);
        actualResult = regexPattern.matcher(actualValue).matches();
    } else {
        throw new RbvException("No implementation for MatchRelation '" + relation.toString() + "' in DbStringFieldMatchingTerm");
    }
    return actualResult;
}
Also used : DbMetaData(com.axway.ats.rbv.db.DbMetaData) Pattern(java.util.regex.Pattern) MetaDataIncorrectException(com.axway.ats.rbv.model.MetaDataIncorrectException) NoSuchMetaDataKeyException(com.axway.ats.rbv.model.NoSuchMetaDataKeyException) RbvException(com.axway.ats.rbv.model.RbvException)

Aggregations

MetaDataIncorrectException (com.axway.ats.rbv.model.MetaDataIncorrectException)7 DbMetaData (com.axway.ats.rbv.db.DbMetaData)4 RbvException (com.axway.ats.rbv.model.RbvException)3 Date (java.util.Date)2 BaseTest (com.axway.ats.rbv.BaseTest)1 NoSuchMetaDataKeyException (com.axway.ats.rbv.model.NoSuchMetaDataKeyException)1 Timestamp (java.sql.Timestamp)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 SimpleTimeZone (java.util.SimpleTimeZone)1 Pattern (java.util.regex.Pattern)1 Test (org.junit.Test)1