use of nl.knaw.huygens.timbuctoo.core.dto.UpdateEntity in project timbuctoo by HuygensING.
the class TinkerPopOperationsTest method replaceEntityUpdatesTheKnownProperties.
@Test
public void replaceEntityUpdatesTheKnownProperties() throws Exception {
Vres vres = createConfiguration();
Collection collection = vres.getCollection("testthings").get();
UUID id = UUID.randomUUID();
TinkerPopGraphManager graphManager = newGraph().withVertex(v -> v.withTimId(id).withVre("test").withType("thing").withProperty("testthing_prop1", "oldValue").withProperty("isLatest", true).withProperty("rev", 1)).wrap();
TinkerPopOperations instance = forGraphWrapperAndMappings(graphManager, vres);
ArrayList<TimProperty<?>> properties = Lists.newArrayList(new StringProperty("prop1", "newValue"), new StringProperty("prop2", "prop2Value"));
UpdateEntity updateEntity = new UpdateEntity(id, properties, 1);
long timeStamp = Instant.now().toEpochMilli();
updateEntity.setModified(new Change(timeStamp, "userId", null));
instance.replaceEntity(collection, updateEntity);
Vertex vertex = graphManager.getGraph().traversal().V().has("tim_id", id.toString()).has("isLatest", true).next();
assertThat(vertex, is(likeVertex().withProperty("testthing_prop1", "newValue").withProperty("testthing_prop2", "prop2Value")));
}
use of nl.knaw.huygens.timbuctoo.core.dto.UpdateEntity in project timbuctoo by HuygensING.
the class TinkerPopOperationsTest method replaceEntityRemovesThePropertiesThatAreNotProvided.
@Test
public void replaceEntityRemovesThePropertiesThatAreNotProvided() throws Exception {
Vres vres = createConfiguration();
Collection collection = vres.getCollection("testthings").get();
UUID id = UUID.randomUUID();
TinkerPopGraphManager graphManager = newGraph().withVertex(v -> v.withTimId(id).withVre("test").withType("thing").withProperty("testthing_prop1", "oldValue1").withProperty("testthing_prop2", "oldValue2").withProperty("isLatest", true).withProperty("rev", 1)).wrap();
TinkerPopOperations instance = forGraphWrapperAndMappings(graphManager, vres);
ArrayList<TimProperty<?>> properties = Lists.newArrayList(new StringProperty("prop1", "newValue"));
UpdateEntity updateEntity = new UpdateEntity(id, properties, 1);
long timeStamp = Instant.now().toEpochMilli();
updateEntity.setModified(new Change(timeStamp, "userId", null));
instance.replaceEntity(collection, updateEntity);
Vertex vertex = graphManager.getGraph().traversal().V().has("tim_id", id.toString()).has("isLatest", true).next();
assertThat(vertex, is(likeVertex().withoutProperty("testthing_prop2")));
}
use of nl.knaw.huygens.timbuctoo.core.dto.UpdateEntity in project timbuctoo by HuygensING.
the class ChangeListenerTest method callsOnAddToCollectionOnReplaceEntityWithNewCollection.
@Test
public void callsOnAddToCollectionOnReplaceEntityWithNewCollection() throws Exception {
ChangeListener changeListener = new ChangeListenerImpl(vertex -> {
assertThat(vertex, likeVertex().withProperty("isLatest", true).withProperty("rev", 2));
Long prevVersions = stream(vertex.vertices(Direction.BOTH, VERSION_OF)).collect(Collectors.counting());
assertThat(prevVersions, is(1L));
});
UUID id = UUID.randomUUID();
ChangeListener spy = spy(changeListener);
DataStoreOperations instance = forReplaceCall(spy, id, 1);
UpdateEntity updateEntity = new UpdateEntity(id, newArrayList(), 1);
updateEntity.setModified(new Change());
Collection collectionMock = mock(Collection.class);
given(collectionMock.getEntityTypeName()).willReturn("something");
instance.replaceEntity(collectionMock, updateEntity);
verify(spy).onAddToCollection(same(collectionMock), any(), any());
}
use of nl.knaw.huygens.timbuctoo.core.dto.UpdateEntity in project timbuctoo by HuygensING.
the class JsonToEntityMapperTest method newUpdateEntityIgnoresThePrefixedFields.
@Test
public void newUpdateEntityIgnoresThePrefixedFields() throws Exception {
Collection collection = new VresBuilder().withVre("WomenWriters", "ww", vre -> vre.withCollection("wwpersons")).build().getCollection("wwpersons").get();
ObjectNode input = JsonBuilder.jsnO("_id", jsn("id"), "^rev", jsn(1), "@type", jsn("wwperson"));
JsonToEntityMapper instance = new JsonToEntityMapper();
UpdateEntity updateEntity = instance.newUpdateEntity(collection, UUID.randomUUID(), input);
assertThat(updateEntity.getProperties(), not(containsInAnyOrder(hasProperty("name", equalTo("_id")), hasProperty("name", equalTo("^rev")), hasProperty("name", equalTo("@type")))));
}
use of nl.knaw.huygens.timbuctoo.core.dto.UpdateEntity in project timbuctoo by HuygensING.
the class TinkerPopOperations method replaceEntity.
@Override
public int replaceEntity(Collection collection, UpdateEntity updateEntity) throws NotFoundException, AlreadyUpdatedException, IOException {
requireCommit = true;
GraphTraversal<Vertex, Vertex> entityTraversal = entityFetcher.getEntity(this.traversal, updateEntity.getId(), null, collection.getCollectionName());
if (!entityTraversal.hasNext()) {
throw new NotFoundException();
}
Vertex entityVertex = entityTraversal.next();
int curRev = getProp(entityVertex, "rev", Integer.class).orElse(1);
if (curRev != updateEntity.getRev()) {
throw new AlreadyUpdatedException();
}
int newRev = updateEntity.getRev() + 1;
entityVertex.property("rev", newRev);
// update properties
TinkerPopPropertyConverter tinkerPopPropertyConverter = new TinkerPopPropertyConverter(collection);
for (TimProperty<?> property : updateEntity.getProperties()) {
try {
Tuple<String, Object> nameValue = property.convert(tinkerPopPropertyConverter);
collection.getWriteableProperties().get(nameValue.getLeft()).setValue(entityVertex, nameValue.getRight());
} catch (IOException e) {
throw new IOException(property.getName() + " could not be saved. " + e.getMessage(), e);
}
}
// Set removed values to null.
Set<String> propertyNames = updateEntity.getProperties().stream().map(prop -> prop.getName()).collect(Collectors.toSet());
for (String name : Sets.difference(collection.getWriteableProperties().keySet(), propertyNames)) {
collection.getWriteableProperties().get(name).setJson(entityVertex, null);
}
String entityTypesStr = getProp(entityVertex, "types", String.class).orElse("[]");
boolean wasAddedToCollection = false;
if (!entityTypesStr.contains("\"" + collection.getEntityTypeName() + "\"")) {
try {
ArrayNode entityTypes = arrayToEncodedArray.tinkerpopToJson(entityTypesStr);
entityTypes.add(collection.getEntityTypeName());
entityVertex.property("types", entityTypes.toString());
wasAddedToCollection = true;
} catch (IOException e) {
// FIXME potential bug?
LOG.error(Logmarkers.databaseInvariant, "property 'types' was not parseable: " + entityTypesStr);
}
}
setModified(entityVertex, updateEntity.getModified());
entityVertex.property("pid").remove();
Vertex duplicate = duplicateVertex(traversal, entityVertex, indexHandler);
listener.onPropertyUpdate(collection, Optional.of(entityVertex), duplicate);
if (wasAddedToCollection) {
listener.onAddToCollection(collection, Optional.of(entityVertex), duplicate);
}
return newRev;
}
Aggregations