This is an automated email from the ASF dual-hosted git repository. ijuma pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/kafka.git
commit a7e49027b2b95bc8656ee9f419306c559ad1cb5c Author: Romain Hardouin <romain_hardo...@yahoo.fr> AuthorDate: Sat Sep 9 00:21:00 2017 +0200 MINOR: Catch JsonMappingException subclass (#3821) Handle InvalidTypeIdException as NOT_IMPLEMENTED and add unit tests for all exceptions. Reviewers: Colin P. Mccabe <cmcc...@confluent.io>, Rajini Sivaram <rajinisiva...@googlemail.com> --- .../kafka/trogdor/rest/RestExceptionMapper.java | 4 +- .../trogdor/rest/RestExceptionMapperTest.java | 96 ++++++++++++++++++++++ 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/tools/src/main/java/org/apache/kafka/trogdor/rest/RestExceptionMapper.java b/tools/src/main/java/org/apache/kafka/trogdor/rest/RestExceptionMapper.java index b063a9a..f62a775 100644 --- a/tools/src/main/java/org/apache/kafka/trogdor/rest/RestExceptionMapper.java +++ b/tools/src/main/java/org/apache/kafka/trogdor/rest/RestExceptionMapper.java @@ -38,12 +38,12 @@ public class RestExceptionMapper implements ExceptionMapper<Throwable> { } if (e instanceof NotFoundException) { return buildResponse(Response.Status.NOT_FOUND, e); + } else if (e instanceof InvalidTypeIdException) { + return buildResponse(Response.Status.NOT_IMPLEMENTED, e); } else if (e instanceof JsonMappingException) { return buildResponse(Response.Status.BAD_REQUEST, e); } else if (e instanceof ClassNotFoundException) { return buildResponse(Response.Status.NOT_IMPLEMENTED, e); - } else if (e instanceof InvalidTypeIdException) { - return buildResponse(Response.Status.NOT_IMPLEMENTED, e); } else if (e instanceof SerializationException) { return buildResponse(Response.Status.BAD_REQUEST, e); } else { diff --git a/tools/src/test/java/org/apache/kafka/trogdor/rest/RestExceptionMapperTest.java b/tools/src/test/java/org/apache/kafka/trogdor/rest/RestExceptionMapperTest.java new file mode 100644 index 0000000..c40f958 --- /dev/null +++ b/tools/src/test/java/org/apache/kafka/trogdor/rest/RestExceptionMapperTest.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.trogdor.rest; + +import static org.junit.Assert.assertEquals; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.exc.InvalidTypeIdException; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.core.Response; +import org.apache.kafka.common.errors.SerializationException; +import org.junit.Test; + +public class RestExceptionMapperTest { + + @Test + public void testToResponseNotFound() { + RestExceptionMapper mapper = new RestExceptionMapper(); + Response resp = mapper.toResponse(new NotFoundException()); + assertEquals(resp.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); + } + + @Test + public void testToResponseInvalidTypeIdException() { + RestExceptionMapper mapper = new RestExceptionMapper(); + JsonParser parser = null; + JavaType type = null; + Response resp = mapper.toResponse(InvalidTypeIdException.from(parser, "dummy msg", type, "dummy typeId")); + assertEquals(resp.getStatus(), Response.Status.NOT_IMPLEMENTED.getStatusCode()); + } + + @Test + public void testToResponseJsonMappingException() { + RestExceptionMapper mapper = new RestExceptionMapper(); + JsonParser parser = null; + Response resp = mapper.toResponse(JsonMappingException.from(parser, "dummy msg")); + assertEquals(resp.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); + } + + @Test + public void testToResponseClassNotFoundException() { + RestExceptionMapper mapper = new RestExceptionMapper(); + Response resp = mapper.toResponse(new ClassNotFoundException()); + assertEquals(resp.getStatus(), Response.Status.NOT_IMPLEMENTED.getStatusCode()); + } + + @Test + public void testToResponseSerializationException() { + RestExceptionMapper mapper = new RestExceptionMapper(); + Response resp = mapper.toResponse(new SerializationException()); + assertEquals(resp.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); + } + + @Test + public void testToResponseUnknownException() { + RestExceptionMapper mapper = new RestExceptionMapper(); + Response resp = mapper.toResponse(new Exception("Unkown exception")); + assertEquals(resp.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + } + + @Test(expected = NotFoundException.class) + public void testToExceptionNotFoundException() throws Exception { + RestExceptionMapper.toException(Response.Status.NOT_FOUND.getStatusCode(), "Not Found"); + } + + @Test(expected = ClassNotFoundException.class) + public void testToExceptionClassNotFoundException() throws Exception { + RestExceptionMapper.toException(Response.Status.NOT_IMPLEMENTED.getStatusCode(), "Not Implemented"); + } + + @Test(expected = SerializationException.class) + public void testToExceptionSerializationException() throws Exception { + RestExceptionMapper.toException(Response.Status.BAD_REQUEST.getStatusCode(), "Bad Request"); + } + + @Test(expected = RuntimeException.class) + public void testToExceptionRuntimeException() throws Exception { + RestExceptionMapper.toException(-1, "Unkown status code"); + } +} -- To stop receiving notification emails like this one, please contact "commits@kafka.apache.org" <commits@kafka.apache.org>.