Search in sources :

Example 1 with AbstractRepository

use of org.molgenis.data.support.AbstractRepository in project molgenis by molgenis.

the class VcfImporterServiceTest method doImportVcfWithSamples.

@SuppressWarnings("unchecked")
@Test
public void doImportVcfWithSamples() {
    // Test with multiple input repositories not possible due to
    // https://github.com/molgenis/molgenis/issues/4544
    String entityTypeId0 = "entity0";
    List<String> entityTypeIds = Arrays.asList(entityTypeId0);
    String sampleEntityName0 = "entity0sample";
    EntityType sampleEntityType0 = mock(EntityType.class);
    when(sampleEntityType0.getId()).thenReturn(sampleEntityName0);
    when(sampleEntityType0.getOwnAttributes()).thenReturn(emptyList());
    when(sampleEntityType0.getOwnLookupAttributes()).thenReturn(emptyList());
    Repository<Entity> outSampleRepo0 = mock(Repository.class);
    when(outSampleRepo0.getName()).thenReturn(sampleEntityName0);
    when(metaDataService.createRepository(argThat(eqName(sampleEntityType0)))).thenReturn(outSampleRepo0);
    Attribute sampleAttr = mock(Attribute.class);
    when(sampleAttr.getName()).thenReturn(VcfAttributes.SAMPLES);
    when(sampleAttr.getRefEntity()).thenReturn(sampleEntityType0);
    when(sampleAttr.getDataType()).thenReturn(MREF);
    EntityType entityType0 = mock(EntityType.class);
    when(entityType0.getId()).thenReturn(entityTypeId0);
    when(entityType0.getAttribute(VcfAttributes.SAMPLES)).thenReturn(sampleAttr);
    when(entityType0.getOwnAttributes()).thenReturn(singletonList(sampleAttr));
    when(entityType0.getOwnLookupAttributes()).thenReturn(emptyList());
    Entity entity0Sample0 = mock(Entity.class);
    Entity entity0Sample1 = mock(Entity.class);
    Entity entity1Sample0 = mock(Entity.class);
    Entity entity1Sample1 = mock(Entity.class);
    Entity entity0 = mock(Entity.class);
    when(entity0.getEntities(VcfAttributes.SAMPLES)).thenReturn(Arrays.asList(entity0Sample0, entity0Sample1));
    Entity entity1 = mock(Entity.class);
    when(entity1.getEntities(VcfAttributes.SAMPLES)).thenReturn(Arrays.asList(entity1Sample0, entity1Sample1));
    List<Entity> entities = Arrays.asList(entity0, entity1);
    Repository<Entity> repo0 = Mockito.spy(new AbstractRepository() {

        @Override
        public Set<RepositoryCapability> getCapabilities() {
            return null;
        }

        public EntityType getEntityType() {
            return entityType0;
        }

        @Override
        public Iterator<Entity> iterator() {
            return entities.iterator();
        }

        @Override
        public Spliterator<Entity> spliterator() {
            return entities.spliterator();
        }

        @Override
        public String getName() {
            return entityTypeId0;
        }

        @Override
        public void forEachBatched(Consumer<List<Entity>> consumer, int batchSize) {
            this.forEachBatched(null, consumer, batchSize);
        }
    });
    when(dataService.hasRepository(entityTypeId0)).thenReturn(false);
    Repository<Entity> outRepo0 = mock(Repository.class);
    when(metaDataService.createRepository(argThat(eqName(entityType0)))).thenReturn(outRepo0);
    when(outRepo0.add(any(Stream.class))).thenAnswer(invocation -> {
        Stream<Entity> entities1 = (Stream<Entity>) invocation.getArguments()[0];
        List<Entity> entityList = entities1.collect(Collectors.toList());
        return entityList.size();
    });
    RepositoryCollection source = mock(RepositoryCollection.class);
    when(source.getEntityTypeIds()).thenReturn(entityTypeIds);
    when(source.getRepository(entityTypeId0)).thenReturn(repo0);
    String defaultPackage = "package";
    Package pack = mock(Package.class);
    when(metaDataService.getPackage(defaultPackage)).thenReturn(pack);
    EntityImportReport entityImportReport = vcfImporterService.doImport(source, DatabaseAction.ADD, defaultPackage);
    EntityImportReport expectedEntityImportReport = new EntityImportReport();
    expectedEntityImportReport.addNewEntity(sampleEntityName0);
    expectedEntityImportReport.addEntityCount(sampleEntityName0, 4);
    expectedEntityImportReport.addNewEntity(entityTypeId0);
    expectedEntityImportReport.addEntityCount(entityTypeId0, entities.size());
    assertEquals(entityImportReport, expectedEntityImportReport);
    verify(metaDataService).createRepository(argThat(eqName(sampleEntityType0)));
    verify(metaDataService).createRepository(argThat(eqName(entityType0)));
    verify(permissionSystemService).giveUserWriteMetaPermissions(entityType0);
    verify(permissionSystemService).giveUserWriteMetaPermissions(sampleEntityType0);
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) EntityImportReport(org.molgenis.data.importer.EntityImportReport) EntityType(org.molgenis.data.meta.model.EntityType) AbstractRepository(org.molgenis.data.support.AbstractRepository) Stream(java.util.stream.Stream) DefaultPackage(org.molgenis.data.meta.DefaultPackage) Package(org.molgenis.data.meta.model.Package) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 2 with AbstractRepository

use of org.molgenis.data.support.AbstractRepository in project molgenis by molgenis.

the class VcfImporterServiceTest method doImportVcfWithoutSamples.

@SuppressWarnings("unchecked")
@Test
public void doImportVcfWithoutSamples() {
    // Test with multiple input repositories not possible due to
    // https://github.com/molgenis/molgenis/issues/4544
    String entityTypeId0 = "entity0";
    List<String> entityTypeIds = Arrays.asList(entityTypeId0);
    EntityType entityType0 = mock(EntityType.class);
    when(entityType0.getId()).thenReturn(entityTypeId0);
    when(entityType0.getOwnAttributes()).thenReturn(emptyList());
    when(entityType0.getOwnLookupAttributes()).thenReturn(emptyList());
    Entity entity0 = mock(Entity.class);
    Entity entity1 = mock(Entity.class);
    List<Entity> entities = Arrays.asList(entity0, entity1);
    Repository<Entity> repo0 = Mockito.spy(new AbstractRepository() {

        @Override
        public Set<RepositoryCapability> getCapabilities() {
            return null;
        }

        public EntityType getEntityType() {
            return entityType0;
        }

        @Override
        public Iterator<Entity> iterator() {
            return entities.iterator();
        }

        @Override
        public Spliterator<Entity> spliterator() {
            return entities.spliterator();
        }

        @Override
        public String getName() {
            return entityTypeId0;
        }

        @Override
        public void forEachBatched(Consumer<List<Entity>> consumer, int batchSize) {
            this.forEachBatched(null, consumer, batchSize);
        }
    });
    when(dataService.hasRepository(entityTypeId0)).thenReturn(false);
    Repository<Entity> outRepo0 = mock(Repository.class);
    when(metaDataService.createRepository(argThat(eqName(entityType0)))).thenReturn(outRepo0);
    when(outRepo0.add(any(Stream.class))).thenAnswer(invocation -> {
        Stream<Entity> entities1 = (Stream<Entity>) invocation.getArguments()[0];
        List<Entity> entityList = entities1.collect(Collectors.toList());
        return entityList.size();
    });
    RepositoryCollection source = mock(RepositoryCollection.class);
    when(source.getEntityTypeIds()).thenReturn(entityTypeIds);
    when(source.getRepository(entityTypeId0)).thenReturn(repo0);
    String defaultPackage = "package";
    Package pack = mock(Package.class);
    when(metaDataService.getPackage(defaultPackage)).thenReturn(pack);
    EntityImportReport entityImportReport = vcfImporterService.doImport(source, DatabaseAction.ADD, defaultPackage);
    EntityImportReport expectedEntityImportReport = new EntityImportReport();
    expectedEntityImportReport.addEntityCount(entityTypeId0, entities.size());
    expectedEntityImportReport.addNewEntity(entityTypeId0);
    assertEquals(entityImportReport, expectedEntityImportReport);
    verify(metaDataService, times(1)).createRepository(argThat(eqName(entityType0)));
    verify(permissionSystemService, times(1)).giveUserWriteMetaPermissions(entityType0);
}
Also used : EntityImportReport(org.molgenis.data.importer.EntityImportReport) EntityType(org.molgenis.data.meta.model.EntityType) AbstractRepository(org.molgenis.data.support.AbstractRepository) Stream(java.util.stream.Stream) DefaultPackage(org.molgenis.data.meta.DefaultPackage) Package(org.molgenis.data.meta.model.Package) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Aggregations

Stream (java.util.stream.Stream)2 EntityImportReport (org.molgenis.data.importer.EntityImportReport)2 DefaultPackage (org.molgenis.data.meta.DefaultPackage)2 EntityType (org.molgenis.data.meta.model.EntityType)2 Package (org.molgenis.data.meta.model.Package)2 AbstractRepository (org.molgenis.data.support.AbstractRepository)2 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)2 Test (org.testng.annotations.Test)2 Attribute (org.molgenis.data.meta.model.Attribute)1