private static ThrowingConsumer<HttpServletResponse, IOException> passwordChangeRequired( final String username, final String baseURL ) { URI path = UriBuilder.fromUri( baseURL ).path( format( "/user/%s/password", username ) ).build(); return error( 403, map( "errors", singletonList( map( "code", Status.Security.Forbidden.code().serialize(), "message", "User is required to change their password." ) ), "password_change", path.toString() ) ); }
@Override public void describeTo( Description description ) { description.appendValue( FAILURE ) .appendText( format( " with status code %s", status.code().serialize() ) ); } };
protected static ThrowingConsumer<HttpServletResponse, IOException> unauthorizedAccess( final String message ) { return error( 403, map( "errors", singletonList( map( "code", Status.Security.Forbidden.code().serialize(), "message", String.format("Unauthorized access violation: %s.", message ) ) ) ) ); }
@Test public void shouldRespondWith404WhenNoRelationshipFromWhichToRemoveProperties() throws Exception { long relationshipId = helper.createRelationship( "KNOWS" ); Response response = service.deleteAllRelationshipProperties( relationshipId + 1000 ); assertEquals( 404, response.getStatus() ); assertEquals( Statement.EntityNotFound.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldGet404WhenRetrievingRelationshipThatDoesNotExist() throws Exception { Response response = service.getRelationship( 999999 ); assertEquals( 404, response.getStatus() ); assertEquals( Statement.EntityNotFound.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldRespondWith400WhenTryingToCreateRelationshipWithBadJson() throws Exception { long startNode = helper.createNode(); long endNode = helper.createNode(); Response response = service.createRelationship( startNode, "{\"to\" : \"" + BASE_URI + endNode + "\", \"type\" ***and junk*** : \"LOVES\"}" ); assertEquals( 400, response.getStatus() ); assertEquals( Request.InvalidFormat.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldRespondWith404WhenGettingIncomingRelationshipsForNonExistingNode() throws Exception { Response response = service.getNodeRelationships( 999999, RelationshipDirection.all, new AmpersandSeparatedCollection( "" ) ); assertEquals( 404, response.getStatus() ); assertEquals( Statement.EntityNotFound.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldGet404WhenCannotResolveAPropertyOnRelationship() throws Exception { long relationshipId = helper.createRelationship( "knows" ); Response response = service.getRelationshipProperty( relationshipId, "some-key" ); assertEquals( 404, response.getStatus() ); assertEquals( Statement.PropertyNotFound.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldGet404WhenRemovingNonExistingProperty() throws Exception { long nodeId = helper.createNode(); Map<String, Object> properties = new HashMap<>(); properties.put( "foo", "bar" ); properties.put( "number", 15 ); helper.setNodeProperties( nodeId, properties ); Response response = service.deleteNodeProperty( nodeId, "baz" ); assertEquals( 404, response.getStatus() ); assertEquals( Statement.PropertyNotFound.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldRespondWith404ForGetNonExistentNodeProperty() throws Exception { long nodeId = helper.createNode(); Response response = service.getNodeProperty( nodeId, "foo" ); assertEquals( 404, response.getStatus() ); assertEquals( Statement.PropertyNotFound.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldRespondWith404IfNodeToBeDeletedDoesNotExist() throws Exception { long nonExistentId = 999999; Response response = service.deleteNode( nonExistentId ); assertEquals( 404, response.getStatus() ); assertEquals( Statement.EntityNotFound.code().serialize(), singleErrorCode( response ) ); }
@Override public String getCode() { return status.code().serialize(); }
@Override public void markFailed( Neo4jError error ) { currentResponse.setResponse( FAILURE ); onMetadata( "code", stringValue( error.status().code().serialize() ) ); onMetadata( "message", stringOrNoValue( error.message() ) ); }
@Test public void shouldRespondWith404WhenSettingRelationshipPropertyOnNonExistingRelationship() throws Exception { long relationshipId = 99999999; String json = "\"Mattias\""; Response response = service.setRelationshipProperty( relationshipId, "name", json ); assertEquals( 404, response.getStatus() ); assertEquals( Statement.EntityNotFound.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldRespondWith400WhenSettingRelationshipPropertyWithBadJson() throws Exception { long relationshipId = helper.createRelationship( "KNOWS" ); String json = "}Mattias"; Response response = service.setRelationshipProperty( relationshipId, "name", json ); assertEquals( 400, response.getStatus() ); assertEquals( Request.InvalidFormat.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldGet404WhenRemovingNonExistentRelationship() throws Exception { long relationshipId = helper.createRelationship( "KNOWS" ); Response response = service.deleteRelationship( relationshipId + 1000 ); assertEquals( 404, response.getStatus() ); assertEquals( Statement.EntityNotFound.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldRespondWith404ForRemoveNodePropertiesForNonExistingNode() throws Exception { long nodeId = 999999; Response response = service.deleteAllNodeProperties( nodeId ); assertEquals( 404, response.getStatus() ); assertEquals( Statement.EntityNotFound.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldRespondWith400ForSetNodePropertyWithInvalidJson() throws Exception { String key = "foo"; String json = "{invalid json"; Response response = service.setNodeProperty( 999999, key, json ); assertEquals( 400, response.getStatus() ); assertEquals( Request.InvalidFormat.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldRespondWith404WhenSettingPropertiesOnNodeThatDoesNotExist() throws Exception { Response response = service.setAllNodeProperties( 9000000000000L, "{\"foo\" : \"bar\"}" ); assertEquals( 404, response.getStatus() ); assertEquals( Statement.EntityNotFound.code().serialize(), singleErrorCode( response ) ); }
@Override protected void serialize( MappingSerializer serializer ) { serializer.putString( "code", error.status().code().serialize() ); serializer.putString( "message", error.getMessage() ); if ( error.shouldSerializeStackTrace() ) { serializer.putString( "stackTrace", error.getStackTraceAsString() ); } }