Search in sources :

Example 1 with TinkerPopPropertyConverter

use of nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter in project timbuctoo by HuygensING.

the class TinkerPopPropertyConverterTest method fromThrowsAnUnknownPropertyExceptionWhenThePropertyDoesNotExistInTheCollection.

@Test(expected = UnknownPropertyException.class)
public void fromThrowsAnUnknownPropertyExceptionWhenThePropertyDoesNotExistInTheCollection() throws UnknownPropertyException, IOException {
    Collection collection = mock(Collection.class);
    when(collection.getProperty(PROPERTY_NAME)).thenReturn(Optional.empty());
    TinkerPopPropertyConverter instance = new TinkerPopPropertyConverter(collection);
    instance.from(PROPERTY_NAME, STRING_VALUE);
}
Also used : TinkerPopPropertyConverter(nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Test(org.junit.Test)

Example 2 with TinkerPopPropertyConverter

use of nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter in project timbuctoo by HuygensING.

the class TinkerPopPropertyConverterTest method toReturnsAJsonEncodedStringForADatableProperty.

@Test
public void toReturnsAJsonEncodedStringForADatableProperty() throws Exception {
    TinkerPopPropertyConverter instance = new TinkerPopPropertyConverter(null);
    DatableProperty property = new DatableProperty(PROPERTY_NAME, "1800");
    Tuple<String, Object> value = instance.to(property);
    assertThat(value.getRight(), is("\"1800\""));
}
Also used : TinkerPopPropertyConverter(nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter) DatableProperty(nl.knaw.huygens.timbuctoo.core.dto.property.DatableProperty) Test(org.junit.Test)

Example 3 with TinkerPopPropertyConverter

use of nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter in project timbuctoo by HuygensING.

the class TinkerPopPropertyConverterTest method fromReturnsADatablePropertyWithADecodedStringValue.

// Datable tests
@Test
public void fromReturnsADatablePropertyWithADecodedStringValue() throws Exception {
    Collection collection = mock(Collection.class);
    ReadableProperty readableProperty = mock(ReadableProperty.class);
    when(readableProperty.getUniqueTypeId()).thenReturn("datable");
    when(collection.getProperty(PROPERTY_NAME)).thenReturn(Optional.of(readableProperty));
    TinkerPopPropertyConverter instance = new TinkerPopPropertyConverter(collection);
    TimProperty<?> from = instance.from(PROPERTY_NAME, "\"1800\"");
    assertThat(from.getValue(), is("1800"));
}
Also used : ReadableProperty(nl.knaw.huygens.timbuctoo.model.properties.ReadableProperty) TinkerPopPropertyConverter(nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Test(org.junit.Test)

Example 4 with TinkerPopPropertyConverter

use of nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter in project timbuctoo by HuygensING.

the class TinkerPopOperations method createEntity.

@Override
public void createEntity(Collection col, Optional<Collection> baseCollection, CreateEntity input) throws IOException {
    requireCommit = true;
    Map<String, LocalProperty> mapping = col.getWriteableProperties();
    TinkerPopPropertyConverter colConverter = new TinkerPopPropertyConverter(col);
    Map<String, LocalProperty> baseMapping = baseCollection.isPresent() ? baseCollection.get().getWriteableProperties() : Maps.newHashMap();
    TinkerPopPropertyConverter baseColConverter = // converter not needed without mapping
    baseCollection.isPresent() ? new TinkerPopPropertyConverter(baseCollection.get()) : null;
    GraphTraversal<Vertex, Vertex> traversalWithVertex = traversal.addV();
    Vertex vertex = traversalWithVertex.next();
    for (TimProperty<?> property : input.getProperties()) {
        String fieldName = property.getName();
        if (mapping.containsKey(fieldName)) {
            try {
                String dbName = mapping.get(fieldName).getDatabasePropertyName();
                Tuple<String, Object> convertedProp = property.convert(colConverter);
                vertex.property(dbName, convertedProp.getRight());
            } catch (IOException e) {
                throw new IOException(fieldName + " could not be saved. " + e.getMessage(), e);
            }
        } else {
            throw new IOException(String.format("Items of %s have no property %s", col.getCollectionName(), fieldName));
        }
        if (baseMapping.containsKey(fieldName)) {
            try {
                property.convert(baseColConverter);
                Tuple<String, Object> convertedProp = property.convert(baseColConverter);
                baseMapping.get(fieldName).setValue(vertex, convertedProp.getRight());
            } catch (IOException e) {
                LOG.error(configurationFailure, "Field could not be parsed by Admin VRE converter {}_{}", baseCollection.get().getCollectionName(), fieldName);
            }
        }
    }
    setAdministrativeProperties(col, vertex, input);
    Vertex duplicate = duplicateVertex(traversal, vertex, indexHandler);
    listener.onCreate(col, duplicate);
    // not passing oldVertex because old has never been passed to addToCollection so it doesn't need to be removed
    listener.onAddToCollection(col, Optional.empty(), duplicate);
    baseCollection.ifPresent(baseCol -> listener.onAddToCollection(baseCol, Optional.empty(), duplicate));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexDuplicator.duplicateVertex(nl.knaw.huygens.timbuctoo.database.tinkerpop.VertexDuplicator.duplicateVertex) TinkerPopPropertyConverter(nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter) LocalProperty(nl.knaw.huygens.timbuctoo.model.properties.LocalProperty) IOException(java.io.IOException)

Example 5 with TinkerPopPropertyConverter

use of nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter in project timbuctoo by HuygensING.

the class TinkerPopPropertyConverterTest method fromThrowsAnUnknownPropertyExceptionWhenThePropertyHasAnUnknownUniqueTypeId.

@Test(expected = UnknownPropertyException.class)
public void fromThrowsAnUnknownPropertyExceptionWhenThePropertyHasAnUnknownUniqueTypeId() throws UnknownPropertyException, IOException {
    Collection collection = mock(Collection.class);
    ReadableProperty readableProperty = mock(ReadableProperty.class);
    when(readableProperty.getUniqueTypeId()).thenReturn("unknownType");
    when(collection.getProperty(PROPERTY_NAME)).thenReturn(Optional.of(readableProperty));
    TinkerPopPropertyConverter instance = new TinkerPopPropertyConverter(collection);
    instance.from(PROPERTY_NAME, STRING_VALUE);
}
Also used : ReadableProperty(nl.knaw.huygens.timbuctoo.model.properties.ReadableProperty) TinkerPopPropertyConverter(nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Test(org.junit.Test)

Aggregations

TinkerPopPropertyConverter (nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter)11 Test (org.junit.Test)8 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)6 ReadableProperty (nl.knaw.huygens.timbuctoo.model.properties.ReadableProperty)5 IOException (java.io.IOException)3 VertexDuplicator.duplicateVertex (nl.knaw.huygens.timbuctoo.database.tinkerpop.VertexDuplicator.duplicateVertex)3 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)3 TimProperty (nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 URI (java.net.URI)1 Clock (java.time.Clock)1 Instant (java.time.Instant)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1