@SuppressWarnings("unchecked") @Test void doImportVcfWithSamples() { when(dataService.getMeta()).thenReturn(metaDataService); when(metaDataService.getDefaultBackend()).thenReturn(repositoryCollection); when(repositoryCollection.getName()).thenReturn("default"); String entityTypeId0 = "entity0"; List<String> entityTypeIds = singletonList(entityTypeId0); String sampleEntityName0 = "entity0sample"; EntityType sampleEntityType0 = mock(EntityType.class); when(sampleEntityType0.getId()).thenReturn(sampleEntityName0); Repository<Entity> outSampleRepo0 = mock(Repository.class); when(outSampleRepo0.getName()).thenReturn(sampleEntityName0); doReturn(outSampleRepo0) .when(metaDataService) .createRepository(argThat(eqName(sampleEntityType0))); Attribute sampleAttr = mock(Attribute.class); when(sampleAttr.getRefEntity()).thenReturn(sampleEntityType0); EntityType entityType0 = mock(EntityType.class); when(entityType0.getId()).thenReturn(entityTypeId0); when(entityType0.getAttribute(VcfAttributes.SAMPLES)).thenReturn(sampleAttr); 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); doReturn(outRepo0).when(metaDataService).createRepository(argThat(eqName(entityType0))); 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 importPackageId = "package"; Package importPackage = mock(Package.class); when(metaDataService.getPackage(importPackageId)).thenReturn(Optional.of(importPackage)); EntityImportReport entityImportReport = vcfImporterService.doImport(source, MetadataAction.ADD, DataAction.ADD, importPackageId); EntityImportReport expectedEntityImportReport = new EntityImportReport(); expectedEntityImportReport.addNewEntity(sampleEntityName0); expectedEntityImportReport.addEntityCount(sampleEntityName0, 4); expectedEntityImportReport.addNewEntity(entityTypeId0); expectedEntityImportReport.addEntityCount(entityTypeId0, entities.size()); assertEquals(expectedEntityImportReport, entityImportReport); verify(metaDataService).createRepository(argThat(eqName(sampleEntityType0))); verify(metaDataService).createRepository(argThat(eqName(entityType0))); verify(permissionSystemService).giveUserWriteMetaPermissions(entityType0); verify(permissionSystemService).giveUserWriteMetaPermissions(sampleEntityType0); }
@Transactional @Override public EntityImportReport doImport( RepositoryCollection source, MetadataAction metadataAction, DataAction dataAction, @Nullable @CheckForNull String packageId) { if (dataAction != DataAction.ADD) { throw new IllegalArgumentException("Only ADD is supported"); } Package importPackage = null; if (packageId != null) { importPackage = metaDataService .getPackage(packageId) .orElseThrow(() -> new UnknownEntityException(PACKAGE, packageId)); } List<EntityType> addedEntities = new ArrayList<>(); EntityImportReport report; Iterator<String> it = source.getEntityTypeIds().iterator(); if (it.hasNext()) { try (Repository<Entity> repo = source.getRepository(it.next())) { report = importVcf(repo, addedEntities, importPackage); } catch (IOException e) { throw new MolgenisDataException(e); } } else { report = new EntityImportReport(); } return report; }
VcfImporterService implements ImportService { @Transactional @Override public EntityImportReport doImport( RepositoryCollection source, MetadataAction metadataAction, DataAction dataAction, @Nullable @CheckForNull String packageId) { if (dataAction != DataAction.ADD) { throw new IllegalArgumentException("Only ADD is supported"); } Package importPackage = null; if (packageId != null) { importPackage = metaDataService .getPackage(packageId) .orElseThrow(() -> new UnknownEntityException(PACKAGE, packageId)); } List<EntityType> addedEntities = new ArrayList<>(); EntityImportReport report; Iterator<String> it = source.getEntityTypeIds().iterator(); if (it.hasNext()) { try (Repository<Entity> repo = source.getRepository(it.next())) { report = importVcf(repo, addedEntities, importPackage); } catch (IOException e) { throw new MolgenisDataException(e); } } else { report = new EntityImportReport(); } return report; } }
VcfImporterService implements ImportService { @Transactional @Override public EntityImportReport doImport( RepositoryCollection source, MetadataAction metadataAction, DataAction dataAction, @Nullable @CheckForNull String packageId) { if (dataAction != DataAction.ADD) { throw new IllegalArgumentException("Only ADD is supported"); } Package importPackage = null; if (packageId != null) { importPackage = metaDataService .getPackage(packageId) .orElseThrow(() -> new UnknownEntityException(PACKAGE, packageId)); } List<EntityType> addedEntities = new ArrayList<>(); EntityImportReport report; Iterator<String> it = source.getEntityTypeIds().iterator(); if (it.hasNext()) { try (Repository<Entity> repo = source.getRepository(it.next())) { report = importVcf(repo, addedEntities, importPackage); } catch (IOException e) { throw new MolgenisDataException(e); } } else { report = new EntityImportReport(); } return report; } VcfImporterService(
      DataService dataService,
      PermissionSystemService permissionSystemService,
      MetaDataService metaDataService); }
VcfImporterService implements ImportService { @Transactional @Override public EntityImportReport doImport( RepositoryCollection source, MetadataAction metadataAction, DataAction dataAction, @Nullable @CheckForNull String packageId) { if (dataAction != DataAction.ADD) { throw new IllegalArgumentException("Only ADD is supported"); } Package importPackage = null; if (packageId != null) { importPackage = metaDataService .getPackage(packageId) .orElseThrow(() -> new UnknownEntityException(PACKAGE, packageId)); } List<EntityType> addedEntities = new ArrayList<>(); EntityImportReport report; Iterator<String> it = source.getEntityTypeIds().iterator(); if (it.hasNext()) { try (Repository<Entity> repo = source.getRepository(it.next())) { report = importVcf(repo, addedEntities, importPackage); } catch (IOException e) { throw new MolgenisDataException(e); } } else { report = new EntityImportReport(); } return report; } VcfImporterService(
      DataService dataService,
      PermissionSystemService permissionSystemService,
      MetaDataService metaDataService); @Transactional @Override EntityImportReport doImport(
      RepositoryCollection source,
      MetadataAction metadataAction,
      DataAction dataAction,
      @Nullable @CheckForNull String packageId); @Override EntitiesValidationReport validateImport(RepositoryCollection source); @Override boolean canImport(File file, RepositoryCollection source); @Override int getOrder(); @Override List<MetadataAction> getSupportedMetadataActions(); @Override List<DataAction> getSupportedDataActions(); @Override boolean getMustChangeEntityName(); @Override Set<String> getSupportedFileExtensions(); @Override Map<String, Boolean> determineImportableEntities(
      MetaDataService metaDataService,
      RepositoryCollection repositoryCollection,
      String defaultPackage); @Override MetadataAction getMetadataAction(RepositoryCollection source); }
VcfImporterService implements ImportService { @Transactional @Override public EntityImportReport doImport( RepositoryCollection source, MetadataAction metadataAction, DataAction dataAction, @Nullable @CheckForNull String packageId) { if (dataAction != DataAction.ADD) { throw new IllegalArgumentException("Only ADD is supported"); } Package importPackage = null; if (packageId != null) { importPackage = metaDataService .getPackage(packageId) .orElseThrow(() -> new UnknownEntityException(PACKAGE, packageId)); } List<EntityType> addedEntities = new ArrayList<>(); EntityImportReport report; Iterator<String> it = source.getEntityTypeIds().iterator(); if (it.hasNext()) { try (Repository<Entity> repo = source.getRepository(it.next())) { report = importVcf(repo, addedEntities, importPackage); } catch (IOException e) { throw new MolgenisDataException(e); } } else { report = new EntityImportReport(); } return report; } VcfImporterService(
      DataService dataService,
      PermissionSystemService permissionSystemService,
      MetaDataService metaDataService); @Transactional @Override EntityImportReport doImport(
      RepositoryCollection source,
      MetadataAction metadataAction,
      DataAction dataAction,
      @Nullable @CheckForNull String packageId); @Override EntitiesValidationReport validateImport(RepositoryCollection source); @Override boolean canImport(File file, RepositoryCollection source); @Override int getOrder(); @Override List<MetadataAction> getSupportedMetadataActions(); @Override List<DataAction> getSupportedDataActions(); @Override boolean getMustChangeEntityName(); @Override Set<String> getSupportedFileExtensions(); @Override Map<String, Boolean> determineImportableEntities(
      MetaDataService metaDataService,
      RepositoryCollection repositoryCollection,
      String defaultPackage); @Override MetadataAction getMetadataAction(RepositoryCollection source);  }