use of org.molgenis.data.Entity in project molgenis by molgenis.
the class ExcelRepositoryTest method addCellProcessor_data.
@Test
public void addCellProcessor_data() {
CellProcessor processor = when(mock(CellProcessor.class).processData()).thenReturn(true).getMock();
excelSheetReader.addCellProcessor(processor);
for (Entity entity : excelSheetReader) entity.get("col2");
verify(processor).process("val2");
verify(processor).process("val4");
verify(processor).process("val6");
}
use of org.molgenis.data.Entity in project molgenis by molgenis.
the class DynamicEntity method validateValueType.
/**
* 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(entityType, 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:
if (!(value instanceof LocalDate)) {
throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), LocalDate.class.getSimpleName(), attrName));
}
break;
case DATE_TIME:
if (!(value instanceof Instant)) {
throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Instant.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));
}
if (((Double) value).isNaN()) {
throw new MolgenisDataException(format("Value [%s] for type [%s] is not allowed for attribute: [%s]", value.toString(), 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 UnexpectedEnumException(dataType);
}
}
use of org.molgenis.data.Entity in project molgenis by molgenis.
the class LazyEntityTest method getEntityString.
@Test
public void getEntityString() {
String attrName = "attr";
Entity value = mock(Entity.class);
when(entity.getEntity(attrName)).thenReturn(value);
assertEquals(value, lazyEntity.getEntity(attrName));
assertEquals(value, lazyEntity.getEntity(attrName));
verify(dataService, times(1)).findOneById(ENTITY_NAME, id);
}
use of org.molgenis.data.Entity in project molgenis by molgenis.
the class LazyEntityTest method setEntity.
@Test
public void setEntity() {
Entity value = mock(Entity.class);
lazyEntity.set(value);
lazyEntity.set(value);
verify(dataService, times(1)).findOneById(ENTITY_NAME, id);
verify(entity, times(2)).set(value);
}
use of org.molgenis.data.Entity in project molgenis by molgenis.
the class LazyEntityTest method getAttributeNames.
@Test
public void getAttributeNames() {
Entity entity = new DynamicEntity(entityType);
Attribute attr0 = when(mock(Attribute.class).getName()).thenReturn("attr0").getMock();
Attribute attr1 = when(mock(Attribute.class).getName()).thenReturn("attr1").getMock();
when(entityType.getAtomicAttributes()).thenReturn(Arrays.asList(attr0, attr1));
assertEquals(Lists.newArrayList(entity.getAttributeNames()), Arrays.asList("attr0", "attr1"));
}
Aggregations