Permalink
Browse files

in case of schema validation failure, the exception body is returned …

…as body of response
  • Loading branch information...
chemicL committed Feb 17, 2016
1 parent 7fc4ff6 commit 70c8e793cf0e5ca0fb5ccaff43ba0e0593ace84d
@@ -154,7 +154,7 @@ private SchemaEntry handleRegisterRequest(String path, String schema)
ClientResponse cr = e.getResponse();
if (ClientResponse.Status.fromStatusCode(cr.getStatus()).equals(
ClientResponse.Status.FORBIDDEN)) {
throw new SchemaValidationException("Invalid schema: " + schema);
throw new SchemaValidationException("Invalid schema: " + schema + ". Reason: " + cr.getEntity(String.class));
} else {
//any other status should return null
return null;
@@ -208,7 +208,7 @@ private SchemaEntry handleRegisterRequest(String path, String schema, boolean re
} catch (UniformInterfaceException e) {
ClientResponse cr = e.getResponse();
if (ClientResponse.Status.fromStatusCode(cr.getStatus()).equals(FORBIDDEN)) {
throw new SchemaValidationException("Invalid schema: " + schema);
throw new SchemaValidationException("Invalid schema: " + schema + ". Reason: " + cr.getEntity(String.class));
} else {
//any other status should return null
handleException(e, format("Failed to register new schema for subject %s", getName()), resourceNotFoundExpected);
@@ -195,9 +195,9 @@ public String idFromSchema(@PathParam("subject") String subject, String schema)
* The subject name to register the schema in
* @param schema
* The schema to register
* @return A 200 response with the corresponding id if successful, a 403
* forbidden response if the schema fails validation, or a 404 not
* found response if the subject does not exist
* @return A 200 response with the corresponding id if successful,
* a 403 forbidden response with exception message if the schema fails validation,
* or a 404 not found response if the subject does not exist
*/
@PUT
@Path("{subject}/register")
@@ -206,7 +206,7 @@ public Response addSchema(@PathParam("subject") String subject, String schema) {
try {
return Response.ok(getSubject(subject).register(schema).getId()).build();
} catch (SchemaValidationException e) {
return Response.status(Status.FORBIDDEN).build();
return Response.status(Status.FORBIDDEN).entity(e.getMessage()).build();
}
}
@@ -223,8 +223,8 @@ public Response addSchema(@PathParam("subject") String subject, String schema) {
* the schema to attempt to register
* @return a 200 response with the id of the newly registered schema, or a 404
* response if the subject or id does not exist or a 409 conflict if
* the id does not match the latest id or a 403 forbidden response if
* the schema failed validation
* the id does not match the latest id or a 403 forbidden response
* with exception message if the schema fails validation
*/
@PUT
@Path("{subject}/register_if_latest/{latestId: .*}")
@@ -246,7 +246,7 @@ public Response addSchema(@PathParam("subject") String subject,
}
return Response.ok(created.getId()).build();
} catch (SchemaValidationException e) {
return Response.status(Status.FORBIDDEN).build();
return Response.status(Status.FORBIDDEN).entity(e.getMessage()).build();
}
}
@@ -18,19 +18,8 @@
package org.schemarepo.server;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import com.sun.jersey.api.NotFoundException;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -39,8 +28,19 @@
import org.schemarepo.ValidatorFactory;
import org.schemarepo.json.GsonJsonUtil;
import com.sun.jersey.api.NotFoundException;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class TestRESTRepository {
@@ -134,4 +134,27 @@ public void testInfluenceOfMediaTypeFailure() {
assertEquals(Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatus());
}
@Test
public void testSchemaGetsCreated() {
repo.createSubject("dummy", new MultivaluedMapImpl());
Response response = repo.addSchema("dummy", "schema");
assertEquals(Status.OK.getStatusCode(), response.getStatus());
}
@Test(expected = NotFoundException.class)
public void testSchemaFailsCreationOnMissingSubject() {
repo.addSchema("missing", "schema");
}
@Test
public void testFailingValidationReportsErrors() {
MultivaluedMapImpl configParams = new MultivaluedMapImpl();
configParams.putSingle("repo.validators", "repo.reject");
repo.createSubject("dummy", configParams);
Response response = repo.addSchema("dummy", "schema");
assertEquals(Status.FORBIDDEN.getStatusCode(), response.getStatus());
assertThat((String) response.getEntity(), containsString("repo.validator.reject validator always rejects validation"));
}
}

0 comments on commit 70c8e79

Please sign in to comment.