Search in sources :

Example 1 with LogLevel

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;
}
Also used : TestItemIssueType(com.epam.ta.reportportal.database.entity.item.issue.TestItemIssueType) ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) ObjectId(org.bson.types.ObjectId) Date(java.util.Date) LogLevel(com.epam.ta.reportportal.database.entity.LogLevel)

Example 2 with LogLevel

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());
    });
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) LogLevel(com.epam.ta.reportportal.database.entity.LogLevel) Test(org.junit.Test)

Aggregations

LogLevel (com.epam.ta.reportportal.database.entity.LogLevel)2 TestItemIssueType (com.epam.ta.reportportal.database.entity.item.issue.TestItemIssueType)1 ReportPortalException (com.epam.ta.reportportal.exception.ReportPortalException)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 Date (java.util.Date)1 ObjectId (org.bson.types.ObjectId)1 Test (org.junit.Test)1