use of org.molgenis.data.EntityManager in project molgenis by molgenis.
the class EntityUtilsTest method getTypedValueStringAttributeEntityManagerXref.
@Test
public void getTypedValueStringAttributeEntityManagerXref() {
String valueStr = "0";
Attribute attr = mock(Attribute.class);
EntityType refEntityType = mock(EntityType.class);
Attribute refIdAttr = mock(Attribute.class);
when(refIdAttr.getDataType()).thenReturn(STRING);
when(refEntityType.getIdAttribute()).thenReturn(refIdAttr);
when(attr.getRefEntity()).thenReturn(refEntityType);
when(attr.getDataType()).thenReturn(XREF);
Entity entity = mock(Entity.class);
EntityManager entityManager = mock(EntityManager.class);
when(entityManager.getReference(refEntityType, valueStr)).thenReturn(entity);
assertEquals(EntityUtils.getTypedValue(valueStr, attr, entityManager), entity);
}
use of org.molgenis.data.EntityManager in project molgenis by molgenis.
the class RestServiceTest method setUpBeforeMethod.
@BeforeMethod
public void setUpBeforeMethod() {
dataService = mock(DataService.class);
idGenerator = mock(IdGenerator.class);
FileStore fileStore = mock(FileStore.class);
fileMetaFactory = mock(FileMetaFactory.class);
entityManager = mock(EntityManager.class);
servletUriComponentsBuilderFactory = mock(ServletUriComponentsBuilderFactory.class);
this.restService = new RestService(dataService, idGenerator, fileStore, fileMetaFactory, entityManager, servletUriComponentsBuilderFactory);
}
use of org.molgenis.data.EntityManager in project molgenis by molgenis.
the class QueryValidatorTest method setUpBeforeMethod.
@BeforeMethod
public void setUpBeforeMethod() {
EntityManager entityManager = mock(EntityManager.class);
this.queryValidator = new QueryValidator(entityManager);
}
use of org.molgenis.data.EntityManager in project molgenis by molgenis.
the class EntityUtils method getTypedValue.
/**
* Convert a string value to a typed value based on the attribute data type.
*
* @param valueStr string value
* @param attr attribute
* @param entityManager entity manager used to convert referenced entity values
* @return typed value
*/
public static Object getTypedValue(String valueStr, Attribute attr, EntityManager entityManager) {
if (valueStr == null)
return null;
switch(attr.getDataType()) {
case BOOL:
return Boolean.valueOf(valueStr);
case CATEGORICAL:
case FILE:
case XREF:
EntityType xrefEntity = attr.getRefEntity();
Object xrefIdValue = getTypedValue(valueStr, xrefEntity.getIdAttribute(), entityManager);
return entityManager.getReference(xrefEntity, xrefIdValue);
case CATEGORICAL_MREF:
case MREF:
case ONE_TO_MANY:
EntityType mrefEntity = attr.getRefEntity();
List<String> mrefIdStrValues = ListEscapeUtils.toList(valueStr);
return mrefIdStrValues.stream().map(mrefIdStrValue -> getTypedValue(mrefIdStrValue, mrefEntity.getIdAttribute(), entityManager)).map(mrefIdValue -> entityManager.getReference(mrefEntity, mrefIdValue)).collect(toList());
case COMPOUND:
throw new IllegalArgumentException("Compound attribute has no value");
case DATE:
try {
return parseLocalDate(valueStr);
} catch (DateTimeParseException e) {
throw new MolgenisDataException(format(FAILED_TO_PARSE_ATTRIBUTE_AS_DATE_MESSAGE, attr.getName(), valueStr), e);
}
case DATE_TIME:
try {
return parseInstant(valueStr);
} catch (DateTimeParseException e) {
throw new MolgenisDataException(format(FAILED_TO_PARSE_ATTRIBUTE_AS_DATETIME_MESSAGE, attr.getName(), valueStr), e);
}
case DECIMAL:
return Double.valueOf(valueStr);
case EMAIL:
case ENUM:
case HTML:
case HYPERLINK:
case SCRIPT:
case STRING:
case TEXT:
return valueStr;
case INT:
return Integer.valueOf(valueStr);
case LONG:
return Long.valueOf(valueStr);
default:
throw new UnexpectedEnumException(attr.getDataType());
}
}
use of org.molgenis.data.EntityManager in project molgenis by molgenis.
the class PartialEntityTest method setUpBeforeMethod.
@BeforeMethod
public void setUpBeforeMethod() {
Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn("id").getMock();
meta = when(mock(EntityType.class).getId()).thenReturn("entity").getMock();
when(meta.getIdAttribute()).thenReturn(idAttr);
originalEntity = mock(Entity.class);
decoratedEntity = mock(Entity.class);
when(decoratedEntity.getEntityType()).thenReturn(meta);
when(decoratedEntity.getIdValue()).thenReturn("id");
fetch = new Fetch().field("id");
entityManager = mock(EntityManager.class);
when(entityManager.getReference(meta, "id")).thenReturn(originalEntity);
partialEntity = new PartialEntity(decoratedEntity, fetch, entityManager);
}
Aggregations