Search in sources :

Example 6 with MetaDataService

use of org.molgenis.data.meta.MetaDataService in project molgenis by molgenis.

the class PlatformIT method testDeletePackageWithOneToMany.

@WithMockUser(username = USERNAME)
@Test(singleThreaded = true)
public void testDeletePackageWithOneToMany() {
    populateUserPermissions();
    runAsSystem(() -> {
        MetaDataService metadataService = dataService.getMeta();
        Package package_ = packageFactory.create("package_onetomany").setLabel("package");
        metadataService.upsertPackages(Stream.of(package_));
        EntityType refEntityType = testHarness.createDynamicRefEntityType("entityType_onetomany", package_);
        EntityType entityType = testHarness.createDynamicTestEntityType("refEntityType_onetomany", package_, refEntityType);
        Attribute oneToManyAttribute = attributeFactory.create("onetomany").setName("onetomany").setDataType(AttributeType.ONE_TO_MANY).setRefEntity(entityType).setMappedBy(entityType.getAttribute(ATTR_XREF));
        refEntityType.addAttribute(oneToManyAttribute);
        metadataService.upsertEntityTypes(asList(refEntityType, entityType));
        List<Entity> entities = createAndAdd(entityType, refEntityType, 5);
        Set<Entity> refEntities = entities.stream().map(e -> e.getEntity(ATTR_XREF)).collect(toSet());
        assertPresent(entityType, entities);
        assertPresent(refEntityType, newArrayList(refEntities));
        dataService.deleteById(PACKAGE, "package_onetomany");
        assertNull(metadataService.getPackage("package_onetomany"));
        assertNull(dataService.getEntityType(entityType.getId()));
        assertNull(dataService.getEntityType(refEntityType.getId()));
        entities.forEach(this::assertNotPresent);
        refEntities.forEach(this::assertNotPresent);
    });
}
Also used : AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) MetaDataServiceImpl(org.molgenis.data.meta.MetaDataServiceImpl) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) RunAsSystemAspect.runAsSystem(org.molgenis.security.core.runas.RunAsSystemAspect.runAsSystem) IndexActionRegisterServiceImpl(org.molgenis.data.index.IndexActionRegisterServiceImpl) L10N_STRING(org.molgenis.data.i18n.model.L10nStringMetaData.L10N_STRING) EntityListener(org.molgenis.data.listeners.EntityListener) MetaDataService(org.molgenis.data.meta.MetaDataService) Collections.singletonList(java.util.Collections.singletonList) TestExecutionListeners(org.springframework.test.context.TestExecutionListeners) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) ElasticsearchService(org.molgenis.data.elasticsearch.ElasticsearchService) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Collectors.toSet(java.util.stream.Collectors.toSet) L10nString(org.molgenis.data.i18n.model.L10nString) AbstractTestNGSpringContextTests(org.springframework.test.context.testng.AbstractTestNGSpringContextTests) EntityTestHarness(org.molgenis.data.EntityTestHarness) LanguageService(org.molgenis.i18n.LanguageService) org.molgenis.data(org.molgenis.data) IndexJobScheduler(org.molgenis.data.index.job.IndexJobScheduler) IndexAction(org.molgenis.data.index.meta.IndexAction) READ(org.molgenis.data.security.EntityTypePermission.READ) PACKAGE(org.molgenis.data.meta.model.PackageMetadata.PACKAGE) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) WithMockUser(org.springframework.security.test.context.support.WithMockUser) LANGUAGE(org.molgenis.data.i18n.model.LanguageMetadata.LANGUAGE) java.util(java.util) org.testng.annotations(org.testng.annotations) QueryImpl(org.molgenis.data.support.QueryImpl) EntityListenersService(org.molgenis.data.listeners.EntityListenersService) EntityTypePermission(org.molgenis.data.security.EntityTypePermission) ENTITY_TYPE_META_DATA(org.molgenis.data.meta.model.EntityTypeMetadata.ENTITY_TYPE_META_DATA) AggregateResult(org.molgenis.data.aggregation.AggregateResult) Assert(org.testng.Assert) WithSecurityContextTestExecutionListener(org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener) Objects.requireNonNull(java.util.Objects.requireNonNull) L10nStringMetaData(org.molgenis.data.i18n.model.L10nStringMetaData) ATTRIBUTE_META_DATA(org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA) AttributeType(org.molgenis.data.meta.AttributeType) Logger(org.slf4j.Logger) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) IndexActionMetaData(org.molgenis.data.index.meta.IndexActionMetaData) L10nStringFactory(org.molgenis.data.i18n.model.L10nStringFactory) LanguageFactory(org.molgenis.data.i18n.model.LanguageFactory) Collectors.toList(java.util.stream.Collectors.toList) org.molgenis.data.meta.model(org.molgenis.data.meta.model) ContextConfiguration(org.springframework.test.context.ContextConfiguration) WRITE(org.molgenis.data.security.EntityTypePermission.WRITE) Package(org.molgenis.data.meta.model.Package) Transactional(org.springframework.transaction.annotation.Transactional) MetaDataService(org.molgenis.data.meta.MetaDataService) Package(org.molgenis.data.meta.model.Package) WithMockUser(org.springframework.security.test.context.support.WithMockUser)

Example 7 with MetaDataService

use of org.molgenis.data.meta.MetaDataService in project molgenis by molgenis.

the class DataPersisterImplTest method setUpBeforeMethod.

@SuppressWarnings("unchecked")
@BeforeMethod
public void setUpBeforeMethod() {
    dataService = mock(DataService.class, RETURNS_DEEP_STUBS);
    dataPersisterImpl = new DataPersisterImpl(metaDataService, dataService, entityTypeDependencyResolver);
    String entityTypeId0 = "entityTypeId0";
    entityType0 = mock(EntityType.class);
    when(entityType0.getId()).thenReturn(entityTypeId0).getMock();
    String entityTypeId1 = "entityTypeId1";
    entityType1 = mock(EntityType.class);
    when(entityType1.getId()).thenReturn(entityTypeId1).getMock();
    String entityTypeId2 = "entityTypeId2";
    entityType2 = mock(EntityType.class);
    when(entityType2.getId()).thenReturn(entityTypeId2).getMock();
    when(dataProvider.getEntityTypes()).thenReturn(Stream.of(entityType0, entityType1, entityType2));
    when(dataProvider.hasEntities(entityType0)).thenReturn(true);
    Entity entity0a = mock(Entity.class);
    Entity entity0b = mock(Entity.class);
    when(dataProvider.getEntities(entityType0)).thenReturn(Stream.of(entity0a, entity0b));
    when(dataProvider.hasEntities(entityType1)).thenReturn(true);
    Entity entity1a = mock(Entity.class);
    Entity entity1b = mock(Entity.class);
    Entity entity1c = mock(Entity.class);
    when(dataProvider.getEntities(entityType1)).thenReturn(Stream.of(entity1a, entity1b, entity1c));
    when(dataProvider.hasEntities(entityType2)).thenReturn(false);
    when(entityTypeDependencyResolver.resolve(asList(entityType0, entityType1, entityType2))).thenReturn(asList(entityType2, entityType1, entityType0));
    doAnswer(invocation -> {
        Stream<Entity> entityStream = (Stream<Entity>) invocation.getArguments()[1];
        try {
            // noinspection ResultOfMethodCallIgnored
            entityStream.collect(toList());
        } catch (IllegalStateException ignored) {
        }
        return null;
    }).when(dataService).add(anyString(), any(Stream.class));
    doAnswer(invocation -> {
        Stream<Entity> entityStream = (Stream<Entity>) invocation.getArguments()[1];
        try {
            // noinspection ResultOfMethodCallIgnored
            entityStream.collect(toList());
        } catch (IllegalStateException ignored) {
        }
        return null;
    }).when(dataService).update(anyString(), any(Stream.class));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) Stream(java.util.stream.Stream) MetaDataService(org.molgenis.data.meta.MetaDataService) DataService(org.molgenis.data.DataService) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 8 with MetaDataService

use of org.molgenis.data.meta.MetaDataService in project molgenis by molgenis.

the class RepositoryValidationDecoratorTest method beforeMethod.

@SuppressWarnings("unchecked")
@BeforeMethod
public void beforeMethod() {
    // ref entity meta
    refEntityName = "refEntity";
    refAttrIdName = "refId";
    Attribute refIdAttr = when(mock(Attribute.class).getName()).thenReturn(refAttrIdName).getMock();
    when(refIdAttr.getDataType()).thenReturn(STRING);
    refEntityType = mock(EntityType.class);
    when(refEntityType.getId()).thenReturn(refEntityName);
    when(refEntityType.getLabel()).thenReturn(refEntityName);
    when(refEntityType.getIdAttribute()).thenReturn(refIdAttr);
    when(refEntityType.getAtomicAttributes()).thenReturn(Arrays.asList(refIdAttr));
    // entity meta
    entityTypeId = "entity";
    attrIdName = "id";
    attrXrefName = "xrefAttr";
    attrNillableXrefName = "nillableXrefAttr";
    attrMrefName = "mrefAttr";
    attrNillableMrefName = "nillableMrefAttr";
    attrUniqueStringName = "uniqueStringAttr";
    attrUniqueXrefName = "uniqueXrefAttr";
    idAttr = when(mock(Attribute.class).getName()).thenReturn(attrIdName).getMock();
    when(idAttr.getDataType()).thenReturn(STRING);
    xrefAttr = when(mock(Attribute.class).getName()).thenReturn(attrXrefName).getMock();
    when(xrefAttr.getRefEntity()).thenReturn(refEntityType);
    when(xrefAttr.getDataType()).thenReturn(XREF);
    when(xrefAttr.isNillable()).thenReturn(false);
    nillableXrefAttr = when(mock(Attribute.class).getName()).thenReturn(attrNillableXrefName).getMock();
    when(nillableXrefAttr.getRefEntity()).thenReturn(refEntityType);
    when(nillableXrefAttr.getDataType()).thenReturn(XREF);
    when(nillableXrefAttr.isNillable()).thenReturn(true);
    mrefAttr = when(mock(Attribute.class).getName()).thenReturn(attrMrefName).getMock();
    when(mrefAttr.getRefEntity()).thenReturn(refEntityType);
    when(mrefAttr.getDataType()).thenReturn(MREF);
    when(mrefAttr.isNillable()).thenReturn(false);
    nillableMrefAttr = when(mock(Attribute.class).getName()).thenReturn(attrNillableMrefName).getMock();
    when(nillableMrefAttr.getRefEntity()).thenReturn(refEntityType);
    when(nillableMrefAttr.getDataType()).thenReturn(MREF);
    when(nillableMrefAttr.isNillable()).thenReturn(true);
    uniqueStringAttr = when(mock(Attribute.class).getName()).thenReturn(attrUniqueStringName).getMock();
    when(uniqueStringAttr.getDataType()).thenReturn(STRING);
    when(uniqueStringAttr.isUnique()).thenReturn(true);
    uniqueXrefAttr = when(mock(Attribute.class).getName()).thenReturn(attrUniqueXrefName).getMock();
    when(uniqueXrefAttr.getRefEntity()).thenReturn(refEntityType);
    when(uniqueXrefAttr.getDataType()).thenReturn(XREF);
    when(uniqueXrefAttr.isUnique()).thenReturn(true);
    entityType = mock(EntityType.class);
    when(entityType.getId()).thenReturn(entityTypeId);
    when(entityType.getLabel()).thenReturn(entityTypeId);
    when(entityType.getIdAttribute()).thenReturn(idAttr);
    when(entityType.getAttribute(attrIdName)).thenReturn(idAttr);
    when(entityType.getAttribute(attrXrefName)).thenReturn(xrefAttr);
    when(entityType.getAttribute(attrNillableXrefName)).thenReturn(nillableXrefAttr);
    when(entityType.getAttribute(attrMrefName)).thenReturn(mrefAttr);
    when(entityType.getAttribute(attrNillableMrefName)).thenReturn(nillableMrefAttr);
    when(entityType.getAttribute(attrUniqueStringName)).thenReturn(uniqueStringAttr);
    when(entityType.getAttribute(attrUniqueXrefName)).thenReturn(uniqueXrefAttr);
    when(entityType.getAtomicAttributes()).thenReturn(Arrays.asList(idAttr, xrefAttr, nillableXrefAttr, mrefAttr, nillableMrefAttr, uniqueStringAttr, uniqueXrefAttr));
    // ref entities
    String refEntity0Id = "idref0";
    refEntity0 = mock(Entity.class);
    when(refEntity0.getEntityType()).thenReturn(refEntityType);
    when(refEntity0.getIdValue()).thenReturn(refEntity0Id);
    when(refEntity0.get(refAttrIdName)).thenReturn(refEntity0Id);
    when(refEntity0.getString(refAttrIdName)).thenReturn(refEntity0Id);
    refEntity0Clone = mock(Entity.class);
    when(refEntity0Clone.getEntityType()).thenReturn(refEntityType);
    when(refEntity0Clone.getIdValue()).thenReturn(refEntity0Id);
    when(refEntity0Clone.get(refAttrIdName)).thenReturn(refEntity0Id);
    when(refEntity0Clone.getString(refAttrIdName)).thenReturn(refEntity0Id);
    String refEntity1Id = "idref1";
    refEntity1 = mock(Entity.class);
    when(refEntity1.getEntityType()).thenReturn(refEntityType);
    when(refEntity1.getIdValue()).thenReturn(refEntity1Id);
    when(refEntity1.get(refAttrIdName)).thenReturn(refEntity1Id);
    when(refEntity1.getString(refAttrIdName)).thenReturn(refEntity1Id);
    refEntity1Clone = mock(Entity.class);
    when(refEntity1Clone.getEntityType()).thenReturn(refEntityType);
    when(refEntity1Clone.getIdValue()).thenReturn(refEntity1Id);
    when(refEntity1Clone.get(refAttrIdName)).thenReturn(refEntity1Id);
    when(refEntity1Clone.getString(refAttrIdName)).thenReturn(refEntity1Id);
    // beans
    delegateRepository = mock(Repository.class);
    when(delegateRepository.getEntityType()).thenReturn(entityType);
    when(delegateRepository.getName()).thenReturn(entityTypeId);
    when(delegateRepository.findAll(new QueryImpl<>().fetch(new Fetch().field(attrUniqueStringName).field(attrUniqueXrefName)))).thenReturn(Stream.empty());
    refRepo = mock(Repository.class);
    when(refRepo.getEntityType()).thenReturn(refEntityType);
    dataService = mock(DataService.class);
    when(dataService.getRepository(entityTypeId)).thenReturn(delegateRepository);
    when(dataService.getRepository(refEntityName)).thenReturn(refRepo);
    when(dataService.findAll(refEntityName, new QueryImpl<>().fetch(new Fetch().field(refAttrIdName)))).thenReturn(Stream.of(refEntity0, refEntity1));
    metaDataService = mock(MetaDataService.class);
    when(dataService.getMeta()).thenReturn(metaDataService);
    expressionValidator = mock(ExpressionValidator.class);
    entityAttributesValidator = mock(EntityAttributesValidator.class);
    defaultValueReferenceValidator = mock(DefaultValueReferenceValidator.class);
    repositoryValidationDecorator = new RepositoryValidationDecorator(dataService, delegateRepository, entityAttributesValidator, defaultValueReferenceValidator);
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) MetaDataService(org.molgenis.data.meta.MetaDataService) EntityType(org.molgenis.data.meta.model.EntityType) QueryImpl(org.molgenis.data.support.QueryImpl) MetaDataService(org.molgenis.data.meta.MetaDataService) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 9 with MetaDataService

use of org.molgenis.data.meta.MetaDataService in project molgenis by molgenis.

the class DataServiceImplTest method beforeMethod.

@SuppressWarnings("unchecked")
@BeforeMethod
public void beforeMethod() {
    dataService = new DataServiceImpl();
    repo1 = when(mock(Repository.class).getName()).thenReturn("Entity1").getMock();
    repo2 = mock(Repository.class);
    repo2 = when(mock(Repository.class).getName()).thenReturn("Entity2").getMock();
    repoToRemove = mock(Repository.class);
    repoToRemove = when(mock(Repository.class).getName()).thenReturn("Entity3").getMock();
    metaDataService = mock(MetaDataService.class);
    when(metaDataService.getRepository("Entity1")).thenReturn(repo1);
    when(metaDataService.getRepository("Entity2")).thenReturn(repo2);
    when(metaDataService.getRepository("Entity3")).thenReturn(repoToRemove);
    EntityType entityType1 = when(mock(EntityType.class).getId()).thenReturn("Entity1").getMock();
    EntityType entityType2 = when(mock(EntityType.class).getId()).thenReturn("Entity2").getMock();
    EntityType entityType3 = when(mock(EntityType.class).getId()).thenReturn("Entity3").getMock();
    when(metaDataService.getEntityTypes()).thenAnswer(invocation -> Stream.of(entityType1, entityType2, entityType3));
    dataService.setMetaDataService(metaDataService);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) MetaDataService(org.molgenis.data.meta.MetaDataService) DataServiceImpl(org.molgenis.data.support.DataServiceImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 10 with MetaDataService

use of org.molgenis.data.meta.MetaDataService in project molgenis by molgenis.

the class DataExplorerControllerTest method initSortEntitiesByLabel.

@Test
public void initSortEntitiesByLabel() {
    MetaDataService metaDataService = mock(MetaDataService.class);
    when(dataService.getMeta()).thenReturn(metaDataService);
    EntityType entity1 = mock(EntityType.class);
    when(entity1.getId()).thenReturn("1");
    when(entity1.getLabel()).thenReturn("zzz");
    EntityType entity2 = mock(EntityType.class);
    when(entity2.getId()).thenReturn("2");
    when(entity2.getLabel()).thenReturn("aaa");
    Stream<EntityType> entityStream = Stream.of(entity1, entity2);
    when(metaDataService.getEntityTypes()).thenReturn(entityStream);
    controller.init(null, null, model);
    LinkedHashMap expected = new LinkedHashMap<>(Stream.of(entity1, entity2).sorted(Comparator.comparing(EntityType::getLabel)).collect(Collectors.toMap(EntityType::getId, Function.identity())));
    verify(model).addAttribute("entitiesMeta", expected);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) MetaDataService(org.molgenis.data.meta.MetaDataService) Test(org.testng.annotations.Test)

Aggregations

MetaDataService (org.molgenis.data.meta.MetaDataService)12 EntityType (org.molgenis.data.meta.model.EntityType)6 Test (org.testng.annotations.Test)5 QueryImpl (org.molgenis.data.support.QueryImpl)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 Stream (java.util.stream.Stream)3 Attribute (org.molgenis.data.meta.model.Attribute)3 Package (org.molgenis.data.meta.model.Package)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)2 java.util (java.util)2 Arrays.asList (java.util.Arrays.asList)2 Collections.singletonList (java.util.Collections.singletonList)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Collectors.toList (java.util.stream.Collectors.toList)2 Collectors.toSet (java.util.stream.Collectors.toSet)2 org.molgenis.data (org.molgenis.data)2 EntityTestHarness (org.molgenis.data.EntityTestHarness)2 AggregateQuery (org.molgenis.data.aggregation.AggregateQuery)2