GraphSONMessageSerializerV2d0 extends AbstractGraphSONMessageSerializerV2d0 implements MessageTextSerializer { @Override public String serializeResponseAsString(final ResponseMessage responseMessage) throws SerializationException { try { return mapper.writeValueAsString(responseMessage); } catch (Exception ex) { logger.warn("Response [{}] could not be serialized by {}.", responseMessage.toString(), AbstractGraphSONMessageSerializerV2d0.class.getName()); throw new SerializationException(ex); } } GraphSONMessageSerializerV2d0(); @Deprecated  GraphSONMessageSerializerV2d0(final GraphSONMapper mapper);  GraphSONMessageSerializerV2d0(final GraphSONMapper.Builder mapperBuilder); @Override String[] mimeTypesSupported(); @Override ResponseMessage deserializeResponse(final String msg); @Override String serializeResponseAsString(final ResponseMessage responseMessage); @Override RequestMessage deserializeRequest(final String msg); @Override String serializeRequestAsString(final RequestMessage requestMessage);  }
@Test public void shouldConfigureIoRegistry() throws Exception { final GraphSONMessageSerializerV1d0 serializer = new GraphSONMessageSerializerV1d0(); final Map<String, Object> config = new HashMap<String, Object>() {{ put(GryoMessageSerializerV1d0.TOKEN_IO_REGISTRIES, Arrays.asList(ColorIoRegistry.class.getName())); }}; serializer.configure(config, null); final ResponseMessage toSerialize = ResponseMessage.build(UUID.fromString("2D62161B-9544-4F39-AF44-62EC49F9A595")) .result(Color.RED).create(); final String results = serializer.serializeResponseAsString(toSerialize); final JsonNode json = mapper.readTree(results); assertNotNull(json); assertThat(json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA).booleanValue(), is(true)); }
@Test public void shouldSerializeToJsonNullResultReturnsNull() throws Exception { final ResponseMessage message = ResponseMessage.build(msg).create(); final String results = SERIALIZER.serializeResponseAsString(message); final JsonNode json = mapper.readTree(results); assertNotNull(json); assertEquals(msg.getRequestId().toString(), json.path(SerTokens.TOKEN_REQUEST).asText()); assertEquals(NullNode.getInstance(), json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA)); }
@Test public void shouldSerializeToJsonIterable() throws Exception { final ArrayList<FunObject> funList = new ArrayList<>(); funList.add(new FunObject("x")); funList.add(new FunObject("y")); final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(funList).create()); final JsonNode json = mapper.readTree(results); assertNotNull(json); assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText()); final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA); assertEquals(2, converted.size()); assertEquals("x", converted.get(0).asText()); assertEquals("y", converted.get(1).asText()); }
@Test public void shouldSerializeToJsonIterator() throws Exception { final ArrayList<FunObject> funList = new ArrayList<>(); funList.add(new FunObject("x")); funList.add(new FunObject("y")); final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(funList).create()); final JsonNode json = mapper.readTree(results); assertNotNull(json); assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText()); final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA); assertEquals(2, converted.size()); assertEquals("x", converted.get(0).asText()); assertEquals("y", converted.get(1).asText()); }
@Test public void shouldSerializeToJsonIteratorNullElement() throws Exception { final ArrayList<FunObject> funList = new ArrayList<>(); funList.add(new FunObject("x")); funList.add(null); funList.add(new FunObject("y")); final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(funList.iterator()).create()); final JsonNode json = mapper.readTree(results); assertNotNull(json); assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText()); final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA); assertEquals(3, converted.size()); assertEquals("x", converted.get(0).asText()); assertEquals(NullNode.getInstance(), converted.get(1)); assertEquals("y", converted.get(2).asText()); }
@Test public void shouldSerializeToJsonMap() throws Exception { final Map<String, Object> map = new HashMap<>(); final Map<String, String> innerMap = new HashMap<>(); innerMap.put("a", "b"); map.put("x", new FunObject("x")); map.put("y", "some"); map.put("z", innerMap); final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(map).create()); final JsonNode json = mapper.readTree(results); assertNotNull(json); assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText()); final JsonNode jsonObject = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA); assertNotNull(jsonObject); assertEquals("some", jsonObject.get("y").asText()); assertEquals("x", jsonObject.get("x").asText()); final JsonNode innerJsonObject = jsonObject.get("z"); assertNotNull(innerJsonObject); assertEquals("b", innerJsonObject.get("a").asText()); }
@Test public void shouldShouldSerializeMapEntries() 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 String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(IteratorUtils.asList(map)).create()); final JsonNode json = mapper.readTree(results); assertNotNull(json); assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText()); final JsonNode jsonObject = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA); jsonObject.elements().forEachRemaining(e -> { if (e.has("x")) assertEquals(1, e.get("x").get(GraphSONTokens.VALUEPROP).asInt()); else if (e.has(v1.id().toString())) assertEquals(100, e.get(v1.id().toString()).get(GraphSONTokens.VALUEPROP).asInt()); else if (e.has(StdDateFormat.instance.format(d))) assertEquals("test", e.get(StdDateFormat.instance.format(d)).asText()); else fail("Map entries contains a key that is not part of what was serialized"); }); }
@Test public void shouldSerializeEdge() throws Exception { final Graph g = TinkerGraph.open(); final Vertex v1 = g.addVertex(); final Vertex v2 = g.addVertex(); final Edge e = v1.addEdge("test", v2); e.property("abc", 123); final Iterable<Edge> iterable = IteratorUtils.list(g.edges()); final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create()); final JsonNode json = mapper.readTree(results); assertNotNull(json); assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText()); final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA); assertNotNull(converted); assertEquals(1, converted.size()); final JsonNode edgeAsJson = converted.get(0).get(GraphSONTokens.VALUEPROP); assertNotNull(edgeAsJson); assertEquals(((Long) e.id()).longValue(), edgeAsJson.get(GraphSONTokens.ID).get(GraphSONTokens.VALUEPROP).asLong()); assertEquals(((Long) v1.id()).longValue(), edgeAsJson.get(GraphSONTokens.OUT).get(GraphSONTokens.VALUEPROP).asLong()); assertEquals(((Long) v2.id()).longValue(), edgeAsJson.get(GraphSONTokens.IN).get(GraphSONTokens.VALUEPROP).asLong()); assertEquals(e.label(), edgeAsJson.get(GraphSONTokens.LABEL).asText()); final JsonNode properties = edgeAsJson.get(GraphSONTokens.PROPERTIES); assertNotNull(properties); assertEquals(123, properties.get("abc").get(GraphSONTokens.VALUEPROP).get("value").get(GraphSONTokens.VALUEPROP).asInt()); }
@Test public void shouldSerializeEdgeProperty() throws Exception { final Graph g = TinkerGraph.open(); final Vertex v1 = g.addVertex(); final Vertex v2 = g.addVertex(); final Edge e = v1.addEdge("test", v2); e.property("abc", 123); final Iterable<Property<Object>> iterable = IteratorUtils.list(e.properties("abc")); final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create()); final JsonNode json = mapper.readTree(results); assertNotNull(json); assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText()); final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA); assertNotNull(converted); assertEquals(1, converted.size()); final JsonNode propertyAsJson = converted.get(0); assertNotNull(propertyAsJson); assertEquals(123, propertyAsJson.get(GraphSONTokens.VALUEPROP).get("value").get(GraphSONTokens.VALUEPROP).asInt()); }
@Test public void shouldSerializeToJsonIteratorWithEmbeddedMap() throws Exception { final Graph g = TinkerGraph.open(); final Vertex v = g.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 Iterable iterable = IteratorUtils.list(g.vertices()); final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create()); final JsonNode json = mapper.readTree(results); assertNotNull(json); assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText()); final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA); assertNotNull(converted); assertEquals(1, converted.size()); final JsonNode vertexAsJson = converted.get(0).get(GraphSONTokens.VALUEPROP); assertNotNull(vertexAsJson); final JsonNode properties = vertexAsJson.get(GraphSONTokens.PROPERTIES); assertNotNull(properties); assertEquals(1, properties.size()); final JsonNode friendProperties = properties.get("friends"); assertEquals(1, friendProperties.size()); final JsonNode friendsProperty = friendProperties.get(0).get(GraphSONTokens.VALUEPROP).get(GraphSONTokens.VALUE); assertNotNull(friendsProperty); assertEquals(3, friendsProperty.size()); final String object1 = friendsProperty.get(0).asText(); assertEquals("x", object1); final int object2 = friendsProperty.get(1).get(GraphSONTokens.VALUEPROP).asInt(); assertEquals(5, object2); final JsonNode object3 = friendsProperty.get(2); assertEquals(500, object3.get("x").get(GraphSONTokens.VALUEPROP).asInt()); assertEquals("some", object3.get("y").asText()); }
@Test public void shouldSerializeToJsonMapWithElementForKey() throws Exception { final TinkerGraph graph = TinkerFactory.createClassic(); final GraphTraversalSource g = graph.traversal(); final Map<Vertex, Integer> map = new HashMap<>(); map.put(g.V().has("name", "marko").next(), 1000); final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(map).create()); final JsonNode json = mapper.readTree(results); assertNotNull(json); assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText()); final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA); assertNotNull(converted); assertEquals(1000, converted.get("1").get(GraphSONTokens.VALUEPROP).asInt()); }
@Test public void shouldSerializeToTreeJson() throws Exception { final TinkerGraph graph = TinkerFactory.createClassic(); final GraphTraversalSource g = graph.traversal(); final Tree t = g.V(1).out().properties("name").tree().next(); final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(t).create()); final JsonNode json = mapper.readTree(results); assertNotNull(json); assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText()); final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA); assertNotNull(converted); assertEquals(1, converted.get(GraphSONTokens.VALUEPROP) .get(0) .get(GraphSONTokens.KEY).get(GraphSONTokens.VALUEPROP) .get(GraphSONTokens.ID).get(GraphSONTokens.VALUEPROP).asInt()); assertEquals("marko", converted.get(GraphSONTokens.VALUEPROP) .get(0) .get(GraphSONTokens.KEY).get(GraphSONTokens.VALUEPROP) .get(GraphSONTokens.PROPERTIES) .get("name") .get(0) .get(GraphSONTokens.VALUEPROP).get(GraphSONTokens.VALUE).asText()); assertEquals("vadas", converted.get(GraphSONTokens.VALUEPROP) .get(0) .get(GraphSONTokens.VALUE).get(GraphSONTokens.VALUEPROP) .get(0) .get(GraphSONTokens.KEY).get(GraphSONTokens.VALUEPROP) .get(GraphSONTokens.PROPERTIES) .get("name") .get(0) .get(GraphSONTokens.VALUEPROP).get(GraphSONTokens.VALUE).asText()); assertEquals("lop", converted.get(GraphSONTokens.VALUEPROP) .get(0) .get(GraphSONTokens.VALUE).get(GraphSONTokens.VALUEPROP) .get(1) .get(GraphSONTokens.KEY).get(GraphSONTokens.VALUEPROP) .get(GraphSONTokens.PROPERTIES) .get("name") .get(0) .get(GraphSONTokens.VALUEPROP).get(GraphSONTokens.VALUE).asText()); assertEquals("josh", converted.get(GraphSONTokens.VALUEPROP) .get(0) .get(GraphSONTokens.VALUE).get(GraphSONTokens.VALUEPROP) .get(2) .get(GraphSONTokens.KEY).get(GraphSONTokens.VALUEPROP) .get(GraphSONTokens.PROPERTIES) .get("name") .get(0) .get(GraphSONTokens.VALUEPROP).get(GraphSONTokens.VALUE).asText()); }