Search in sources :

Example 71 with Package

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

the class VcfToEntityTest method testGetEntityType.

@Test
public void testGetEntityType() {
    EntityType expectedEntityType = entityTypeFactory.create("EntityNameSmall");
    expectedEntityType.addAttribute(vcfAttrs.getChromAttribute());
    expectedEntityType.addAttribute(vcfAttrs.getAltAttribute());
    expectedEntityType.addAttribute(vcfAttrs.getPosAttribute());
    expectedEntityType.addAttribute(vcfAttrs.getRefAttribute());
    expectedEntityType.addAttribute(vcfAttrs.getFilterAttribute());
    expectedEntityType.addAttribute(vcfAttrs.getQualAttribute());
    expectedEntityType.addAttribute(vcfAttrs.getIdAttribute());
    Attribute internalIdMeta = attrMetaFactory.create().setName(INTERNAL_ID).setDataType(STRING).setVisible(false);
    expectedEntityType.addAttribute(internalIdMeta, ROLE_ID);
    Attribute infoMetaData = attrMetaFactory.create().setName(INFO).setDataType(COMPOUND).setNillable(true);
    Attribute infoNS = attrMetaFactory.create().setName("NS").setDataType(INT).setDescription("Number of Samples With Data").setAggregatable(true).setParent(infoMetaData);
    Attribute infoDF = attrMetaFactory.create().setName("DF").setDataType(BOOL).setDescription("Flag field").setAggregatable(true).setParent(infoMetaData);
    Attribute infoDF2 = attrMetaFactory.create().setName("DF2").setDataType(BOOL).setDescription("Flag field 2").setAggregatable(true).setParent(infoMetaData);
    Attribute infoChar2 = attrMetaFactory.create().setName("CHAR").setDataType(STRING).setDescription("char field").setAggregatable(true).setParent(infoMetaData);
    expectedEntityType.addAttribute(infoNS);
    expectedEntityType.addAttribute(infoDF);
    expectedEntityType.addAttribute(infoDF2);
    expectedEntityType.addAttribute(infoChar2);
    expectedEntityType.addAttribute(infoMetaData);
    EntityType actualEntityType = vcfToEntitySmall.getEntityType();
    String backend = "test";
    expectedEntityType.setBackend(backend);
    actualEntityType.setBackend(backend);
    Package package_ = mock(Package.class);
    when(package_.getIdValue()).thenReturn("pck0");
    expectedEntityType.setPackage(package_);
    actualEntityType.setPackage(package_);
    expectedEntityType.setId("dummyId");
    expectedEntityType.setLabel("EntityNameSmall");
    expectedEntityType.getOwnAllAttributes().forEach(attr -> attr.setIdentifier(null));
    actualEntityType.setId("dummyId");
    actualEntityType.getOwnAllAttributes().forEach(attr -> attr.setIdentifier(null));
    assertTrue(EntityUtils.equals(expectedEntityType, actualEntityType));
}
Also used : Package(org.molgenis.data.meta.model.Package) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 72 with Package

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

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

Example 74 with Package

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

the class PackageRepositoryValidationDecorator method deleteById.

@Override
public void deleteById(Object id) {
    Package package_ = findOneById(id);
    if (package_ == null) {
        throw new UnknownEntityException(format("Unknown package [%s]", id.toString()));
    }
    packageValidator.validate(package_);
    super.deleteById(id);
}
Also used : UnknownEntityException(org.molgenis.data.UnknownEntityException) Package(org.molgenis.data.meta.model.Package)

Example 75 with Package

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

the class PermissionManagerController method toPackagePermissions.

private Permissions toPackagePermissions(List<Package> packages, Map<ObjectIdentity, Acl> aclMap, Sid sid) {
    Permissions permissions = new Permissions();
    // set permissions: entity ids
    Map<String, String> entityTypeMap = packages.stream().collect(toMap(Package::getId, Package::getId, (u, v) -> {
        throw new IllegalStateException(format("Duplicate key %s", u));
    }, LinkedHashMap::new));
    permissions.setEntityIds(entityTypeMap);
    return toEntityTypePermissions(aclMap, sid, permissions);
}
Also used : PluginController(org.molgenis.web.PluginController) java.util(java.util) EntityTypeMetadata(org.molgenis.data.meta.model.EntityTypeMetadata) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) WebRequest(org.springframework.web.context.request.WebRequest) LoggerFactory(org.slf4j.LoggerFactory) PackageMetadata(org.molgenis.data.meta.model.PackageMetadata) Controller(org.springframework.stereotype.Controller) Collections.singletonList(java.util.Collections.singletonList) Valid(javax.validation.Valid) User(org.molgenis.data.security.auth.User) Model(org.springframework.ui.Model) Lists(com.google.common.collect.Lists) ANONYMOUS_USERNAME(org.molgenis.security.core.utils.SecurityUtils.ANONYMOUS_USERNAME) Collectors.toMap(java.util.stream.Collectors.toMap) USER(org.molgenis.data.security.auth.UserMetaData.USER) PluginPermission(org.molgenis.data.plugin.model.PluginPermission) Objects.requireNonNull(java.util.Objects.requireNonNull) PluginIdentity(org.molgenis.data.plugin.model.PluginIdentity) SystemEntityTypeRegistry(org.molgenis.data.meta.system.SystemEntityTypeRegistry) org.springframework.security.acls.model(org.springframework.security.acls.model) Comparator.comparing(java.util.Comparator.comparing) URI(org.molgenis.core.ui.admin.permission.PermissionManagerController.URI) SidUtils(org.molgenis.security.acl.SidUtils) Logger(org.slf4j.Logger) USERNAME(org.molgenis.data.security.auth.UserMetaData.USERNAME) org.molgenis.data.security(org.molgenis.data.security) SidUtils.createAnonymousSid(org.molgenis.security.acl.SidUtils.createAnonymousSid) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) MutableAclClassService(org.molgenis.security.acl.MutableAclClassService) String.format(java.lang.String.format) GrantedAuthoritySid(org.springframework.security.acls.domain.GrantedAuthoritySid) PrincipalSid(org.springframework.security.acls.domain.PrincipalSid) HttpStatus(org.springframework.http.HttpStatus) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) GROUP(org.molgenis.data.security.auth.GroupMetaData.GROUP) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) DataService(org.molgenis.data.DataService) Package(org.molgenis.data.meta.model.Package) Group(org.molgenis.data.security.auth.Group) PLUGIN(org.molgenis.data.plugin.model.PluginMetadata.PLUGIN) Permissions(org.molgenis.security.permission.Permissions) Plugin(org.molgenis.data.plugin.model.Plugin) Transactional(org.springframework.transaction.annotation.Transactional) Permissions(org.molgenis.security.permission.Permissions)

Aggregations

Package (org.molgenis.data.meta.model.Package)98 Test (org.testng.annotations.Test)70 EntityType (org.molgenis.data.meta.model.EntityType)22 Stream (java.util.stream.Stream)20 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)13 Attribute (org.molgenis.data.meta.model.Attribute)11 DefaultPackage (org.molgenis.data.meta.DefaultPackage)10 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)10 Objects.requireNonNull (java.util.Objects.requireNonNull)6 L10nString (org.molgenis.data.i18n.model.L10nString)6 PackageIdentity (org.molgenis.data.security.PackageIdentity)6 Collectors.toList (java.util.stream.Collectors.toList)5 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)5 DataService (org.molgenis.data.DataService)5 QueryImpl (org.molgenis.data.support.QueryImpl)5 RepositoryCollection (org.molgenis.data.RepositoryCollection)4 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)3 java.util (java.util)3 Collections.singletonList (java.util.Collections.singletonList)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3