Search in sources :

Example 31 with Entity

use of org.molgenis.data.Entity in project molgenis by molgenis.

the class NegotiatorController method validateNegotiatorExport.

@PostMapping("/validate")
@ResponseBody
public ExportValidationResponse validateNegotiatorExport(@RequestBody NegotiatorRequest request) {
    boolean isValidRequest = true;
    String message = "";
    List<String> enabledCollectionsLabels;
    List<String> disabledCollectionLabels;
    NegotiatorEntityConfig entityConfig = getNegotiatorEntityConfig(request.getEntityId());
    if (null != entityConfig) {
        LOG.info("Validating negotiator request\n\n{}", request);
        List<Entity> collectionEntities = getCollectionEntities(request);
        List<Entity> disabledCollections = getDisabledCollections(collectionEntities, entityConfig);
        Function<Entity, String> getLabel = entity -> entity.getLabelValue().toString();
        disabledCollectionLabels = disabledCollections.stream().map(getLabel).collect(toList());
        enabledCollectionsLabels = collectionEntities.stream().filter(e -> !disabledCollections.contains(e)).map(getLabel).collect(toList());
        if (!disabledCollections.isEmpty()) {
            message = messageSource.getMessage("dataexplorer_directory_disabled", new Object[] { disabledCollections.size(), collectionEntities.size() }, getLocale());
        }
        if (collectionEntities.isEmpty() || (collectionEntities.size() == disabledCollections.size())) {
            isValidRequest = false;
            message = messageSource.getMessage("dataexplorer_directory_no_rows", new Object[] {}, getLocale());
        }
    } else {
        throw new MolgenisDataException(messageSource.getMessage("dataexplorer_directory_no_config", new Object[] {}, getLocale()));
    }
    return ExportValidationResponse.create(isValidRequest, message, enabledCollectionsLabels, disabledCollectionLabels);
}
Also used : PluginController(org.molgenis.web.PluginController) NegotiatorConfig(org.molgenis.dataexplorer.negotiator.config.NegotiatorConfig) ErrorMessageResponse(org.molgenis.web.ErrorMessageResponse) LoggerFactory(org.slf4j.LoggerFactory) QueryImpl(org.molgenis.data.support.QueryImpl) Controller(org.springframework.stereotype.Controller) Attribute(org.molgenis.data.meta.model.Attribute) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem) Function(java.util.function.Function) PluginPermission(org.molgenis.data.plugin.model.PluginPermission) Objects.requireNonNull(java.util.Objects.requireNonNull) PluginIdentity(org.molgenis.data.plugin.model.PluginIdentity) JsMagmaScriptEvaluator(org.molgenis.js.magma.JsMagmaScriptEvaluator) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) RestTemplate(org.springframework.web.client.RestTemplate) MessageSource(org.springframework.context.MessageSource) RestClientException(org.springframework.web.client.RestClientException) QueryRsqlConverter(org.molgenis.data.rest.convert.QueryRsqlConverter) Logger(org.slf4j.Logger) URI(org.molgenis.dataexplorer.negotiator.NegotiatorController.URI) LocaleContextHolder.getLocale(org.springframework.context.i18n.LocaleContextHolder.getLocale) HttpHeaders(org.springframework.http.HttpHeaders) UTF_8(java.nio.charset.StandardCharsets.UTF_8) NegotiatorEntityConfigMeta(org.molgenis.dataexplorer.negotiator.config.NegotiatorEntityConfigMeta) EntityTypeUtils(org.molgenis.data.support.EntityTypeUtils) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) HttpStatus(org.springframework.http.HttpStatus) HttpEntity(org.springframework.http.HttpEntity) Base64(java.util.Base64) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ENABLED_EXPRESSION(org.molgenis.dataexplorer.negotiator.config.NegotiatorEntityConfigMeta.ENABLED_EXPRESSION) UserPermissionEvaluator(org.molgenis.security.core.UserPermissionEvaluator) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) DataService(org.molgenis.data.DataService) Query(org.molgenis.data.Query) NegotiatorEntityConfig(org.molgenis.dataexplorer.negotiator.config.NegotiatorEntityConfig) MolgenisDataException(org.molgenis.data.MolgenisDataException) Entity(org.molgenis.data.Entity) NegotiatorEntityConfig(org.molgenis.dataexplorer.negotiator.config.NegotiatorEntityConfig) HttpEntity(org.springframework.http.HttpEntity) Entity(org.molgenis.data.Entity) MolgenisDataException(org.molgenis.data.MolgenisDataException)

Example 32 with Entity

use of org.molgenis.data.Entity in project molgenis by molgenis.

the class EntityReferenceTest method testSetEntityIdAttribute.

@Test
public void testSetEntityIdAttribute() {
    Entity entity = mock(Entity.class);
    when(entity.getAttributeNames()).thenReturn(singletonList(ID_ATTR_NAME)).getMock();
    when(entity.get(ID_ATTR_NAME)).thenReturn("newEntityId");
    entityReference.set(entity);
    assertEquals(entityReference.getIdValue(), "newEntityId");
}
Also used : Entity(org.molgenis.data.Entity) Test(org.testng.annotations.Test)

Example 33 with Entity

use of org.molgenis.data.Entity in project molgenis by molgenis.

the class AbstractRepositoryTest method testUpsertBatch.

// // Note: streamFetch cannot be tested because mocking default methods is not supported by Mockito
@Test
public void testUpsertBatch() {
    Object id0 = "id0";
    Object id1 = "id1";
    Entity entity0 = when(mock(Entity.class).getIdValue()).thenReturn(id0).getMock();
    Entity entity1 = when(mock(Entity.class).getIdValue()).thenReturn(id1).getMock();
    List<Entity> batch = Arrays.asList(entity0, entity1);
    doReturn(Stream.of(entity0)).when(abstractRepository).findAll(objectStreamCaptor.capture(), any(Fetch.class));
    doReturn(1).when(abstractRepository).add(addStreamCaptor.capture());
    doNothing().when(abstractRepository).update(updateStreamCaptor.capture());
    abstractRepository.upsertBatch(batch);
    assertEquals(addStreamCaptor.getValue().collect(Collectors.toList()), Collections.singletonList(entity1), "New entity should get added.");
    assertEquals(updateStreamCaptor.getValue().collect(Collectors.toList()), Collections.singletonList(entity0), "Existing entity should get updated.");
}
Also used : Fetch(org.molgenis.data.Fetch) Entity(org.molgenis.data.Entity) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 34 with Entity

use of org.molgenis.data.Entity in project molgenis by molgenis.

the class AbstractRepositoryTest method beforeTest.

@BeforeTest
public void beforeTest() {
    MockitoAnnotations.initMocks(this);
    Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn("id").getMock();
    entityType = when(mock(EntityType.class).getId()).thenReturn("entity").getMock();
    when(entityType.getIdAttribute()).thenReturn(idAttr);
    abstractRepository = Mockito.spy(new AbstractRepository() {

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

        public EntityType getEntityType() {
            return entityType;
        }

        @Override
        public Set<RepositoryCapability> getCapabilities() {
            return Collections.emptySet();
        }
    });
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) RepositoryCapability(org.molgenis.data.RepositoryCapability) BeforeTest(org.testng.annotations.BeforeTest)

Example 35 with Entity

use of org.molgenis.data.Entity in project molgenis by molgenis.

the class DocumentContentBuilderTest method createDocumentReferenceAttribute.

@Test(dataProvider = "createDocumentReference")
public void createDocumentReferenceAttribute(AttributeType attributeType, Entity value, String expectedContent) {
    String attrIdentifier = "attr";
    Entity entity = createEntity(attrIdentifier, attributeType);
    when(entity.getEntity(attrIdentifier)).thenReturn(value);
    Document document = documentContentBuilder.createDocument(entity);
    assertDocumentEquals(document, expectedContent);
}
Also used : Entity(org.molgenis.data.Entity) Document(org.molgenis.data.elasticsearch.generator.model.Document) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Aggregations

Entity (org.molgenis.data.Entity)448 Test (org.testng.annotations.Test)295 DynamicEntity (org.molgenis.data.support.DynamicEntity)192 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)120 Attribute (org.molgenis.data.meta.model.Attribute)111 EntityType (org.molgenis.data.meta.model.EntityType)110 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)37 MolgenisDataException (org.molgenis.data.MolgenisDataException)20 QueryImpl (org.molgenis.data.support.QueryImpl)20 AttributeType (org.molgenis.data.meta.AttributeType)18 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)18 Stream (java.util.stream.Stream)17 QueryRule (org.molgenis.data.QueryRule)16 MultiAllelicResultFilter (org.molgenis.data.annotation.core.filter.MultiAllelicResultFilter)16 RunAsSystem (org.molgenis.security.core.runas.RunAsSystem)16 Objects.requireNonNull (java.util.Objects.requireNonNull)15 DataService (org.molgenis.data.DataService)15 AttributeMapping (org.molgenis.semanticmapper.mapping.model.AttributeMapping)15 WithMockUser (org.springframework.security.test.context.support.WithMockUser)14 Instant (java.time.Instant)13