use of com.epam.ta.reportportal.database.entity.LogLevel in project commons-dao by reportportal.
the class CriteriaHolder method castValue.
/**
* Casting provided criteriaHolder by specified {@link Class} for specified value.
* <p>
* NOTE:<br>
* errorType - error which should be thrown when unable cast value
*
* @param oneValue Value to cast
* @param errorType ErrorType in case of error
* @return Casted value
*/
public Object castValue(String oneValue, ErrorType errorType) {
Object castedValue;
if (Number.class.isAssignableFrom(getDataType())) {
/* Verify correct number */
Long parsedLong = NumberUtils.toLong(oneValue, -1);
BusinessRule.expect(parsedLong, FilterRules.numberIsPositive()).verify(errorType, Suppliers.formattedSupplier("Cannot convert '{}' to valid positive number", oneValue));
castedValue = parsedLong;
} else if (Date.class.isAssignableFrom(getDataType())) {
/* Verify correct date */
BusinessRule.expect(oneValue, FilterRules.dateInMillis()).verify(errorType, Suppliers.formattedSupplier("Cannot convert '{}' to valid date", oneValue));
castedValue = new Date(Long.parseLong(oneValue));
} else if (boolean.class.equals(getDataType()) || Boolean.class.isAssignableFrom(getDataType())) {
castedValue = BooleanUtils.toBoolean(oneValue);
} else if (LogLevel.class.isAssignableFrom(getDataType())) {
castedValue = LogLevel.toLevel(oneValue);
BusinessRule.expect(castedValue, Predicates.notNull()).verify(errorType, Suppliers.formattedSupplier("Cannot convert '{}' to valid 'LogLevel'", oneValue));
} else if (Status.class.isAssignableFrom(getDataType())) {
castedValue = Status.fromValue(oneValue).orElseThrow(() -> new ReportPortalException(errorType, Suppliers.formattedSupplier("Cannot convert '{}' to valid 'Status'", oneValue)));
} else if (TestItemIssueType.class.isAssignableFrom(getDataType())) {
castedValue = TestItemIssueType.validate(oneValue);
BusinessRule.expect(castedValue, Predicates.notNull()).verify(errorType, Suppliers.formattedSupplier("Cannot convert '{}' to valid 'Issue Type'", oneValue));
} else if (Collection.class.isAssignableFrom(getDataType())) {
/* Collection doesn't stores objects as ObjectId */
castedValue = oneValue;
} else if (String.class.isAssignableFrom(getDataType())) {
castedValue = oneValue != null ? oneValue.trim() : null;
} else {
castedValue = ObjectId.isValid(oneValue) ? new ObjectId(oneValue) : oneValue;
}
return castedValue;
}
use of com.epam.ta.reportportal.database.entity.LogLevel in project commons-dao by reportportal.
the class CustomMongoConvertersTest method convertLogLevel.
@Test
public void convertLogLevel() {
LogLevel[] values = LogLevel.values();
Arrays.stream(values).forEach(it -> {
DBObject convert = CustomMongoConverters.LogLevelToIntConverter.INSTANCE.convert(it);
Assert.assertEquals(convert.get("log_level"), it.toInt());
});
}
Aggregations