Search in sources :

Example 81 with EntityType

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

the class RestControllerTest method retrieveEntityAttributeUnknownAttribute.

@Test
public void retrieveEntityAttributeUnknownAttribute() throws Exception {
    @SuppressWarnings("unchecked") Repository<Entity> repo = mock(Repository.class);
    EntityType entityType = mock(EntityType.class);
    when(entityType.getId()).thenReturn(ENTITY_NAME);
    when(entityType.getLabel("en")).thenReturn("My Entity");
    when(entityType.getAttribute("name")).thenReturn(null);
    Attribute idAttr = when(mock(Attribute.class).getDataType()).thenReturn(STRING).getMock();
    when(entityType.getIdAttribute()).thenReturn(idAttr);
    when(repo.getEntityType()).thenReturn(entityType);
    when(dataService.getEntityType(ENTITY_NAME)).thenReturn(entityType);
    when(dataService.getRepository(ENTITY_NAME)).thenReturn(repo);
    mockMvc.perform(get(HREF_ENTITY_ID + "/name")).andExpect(status().isNotFound()).andExpect(jsonPath(FIRST_ERROR_CODE, is("D04"))).andExpect(jsonPath(FIRST_ERROR_MESSAGE, is("Unknown attribute 'name' of entity type 'My Entity'.")));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Attribute(org.molgenis.data.meta.model.Attribute) Test(org.testng.annotations.Test)

Example 82 with EntityType

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

the class RestControllerTest method beforeMethod.

@BeforeMethod
public void beforeMethod() {
    MockitoAnnotations.initMocks(this);
    reset(permissionService, dataService, metaDataService, tokenService);
    when(dataService.getMeta()).thenReturn(metaDataService);
    @SuppressWarnings("unchecked") Repository<Entity> repo = mock(Repository.class);
    // test entity meta data
    EntityType entityType = mock(EntityType.class);
    Attribute attrId = when(mock(Attribute.class).getName()).thenReturn("id").getMock();
    when(attrId.getLabel()).thenReturn("id");
    when(attrId.getLabel("en")).thenReturn("id");
    when(attrId.getDataType()).thenReturn(STRING);
    when(attrId.isReadOnly()).thenReturn(true);
    when(attrId.isUnique()).thenReturn(true);
    when(attrId.isNillable()).thenReturn(false);
    when(attrId.isVisible()).thenReturn(false);
    when(attrId.getChildren()).thenReturn(emptyList());
    when(attrId.getEnumOptions()).thenReturn(emptyList());
    Attribute attrName = when(mock(Attribute.class).getName()).thenReturn("name").getMock();
    when(attrName.getLabel()).thenReturn("name");
    when(attrName.getLabel("en")).thenReturn("name");
    when(attrName.getDataType()).thenReturn(STRING);
    when(attrName.isNillable()).thenReturn(true);
    when(attrName.isVisible()).thenReturn(true);
    when(attrName.getChildren()).thenReturn(emptyList());
    when(attrName.getEnumOptions()).thenReturn(emptyList());
    Attribute attrEnum = when(mock(Attribute.class).getName()).thenReturn("enum").getMock();
    when(attrEnum.getLabel()).thenReturn("enum");
    when(attrEnum.getLabel("en")).thenReturn("enum");
    when(attrEnum.getDataType()).thenReturn(AttributeType.ENUM);
    when(attrEnum.getEnumOptions()).thenReturn(singletonList("enum0, enum1"));
    when(attrEnum.isNillable()).thenReturn(true);
    when(attrEnum.isVisible()).thenReturn(true);
    when(attrEnum.getChildren()).thenReturn(emptyList());
    Attribute attrInt = when(mock(Attribute.class).getName()).thenReturn("int").getMock();
    when(attrInt.getLabel()).thenReturn("int");
    when(attrInt.getLabel("en")).thenReturn("int");
    when(attrInt.getDataType()).thenReturn(AttributeType.INT);
    when(attrInt.isNillable()).thenReturn(true);
    when(attrInt.isVisible()).thenReturn(true);
    when(attrInt.getChildren()).thenReturn(emptyList());
    when(entityType.getAttribute("id")).thenReturn(attrId);
    when(entityType.getAttribute("name")).thenReturn(attrName);
    when(entityType.getAttribute("enum")).thenReturn(attrEnum);
    when(entityType.getAttribute("int")).thenReturn(attrInt);
    when(entityType.getMappedByAttributes()).thenReturn(Stream.empty());
    when(entityType.getIdAttribute()).thenReturn(attrId);
    // TODO: This upgrades the test to mockito 2 but actually shows an error in the test
    when(entityType.getLookupAttributes()).thenReturn(null);
    when(entityType.getAttributes()).thenReturn(asList(attrName, attrId, attrEnum, attrInt));
    when(entityType.getAtomicAttributes()).thenReturn(asList(attrName, attrId, attrEnum, attrInt));
    when(entityType.getId()).thenReturn(ENTITY_NAME);
    when(entityType.getLabel("en")).thenReturn(null);
    when(repo.getEntityType()).thenReturn(entityType);
    when(repo.getName()).thenReturn(ENTITY_NAME);
    when(dataService.getEntityType(ENTITY_NAME)).thenReturn(entityType);
    when(entityManager.create(entityType, POPULATE)).thenReturn(new DynamicEntity(entityType));
    // test entities
    Entity entityXref = new DynamicEntity(entityType);
    entityXref.set("id", ENTITY_UNTYPED_ID);
    entityXref.set("name", "PietXREF");
    Entity entity = new DynamicEntity(entityType);
    entity.set("id", ENTITY_UNTYPED_ID);
    entity.set("name", "Piet");
    entity.set("enum", "enum1");
    entity.set("int", 1);
    Entity entity2 = new DynamicEntity(entityType);
    entity2.set("id", "p2");
    entity2.set("name", "Klaas");
    entity2.set("int", 2);
    when(dataService.getEntityTypeIds()).thenReturn(Stream.of(ENTITY_NAME));
    when(dataService.getRepository(ENTITY_NAME)).thenReturn(repo);
    when(dataService.findOneById(ArgumentMatchers.eq(ENTITY_NAME), ArgumentMatchers.eq(ENTITY_UNTYPED_ID), any(Fetch.class))).thenReturn(entity);
    when(dataService.findOneById(ENTITY_NAME, ENTITY_UNTYPED_ID)).thenReturn(entity);
    Query<Entity> q = new QueryImpl<>().eq("name", "Piet").pageSize(10).offset(5);
    when(dataService.findAll(ENTITY_NAME, q)).thenReturn(Stream.of(entity));
    Query<Entity> q2 = new QueryImpl<>().sort(new Sort().on("name", Sort.Direction.DESC)).pageSize(100).offset(0);
    when(dataService.findAll(ENTITY_NAME, q2)).thenReturn(Stream.of(entity2, entity));
    when(localeResolver.resolveLocale(any())).thenReturn(ENGLISH);
    mockMvc = MockMvcBuilders.standaloneSetup(restController).setMessageConverters(gsonHttpMessageConverter, new CsvHttpMessageConverter()).setCustomArgumentResolvers(new TokenExtractor()).setControllerAdvice(new GlobalControllerExceptionHandler(), new FallbackExceptionHandler(), new SpringExceptionHandler()).setLocaleResolver(localeResolver).build();
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Attribute(org.molgenis.data.meta.model.Attribute) DynamicEntity(org.molgenis.data.support.DynamicEntity) SpringExceptionHandler(org.molgenis.web.exception.SpringExceptionHandler) CsvHttpMessageConverter(org.molgenis.core.ui.messageconverter.CsvHttpMessageConverter) EntityType(org.molgenis.data.meta.model.EntityType) TokenExtractor(org.molgenis.security.token.TokenExtractor) GlobalControllerExceptionHandler(org.molgenis.web.exception.GlobalControllerExceptionHandler) FallbackExceptionHandler(org.molgenis.web.exception.FallbackExceptionHandler) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 83 with EntityType

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

the class RestControllerTest method retrieveEntityAttributeXref.

@Test
public void retrieveEntityAttributeXref() throws Exception {
    reset(dataService);
    @SuppressWarnings("unchecked") Repository<Entity> repo = mock(Repository.class);
    when(dataService.getRepository(ENTITY_NAME)).thenReturn(repo);
    when(dataService.getEntityTypeIds()).thenReturn(Stream.of(ENTITY_NAME));
    // entity meta data
    EntityType refEntityType = when(mock(EntityType.class).getId()).thenReturn("refEntity").getMock();
    Attribute attrId = when(mock(Attribute.class).getName()).thenReturn("id").getMock();
    when(attrId.getLabel()).thenReturn("id");
    when(attrId.getLabel(ArgumentMatchers.anyString())).thenReturn("id");
    when(attrId.getDataType()).thenReturn(STRING);
    when(attrId.isReadOnly()).thenReturn(true);
    when(attrId.isUnique()).thenReturn(true);
    when(attrId.isNillable()).thenReturn(false);
    when(attrId.isVisible()).thenReturn(false);
    when(attrId.getChildren()).thenReturn(emptyList());
    when(attrId.getEnumOptions()).thenReturn(emptyList());
    Attribute attrName = when(mock(Attribute.class).getName()).thenReturn("name").getMock();
    when(attrName.getLabel()).thenReturn("name");
    when(attrName.getLabel(ArgumentMatchers.anyString())).thenReturn("name");
    when(attrName.getDataType()).thenReturn(STRING);
    when(attrName.isNillable()).thenReturn(true);
    when(attrName.isVisible()).thenReturn(true);
    when(attrName.getChildren()).thenReturn(emptyList());
    when(attrName.getEnumOptions()).thenReturn(emptyList());
    when(refEntityType.getAttribute("id")).thenReturn(attrId);
    when(refEntityType.getAttribute("name")).thenReturn(attrName);
    when(refEntityType.getIdAttribute()).thenReturn(attrId);
    when(refEntityType.getAttributes()).thenReturn(asList(attrId, attrName));
    when(refEntityType.getAtomicAttributes()).thenReturn(asList(attrId, attrName));
    when(refEntityType.getId()).thenReturn("refEntity");
    EntityType entityType = when(mock(EntityType.class).getId()).thenReturn(ENTITY_NAME).getMock();
    Attribute attrXref = when(mock(Attribute.class).getName()).thenReturn("xrefValue").getMock();
    when(attrXref.getLabel()).thenReturn("xrefValue");
    when(attrXref.getLabel(ArgumentMatchers.anyString())).thenReturn("xrefValue");
    when(attrXref.getDataType()).thenReturn(AttributeType.XREF);
    when(attrXref.isNillable()).thenReturn(true);
    when(attrXref.isVisible()).thenReturn(true);
    when(attrXref.getChildren()).thenReturn(emptyList());
    when(attrXref.getEnumOptions()).thenReturn(emptyList());
    when(attrXref.getRefEntity()).thenReturn(refEntityType);
    when(entityType.getAttribute("id")).thenReturn(attrId);
    when(entityType.getAttribute("xrefValue")).thenReturn(attrXref);
    when(entityType.getIdAttribute()).thenReturn(attrId);
    when(entityType.getAttributes()).thenReturn(asList(attrId, attrXref));
    when(entityType.getAtomicAttributes()).thenReturn(asList(attrId, attrXref));
    when(entityType.getId()).thenReturn(ENTITY_NAME);
    Entity entityXref = new DynamicEntity(refEntityType);
    entityXref.set("id", ENTITY_UNTYPED_ID);
    entityXref.set("name", "Piet");
    Entity entity = new DynamicEntity(entityType);
    entity.set("id", ENTITY_UNTYPED_ID);
    entity.set("xrefValue", entityXref);
    when(dataService.findOneById(ENTITY_NAME, ENTITY_UNTYPED_ID)).thenReturn(entity);
    when(dataService.findOneById("refEntity", ENTITY_UNTYPED_ID)).thenReturn(entityXref);
    when(dataService.getEntityType(ENTITY_NAME)).thenReturn(entityType);
    when(dataService.getEntityType("refEntity")).thenReturn(refEntityType);
    mockMvc = MockMvcBuilders.standaloneSetup(restController).setMessageConverters(gsonHttpMessageConverter).build();
    mockMvc.perform(get(HREF_ENTITY_ID + "/xrefValue")).andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JSON_UTF8)).andExpect(content().string("{\"href\":\"/api/v1/Person/p1/xrefValue\",\"id\":\"p1\",\"name\":\"Piet\"}"));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Attribute(org.molgenis.data.meta.model.Attribute) DynamicEntity(org.molgenis.data.support.DynamicEntity) Test(org.testng.annotations.Test)

Example 84 with EntityType

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

the class RestController method retrieveEntityTypePost.

/**
 * Same as retrieveEntityType (GET) only tunneled through POST.
 * <p>
 * Example url: /api/v1/person/meta?_method=GET
 *
 * @return EntityType
 */
@PostMapping(value = "/{entityTypeId}/meta", params = "_method=GET", produces = APPLICATION_JSON_VALUE)
public EntityTypeResponse retrieveEntityTypePost(@PathVariable("entityTypeId") String entityTypeId, @Valid @RequestBody EntityTypeRequest request) {
    Set<String> attributesSet = toAttributeSet(request != null ? request.getAttributes() : null);
    Map<String, Set<String>> attributeExpandSet = toExpandMap(request != null ? request.getExpand() : null);
    EntityType meta = dataService.getEntityType(entityTypeId);
    if (meta == null) {
        throw new UnknownEntityTypeException(entityTypeId);
    }
    return new EntityTypeResponse(meta, attributesSet, attributeExpandSet, permissionService, dataService);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType)

Example 85 with EntityType

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

the class RestController method delete.

/**
 * Deletes an entity by it's id
 */
@Transactional
@DeleteMapping("/{entityTypeId}/{id}")
@ResponseStatus(NO_CONTENT)
public void delete(@PathVariable("entityTypeId") String entityTypeId, @PathVariable("id") String untypedId) {
    EntityType entityType = dataService.getEntityType(entityTypeId);
    Object id = getTypedValue(untypedId, entityType.getIdAttribute());
    if (ATTRIBUTE_META_DATA.equals(entityTypeId)) {
        dataService.getMeta().deleteAttributeById(id);
    } else {
        dataService.deleteById(entityTypeId, id);
    }
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

EntityType (org.molgenis.data.meta.model.EntityType)581 Test (org.testng.annotations.Test)367 Attribute (org.molgenis.data.meta.model.Attribute)315 Entity (org.molgenis.data.Entity)98 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)71 DynamicEntity (org.molgenis.data.support.DynamicEntity)61 Stream (java.util.stream.Stream)44 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)43 WithMockUser (org.springframework.security.test.context.support.WithMockUser)40 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)36 QueryImpl (org.molgenis.data.support.QueryImpl)33 Package (org.molgenis.data.meta.model.Package)32 Objects.requireNonNull (java.util.Objects.requireNonNull)22 Collectors.toList (java.util.stream.Collectors.toList)22 BeforeMethod (org.testng.annotations.BeforeMethod)20 AttributeType (org.molgenis.data.meta.AttributeType)19 List (java.util.List)17 String.format (java.lang.String.format)16 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)16 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)15