use of nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl in project timbuctoo by HuygensING.
the class EntityToJsonMapperTest method mapEntityAddsADisplayNameWhenItIsKnown.
@Test
public void mapEntityAddsADisplayNameWhenItIsKnown() {
ReadEntityImpl readEntity = new ReadEntityImpl();
UUID id = UUID.randomUUID();
readEntity.setId(id);
String userId = "userId";
Change change = new Change(Instant.now().toEpochMilli(), userId, null);
readEntity.setCreated(change);
readEntity.setModified(change);
String type = "otherType";
readEntity.setTypes(Lists.newArrayList("type", type));
readEntity.setDeleted(false);
readEntity.setRev(1);
readEntity.setPid("pid");
readEntity.setProperties(Lists.newArrayList());
readEntity.setRelations(Lists.newArrayList());
String displayName = "displayName";
readEntity.setDisplayName(displayName);
Collection collection = mock(Collection.class);
when(collection.getEntityTypeName()).thenReturn(type);
ObjectNode resutlJson = instance.mapEntity(collection, readEntity, true, (readEntity1, resultJson) -> {
}, (relationRef, resultJson) -> {
});
assertThat(resutlJson.has("@displayName"), is(true));
assertThat(resutlJson.get("@displayName"), is(jsn(displayName)));
}
use of nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl in project timbuctoo by HuygensING.
the class EntityToJsonMapperTest method mapEntityMapsTheTypeAndId.
@Test
public void mapEntityMapsTheTypeAndId() throws Exception {
ReadEntityImpl readEntity = new ReadEntityImpl();
UUID id = UUID.randomUUID();
readEntity.setId(id);
Change change = new Change(Instant.now().toEpochMilli(), USER_ID, null);
readEntity.setCreated(change);
readEntity.setModified(change);
String type = "otherType";
readEntity.setTypes(Lists.newArrayList("type", type));
readEntity.setDeleted(false);
readEntity.setRev(1);
readEntity.setPid("pid");
readEntity.setProperties(Lists.newArrayList());
EntityToJsonMapper instance = new EntityToJsonMapper(userValidator, (collection, id1, rev) -> URI.create("www.example.com"));
Collection collection = mock(Collection.class);
when(collection.getEntityTypeName()).thenReturn(type);
ObjectNode resutlJson = instance.mapEntity(collection, readEntity, false, (readEntity1, resultJson) -> {
}, (relationRef, resultJson) -> {
});
assertThat(resutlJson.toString(), sameJSONAs(jsnO("_id", jsn(id.toString()), "@type", jsn(type)).toString()).allowingExtraUnexpectedFields());
}
use of nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl in project timbuctoo by HuygensING.
the class EntityToJsonMapperTest method mapEntityMapsTheRelations.
@Test
public void mapEntityMapsTheRelations() throws Exception {
ReadEntityImpl readEntity = new ReadEntityImpl();
UUID id = UUID.randomUUID();
readEntity.setId(id);
String userId = "userId";
Change change = new Change(Instant.now().toEpochMilli(), userId, null);
readEntity.setCreated(change);
readEntity.setModified(change);
String type = "otherType";
readEntity.setTypes(Lists.newArrayList("type", type));
readEntity.setDeleted(false);
readEntity.setRev(1);
readEntity.setPid("pid");
readEntity.setProperties(Lists.newArrayList());
String otherEntity = UUID.randomUUID().toString();
String relType = "relType";
readEntity.setRelations(Lists.newArrayList(new RelationRef(otherEntity, "rdfUri", new String[] { "origUri" }, "otherColl", "otherType", true, "relId", "rdfUri", 1, relType, "displayName")));
Collection collection = mock(Collection.class);
when(collection.getEntityTypeName()).thenReturn(type);
ObjectNode resutlJson = instance.mapEntity(collection, readEntity, true, (readEntity1, resultJson) -> {
}, (relationRef, resultJson) -> {
});
assertThat(resutlJson.toString(), sameJSONAs(jsnO("@relationCount", jsn(1), "@relations", jsnO(relType, jsnA(jsnO("id", jsn(otherEntity))))).toString()).allowingExtraUnexpectedFields());
}
use of nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl in project timbuctoo by HuygensING.
the class TinkerPopToEntityMapper method mapEntity.
public ReadEntity mapEntity(GraphTraversal<Vertex, Vertex> entityT, boolean withRelations) {
final List<TimProperty<?>> properties = Lists.newArrayList();
TinkerPopPropertyConverter dbPropertyConverter = new TinkerPopPropertyConverter(collection);
String entityTypeName = collection.getEntityTypeName();
GraphTraversal[] propertyGetters = collection.getReadableProperties().entrySet().stream().map(prop -> prop.getValue().traversalRaw().sideEffect(x -> x.get().onSuccess(value -> {
try {
properties.add(dbPropertyConverter.from(prop.getKey(), value));
} catch (UnknownPropertyException e) {
LOG.error("Unknown property", e);
} catch (IOException e) {
LOG.error(databaseInvariant, "Property '" + prop.getKey() + "' is not encoded correctly", e.getCause());
}
}).onFailure(e -> {
if (e.getCause() instanceof IOException) {
LOG.error(databaseInvariant, "Property '" + prop.getKey() + "' is not encoded correctly", e.getCause());
} else {
LOG.error("Something went wrong while reading the property '" + prop.getKey() + "'.", e.getCause());
}
}))).toArray(GraphTraversal[]::new);
entityT.asAdmin().clone().union(propertyGetters).forEachRemaining(x -> {
// Force side effects to happen
});
ReadEntityImpl entity = new ReadEntityImpl();
entity.setProperties(properties);
Vertex entityVertex = entityT.asAdmin().clone().next();
// TODO make use conversion for the types
entity.setRev(getProp(entityVertex, "rev", Integer.class).orElse(-1));
entity.setDeleted(getProp(entityVertex, "deleted", Boolean.class).orElse(false));
entity.setPid(getProp(entityVertex, "pid", String.class).orElse(null));
URI rdfUri = getProp(entityVertex, RDF_URI_PROP, String.class).map(x -> {
try {
return new URI(x);
} catch (URISyntaxException e) {
return null;
}
}).orElse(null);
entity.setRdfUri(rdfUri);
Property<String[]> rdfAlternativesProp = entityVertex.property(RDF_SYNONYM_PROP);
if (rdfAlternativesProp.isPresent()) {
try {
entity.setRdfAlternatives(Lists.newArrayList(rdfAlternativesProp.value()));
} catch (Exception e) {
LOG.error(databaseInvariant, "Error while reading rdfAlternatives", e);
}
}
Optional<String> typesOptional = getProp(entityVertex, "types", String.class);
if (typesOptional.isPresent()) {
try {
List<String> types = new ObjectMapper().readValue(typesOptional.get(), new TypeReference<List<String>>() {
});
entity.setTypes(types);
} catch (Exception e) {
LOG.error(databaseInvariant, "Error while generating variation refs", e);
entity.setTypes(Lists.newArrayList(entityTypeName));
}
} else {
entity.setTypes(Lists.newArrayList(entityTypeName));
}
Optional<String> modifiedStringOptional = getProp(entityVertex, "modified", String.class);
if (modifiedStringOptional.isPresent()) {
try {
entity.setModified(new ObjectMapper().readValue(modifiedStringOptional.get(), Change.class));
} catch (IOException e) {
LOG.error(databaseInvariant, "Change cannot be converted", e);
entity.setModified(new Change());
}
} else {
entity.setModified(new Change());
}
Optional<String> createdStringOptional = getProp(entityVertex, "created", String.class);
if (createdStringOptional.isPresent()) {
try {
entity.setCreated(new ObjectMapper().readValue(createdStringOptional.get(), Change.class));
} catch (IOException e) {
LOG.error(databaseInvariant, "Change cannot be converted", e);
entity.setCreated(new Change());
}
} else {
entity.setCreated(new Change());
}
entity.setDisplayName(DisplayNameHelper.getDisplayname(traversalSource, entityVertex, collection).orElse(""));
entity.setId(getIdOrDefault(entityVertex));
if (withRelations) {
entity.setRelations(getRelations(entityVertex, traversalSource, collection));
}
customEntityProperties.execute(entity, entityVertex);
return entity;
}
use of nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl in project timbuctoo by HuygensING.
the class EntityToJsonMapperTest method mapEntityDoesNotAddADisplayPropertyWhenTheDisplayNameIsNull.
@Test
public void mapEntityDoesNotAddADisplayPropertyWhenTheDisplayNameIsNull() {
ReadEntityImpl readEntity = new ReadEntityImpl();
UUID id = UUID.randomUUID();
readEntity.setId(id);
String userId = "userId";
Change change = new Change(Instant.now().toEpochMilli(), userId, null);
readEntity.setCreated(change);
readEntity.setModified(change);
String type = "otherType";
readEntity.setTypes(Lists.newArrayList("type", type));
readEntity.setDeleted(false);
readEntity.setRev(1);
readEntity.setPid("pid");
readEntity.setProperties(Lists.newArrayList());
readEntity.setRelations(Lists.newArrayList());
Collection collection = mock(Collection.class);
when(collection.getEntityTypeName()).thenReturn(type);
ObjectNode resutlJson = instance.mapEntity(collection, readEntity, true, (readEntity1, resultJson) -> {
}, (relationRef, resultJson) -> {
});
assertThat(resutlJson.has("@displayName"), is(false));
}
Aggregations