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());
}
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;
}
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");
}
}
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");
}
}
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;
}
Aggregations