@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"))); }
@GET @Path(PATH_EXTENSION) public Response getExtensionList(@PathParam("name") String name) { try { return output.ok(this.extensionList(name)); } catch (PluginLookupException e) { return output.notFound(e); } }
@GET @Path(PATH_GRAPHDB_EXTENSION_METHOD) public Response getGraphDatabaseExtensionDescription( @PathParam("name") String name, @PathParam("method") String method) { try { return output.ok(this.describeGraphDatabaseExtension(name, method)); } catch (PluginLookupException e) { return output.notFound(e); } }
@Test public void cannotProvideStreamingForOtherMediaTypes() throws Exception { final Response.ResponseBuilder responseBuilder = mock(Response.ResponseBuilder.class); // no streaming when(responseBuilder.entity(anyString())).thenReturn(responseBuilder); Mockito.verify(responseBuilder, never()).entity(isA(StreamingOutput.class)); when(responseBuilder.type(Matchers.<MediaType>any())).thenReturn(responseBuilder); when(responseBuilder.build()).thenReturn(null); OutputFormat format = repository.outputFormat( asList(MediaType.TEXT_HTML_TYPE), new URI("http://some.host"), streamingHeader()); assertNotNull(format); format.response(responseBuilder, new ExceptionRepresentation(new RuntimeException())); }
@GET @Path(PATH_RELATIONSHIP_EXTENSION_METHOD) public Response getRelationshipExtensionDescription( @PathParam("name") String name, @PathParam("method") String method, @PathParam("relationshipId") long relationshipId) { try { return output.ok(this.describeRelationshipExtension(name, method)); } catch (PluginLookupException e) { return output.notFound(e); } catch (Exception e) { return output.serverError(e.getCause()); } }
@GET public Response getServiceDefinition() { ConsoleServiceRepresentation result = new ConsoleServiceRepresentation(SERVICE_PATH, sessionFactory.supportedEngines()); return output.ok(result); }
@Test public void canProvideStreamingJsonOutputFormat() throws Exception { Response response = mock(Response.class); final AtomicReference<StreamingOutput> ref = new AtomicReference<>(); final Response.ResponseBuilder responseBuilder = mockResponsBuilder(response, ref); OutputFormat format = repository.outputFormat(asList(MediaType.APPLICATION_JSON_TYPE), null, streamingHeader()); assertNotNull(format); Response returnedResponse = format.response(responseBuilder, new MapRepresentation(map("a", "test"))); assertSame(response, returnedResponse); StreamingOutput streamingOutput = ref.get(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); streamingOutput.write(baos); assertEquals("{\"a\":\"test\"}", baos.toString()); }
@Test @Graph(value = {"I know you"}) public void runSimpleQuery() throws Exception { Node i = data.get().get("I"); Representation result = testQuery("start n=node(" + i.getId() + ") return n"); assertTrue(json.assemble(result).contains("I")); }
@POST @Path(PATH_RELATIONSHIP_EXTENSION_METHOD) public Response invokeRelationshipExtension( @PathParam("name") String name, @PathParam("method") String method, @PathParam("relationshipId") long relationshipId, String data) { try { return output.ok( this.invokeRelationshipExtension( relationshipId, name, method, input.readParameterList(data))); } catch (RelationshipNotFoundException e) { return output.notFound(e); } catch (BadInputException e) { return output.badRequest(e); } catch (PluginLookupException e) { return output.notFound(e); } catch (BadPluginInvocationException e) { return output.badRequest(e.getCause()); } catch (PluginInvocationFailureException e) { return output.serverError(e.getCause()); } catch (Exception e) { return output.serverError(e.getCause()); } }
@Test public void canFormatEmptyObject() throws Exception { json.assemble( new MappingRepresentation("empty") { @Override protected void serialize(MappingSerializer serializer) {} }); assertEquals(JsonHelper.createJsonFrom(Collections.emptyMap()), stream.toString()); }
@Test public void canFormatNode() throws Exception { GraphDatabaseService db = new TestGraphDatabaseFactory().newImpermanentDatabase(); try { final Node refNode = db.getReferenceNode(); json.assemble(new NodeRepresentation(refNode)); } finally { db.shutdown(); } assertTrue(stream.toString().contains("\"self\" : \"http://localhost/node/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)); } }
@GET @Produces(MediaType.APPLICATION_JSON) public Response getDiscoveryDocument() throws URISyntaxException { String webAdminManagementUri = configuration.getString( Configurator.MANAGEMENT_PATH_PROPERTY_KEY, Configurator.DEFAULT_MANAGEMENT_API_PATH); String dataUri = configuration.getString( Configurator.REST_API_PATH_PROPERTY_KEY, Configurator.DEFAULT_DATA_API_PATH); DiscoveryRepresentation dr = new DiscoveryRepresentation(webAdminManagementUri, dataUri); return outputFormat.ok(dr); }
@Test public void canFormatObjectWithStringField() throws Exception { json.assemble( new MappingRepresentation("string") { @Override protected void serialize(MappingSerializer serializer) { serializer.putString("key", "expected string"); } }); assertEquals( JsonHelper.createJsonFrom(Collections.singletonMap("key", "expected string")), stream.toString()); }
@Test public void canFormatObjectWithUriField() throws Exception { json.assemble( new MappingRepresentation("uri") { @Override protected void serialize(MappingSerializer serializer) { serializer.putUri("URL", "subpath"); } }); assertEquals( JsonHelper.createJsonFrom(Collections.singletonMap("URL", "http://localhost/subpath")), stream.toString()); }
@Test public void canFormatObjectWithNestedObject() throws Exception { json.assemble( new MappingRepresentation("nesting") { @Override protected void serialize(MappingSerializer serializer) { serializer.putMapping( "nested", new MappingRepresentation("data") { @Override protected void serialize(MappingSerializer nested) { nested.putString("data", "expected data"); } }); } }); assertEquals( JsonHelper.createJsonFrom( Collections.singletonMap("nested", Collections.singletonMap("data", "expected data"))), stream.toString()); }
@Test @Graph( value = {"I know you", "I know him"}, nodes = { @NODE( name = "you", properties = { @PROP(key = "bool", value = "true", type = GraphDescription.PropType.BOOLEAN), @PROP(key = "name", value = "you"), @PROP(key = "int", value = "1", type = GraphDescription.PropType.INTEGER) }) }) public void checkColumns() throws Exception { Node i = data.get().get("I"); Representation result = testQuery( "start x =node(" + i.getId() + ") match (x) -- (n) return n, n.name?, n.bool?, n.int?"); String formatted = json.assemble(result); System.out.println(formatted); assertTrue(formatted.contains("columns")); assertTrue(formatted.contains("name")); }
@POST @Path(PATH_GRAPHDB_EXTENSION_METHOD) public Response invokeGraphDatabaseExtension( @PathParam("name") String name, @PathParam("method") String method, String data) { try { return output.ok( this.invokeGraphDatabaseExtension(name, method, input.readParameterList(data))); } catch (BadInputException e) { return output.badRequest(e); } catch (PluginLookupException e) { return output.notFound(e); } catch (BadPluginInvocationException e) { return output.badRequest(e.getCause()); } catch (SyntaxException e) { return output.badRequest(e.getCause()); } catch (PluginInvocationFailureException e) { return output.serverError(e.getCause()); } catch (Exception e) { return output.serverError(e); } }
@GET public Response getExtensionsList() { return output.ok(this.extensionsList()); }
@Test public void canFormatString() throws Exception { json.assemble(ValueRepresentation.string("expected value")); assertEquals(stream.toString(), "\"expected value\""); }
@Test public void canFormatListOfStrings() throws Exception { json.assemble(ListRepresentation.strings("hello", "world")); String expectedString = JsonHelper.createJsonFrom(Arrays.asList("hello", "world")); assertEquals(expectedString, stream.toString()); }
@Test public void canFormatInteger() throws Exception { json.assemble(ValueRepresentation.number(10)); assertEquals("10", stream.toString()); }