@Test
 public void canProvideJsonOutputFormat() throws Exception {
   OutputFormat format =
       repository.outputFormat(asList(MediaType.APPLICATION_JSON_TYPE), null, null);
   assertNotNull(format);
   assertEquals("\"test\"", format.assemble(ValueRepresentation.string("test")));
 }
Beispiel #2
0
  @POST
  public Response exec(@Context InputFormat input, String data) {
    Map<String, Object> args;
    try {
      args = input.readMap(data);
    } catch (BadInputException e) {
      return output.badRequest(e);
    }

    if (!args.containsKey("command")) {
      return Response.status(Status.BAD_REQUEST)
          .entity("Expected command argument not present.")
          .build();
    }

    ScriptSession scriptSession;
    try {
      scriptSession = getSession(args);
    } catch (IllegalArgumentException e) {
      return output.badRequest(e);
    }

    log.debug(scriptSession.toString());
    try {
      Pair<String, String> result = scriptSession.evaluate((String) args.get("command"));
      List<Representation> list =
          new ArrayList<Representation>(
              asList(
                  ValueRepresentation.string(result.first()),
                  ValueRepresentation.string(result.other())));

      return output.ok(new ListRepresentation(RepresentationType.STRING, list));
    } catch (Exception e) {
      List<Representation> list =
          new ArrayList<Representation>(
              asList(
                  ValueRepresentation.string(e.getClass() + " : " + e.getMessage() + "\n"),
                  ValueRepresentation.string(null)));
      return output.ok(new ListRepresentation(RepresentationType.STRING, list));
    }
  }
  static Representation getSingleRepresentation(Object result) {
    if (result == null) {
      return ObjectToRepresentationConverter.getSingleRepresentation(result);
    }

    if (result instanceof Neo4j2Vertex) {
      return new NodeRepresentation(((Neo4j2Vertex) result).getRawVertex());
    }
    if (result instanceof Neo4j2Edge) {
      return new RelationshipRepresentation(((Neo4j2Edge) result).getRawEdge());
    }
    if (result instanceof Neo4j2Graph) {
      return ValueRepresentation.string(((Neo4j2Graph) result).getRawGraph().toString());
    }

    return ObjectToRepresentationConverter.getSingleRepresentation(result);
  }
 @Mapping("description")
 public ValueRepresentation getDescription() {
   return ValueRepresentation.string(description);
 }
 @Mapping("name")
 public ValueRepresentation getName() {
   return ValueRepresentation.string(name);
 }
 @Mapping("extends")
 public ValueRepresentation extendedEntity() {
   return ValueRepresentation.string(getExtendedEntity());
 }
 @Test
 public void canFormatInteger() throws Exception {
   json.assemble(ValueRepresentation.number(10));
   assertEquals("10", stream.toString());
 }
 @Test
 public void canFormatString() throws Exception {
   json.assemble(ValueRepresentation.string("expected value"));
   assertEquals(stream.toString(), "\"expected value\"");
 }