Search in sources :

Example 6 with JsonFormat

use of org.neo4j.server.rest.repr.formats.JsonFormat in project neo4j by neo4j.

the class UserServiceTest method shouldReturnValidUserRepresentation.

@Test
public void shouldReturnValidUserRepresentation() throws Exception {
    // Given
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getUserPrincipal()).thenReturn(neo4jPrinciple);
    OutputFormat outputFormat = new EntityOutputFormat(new JsonFormat(), new URI("http://www.example.com"), null);
    UserService userService = new UserService(userManagerSupplier, new JsonFormat(), outputFormat);
    // When
    Response response = userService.getUser("neo4j", req);
    // Then
    assertThat(response.getStatus(), equalTo(200));
    String json = new String((byte[]) response.getEntity());
    assertNotNull(json);
    assertThat(json, containsString("\"username\" : \"neo4j\""));
    assertThat(json, containsString("\"password_change\" : \"http://www.example.com/user/neo4j/password\""));
    assertThat(json, containsString("\"password_change_required\" : true"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) JsonFormat(org.neo4j.server.rest.repr.formats.JsonFormat) EntityOutputFormat(org.neo4j.test.server.EntityOutputFormat) OutputFormat(org.neo4j.server.rest.repr.OutputFormat) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) EntityOutputFormat(org.neo4j.test.server.EntityOutputFormat) Test(org.junit.Test)

Example 7 with JsonFormat

use of org.neo4j.server.rest.repr.formats.JsonFormat in project neo4j by neo4j.

the class UserServiceTest method shouldReturn422IfEmptyPassword.

@Test
public void shouldReturn422IfEmptyPassword() throws Exception {
    // Given
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getUserPrincipal()).thenReturn(neo4jPrinciple);
    OutputFormat outputFormat = new EntityOutputFormat(new JsonFormat(), new URI("http://www.example.com"), null);
    UserService userService = new UserService(userManagerSupplier, new JsonFormat(), outputFormat);
    // When
    Response response = userService.setPassword("neo4j", req, "{ \"password\" : \"\" }");
    // Then
    assertThat(response.getStatus(), equalTo(422));
    String json = new String((byte[]) response.getEntity());
    assertNotNull(json);
    assertThat(json, containsString("\"code\" : \"Neo.ClientError.General.InvalidArguments\""));
    assertThat(json, containsString("\"message\" : \"A password cannot be empty.\""));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) JsonFormat(org.neo4j.server.rest.repr.formats.JsonFormat) EntityOutputFormat(org.neo4j.test.server.EntityOutputFormat) OutputFormat(org.neo4j.server.rest.repr.OutputFormat) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) EntityOutputFormat(org.neo4j.test.server.EntityOutputFormat) Test(org.junit.Test)

Example 8 with JsonFormat

use of org.neo4j.server.rest.repr.formats.JsonFormat in project neo4j by neo4j.

the class DiscoveryServiceTest method shouldReturnRedirectToAbsoluteAPIUsingOutputFormat.

@Test
public void shouldReturnRedirectToAbsoluteAPIUsingOutputFormat() throws Exception {
    Config mockConfig = mock(Config.class);
    URI browserUri = new URI("/browser/");
    when(mockConfig.get(ServerSettings.browser_path)).thenReturn(browserUri);
    String baseUri = "http://www.example.com:5435";
    DiscoveryService ds = new DiscoveryService(mockConfig, new EntityOutputFormat(new JsonFormat(), new URI(baseUri), null));
    Response response = ds.redirectToBrowser();
    assertThat(response.getMetadata().getFirst("Location"), is(new URI("http://www.example" + ".com:5435/browser/")));
}
Also used : Response(javax.ws.rs.core.Response) JsonFormat(org.neo4j.server.rest.repr.formats.JsonFormat) Config(org.neo4j.kernel.configuration.Config) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) EntityOutputFormat(org.neo4j.test.server.EntityOutputFormat) Test(org.junit.Test)

Example 9 with JsonFormat

use of org.neo4j.server.rest.repr.formats.JsonFormat in project neo4j by neo4j.

the class MapRepresentationTest method shouldSerializeMapWithListsOfSimpleTypes.

@Test
@SuppressWarnings("unchecked")
public void shouldSerializeMapWithListsOfSimpleTypes() throws Exception {
    MapRepresentation rep = new MapRepresentation(map("lists of nulls", asList(null, null), "lists of strings", asList("a string", "another string"), "lists of numbers", asList(23, 87, 42), "lists of booleans", asList(true, false, true)));
    OutputFormat format = new OutputFormat(new JsonFormat(), new URI("http://localhost/"), null);
    String serializedMap = format.assemble(rep);
    Map<String, Object> map = JsonHelper.jsonToMap(serializedMap);
    assertThat((List<Object>) map.get("lists of nulls"), is(asList(null, null)));
    assertThat((List<String>) map.get("lists of strings"), is(asList("a string", "another string")));
    assertThat((List<Integer>) map.get("lists of numbers"), is(asList(23, 87, 42)));
    assertThat((List<Boolean>) map.get("lists of booleans"), is(asList(true, false, true)));
}
Also used : JsonFormat(org.neo4j.server.rest.repr.formats.JsonFormat) URI(java.net.URI) Test(org.junit.Test)

Example 10 with JsonFormat

use of org.neo4j.server.rest.repr.formats.JsonFormat in project neo4j by neo4j.

the class MapRepresentationTest method shouldSerializeMapWithArrayTypes.

@Test
@SuppressWarnings("unchecked")
public void shouldSerializeMapWithArrayTypes() throws Exception {
    MapRepresentation rep = new MapRepresentation(map("strings", new String[] { "a string", "another string" }, "numbers", new int[] { 42, 87 }, "booleans", new boolean[] { true, false }, "Booleans", new Boolean[] { TRUE, FALSE }));
    OutputFormat format = new OutputFormat(new JsonFormat(), new URI("http://localhost/"), null);
    String serializedMap = format.assemble(rep);
    Map<String, Object> map = JsonHelper.jsonToMap(serializedMap);
    assertThat((List<String>) map.get("strings"), is(asList("a string", "another string")));
    assertThat((List<Integer>) map.get("numbers"), is(asList(42, 87)));
    assertThat((List<Boolean>) map.get("booleans"), is(asList(true, false)));
    assertThat((List<Boolean>) map.get("Booleans"), is(asList(true, false)));
}
Also used : JsonFormat(org.neo4j.server.rest.repr.formats.JsonFormat) URI(java.net.URI) Test(org.junit.Test)

Aggregations

JsonFormat (org.neo4j.server.rest.repr.formats.JsonFormat)24 URI (java.net.URI)21 Test (org.junit.Test)21 EntityOutputFormat (org.neo4j.test.server.EntityOutputFormat)17 Response (javax.ws.rs.core.Response)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 OutputFormat (org.neo4j.server.rest.repr.OutputFormat)13 Matchers.containsString (org.hamcrest.Matchers.containsString)7 BasicAuthManager (org.neo4j.server.security.auth.BasicAuthManager)5 WrappedDatabase (org.neo4j.server.database.WrappedDatabase)3 GraphDbHelper (org.neo4j.server.rest.domain.GraphDbHelper)3 LeaseManager (org.neo4j.server.rest.paging.LeaseManager)3 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)3 Map (java.util.Map)2 Before (org.junit.Before)2 BeforeClass (org.junit.BeforeClass)1 UserManager (org.neo4j.kernel.api.security.UserManager)1 Config (org.neo4j.kernel.configuration.Config)1 ConfigAdapter (org.neo4j.server.plugins.ConfigAdapter)1