@BeforeTest public void createEntity() { entityType = when(mock(EntityType.class).getName()).thenReturn("Source").getMock(); Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn("Identifier").getMock(); when(idAttr.getDataType()).thenReturn(INT); Attribute intAttr = when(mock(Attribute.class).getName()).thenReturn("Int").getMock(); when(intAttr.getDataType()).thenReturn(INT); Attribute stringAttr = when(mock(Attribute.class).getName()).thenReturn("String").getMock(); when(stringAttr.getDataType()).thenReturn(STRING); Attribute nonNumericStringAttr = when(mock(Attribute.class).getName()).thenReturn("NonNumericString").getMock(); when(nonNumericStringAttr.getDataType()).thenReturn(STRING); Attribute longAttr = when(mock(Attribute.class).getName()).thenReturn("Long").getMock(); when(longAttr.getDataType()).thenReturn(LONG); when(entityType.getIdAttribute()).thenReturn(idAttr); when(entityType.getAttribute("Identifier")).thenReturn(idAttr); when(entityType.getAttribute("Int")).thenReturn(intAttr); when(entityType.getAttribute("String")).thenReturn(stringAttr); when(entityType.getAttribute("NonNumericString")).thenReturn(nonNumericStringAttr); when(entityType.getAttribute("Long")).thenReturn(longAttr); entity = new DynamicEntity(entityType); entity.set("Int", 1); entity.set("String", "12"); entity.set("Long", 10L); entity.set("NonNumericString", "Hello World!"); }
private static Attribute getQueryRuleAttribute(QueryRule queryRule, EntityType entityType) { String queryRuleField = queryRule.getField(); if (queryRuleField == null) { throw new MolgenisValidationException( new ConstraintViolation( format( "Query rule with operator [%s] is missing required field", queryRule.getOperator().toString()))); } Attribute attr = entityType.getAttribute(queryRuleField); if (attr == null) { throw new MolgenisValidationException( new ConstraintViolation( format( "Query rule field [%s] refers to unknown attribute in entity type [%s]", queryRuleField, entityType.getName()))); } return attr; }
/** * Validate is value is of the type defined by the attribute data type. * * @param attrName attribute name * @param value value (must be of the type defined by the attribute data type.) */ protected void validateValueType(String attrName, Object value) { if (value == null) { return; } Attribute attr = entityType.getAttribute(attrName); if (attr == null) { throw new UnknownAttributeException(format("Unknown attribute [%s]", attrName)); } AttributeType dataType = attr.getDataType(); switch (dataType) { case BOOL: if (!(value instanceof Boolean)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Boolean.class.getSimpleName(), attrName)); } break; case CATEGORICAL: // expected type is FileMeta. validation is not possible because molgenis-data does not // depend on molgenis-file case FILE: case XREF: if (!(value instanceof Entity)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Entity.class.getSimpleName(), attrName)); } break; case CATEGORICAL_MREF: case MREF: case ONE_TO_MANY: if (!(value instanceof Iterable)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Iterable.class.getSimpleName(), attrName)); } break; case COMPOUND: throw new IllegalArgumentException( format("Unexpected data type [%s] for attribute: [%s]", dataType.toString(), attrName)); case DATE: case DATE_TIME: if (!(value instanceof java.util.Date)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), java.util.Date.class.getSimpleName(), attrName)); } break; case DECIMAL: if (!(value instanceof Double)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Double.class.getSimpleName(), attrName)); } break; case EMAIL: case ENUM: case HTML: case HYPERLINK: case SCRIPT: case STRING: case TEXT: if (!(value instanceof String)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), String.class.getSimpleName(), attrName)); } break; case INT: if (!(value instanceof Integer)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Integer.class.getSimpleName(), attrName)); } break; case LONG: if (!(value instanceof Long)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Long.class.getSimpleName(), attrName)); } break; default: throw new RuntimeException(format("Unknown data type [%s]", dataType.toString())); } }