Search in sources :

Example 16 with JsonFormat

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

the class UserServiceTest method shouldReturn422IfInvalidPasswordType.

@Test
public void shouldReturn422IfInvalidPasswordType() 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(mock(BasicAuthManager.class), new JsonFormat(), outputFormat);
    // When
    Response response = userService.setPassword("neo4j", req, "{ \"password\" : 1 }");
    // Then
    assertThat(response.getStatus(), equalTo(422));
    String json = new String((byte[]) response.getEntity());
    assertNotNull(json);
    assertThat(json, containsString("\"code\" : \"Neo.ClientError.Request.InvalidFormat\""));
    assertThat(json, containsString("\"message\" : \"Expected 'password' to be a string.\""));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) JsonFormat(org.neo4j.server.rest.repr.formats.JsonFormat) BasicAuthManager(org.neo4j.server.security.auth.BasicAuthManager) 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 17 with JsonFormat

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

the class UserServiceTest method shouldReturn404WhenRequestingUserIfNotAuthenticated.

@Test
public void shouldReturn404WhenRequestingUserIfNotAuthenticated() throws Exception {
    // Given
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getUserPrincipal()).thenReturn(null);
    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(404));
}
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) URI(java.net.URI) EntityOutputFormat(org.neo4j.test.server.EntityOutputFormat) Test(org.junit.Test)

Example 18 with JsonFormat

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

the class UserServiceTest method shouldReturn400IfPayloadIsInvalid.

@Test
public void shouldReturn400IfPayloadIsInvalid() 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(mock(BasicAuthManager.class), new JsonFormat(), outputFormat);
    // When
    Response response = userService.setPassword("neo4j", req, "xxx");
    // Then
    assertThat(response.getStatus(), equalTo(400));
    String json = new String((byte[]) response.getEntity());
    assertNotNull(json);
    assertThat(json, containsString("\"code\" : \"Neo.ClientError.Request.InvalidFormat\""));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) JsonFormat(org.neo4j.server.rest.repr.formats.JsonFormat) BasicAuthManager(org.neo4j.server.security.auth.BasicAuthManager) 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 19 with JsonFormat

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

the class MapRepresentationTest method shouldSerializeMapsWithNullKeys.

@Test
public void shouldSerializeMapsWithNullKeys() throws Exception {
    Object[] values = { null, "string", 42, true, new String[] { "a string", "another string" }, new int[] { 42, 87 }, new boolean[] { true, false }, asList(true, false, true), map("numbers", 42, null, "something"), map("a list", asList(42, 87), null, asList("a", "b")), asList(map("foo", "bar", null, false)) };
    for (Object value : values) {
        MapRepresentation rep = new MapRepresentation(map((Object) null, value));
        OutputFormat format = new OutputFormat(new JsonFormat(), new URI("http://localhost/"), null);
        String serializedMap = format.assemble(rep);
        Map<String, Object> map = JsonHelper.jsonToMap(serializedMap);
        assertEquals(1, map.size());
        Object actual = map.get("null");
        if (value == null) {
            assertNull(actual);
        } else {
            assertNotNull(actual);
        }
    }
}
Also used : JsonFormat(org.neo4j.server.rest.repr.formats.JsonFormat) URI(java.net.URI) Test(org.junit.Test)

Example 20 with JsonFormat

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

the class MapRepresentationTest method shouldSerializeMapWithMapsOfSimpleTypes.

@Test
public void shouldSerializeMapWithMapsOfSimpleTypes() throws Exception {
    MapRepresentation rep = new MapRepresentation(map("maps with nulls", map("nulls", null), "maps with strings", map("strings", "a string"), "maps with numbers", map("numbers", 42), "maps with booleans", map("booleans", 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(((Map) map.get("maps with nulls")).get("nulls"), is(nullValue()));
    assertThat((String) ((Map) map.get("maps with strings")).get("strings"), is("a string"));
    assertThat((Integer) ((Map) map.get("maps with numbers")).get("numbers"), is(42));
    assertThat((Boolean) ((Map) map.get("maps with booleans")).get("booleans"), is(true));
}
Also used : JsonFormat(org.neo4j.server.rest.repr.formats.JsonFormat) URI(java.net.URI) Map(java.util.Map) 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