use of org.molgenis.data.meta.model.Attribute 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'.")));
}
use of org.molgenis.data.meta.model.Attribute 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();
}
use of org.molgenis.data.meta.model.Attribute 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\"}"));
}
use of org.molgenis.data.meta.model.Attribute in project molgenis by molgenis.
the class RestController method retrieveEntityAttributeInternal.
private Object retrieveEntityAttributeInternal(String entityTypeId, String untypedId, String refAttributeName, EntityCollectionRequest request, Set<String> attributesSet, Map<String, Set<String>> attributeExpandSet) {
EntityType meta = dataService.getEntityType(entityTypeId);
if (meta == null) {
throw new UnknownEntityTypeException(entityTypeId);
}
Object id = getTypedValue(untypedId, meta.getIdAttribute());
// Check if the entity has an attribute with name refAttributeName
Attribute attr = meta.getAttribute(refAttributeName);
if (attr == null) {
throw new UnknownAttributeException(meta, refAttributeName);
}
// Get the entity
Entity entity = dataService.findOneById(entityTypeId, id);
if (entity == null) {
throw new UnknownEntityException(entityTypeId + " " + id + " not found");
}
String attrHref = Href.concatAttributeHref(RestController.BASE_URI, meta.getId(), entity.getIdValue(), refAttributeName);
switch(attr.getDataType()) {
case COMPOUND:
Map<String, Object> entityHasAttributeMap = new LinkedHashMap<>();
entityHasAttributeMap.put("href", attrHref);
@SuppressWarnings("unchecked") Iterable<Attribute> attributeParts = (Iterable<Attribute>) entity.get(refAttributeName);
for (Attribute attribute : attributeParts) {
String attrName = attribute.getName();
entityHasAttributeMap.put(attrName, entity.get(attrName));
}
return entityHasAttributeMap;
case CATEGORICAL_MREF:
case MREF:
case ONE_TO_MANY:
List<Entity> mrefEntities = new ArrayList<>();
for (Entity e : entity.getEntities((attr.getName()))) mrefEntities.add(e);
int count = mrefEntities.size();
int toIndex = request.getStart() + request.getNum();
mrefEntities = mrefEntities.subList(request.getStart(), toIndex > count ? count : toIndex);
List<Map<String, Object>> refEntityMaps = new ArrayList<>();
for (Entity refEntity : mrefEntities) {
Map<String, Object> refEntityMap = getEntityAsMap(refEntity, attr.getRefEntity(), attributesSet, attributeExpandSet);
refEntityMaps.add(refEntityMap);
}
EntityPager pager = new EntityPager(request.getStart(), request.getNum(), (long) count, mrefEntities);
return new EntityCollectionResponse(pager, refEntityMaps, attrHref, null, permissionService, dataService);
case CATEGORICAL:
case XREF:
Map<String, Object> entityXrefAttributeMap = getEntityAsMap((Entity) entity.get(refAttributeName), attr.getRefEntity(), attributesSet, attributeExpandSet);
entityXrefAttributeMap.put("href", attrHref);
return entityXrefAttributeMap;
default:
Map<String, Object> entityAttributeMap = new LinkedHashMap<>();
entityAttributeMap.put("href", attrHref);
entityAttributeMap.put(refAttributeName, entity.get(refAttributeName));
return entityAttributeMap;
}
}
use of org.molgenis.data.meta.model.Attribute in project molgenis by molgenis.
the class EntityIdentityUtilsTest method testToIdType.
@Test(dataProvider = "testToIdTypeProvider")
public void testToIdType(AttributeType attributeType, Class<?> expectedIdType) {
EntityType entityType = mock(EntityType.class);
Attribute idAttribute = when(mock(Attribute.class).getDataType()).thenReturn(attributeType).getMock();
when(entityType.getIdAttribute()).thenReturn(idAttribute);
assertEquals(EntityIdentityUtils.toIdType(entityType), expectedIdType);
}
Aggregations