Search in sources :

Example 51 with Attribute

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

the class RestServiceTest method toEntityValueMrefToIntAttr.

@Test(dataProvider = "toEntityValueMrefProvider")
public void toEntityValueMrefToIntAttr(AttributeType attrType) {
    Entity entity0 = mock(Entity.class);
    Entity entity1 = mock(Entity.class);
    String refEntityName = "refEntity";
    Attribute refIdAttr = mock(Attribute.class);
    when(refIdAttr.getDataType()).thenReturn(INT);
    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 52 with Attribute

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

the class RestServiceTest method toEntityFileValueValid.

@Test
public void toEntityFileValueValid() throws ParseException {
    String generatedId = "id";
    String downloadUriAsString = "http://somedownloaduri";
    ServletUriComponentsBuilder mockBuilder = mock(ServletUriComponentsBuilder.class);
    UriComponents downloadUri = mock(UriComponents.class);
    FileMeta fileMeta = mock(FileMeta.class);
    Attribute fileAttr = when(mock(Attribute.class).getName()).thenReturn("fileAttr").getMock();
    when(fileAttr.getDataType()).thenReturn(FILE);
    when(idGenerator.generateId()).thenReturn(generatedId);
    when(fileMetaFactory.create(generatedId)).thenReturn(fileMeta);
    when(mockBuilder.replacePath(anyString())).thenReturn(mockBuilder);
    when(mockBuilder.replaceQuery(null)).thenReturn(mockBuilder);
    when(downloadUri.toUriString()).thenReturn(downloadUriAsString);
    when(mockBuilder.build()).thenReturn(downloadUri);
    when(servletUriComponentsBuilderFactory.fromCurrentRequest()).thenReturn(mockBuilder);
    byte[] content = { 'a', 'b' };
    MockMultipartFile mockMultipartFile = new MockMultipartFile("name", "fileName", "contentType", content);
    assertEquals(restService.toEntityValue(fileAttr, mockMultipartFile, null), fileMeta);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) UriComponents(org.springframework.web.util.UriComponents) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) Attribute(org.molgenis.data.meta.model.Attribute) FileMeta(org.molgenis.data.file.model.FileMeta) Test(org.testng.annotations.Test)

Example 53 with Attribute

use of org.molgenis.data.meta.model.Attribute 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 54 with Attribute

use of org.molgenis.data.meta.model.Attribute 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 55 with Attribute

use of org.molgenis.data.meta.model.Attribute 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)

Aggregations

Attribute (org.molgenis.data.meta.model.Attribute)600 Test (org.testng.annotations.Test)351 EntityType (org.molgenis.data.meta.model.EntityType)321 Entity (org.molgenis.data.Entity)109 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)54 DynamicEntity (org.molgenis.data.support.DynamicEntity)53 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)47 Stream (java.util.stream.Stream)39 AttributeType (org.molgenis.data.meta.AttributeType)34 QueryImpl (org.molgenis.data.support.QueryImpl)29 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)29 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)28 BeforeMethod (org.testng.annotations.BeforeMethod)20 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)18 WithMockUser (org.springframework.security.test.context.support.WithMockUser)18 Collectors.toList (java.util.stream.Collectors.toList)17 Relation (org.molgenis.data.semantic.Relation)17 Objects.requireNonNull (java.util.Objects.requireNonNull)16 MolgenisDataException (org.molgenis.data.MolgenisDataException)16 Package (org.molgenis.data.meta.model.Package)16