Merge branch 'tp32' into tp33 Conflicts: gremlin-python/src/main/jython/radish/feature_steps.py
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/92e899f4 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/92e899f4 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/92e899f4 Branch: refs/heads/tp33 Commit: 92e899f4ceb6c5aa7e614087bca633d46f2fa61d Parents: 9eed217 2350cbe Author: Stephen Mallette <sp...@genoprime.com> Authored: Thu Mar 1 12:52:27 2018 -0500 Committer: Stephen Mallette <sp...@genoprime.com> Committed: Thu Mar 1 12:52:27 2018 -0500 ---------------------------------------------------------------------- CHANGELOG.asciidoc | 1 + .../gremlin/structure/io/graphson/GraphSONTokens.java | 1 + .../structure/io/graphson/TraversalSerializersV2d0.java | 2 ++ .../structure/io/graphson/TraversalSerializersV3d0.java | 2 ++ .../ser/GraphSONMessageSerializerGremlinV2d0Test.java | 11 +++++++++++ .../driver/ser/GraphSONMessageSerializerV3d0Test.java | 11 +++++++++++ .../gremlin-javascript/test/cucumber/feature-steps.js | 4 ---- gremlin-python/src/main/jython/radish/feature_steps.py | 5 ----- 8 files changed, 28 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/92e899f4/CHANGELOG.asciidoc ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/92e899f4/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTokens.java ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/92e899f4/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/TraversalSerializersV3d0.java ---------------------------------------------------------------------- diff --cc gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/TraversalSerializersV3d0.java index c629b3f,0000000..fd11f25 mode 100644,000000..100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/TraversalSerializersV3d0.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/TraversalSerializersV3d0.java @@@ -1,472 -1,0 +1,474 @@@ +/* + * 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.graphson; + +import org.apache.commons.configuration.ConfigurationConverter; +import org.apache.commons.configuration.MapConfiguration; +import org.apache.tinkerpop.gremlin.process.remote.traversal.DefaultRemoteTraverser; +import org.apache.tinkerpop.gremlin.process.traversal.Bytecode; +import org.apache.tinkerpop.gremlin.process.traversal.P; +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; +import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy; +import org.apache.tinkerpop.gremlin.process.traversal.Traverser; +import org.apache.tinkerpop.gremlin.process.traversal.strategy.TraversalStrategyProxy; +import org.apache.tinkerpop.gremlin.process.traversal.util.AndP; +import org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP; +import org.apache.tinkerpop.gremlin.process.traversal.util.OrP; +import org.apache.tinkerpop.gremlin.util.function.Lambda; +import org.apache.tinkerpop.shaded.jackson.core.JsonGenerator; +import org.apache.tinkerpop.shaded.jackson.core.JsonParser; +import org.apache.tinkerpop.shaded.jackson.core.JsonProcessingException; +import org.apache.tinkerpop.shaded.jackson.core.JsonToken; +import org.apache.tinkerpop.shaded.jackson.databind.DeserializationContext; +import org.apache.tinkerpop.shaded.jackson.databind.JavaType; +import org.apache.tinkerpop.shaded.jackson.databind.SerializerProvider; +import org.apache.tinkerpop.shaded.jackson.databind.deser.std.StdDeserializer; +import org.apache.tinkerpop.shaded.jackson.databind.jsontype.TypeSerializer; +import org.apache.tinkerpop.shaded.jackson.databind.ser.std.StdScalarSerializer; +import org.apache.tinkerpop.shaded.jackson.databind.ser.std.StdSerializer; +import org.apache.tinkerpop.shaded.jackson.databind.type.TypeFactory; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * @author Marko A. Rodriguez (http://markorodriguez.com) + */ +final class TraversalSerializersV3d0 { + + private TraversalSerializersV3d0() { + } + + ///////////////// + // SERIALIZERS // + //////////////// + + final static class TraversalJacksonSerializer extends StdSerializer<Traversal> { + + public TraversalJacksonSerializer() { + super(Traversal.class); + } + + @Override + public void serialize(final Traversal traversal, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) + throws IOException { + jsonGenerator.writeObject(traversal.asAdmin().getBytecode()); + } + + @Override + public void serializeWithType(final Traversal traversal, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) + throws IOException { + serialize(traversal, jsonGenerator, serializerProvider); + } + + } + + final static class BytecodeJacksonSerializer extends StdScalarSerializer<Bytecode> { + + public BytecodeJacksonSerializer() { + super(Bytecode.class); + } + + @Override + public void serialize(final Bytecode bytecode, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) + throws IOException { + jsonGenerator.writeStartObject(); + if (bytecode.getSourceInstructions().iterator().hasNext()) { + jsonGenerator.writeArrayFieldStart(GraphSONTokens.SOURCE); + for (final Bytecode.Instruction instruction : bytecode.getSourceInstructions()) { + jsonGenerator.writeStartArray(); + jsonGenerator.writeString(instruction.getOperator()); + for (final Object argument : instruction.getArguments()) { + jsonGenerator.writeObject(argument); + } + jsonGenerator.writeEndArray(); + } + jsonGenerator.writeEndArray(); + } + if (bytecode.getStepInstructions().iterator().hasNext()) { + jsonGenerator.writeArrayFieldStart(GraphSONTokens.STEP); + for (final Bytecode.Instruction instruction : bytecode.getStepInstructions()) { + jsonGenerator.writeStartArray(); + jsonGenerator.writeString(instruction.getOperator()); + for (final Object argument : instruction.getArguments()) { + jsonGenerator.writeObject(argument); + } + jsonGenerator.writeEndArray(); + } + jsonGenerator.writeEndArray(); + } + + jsonGenerator.writeEndObject(); + } + } + + static class EnumJacksonSerializer extends StdScalarSerializer<Enum> { + + public EnumJacksonSerializer() { + super(Enum.class); + } + + @Override + public void serialize(final Enum enumInstance, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) + throws IOException { + jsonGenerator.writeString(enumInstance.name()); + } + + } + + final static class PJacksonSerializer extends StdScalarSerializer<P> { + + public PJacksonSerializer() { + super(P.class); + } + + @Override + public void serialize(final P p, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) + throws IOException { + jsonGenerator.writeStartObject(); + jsonGenerator.writeStringField(GraphSONTokens.PREDICATE, + p instanceof ConnectiveP ? + p instanceof AndP ? + GraphSONTokens.AND : + GraphSONTokens.OR : + p.getBiPredicate().toString()); + if (p instanceof ConnectiveP) { + jsonGenerator.writeArrayFieldStart(GraphSONTokens.VALUE); + for (final P<?> predicate : ((ConnectiveP<?>) p).getPredicates()) { + jsonGenerator.writeObject(predicate); + } + jsonGenerator.writeEndArray(); + } else + jsonGenerator.writeObjectField(GraphSONTokens.VALUE, p.getValue()); + + jsonGenerator.writeEndObject(); + } + + } + + final static class LambdaJacksonSerializer extends StdScalarSerializer<Lambda> { + + public LambdaJacksonSerializer() { + super(Lambda.class); + } + + @Override + public void serialize(final Lambda lambda, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) + throws IOException { + jsonGenerator.writeStartObject(); + jsonGenerator.writeStringField(GraphSONTokens.SCRIPT, lambda.getLambdaScript()); + jsonGenerator.writeStringField(GraphSONTokens.LANGUAGE, lambda.getLambdaLanguage()); + jsonGenerator.writeNumberField(GraphSONTokens.ARGUMENTS, lambda.getLambdaArguments()); + jsonGenerator.writeEndObject(); + } + + } + + final static class BindingJacksonSerializer extends StdScalarSerializer<Bytecode.Binding> { + + public BindingJacksonSerializer() { + super(Bytecode.Binding.class); + } + + @Override + public void serialize(final Bytecode.Binding binding, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) + throws IOException { + jsonGenerator.writeStartObject(); + jsonGenerator.writeStringField(GraphSONTokens.KEY, binding.variable()); + jsonGenerator.writeObjectField(GraphSONTokens.VALUE, binding.value()); + jsonGenerator.writeEndObject(); + } + + } + + final static class TraverserJacksonSerializer extends StdScalarSerializer<Traverser> { + + public TraverserJacksonSerializer() { + super(Traverser.class); + } + + @Override + public void serialize(final Traverser traverserInstance, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) + throws IOException { + jsonGenerator.writeStartObject(); + jsonGenerator.writeObjectField(GraphSONTokens.BULK, traverserInstance.bulk()); + jsonGenerator.writeObjectField(GraphSONTokens.VALUE, traverserInstance.get()); + jsonGenerator.writeEndObject(); + } + } + + final static class TraversalStrategyJacksonSerializer extends StdScalarSerializer<TraversalStrategy> { + + public TraversalStrategyJacksonSerializer() { + super(TraversalStrategy.class); + } + + @Override + public void serialize(final TraversalStrategy traversalStrategy, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) + throws IOException { + jsonGenerator.writeStartObject(); + for (final Map.Entry<Object, Object> entry : ConfigurationConverter.getMap(traversalStrategy.getConfiguration()).entrySet()) { + jsonGenerator.writeObjectField((String) entry.getKey(), entry.getValue()); + } + jsonGenerator.writeEndObject(); + } + } + + /////////////////// + // DESERIALIZERS // + ////////////////// + + final static class BytecodeJacksonDeserializer extends StdDeserializer<Bytecode> { + private static final JavaType listJavaType = TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Object.class); + private static final JavaType listListJavaType = TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, listJavaType); + + public BytecodeJacksonDeserializer() { + super(Bytecode.class); + } + + @Override + public Bytecode deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + final Bytecode bytecode = new Bytecode(); + + while (jsonParser.nextToken() != JsonToken.END_OBJECT) { + if (jsonParser.getCurrentName().equals(GraphSONTokens.SOURCE)) { + jsonParser.nextToken(); + final List<List<Object>> instructions = deserializationContext.readValue(jsonParser, listListJavaType); + for (final List<Object> instruction : instructions) { + bytecode.addSource((String) instruction.get(0), Arrays.copyOfRange(instruction.toArray(), 1, instruction.size())); + } + } else if (jsonParser.getCurrentName().equals(GraphSONTokens.STEP)) { + jsonParser.nextToken(); + final List<List<Object>> instructions = deserializationContext.readValue(jsonParser, listListJavaType); + for (final List<Object> instruction : instructions) { + bytecode.addStep((String) instruction.get(0), Arrays.copyOfRange(instruction.toArray(), 1, instruction.size())); + } + } + } + return bytecode; + } + + @Override + public boolean isCachable() { + return true; + } + } + + final static class EnumJacksonDeserializer<A extends Enum> extends StdDeserializer<A> { + + public EnumJacksonDeserializer(final Class<A> enumClass) { + super(enumClass); + } + + @Override + public A deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + final Class<A> enumClass = (Class<A>) this._valueClass; + final String enumName = jsonParser.getText(); + for (final Enum a : enumClass.getEnumConstants()) { + if (a.name().equals(enumName)) + return (A) a; + } + throw new IOException("Unknown enum type: " + enumClass); + } + + @Override + public boolean isCachable() { + return true; + } + } + + final static class PJacksonDeserializer extends StdDeserializer<P> { + + public PJacksonDeserializer() { + super(P.class); + } + + @Override + public P deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + String predicate = null; + Object value = null; + + while (jsonParser.nextToken() != JsonToken.END_OBJECT) { + if (jsonParser.getCurrentName().equals(GraphSONTokens.PREDICATE)) { + jsonParser.nextToken(); + predicate = jsonParser.getText(); + } else if (jsonParser.getCurrentName().equals(GraphSONTokens.VALUE)) { + jsonParser.nextToken(); + value = deserializationContext.readValue(jsonParser, Object.class); + } + } + + if (predicate.equals(GraphSONTokens.AND) || predicate.equals(GraphSONTokens.OR)) { + return predicate.equals(GraphSONTokens.AND) ? new AndP((List<P>) value) : new OrP((List<P>) value); ++ } else if (predicate.equals(GraphSONTokens.NOT) && value instanceof P) { ++ return P.not((P<?>) value); + } else { + try { + if (value instanceof Collection) { + if (predicate.equals("between")) + return P.between(((List) value).get(0), ((List) value).get(1)); + else if (predicate.equals("inside")) + return P.between(((List) value).get(0), ((List) value).get(1)); + else if (predicate.equals("outside")) + return P.outside(((List) value).get(0), ((List) value).get(1)); + else if (predicate.equals("within")) + return P.within((Collection) value); + else if (predicate.equals("without")) + return P.without((Collection) value); + else + return (P) P.class.getMethod(predicate, Collection.class).invoke(null, (Collection) value); + } else { + try { + return (P) P.class.getMethod(predicate, Object.class).invoke(null, value); + } catch (final NoSuchMethodException e) { + return (P) P.class.getMethod(predicate, Object[].class).invoke(null, (Object) new Object[]{value}); + } + } + } catch (final Exception e) { + throw new IllegalStateException(e.getMessage(), e); + } + } + } + + @Override + public boolean isCachable() { + return true; + } + } + + final static class LambdaJacksonDeserializer extends StdDeserializer<Lambda> { + + public LambdaJacksonDeserializer() { + super(Lambda.class); + } + + @Override + public Lambda deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + String script = null; + String language = null; + int arguments = -1; + + while (jsonParser.nextToken() != JsonToken.END_OBJECT) { + if (jsonParser.getCurrentName().equals(GraphSONTokens.SCRIPT)) { + jsonParser.nextToken(); + script = jsonParser.getText(); + } else if (jsonParser.getCurrentName().equals(GraphSONTokens.LANGUAGE)) { + jsonParser.nextToken(); + language = jsonParser.getText(); + } else if (jsonParser.getCurrentName().equals(GraphSONTokens.ARGUMENTS)) { + jsonParser.nextToken(); + arguments = jsonParser.getIntValue(); + } + } + + if (-1 == arguments || arguments > 2) + return new Lambda.UnknownArgLambda(script, language, arguments); + else if (0 == arguments) + return new Lambda.ZeroArgLambda<>(script, language); + else if (1 == arguments) + return new Lambda.OneArgLambda<>(script, language); + else + return new Lambda.TwoArgLambda<>(script, language); + } + + @Override + public boolean isCachable() { + return true; + } + } + + final static class BindingJacksonDeserializer extends StdDeserializer<Bytecode.Binding> { + + public BindingJacksonDeserializer() { + super(Bytecode.Binding.class); + } + + @Override + public Bytecode.Binding deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + String k = null; + Object v = null; + + while (jsonParser.nextToken() != JsonToken.END_OBJECT) { + if (jsonParser.getCurrentName().equals(GraphSONTokens.KEY)) { + jsonParser.nextToken(); + k = jsonParser.getText(); + } else if (jsonParser.getCurrentName().equals(GraphSONTokens.VALUE)) { + jsonParser.nextToken(); + v = deserializationContext.readValue(jsonParser, Object.class); + } + } + return new Bytecode.Binding<>(k, v); + } + + @Override + public boolean isCachable() { + return true; + } + } + + static class TraverserJacksonDeserializer extends StdDeserializer<Traverser> { + + public TraverserJacksonDeserializer() { + super(Traverser.class); + } + + @Override + public Traverser deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + long bulk = 1; + Object v = null; + + while (jsonParser.nextToken() != JsonToken.END_OBJECT) { + if (jsonParser.getCurrentName().equals(GraphSONTokens.BULK)) { + jsonParser.nextToken(); + bulk = deserializationContext.readValue(jsonParser, Long.class); + } else if (jsonParser.getCurrentName().equals(GraphSONTokens.VALUE)) { + jsonParser.nextToken(); + v = deserializationContext.readValue(jsonParser, Object.class); + } + } + + return new DefaultRemoteTraverser<>(v, bulk); + } + + @Override + public boolean isCachable() { + return true; + } + } + + final static class TraversalStrategyProxyJacksonDeserializer<T extends TraversalStrategy> extends AbstractObjectDeserializer<TraversalStrategyProxy> { + + private final Class<T> clazz; + + public TraversalStrategyProxyJacksonDeserializer(final Class<T> clazz) { + super(TraversalStrategyProxy.class); + this.clazz = clazz; + } + + @Override + public TraversalStrategyProxy<T> createObject(final Map<String, Object> data) { + return new TraversalStrategyProxy<>(this.clazz, new MapConfiguration(data)); + } + } +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/92e899f4/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV3d0Test.java ---------------------------------------------------------------------- diff --cc gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV3d0Test.java index 4bd3f6f,0000000..b6398f5 mode 100644,000000..100644 --- a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV3d0Test.java +++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV3d0Test.java @@@ -1,340 -1,0 +1,351 @@@ +/* + * 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.driver.ser; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.UnpooledByteBufAllocator; +import org.apache.tinkerpop.gremlin.driver.MessageSerializer; ++import org.apache.tinkerpop.gremlin.driver.message.RequestMessage; +import org.apache.tinkerpop.gremlin.driver.message.ResponseMessage; +import org.apache.tinkerpop.gremlin.driver.message.ResponseStatusCode; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Graph; +import org.apache.tinkerpop.gremlin.structure.Property; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.VertexProperty; +import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory; +import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph; +import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +/** + * Serializer tests that cover non-lossy serialization/deserialization methods. + * + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +public class GraphSONMessageSerializerV3d0Test { + + private final UUID requestId = UUID.fromString("6457272A-4018-4538-B9AE-08DD5DDC0AA1"); + private final ResponseMessage.Builder responseMessageBuilder = ResponseMessage.build(requestId); + private final static ByteBufAllocator allocator = UnpooledByteBufAllocator.DEFAULT; + + public final MessageSerializer serializer = new GraphSONMessageSerializerV3d0(); + + @Test + public void shouldSerializeIterable() throws Exception { + final ArrayList<Integer> list = new ArrayList<>(); + list.add(1); + list.add(100); + + final ResponseMessage response = convert(list); + assertCommon(response); + + final List<Integer> deserializedFunList = (List<Integer>) response.getResult().getData(); + assertEquals(2, deserializedFunList.size()); + assertEquals(new Integer(1), deserializedFunList.get(0)); + assertEquals(new Integer(100), deserializedFunList.get(1)); + } + + @Test + public void shouldSerializeIterableWithNull() throws Exception { + final ArrayList<Integer> list = new ArrayList<>(); + list.add(1); + list.add(null); + list.add(100); + + final ResponseMessage response = convert(list); + assertCommon(response); + + final List<Integer> deserializedFunList = (List<Integer>) response.getResult().getData(); + assertEquals(3, deserializedFunList.size()); + assertEquals(new Integer(1), deserializedFunList.get(0)); + assertNull(deserializedFunList.get(1)); + assertEquals(new Integer(100), deserializedFunList.get(2)); + } + + @Test + public void shouldSerializeMap() throws Exception { + final Map<String, Object> map = new HashMap<>(); + final Map<String, String> innerMap = new HashMap<>(); + innerMap.put("a", "b"); + + map.put("x", 1); + map.put("y", "some"); + map.put("z", innerMap); + + final ResponseMessage response = convert(map); + assertCommon(response); + + final Map<String, Object> deserializedMap = (Map<String, Object>) response.getResult().getData(); + assertEquals(3, deserializedMap.size()); + assertEquals(1, deserializedMap.get("x")); + assertEquals("some", deserializedMap.get("y")); + + final Map<String, String> deserializedInnerMap = (Map<String, String>) deserializedMap.get("z"); + assertEquals(1, deserializedInnerMap.size()); + assertEquals("b", deserializedInnerMap.get("a")); + } + + @Test + public void shouldSerializeMapEntries() throws Exception { + final Graph graph = TinkerGraph.open(); + final Vertex v1 = graph.addVertex(); + final Date d = new Date(); + + final Map<Object, Object> map = new HashMap<>(); + map.put("x", 1); + map.put(v1, 100); + map.put(d, "test"); + + final ResponseMessage response = convert(IteratorUtils.asList(map.entrySet())); + assertCommon(response); + + final List<Map<Object, Object>> deserializedEntries = (List<Map<Object, Object>>) response.getResult().getData(); + assertEquals(3, deserializedEntries.size()); + deserializedEntries.forEach(m -> { + final Map.Entry<Object,Object> e = m.entrySet().iterator().next(); + if (e.getKey().equals("x")) + assertEquals(1, e.getValue()); + else if (e.getKey() instanceof Vertex && e.getKey().equals(v1)) + assertEquals(100, e.getValue()); + else if (e.getKey() instanceof Date) + assertEquals("test", e.getValue()); + else + fail("Map entries contains a key that is not part of what was serialized"); + }); + } + + @Test + public void shouldSerializeEdge() throws Exception { + final Graph graph = TinkerGraph.open(); + final Vertex v1 = graph.addVertex(); + final Vertex v2 = graph.addVertex(); + final Edge e = v1.addEdge("test", v2); + e.property("abc", 123); + + final Iterable<Edge> iterable = IteratorUtils.list(graph.edges()); + + final ResponseMessage response = convert(iterable); + assertCommon(response); + + final List<Edge> edgeList = (List<Edge>) response.getResult().getData(); + assertEquals(1, edgeList.size()); + + final Edge deserializedEdge = edgeList.get(0); + assertEquals(e.id(), deserializedEdge.id()); + assertEquals(v1.id(), deserializedEdge.outVertex().id()); + assertEquals(v2.id(), deserializedEdge.inVertex().id()); + assertEquals(v1.label(), deserializedEdge.outVertex().label()); + assertEquals(v2.label(), deserializedEdge.inVertex().label()); + assertEquals(e.label(), deserializedEdge.label()); + + final List<Property> properties = new ArrayList<>(); + deserializedEdge.properties().forEachRemaining(properties::add); + assertEquals(1, properties.size()); + + assertNotNull(properties); + assertEquals("abc", properties.get(0).key()); + assertEquals(123, properties.get(0).value()); + + } + + @Test + public void shouldSerializeEdgeProperty() throws Exception { + final Graph graph = TinkerGraph.open(); + final Vertex v1 = graph.addVertex(); + final Vertex v2 = graph.addVertex(); + final Edge e = v1.addEdge("test", v2); + e.property("abc", 123); + + final Iterable<Property<Object>> iterable = IteratorUtils.list(e.properties("abc")); + final ResponseMessage response = convert(iterable); + assertCommon(response); + + final List<Property> propertyList = (List<Property>) response.getResult().getData(); + assertEquals(1, propertyList.size()); + assertEquals(123, propertyList.get(0).value()); + } + + @Test + public void shouldSerializeVertexWithEmbeddedMap() throws Exception { + final Graph graph = TinkerGraph.open(); + final Vertex v = graph.addVertex(); + final Map<String, Object> map = new HashMap<>(); + map.put("x", 500); + map.put("y", "some"); + + final ArrayList<Object> friends = new ArrayList<>(); + friends.add("x"); + friends.add(5); + friends.add(map); + + v.property(VertexProperty.Cardinality.single, "friends", friends); + + final List list = IteratorUtils.list(graph.vertices()); + + final ResponseMessage response = convert(list); + assertCommon(response); + + final List<Vertex> vertexList = (List<Vertex>) response.getResult().getData(); + assertEquals(1, vertexList.size()); + + final Vertex deserializedVertex = vertexList.get(0); + assertEquals(v.id(), deserializedVertex.id()); + assertEquals(Vertex.DEFAULT_LABEL, deserializedVertex.label()); + + final List<VertexProperty> properties = new ArrayList<>(); + deserializedVertex.properties().forEachRemaining(properties::add); + assertEquals(1, properties.size()); + + final VertexProperty friendsProperty = properties.get(0); + final List<Object> deserializedInnerList = (List<Object>) friendsProperty.value(); + + assertEquals(3, deserializedInnerList.size()); + assertEquals("x", deserializedInnerList.get(0)); + assertEquals(5, deserializedInnerList.get(1)); + + final Map<String, Object> deserializedInnerInnerMap = (Map<String, Object>) deserializedInnerList.get(2); + assertEquals(2, deserializedInnerInnerMap.size()); + assertEquals(500, deserializedInnerInnerMap.get("x")); + assertEquals("some", deserializedInnerInnerMap.get("y")); + } + + @Test + public void shouldSerializeToJsonMapWithElementForKey() throws Exception { + final TinkerGraph graph = TinkerFactory.createClassic(); + final GraphTraversalSource g = graph.traversal(); + final Map<Vertex, Integer> map = new HashMap<>(); + final Vertex v1 = g.V().has("name", "marko").next(); + map.put(v1, 1000); + + final ResponseMessage response = convert(map); + assertCommon(response); + + final Map<Vertex, Integer> deserializedMap = (Map<Vertex, Integer>) response.getResult().getData(); + assertEquals(1, deserializedMap.size()); + assertEquals(new Integer(1000), deserializedMap.get(v1)); + } + + @Test + @org.junit.Ignore + public void shouldSerializeToTreeJson() throws Exception { + final TinkerGraph graph = TinkerFactory.createClassic(); + final GraphTraversalSource g = graph.traversal(); + final Map t = g.V(1).out().properties("name").tree().next(); + + final ResponseMessage response = convert(t); + assertCommon(response); + + final Tree deserializedTree = (Tree)response.getResult().getData(); + + //check the first object and its key's properties + assertEquals(1, deserializedTree.size()); + final Vertex v = ((Vertex) deserializedTree.keySet().iterator().next()); + assertEquals(1, v.id()); + assertEquals("marko", v.property("name").value()); + + final Tree firstTree = (Tree)deserializedTree.get(v); + assertEquals(3, firstTree.size()); + Iterator<Vertex> vertexKeys = firstTree.keySet().iterator(); + + Tree t2 = (Tree)firstTree.get(vertexKeys.next()); + VertexProperty vp = (VertexProperty)t2.keySet().iterator().next(); + assertEquals(3, vp.id()); + assertEquals("vadas", vp.value()); + + t2 = (Tree) firstTree.get(vertexKeys.next()); + vp = (VertexProperty) t2.keySet().iterator().next(); + assertEquals(5, vp.id()); + assertEquals("lop", vp.value()); + + t2 = (Tree) firstTree.get(vertexKeys.next()); + vp = (VertexProperty) t2.keySet().iterator().next(); + assertEquals(7, vp.id()); + assertEquals("josh", vp.value()); + } + + @Test + public void shouldSerializeFullResponseMessage() throws Exception { + final UUID id = UUID.randomUUID(); + + final Map<String, Object> metaData = new HashMap<>(); + metaData.put("test", "this"); + metaData.put("one", 1); + + final Map<String, Object> attributes = new HashMap<>(); + attributes.put("test", "that"); + attributes.put("two", 2); + + final ResponseMessage response = ResponseMessage.build(id) + .responseMetaData(metaData) + .code(ResponseStatusCode.SUCCESS) + .result("some-result") + .statusAttributes(attributes) + .statusMessage("worked") + .create(); + + final ByteBuf bb = serializer.serializeResponseAsBinary(response, allocator); + final ResponseMessage deserialized = serializer.deserializeResponse(bb); + + assertEquals(id, deserialized.getRequestId()); + assertEquals("this", deserialized.getResult().getMeta().get("test")); + assertEquals(1, deserialized.getResult().getMeta().get("one")); + assertEquals("some-result", deserialized.getResult().getData()); + assertEquals("that", deserialized.getStatus().getAttributes().get("test")); + assertEquals(2, deserialized.getStatus().getAttributes().get("two")); + assertEquals(ResponseStatusCode.SUCCESS.getValue(), deserialized.getStatus().getCode().getValue()); + assertEquals("worked", deserialized.getStatus().getMessage()); + } ++ ++ @Test ++ public void shouldDeserializeNotPredicate() throws Exception { ++ final String requestMessageWithP = "{\"requestId\":{\"@type\":\"g:UUID\",\"@value\":\"0397b9c0-ffab-470e-a6a8-644fc80c01d6\"},\"op\":\"bytecode\",\"processor\":\"traversal\",\"args\":{\"gremlin\":{\"@type\":\"g:Bytecode\",\"@value\":{\"step\":[[\"V\"],[\"hasLabel\",\"person\"],[\"has\",\"age\",{\"@type\":\"g:P\",\"@value\":{\"predicate\":\"not\",\"value\":{\"@type\":\"g:P\",\"@value\":{\"predicate\":\"lte\",\"value\":{\"@type\":\"g:Int32\",\"@value\":10}}}}}]]}},\"aliases\":{\"g\":\"gmodern\"}}}"; ++ final ByteBuf bb = allocator.buffer(requestMessageWithP.length()); ++ bb.writeBytes(requestMessageWithP.getBytes()); ++ final RequestMessage m = serializer.deserializeRequest(bb); ++ assertEquals("bytecode", m.getOp()); ++ assertNotNull(m.getArgs()); ++ } + + private void assertCommon(final ResponseMessage response) { + assertEquals(requestId, response.getRequestId()); + assertEquals(ResponseStatusCode.SUCCESS, response.getStatus().getCode()); + } + + private ResponseMessage convert(final Object toSerialize) throws SerializationException { + final ByteBuf bb = serializer.serializeResponseAsBinary(responseMessageBuilder.result(toSerialize).create(), allocator); + return serializer.deserializeResponse(bb); + } +} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/92e899f4/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/92e899f4/gremlin-python/src/main/jython/radish/feature_steps.py ---------------------------------------------------------------------- diff --cc gremlin-python/src/main/jython/radish/feature_steps.py index e29607d,5cf9059..8261510 --- a/gremlin-python/src/main/jython/radish/feature_steps.py +++ b/gremlin-python/src/main/jython/radish/feature_steps.py @@@ -44,14 -44,7 +44,9 @@@ tail = __.tai ignores = [ "g.V(v1Id).out().inject(v2).values(\"name\")", # bug in attachment won't connect v2 - # TINKERPOP-1859 - for the following...seems to be related to P.not processing - "g.V().hasLabel(\"person\").has(\"age\", P.not(P.lte(d10).and(P.not(P.between(d11, d20)))).and(P.lt(d29).or(P.eq(d35)))).values(\"name\")", - "g.V(v1Id).out().aggregate(\"x\").out().where(P.not(P.within(\"x\")))", - "g.V().as(\"a\").out().as(\"b\").where(__.and(__.as(\"a\").out(\"knows\").as(\"b\"), __.or(__.as(\"b\").out(\"created\").has(\"name\", \"ripple\"), __.as(\"b\").in(\"knows\").count().is(P.not(P.eq(0)))))).select(\"a\", \"b\")", - "g.V().as(\"a\").out(\"created\").as(\"b\").in(\"created\").as(\"c\").both(\"knows\").both(\"knows\").as(\"d\").where(\"c\", P.not(P.eq(\"a\").or(P.eq(\"d\")))).select(\"a\", \"b\", \"c\", \"d\")", - ] + # not sure yet + "g.V(v1).hasLabel(\"person\").map(l1).order(Scope.local).by(Column.values, Order.decr).by(Column.keys, Order.incr)" + ] @given("the {graph_name:w} graph")