Search in sources :

Example 71 with EntityType

use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.

the class RestServiceTest method toEntityValueMrefToStringAttr.

@Test(dataProvider = "toEntityValueMrefProvider")
public void toEntityValueMrefToStringAttr(AttributeType attrType) {
    Entity entity0 = mock(Entity.class);
    Entity entity1 = mock(Entity.class);
    String refEntityName = "refEntity";
    Attribute refIdAttr = mock(Attribute.class);
    when(refIdAttr.getDataType()).thenReturn(STRING);
    EntityType refEntityType = mock(EntityType.class);
    when(refEntityType.getId()).thenReturn(refEntityName);
    when(refEntityType.getIdAttribute()).thenReturn(refIdAttr);
    Attribute attr = mock(Attribute.class);
    when(attr.getDataType()).thenReturn(attrType);
    when(attr.getRefEntity()).thenReturn(refEntityType);
    when(entityManager.getReference(refEntityType, "0")).thenReturn(entity0);
    when(entityManager.getReference(refEntityType, "1")).thenReturn(entity1);
    // string
    Object entityValue = restService.toEntityValue(attr, "0,1", "test");
    assertEquals(entityValue, Arrays.asList(entity0, entity1));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) Test(org.testng.annotations.Test)

Example 72 with EntityType

use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.

the class RestServiceTest method updateMappedByEntitiesEntityEntity.

@Test
public void updateMappedByEntitiesEntityEntity() {
    String refEntityName = "refEntityName";
    EntityType refEntityMeta = mock(EntityType.class);
    when(refEntityMeta.getId()).thenReturn(refEntityName);
    String mappedByAttrName = "mappedByAttr";
    Attribute mappedByAttr = mock(Attribute.class);
    when(mappedByAttr.getName()).thenReturn(mappedByAttrName);
    EntityType entityMeta = mock(EntityType.class);
    Attribute oneToManyAttr = mock(Attribute.class);
    String oneToManyAttrName = "oneToManyAttr";
    when(oneToManyAttr.getName()).thenReturn(oneToManyAttrName);
    when(oneToManyAttr.getDataType()).thenReturn(ONE_TO_MANY);
    when(oneToManyAttr.isMappedBy()).thenReturn(true);
    when(oneToManyAttr.getMappedBy()).thenReturn(mappedByAttr);
    when(oneToManyAttr.getRefEntity()).thenReturn(refEntityMeta);
    when(entityMeta.getMappedByAttributes()).thenReturn(Stream.of(oneToManyAttr));
    Entity refEntity0 = when(mock(Entity.class).getIdValue()).thenReturn("refEntity0").getMock();
    Entity refEntity1 = when(mock(Entity.class).getIdValue()).thenReturn("refEntity1").getMock();
    Entity refEntity2 = when(mock(Entity.class).getIdValue()).thenReturn("refEntity2").getMock();
    Entity entity = mock(Entity.class);
    when(entity.getEntities(oneToManyAttrName)).thenReturn(newArrayList(refEntity0, refEntity1));
    when(entity.getEntityType()).thenReturn(entityMeta);
    Entity existingEntity = mock(Entity.class);
    when(existingEntity.getEntities(oneToManyAttrName)).thenReturn(newArrayList(refEntity1, refEntity2));
    when(existingEntity.getEntityType()).thenReturn(entityMeta);
    restService.updateMappedByEntities(entity, existingEntity);
    @SuppressWarnings("unchecked") ArgumentCaptor<Stream<Entity>> captor = ArgumentCaptor.forClass(Stream.class);
    verify(dataService).update(eq(refEntityName), captor.capture());
    List<Entity> refEntities = captor.getValue().collect(toList());
    assertEquals(refEntities, newArrayList(refEntity0, refEntity2));
    verify(refEntity0).set(mappedByAttrName, entity);
    verify(refEntity2).set(mappedByAttrName, null);
    verifyNoMoreInteractions(dataService);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) Stream(java.util.stream.Stream) Test(org.testng.annotations.Test)

Example 73 with EntityType

use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.

the class RepositorySecurityDecoratorTest method initPermissionServiceMock.

private void initPermissionServiceMock(EntityTypePermission permission, boolean hasPermission) {
    EntityType entityType = mock(EntityType.class);
    String entityTypeId = "entityTypeId";
    when(entityType.getId()).thenReturn(entityTypeId);
    if (!hasPermission) {
        String entityTypeLabel = "Entity type";
        when(entityType.getLabel()).thenReturn(entityTypeLabel);
    }
    when(delegateRepository.getEntityType()).thenReturn(entityType);
    when(permissionService.hasPermission(new EntityTypeIdentity(entityTypeId), permission)).thenReturn(hasPermission);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType)

Example 74 with EntityType

use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.

the class AttributeRepositorySecurityDecoratorTest method findAllQueryUser.

@WithMockUser(username = USERNAME)
@Test
public void findAllQueryUser() throws Exception {
    String entityType0Name = "entity0";
    EntityType entityType0 = when(mock(EntityType.class).getId()).thenReturn(entityType0Name).getMock();
    String entityType1Name = "entity1";
    EntityType entityType1 = when(mock(EntityType.class).getId()).thenReturn(entityType1Name).getMock();
    String attr0Name = "entity0attr0";
    Attribute attr0 = when(mock(Attribute.class).getName()).thenReturn(attr0Name).getMock();
    when(attr0.getEntity()).thenReturn(entityType0);
    String attr1Name = "entity1attr0";
    Attribute attr1 = when(mock(Attribute.class).getName()).thenReturn(attr1Name).getMock();
    when(attr1.getEntity()).thenReturn(entityType1);
    Query<Attribute> q = new QueryImpl<>();
    @SuppressWarnings("unchecked") ArgumentCaptor<Query<Attribute>> queryCaptor = forClass(Query.class);
    when(delegateRepository.findAll(queryCaptor.capture())).thenReturn(Stream.of(attr0, attr1));
    when(permissionService.hasPermission(new EntityTypeIdentity(entityType0Name), EntityTypePermission.COUNT)).thenReturn(false);
    when(permissionService.hasPermission(new EntityTypeIdentity(entityType1Name), EntityTypePermission.COUNT)).thenReturn(true);
    assertEquals(repo.findAll(q).collect(toList()), singletonList(attr1));
    assertEquals(queryCaptor.getValue().getOffset(), 0);
    assertEquals(queryCaptor.getValue().getPageSize(), Integer.MAX_VALUE);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) QueryImpl(org.molgenis.data.support.QueryImpl) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) Attribute(org.molgenis.data.meta.model.Attribute) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Example 75 with EntityType

use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.

the class AttributeRepositorySecurityDecoratorTest method findOneByIdUserPermissionAllowedAttrInCompoundWithOneAttr.

@WithMockUser(username = USERNAME)
@Test
public void findOneByIdUserPermissionAllowedAttrInCompoundWithOneAttr() throws Exception {
    String entityType0Name = "entity0";
    EntityType entityType0 = when(mock(EntityType.class).getId()).thenReturn(entityType0Name).getMock();
    String attr0Name = "entity0attrCompoundattr0";
    Attribute attr0 = when(mock(Attribute.class).getName()).thenReturn(attr0Name).getMock();
    when(attr0.getEntity()).thenReturn(entityType0);
    String attrCompoundName = "entity0attrCompound";
    Attribute attrCompound = when(mock(Attribute.class).getName()).thenReturn(attrCompoundName).getMock();
    when(attrCompound.getEntity()).thenReturn(entityType0);
    Object id = mock(Object.class);
    when(delegateRepository.findOneById(id)).thenReturn(attr0);
    when(permissionService.hasPermission(new EntityTypeIdentity(entityType0Name), EntityTypePermission.COUNT)).thenReturn(true);
    assertEquals(repo.findOneById(id), attr0);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) Attribute(org.molgenis.data.meta.model.Attribute) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Aggregations

EntityType (org.molgenis.data.meta.model.EntityType)581 Test (org.testng.annotations.Test)367 Attribute (org.molgenis.data.meta.model.Attribute)315 Entity (org.molgenis.data.Entity)98 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)71 DynamicEntity (org.molgenis.data.support.DynamicEntity)61 Stream (java.util.stream.Stream)44 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)43 WithMockUser (org.springframework.security.test.context.support.WithMockUser)40 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)36 QueryImpl (org.molgenis.data.support.QueryImpl)33 Package (org.molgenis.data.meta.model.Package)32 Objects.requireNonNull (java.util.Objects.requireNonNull)22 Collectors.toList (java.util.stream.Collectors.toList)22 BeforeMethod (org.testng.annotations.BeforeMethod)20 AttributeType (org.molgenis.data.meta.AttributeType)19 List (java.util.List)17 String.format (java.lang.String.format)16 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)16 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)15