Search in sources :

Example 1 with EntityServiceImpl

use of org.molgenis.oneclickimporter.service.impl.EntityServiceImpl in project molgenis by molgenis.

the class EntityServiceImplTest method testCreateEntity.

@Test
public void testCreateEntity() throws Exception {
    String tableName = "super-powers";
    List<Object> userNames = Arrays.asList("Mark", "Mariska", "Bart");
    List<Object> superPowers = Arrays.asList("Arrow functions", "Cookies", "Knots");
    List<Column> columns = Arrays.asList(Column.create("user name", 0, userNames), Column.create("super power", 1, superPowers));
    DataCollection dataCollection = DataCollection.create(tableName, columns);
    // mock auto id
    String generatedId = "id_0";
    when(idGenerator.generateId()).thenReturn(generatedId);
    // mock attributes
    Attribute idAttr = mock(Attribute.class);
    when(idAttr.setName(anyString())).thenReturn(idAttr);
    when(idAttr.setVisible(anyBoolean())).thenReturn(idAttr);
    when(idAttr.setAuto(anyBoolean())).thenReturn(idAttr);
    when(idAttr.setIdAttribute(anyBoolean())).thenReturn(idAttr);
    Attribute nameAttr = mock(Attribute.class);
    when(nameAttr.getDataType()).thenReturn(STRING);
    Attribute powerAttr = mock(Attribute.class);
    when(powerAttr.getDataType()).thenReturn(STRING);
    when(attributeFactory.create()).thenReturn(idAttr, nameAttr, powerAttr);
    // mock table
    EntityType table = mock(EntityType.class);
    when(entityTypeFactory.create()).thenReturn(table);
    when(table.getId()).thenReturn(generatedId);
    when(table.getAttribute("user_name")).thenReturn(nameAttr);
    when(table.getAttribute("super_power")).thenReturn(powerAttr);
    // mock package
    Package package_ = mock(Package.class);
    when(metaDataService.getPackage("package_")).thenReturn(package_);
    when(dataService.getMeta()).thenReturn(metaDataService);
    // mock rows
    Entity row1 = mock(Entity.class);
    when(row1.getEntityType()).thenReturn(table);
    Entity row2 = mock(Entity.class);
    when(row2.getEntityType()).thenReturn(table);
    Entity row3 = mock(Entity.class);
    when(row3.getEntityType()).thenReturn(table);
    when(entityManager.create(table, NO_POPULATE)).thenReturn(row1, row2, row3);
    when(oneClickImporterNamingService.asValidColumnName("user name")).thenReturn("user_name");
    when(oneClickImporterNamingService.asValidColumnName("super power")).thenReturn("super_power");
    when(oneClickImporterNamingService.getLabelWithPostFix("super-powers")).thenReturn("super-powers");
    when(attributeTypeService.guessAttributeType(any())).thenReturn(STRING);
    entityService = new EntityServiceImpl(entityTypeFactory, attributeFactory, idGenerator, dataService, metaDataService, entityManager, attributeTypeService, oneClickImporterService, oneClickImporterNamingService, packageFactory, permissionSystemService);
    EntityType entityType = entityService.createEntityType(dataCollection, "package_");
    assertEquals(entityType.getId(), generatedId);
    verify(table).setPackage(package_);
    verify(table).setId(generatedId);
    verify(table).setLabel(tableName);
    verify(permissionSystemService).giveUserWriteMetaPermissions(table);
}
Also used : Entity(org.molgenis.data.Entity) Column(org.molgenis.oneclickimporter.model.Column) DataCollection(org.molgenis.oneclickimporter.model.DataCollection) EntityServiceImpl(org.molgenis.oneclickimporter.service.impl.EntityServiceImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Package(org.molgenis.data.meta.model.Package) Test(org.testng.annotations.Test)

Aggregations

ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 Entity (org.molgenis.data.Entity)1 Package (org.molgenis.data.meta.model.Package)1 Column (org.molgenis.oneclickimporter.model.Column)1 DataCollection (org.molgenis.oneclickimporter.model.DataCollection)1 EntityServiceImpl (org.molgenis.oneclickimporter.service.impl.EntityServiceImpl)1 Test (org.testng.annotations.Test)1