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

com.tinkerpop.gremlin.structure.strategy.SequenceGraphStrategyTest Maven / Gradle / Ivy

package com.tinkerpop.gremlin.structure.strategy;

import com.tinkerpop.gremlin.AbstractGremlinTest;
import com.tinkerpop.gremlin.process.graph.GraphTraversal;
import com.tinkerpop.gremlin.process.graph.util.DefaultGraphTraversal;
import com.tinkerpop.gremlin.structure.Edge;
import com.tinkerpop.gremlin.structure.Property;
import com.tinkerpop.gremlin.structure.Vertex;
import com.tinkerpop.gremlin.util.function.STriFunction;
import org.junit.Test;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

import static org.junit.Assert.*;

/**
 * @author Stephen Mallette (http://stephen.genoprime.com)
 */
public class SequenceGraphStrategyTest extends AbstractGremlinTest {
    @Test
    public void shouldAppendPropertyValuesInOrderToVertex() {
        final StrategyWrappedGraph swg = new StrategyWrappedGraph(g);
        swg.strategy().setGraphStrategy(new SequenceGraphStrategy(
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        return (f) -> (args) -> {
                            final List o = new ArrayList<>(Arrays.asList(args));
                            o.addAll(Arrays.asList("anonymous", "working1"));
                            return f.apply(o.toArray());
                        };
                    }
                },
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        return (f) -> (args) -> {
                            final List o = new ArrayList<>(Arrays.asList(args));
                            o.addAll(Arrays.asList("anonymous", "working2"));
                            o.addAll(Arrays.asList("try", "anything"));
                            return f.apply(o.toArray());
                        };
                    }
                },
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        return UnaryOperator.identity();
                    }
                },
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        return (f) -> (args) -> {
                            final List o = new ArrayList<>(Arrays.asList(args));
                            o.addAll(Arrays.asList("anonymous", "working3"));
                            return f.apply(o.toArray());
                        };
                    }
                }
        ));

        final Vertex v = swg.addVertex("any", "thing");

        assertNotNull(v);
        assertEquals("thing", v.property("any").value());
        assertEquals("working3", v.property("anonymous").value());
        assertEquals("anything", v.property("try").value());
    }

    @Test(expected = RuntimeException.class)
    public void shouldShortCircuitStrategyWithException() {
        final StrategyWrappedGraph swg = new StrategyWrappedGraph(g);
        swg.strategy().setGraphStrategy(new SequenceGraphStrategy(
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        return (f) -> (args) -> {
                            final List o = new ArrayList<>(Arrays.asList(args));
                            o.addAll(Arrays.asList("anonymous", "working1"));
                            return f.apply(o.toArray());
                        };
                    }
                },
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        return (f) -> (args) -> {
                            throw new RuntimeException("test");
                        };
                    }
                },
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        return (f) -> (args) -> {
                            final List o = new ArrayList<>(Arrays.asList(args));
                            o.addAll(Arrays.asList("anonymous", "working3"));
                            return f.apply(o.toArray());
                        };
                    }
                }
        ));

        swg.addVertex("any", "thing");
    }

    @Test
    public void shouldShortCircuitStrategyWithNoOp() {
        final StrategyWrappedGraph swg = new StrategyWrappedGraph(g);
        swg.strategy().setGraphStrategy(new SequenceGraphStrategy(
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        return (f) -> (args) -> {
                            final List o = new ArrayList<>(Arrays.asList(args));
                            o.addAll(Arrays.asList("anonymous", "working1"));
                            return f.apply(o.toArray());
                        };
                    }
                },
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        // if "f" is not applied then the next step and following steps won't process
                        return (f) -> (args) -> null;
                    }
                },
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        return (f) -> (args) -> {
                            final List o = new ArrayList<>(Arrays.asList(args));
                            o.addAll(Arrays.asList("anonymous", "working3"));
                            return f.apply(o.toArray());
                        };
                    }
                }
        ));

        assertNull(swg.addVertex("any", "thing"));
    }

    @Test
    public void shouldDoSomethingBeforeAndAfter() {
        final StrategyWrappedGraph swg = new StrategyWrappedGraph(g);
        swg.strategy().setGraphStrategy(new SequenceGraphStrategy(
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        return (f) -> (args) -> {
                            final List o = new ArrayList<>(Arrays.asList(args));
                            o.addAll(Arrays.asList("anonymous", "working1"));
                            return f.apply(o.toArray());
                        };
                    }
                },
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        return (f) -> (args) -> {
                            final Vertex v = f.apply(args);

                            // this  means that the next strategy and those below it executed including
                            // the implementation
                            assertEquals("working3", v.property("anonymous").value());

                            // now do something with that vertex after the fact
                            v.property("anonymous", "working2");

                            return v;
                        };
                    }
                },
                new GraphStrategy() {
                    @Override
                    public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
                        return (f) -> (args) -> {
                            final List o = new ArrayList<>(Arrays.asList(args));
                            o.addAll(Arrays.asList("anonymous", "working3"));
                            return f.apply(o.toArray());
                        };
                    }
                }
        ));

        final Vertex v = swg.addVertex("any", "thing");

        assertNotNull(v);
        assertEquals("thing", v.property("any").value());
        assertEquals("working2", v.property("anonymous").value());
    }

    @Test
    public void shouldHaveAllMethodsImplemented() throws Exception {
        final Method[] methods = GraphStrategy.class.getDeclaredMethods();
        final SpyGraphStrategy spy = new SpyGraphStrategy();
        final SequenceGraphStrategy strategy = new SequenceGraphStrategy(spy);

        // invoke all the strategy methods
        Stream.of(methods).forEach(method -> {
            try {
                if (method.getName().equals("applyStrategyToTraversal"))
                    method.invoke(strategy, new DefaultGraphTraversal<>());
                else
                    method.invoke(strategy, new Strategy.Context(g, new StrategyWrapped() {
                    }));

            } catch (Exception ex) {
                ex.printStackTrace();
                fail("Should be able to invoke function");
                throw new RuntimeException("fail");
            }
        });

        // check the spy to see that all methods were executed
        assertEquals(methods.length, spy.getCount());
    }

    @Test
    public void shouldGenerateToStringProperty() throws Exception {
        final ReadOnlyGraphStrategy readonly = new ReadOnlyGraphStrategy();
        final IdGraphStrategy id = new IdGraphStrategy.Builder("key").build();
        final SequenceGraphStrategy strategy = new SequenceGraphStrategy(readonly, id);
        assertEquals("readonlygraphstrategy->idgraphstrategy[key]", strategy.toString());
    }

    public class SpyGraphStrategy implements GraphStrategy {

        private int count = 0;

        public int getCount() {
            return count;
        }

        private UnaryOperator spy() {
            count++;
            return UnaryOperator.identity();
        }

        @Override
        public UnaryOperator> getAddVertexStrategy(final Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator> getAddEdgeStrategy(final Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator> getRemoveElementStrategy(final Strategy.Context ctx) {
            return spy();
        }

        @Override
        public  UnaryOperator> getRemovePropertyStrategy(final Strategy.Context> ctx) {
            return spy();
        }

        @Override
        public  UnaryOperator>> getElementGetProperty(final Strategy.Context ctx) {
            return spy();
        }

        @Override
        public  UnaryOperator>> getElementProperty(final Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator> getElementId(final Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator> getElementLabel(final Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator>> getElementKeys(final Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator>> getElementHiddenKeys(final Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator> getGraphvStrategy(final Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator> getGrapheStrategy(final Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator> getElementPropertiesSetter(final Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator>> getElementPropertiesGetter(Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator>> getElementValues(Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator>> getElementHiddenValues(Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator>> getElementHiddens(Strategy.Context ctx) {
            return spy();
        }

        @Override
        public  UnaryOperator> getElementValue(Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator>> getVariableKeysStrategy(Strategy.Context ctx) {
            return spy();
        }

        @Override
        public  UnaryOperator>> getVariableGetStrategy(Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator> getVariableSetStrategy(Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator> getVariableRemoveStrategy(Strategy.Context ctx) {
            return spy();
        }

        @Override
        public UnaryOperator>> getVariableAsMapStrategy(Strategy.Context ctx) {
            return spy();
        }

        @Override
        public GraphTraversal applyStrategyToTraversal(final GraphTraversal traversal) {
            spy();
            return traversal;
        }
    }
}