Search in sources :

Example 46 with Entity

use of org.molgenis.data.Entity in project molgenis by molgenis.

the class EntityHydration method getValueBasedOnType.

private static Object getValueBasedOnType(Entity entity, String name, AttributeType type) {
    Object value;
    switch(type) {
        case CATEGORICAL:
        case FILE:
        case XREF:
            Entity xrefEntity = entity.getEntity(name);
            value = xrefEntity != null ? xrefEntity.getIdValue() : null;
            break;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            List<Object> mrefIdentifiers = newArrayList();
            entity.getEntities(name).forEach(mrefEntity -> {
                if (mrefEntity != null)
                    mrefIdentifiers.add(mrefEntity.getIdValue());
            });
            value = mrefIdentifiers;
            break;
        case DATE:
            value = entity.getLocalDate(name);
            break;
        case DATE_TIME:
            value = entity.getInstant(name);
            break;
        case BOOL:
        case DECIMAL:
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case INT:
        case LONG:
        case SCRIPT:
        case STRING:
        case TEXT:
            value = entity.get(name);
            break;
        case COMPOUND:
            throw new RuntimeException(format("Illegal attribute type [%s]", type.toString()));
        default:
            throw new UnexpectedEnumException(type);
    }
    LOG.trace("Dehydrating attribute '{}' of type [{}] resulted in value: {}", name, type.toString(), value);
    return value;
}
Also used : Entity(org.molgenis.data.Entity) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException)

Example 47 with Entity

use of org.molgenis.data.Entity in project molgenis by molgenis.

the class L1CacheTest method testEvictStream.

@Test
public void testEvictStream() {
    // Start transaction
    l1Cache.transactionStarted(transactionID);
    // Entity has been added to cache, return entity
    l1Cache.put(repository, entity1);
    l1Cache.put(repository, entity2);
    Entity actualEntity = l1Cache.get(repository, entityID1, entityType).get();
    assertTrue(EntityUtils.equals(actualEntity, entity1));
    actualEntity = l1Cache.get(repository, entityID2, entityType).get();
    assertTrue(EntityUtils.equals(actualEntity, entity2));
    l1Cache.evict(Stream.of(EntityKey.create(entity1), EntityKey.create(entity2)));
    Optional<Entity> result = l1Cache.get(repository, entityID1, entityType);
    assertEquals(result, null);
    result = l1Cache.get(repository, entityID2, entityType);
    assertEquals(result, null);
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 48 with Entity

use of org.molgenis.data.Entity in project molgenis by molgenis.

the class L1CacheTest method testEvictStreamOfOneEntity.

@Test
public void testEvictStreamOfOneEntity() {
    // Start transaction
    l1Cache.transactionStarted(transactionID);
    // Entity has been added to cache, return entity
    l1Cache.put(repository, entity1);
    l1Cache.put(repository, entity2);
    Entity actualEntity = l1Cache.get(repository, entityID1, entityType).get();
    assertTrue(EntityUtils.equals(actualEntity, entity1));
    actualEntity = l1Cache.get(repository, entityID2, entityType).get();
    assertTrue(EntityUtils.equals(actualEntity, entity2));
    l1Cache.evict(Stream.of(EntityKey.create(entity2)));
    actualEntity = l1Cache.get(repository, entityID1, entityType).get();
    assertTrue(EntityUtils.equals(actualEntity, entity1));
    Optional<Entity> result = l1Cache.get(repository, entityID2, entityType);
    assertEquals(result, null);
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 49 with Entity

use of org.molgenis.data.Entity in project molgenis by molgenis.

the class CsvWriterTest method addCellProcessor_data.

@Test
public void addCellProcessor_data() throws IOException {
    CellProcessor processor = when(mock(CellProcessor.class).processData()).thenReturn(true).getMock();
    Entity entity = new DynamicEntity(entityType);
    entity.set("col1", "val1");
    entity.set("col2", "val2");
    try (CsvWriter csvWriter = new CsvWriter(new StringWriter())) {
        csvWriter.addCellProcessor(processor);
        csvWriter.writeAttributeNames(Arrays.asList("col1", "col2"));
        csvWriter.add(entity);
    }
    verify(processor).process("val1");
    verify(processor).process("val2");
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) StringWriter(java.io.StringWriter) DynamicEntity(org.molgenis.data.support.DynamicEntity) CellProcessor(org.molgenis.data.file.processor.CellProcessor) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 50 with Entity

use of org.molgenis.data.Entity in project molgenis by molgenis.

the class CsvWriterTest method testLabels.

@Test
public void testLabels() throws IOException {
    StringWriter strWriter = new StringWriter();
    try (CsvWriter csvWriter = new CsvWriter(strWriter)) {
        csvWriter.writeAttributes(Arrays.asList("col1", "col2"), Arrays.asList("label1", "label2"));
        Entity entity = new DynamicEntity(entityType);
        entity.set("col1", "val1");
        entity.set("col2", "val2");
        csvWriter.add(entity);
        assertEquals(strWriter.toString(), "\"label1\",\"label2\"\n\"val1\",\"val2\"\n");
    }
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) StringWriter(java.io.StringWriter) DynamicEntity(org.molgenis.data.support.DynamicEntity) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Aggregations

Entity (org.molgenis.data.Entity)448 Test (org.testng.annotations.Test)295 DynamicEntity (org.molgenis.data.support.DynamicEntity)192 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)120 Attribute (org.molgenis.data.meta.model.Attribute)111 EntityType (org.molgenis.data.meta.model.EntityType)110 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)37 MolgenisDataException (org.molgenis.data.MolgenisDataException)20 QueryImpl (org.molgenis.data.support.QueryImpl)20 AttributeType (org.molgenis.data.meta.AttributeType)18 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)18 Stream (java.util.stream.Stream)17 QueryRule (org.molgenis.data.QueryRule)16 MultiAllelicResultFilter (org.molgenis.data.annotation.core.filter.MultiAllelicResultFilter)16 RunAsSystem (org.molgenis.security.core.runas.RunAsSystem)16 Objects.requireNonNull (java.util.Objects.requireNonNull)15 DataService (org.molgenis.data.DataService)15 AttributeMapping (org.molgenis.semanticmapper.mapping.model.AttributeMapping)15 WithMockUser (org.springframework.security.test.context.support.WithMockUser)14 Instant (java.time.Instant)13