All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.tinkerpop.gremlin.structure.io.IoPropertyTest Maven / Gradle / Ivy

There is a newer version: 3.7.3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.tinkerpop.gremlin.structure.io;

import org.apache.tinkerpop.gremlin.AbstractGremlinTest;
import org.apache.tinkerpop.gremlin.LoadGraphWith;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * @author Stephen Mallette (http://stephen.genoprime.com)
 */
@RunWith(Parameterized.class)
public class IoPropertyTest extends AbstractGremlinTest {

    @Parameterized.Parameters(name = "{0}")
    public static Iterable data() {
        return Arrays.asList(new Object[][]{
                {"graphson", false, false,
                        (Function) g -> g.io(IoCore.graphson()).reader().create(),
                        (Function) g -> g.io(IoCore.graphson()).writer().create()},
                {"graphson-embedded", true, true,
                        (Function) g -> g.io(IoCore.graphson()).reader().mapper(g.io(IoCore.graphson()).mapper().embedTypes(true).create()).create(),
                        (Function) g -> g.io(IoCore.graphson()).writer().mapper(g.io(IoCore.graphson()).mapper().embedTypes(true).create()).create()},
                {"gryo", true, true,
                        (Function) g -> g.io(IoCore.gryo()).reader().create(),
                        (Function) g -> g.io(IoCore.gryo()).writer().create()}
        });
    }

    @Parameterized.Parameter(value = 0)
    public String ioType;

    @Parameterized.Parameter(value = 1)
    public boolean assertIdDirectly;

    @Parameterized.Parameter(value = 2)
    public boolean assertDouble;

    @Parameterized.Parameter(value = 3)
    public Function readerMaker;

    @Parameterized.Parameter(value = 4)
    public Function writerMaker;

    @Test
    @LoadGraphWith(LoadGraphWith.GraphData.CREW)
    public void shouldReadWriteVertexPropertyWithMetaProperties() throws Exception {
        try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            final GraphWriter writer = writerMaker.apply(graph);

            // select any vertexproperty that has both start/end time
            final VertexProperty p = (VertexProperty) g.V(convertToVertexId("marko")).properties("location").as("p").has("endTime").select("p").next();
            writer.writeVertexProperty(os, p);

            final AtomicBoolean called = new AtomicBoolean(false);
            final GraphReader reader = readerMaker.apply(graph);
            try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) {
                reader.readVertexProperty(bais, propertyAttachable -> {
                    assertEquals(p.value(), propertyAttachable.get().value());
                    assertEquals(p.key(), propertyAttachable.get().key());
                    assertEquals(IteratorUtils.count(p.properties()), IteratorUtils.count(propertyAttachable.get().properties()));
                    assertEquals(p.property("startTime").value(), ((Property) propertyAttachable.get().properties("startTime").next()).value());
                    assertEquals(p.property("endTime").value(), ((Property) propertyAttachable.get().properties("endTime").next()).value());
                    called.set(true);
                    return propertyAttachable.get();
                });
            }

            assertTrue(called.get());
        }
    }

    @Test
    @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
    public void shouldReadWriteVertexPropertyNoMetaProperties() throws Exception {
        try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            final GraphWriter writer = writerMaker.apply(graph);
            final VertexProperty p = g.V(convertToVertexId("marko")).next().property("name");
            writer.writeVertexProperty(os, p);

            final AtomicBoolean called = new AtomicBoolean(false);
            final GraphReader reader = readerMaker.apply(graph);
            try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) {
                reader.readVertexProperty(bais, propertyAttachable -> {
                    assertEquals(p.value(), propertyAttachable.get().value());
                    assertEquals(p.key(), propertyAttachable.get().key());
                    assertEquals(0, IteratorUtils.count(propertyAttachable.get().properties()));
                    called.set(true);
                    return propertyAttachable.get();
                });
            }

            assertTrue(called.get());
        }
    }

    @Test
    @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
    public void shouldReadWritePropertyGraphSON() throws Exception {
        try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            final GraphWriter writer = writerMaker.apply(graph);
            final Property p = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight");
            writer.writeProperty(os, p);

            final AtomicBoolean called = new AtomicBoolean(false);
            final GraphReader reader = readerMaker.apply(graph);
            try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) {
                reader.readProperty(bais, propertyAttachable -> {
                    assertEquals(p.value(), propertyAttachable.get().value());
                    assertEquals(p.key(), propertyAttachable.get().key());
                    called.set(true);
                    return propertyAttachable.get();
                });
            }

            assertTrue(called.get());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy