Repository: usergrid Updated Branches: refs/heads/master 40be90f5e -> 949105226
Added fixes for USERGRID-767 and added tests looking for cases that would previously cause uncaught exception errors. Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/3c96d5ff Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/3c96d5ff Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/3c96d5ff Branch: refs/heads/master Commit: 3c96d5ff25ad0ddb015a76dfc14a32e851ce940d Parents: dd8d027 Author: George Reyes <g...@apache.org> Authored: Tue Jan 5 16:04:18 2016 -0800 Committer: George Reyes <g...@apache.org> Committed: Tue Jan 5 16:04:18 2016 -0800 ---------------------------------------------------------------------- .../exceptions/AbstractExceptionMapper.java | 14 +++- .../usergrid/rest/ExceptionResourceIT.java | 81 ++++++++++++++++++++ 2 files changed, 92 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid/blob/3c96d5ff/stack/rest/src/main/java/org/apache/usergrid/rest/exceptions/AbstractExceptionMapper.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/exceptions/AbstractExceptionMapper.java b/stack/rest/src/main/java/org/apache/usergrid/rest/exceptions/AbstractExceptionMapper.java index 9e0f359..b428567 100644 --- a/stack/rest/src/main/java/org/apache/usergrid/rest/exceptions/AbstractExceptionMapper.java +++ b/stack/rest/src/main/java/org/apache/usergrid/rest/exceptions/AbstractExceptionMapper.java @@ -18,10 +18,13 @@ package org.apache.usergrid.rest.exceptions; import org.apache.usergrid.rest.ApiResponse; +import org.apache.usergrid.services.exceptions.UnsupportedServiceOperationException; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.NotAllowedException; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.Response; @@ -30,6 +33,8 @@ import javax.ws.rs.ext.ExceptionMapper; import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE; import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; +import static javax.ws.rs.core.Response.Status.METHOD_NOT_ALLOWED; +import static javax.ws.rs.core.Response.Status.NOT_IMPLEMENTED; import static javax.ws.rs.core.Response.Status.OK; import static org.apache.commons.lang.StringUtils.isNotBlank; import static org.apache.usergrid.rest.utils.JSONPUtils.isJavascript; @@ -55,8 +60,11 @@ public abstract class AbstractExceptionMapper<E extends java.lang.Throwable> imp @Override public Response toResponse( E e ) { + if(e instanceof UnsupportedServiceOperationException || e instanceof NotAllowedException || e instanceof UnsupportedOperationException){ + return toResponse( METHOD_NOT_ALLOWED,e ); + } // if we don't know what type of error it is then it's a 500 - return toResponse( INTERNAL_SERVER_ERROR, (E) new UncaughtException(e) ); + return toResponse( INTERNAL_SERVER_ERROR, e ); } @@ -73,7 +81,7 @@ public abstract class AbstractExceptionMapper<E extends java.lang.Throwable> imp } else { if (logger.isDebugEnabled()) { - logger.debug(e.getClass().getCanonicalName() + " Uncaught Exception (" + status + ")", e); + logger.debug(e.getClass().getCanonicalName() + " Following Exception Thrown (" + status + ")", e); } } @@ -105,7 +113,7 @@ public abstract class AbstractExceptionMapper<E extends java.lang.Throwable> imp // only log real errors as errors logger.error( "Server Error (" + status + "):\n" + jsonResponse ); } else if ( logger.isDebugEnabled() ) { - logger.debug( "Server Error (" + status + "):\n" + jsonResponse ); + logger.debug( "Client Error (" + status + "):\n" + jsonResponse ); } String callback = httpServletRequest.getParameter( "callback" ); if ( isJSONP() && isNotBlank( callback ) ) { http://git-wip-us.apache.org/repos/asf/usergrid/blob/3c96d5ff/stack/rest/src/test/java/org/apache/usergrid/rest/ExceptionResourceIT.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/ExceptionResourceIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/ExceptionResourceIT.java new file mode 100644 index 0000000..49dd317 --- /dev/null +++ b/stack/rest/src/test/java/org/apache/usergrid/rest/ExceptionResourceIT.java @@ -0,0 +1,81 @@ +package org.apache.usergrid.rest; + + +import java.util.Map; + +import javax.ws.rs.NotAllowedException; +import javax.ws.rs.ServerErrorException; + +import org.junit.Test; + +import org.apache.usergrid.persistence.Entity; +import org.apache.usergrid.persistence.index.utils.UUIDUtils; +import org.apache.usergrid.rest.test.resource.AbstractRestIT; +import org.apache.usergrid.rest.test.resource.model.Organization; +import org.apache.usergrid.rest.test.resource.model.Token; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + + +/** + * Created by ApigeeCorporation on 1/5/16. + */ +public class ExceptionResourceIT extends AbstractRestIT{ + + @Test + public void testNonExistingEndpoint(){ + try { + + clientSetup.getRestClient() + .pathResource( getOrgAppPath( "non_existant_delete_endpoint" ) ).delete( ); + }catch(NotAllowedException e){ + // assertNotNull(e); + assertEquals( 405,e.getResponse().getStatus()); + } + } + + //test uncovered endpoints + @Test + public void testNotImplementedException(){ + try { +// Organization organization = createOrgPayload( "testCreateDuplicateOrgName", null ); +// Organization orgCreatedResponse = clientSetup.getRestClient().management().orgs().post( organization ); +// this.refreshIndex(); +// +// assertNotNull( orgCreatedResponse ); +// +// // Ensure that the token from the newly created organization works. +// Token tokenPayload = new Token( "password", organization.getUsername(), organization.getPassword() ); +// Token tokenReturned = clientSetup.getRestClient().management().token() +// .post( false, Token.class, tokenPayload, null ); +// this.management().token().setToken(tokenReturned); + + + clientSetup.getRestClient().management().orgs().delete( true ); + + }catch(NotAllowedException e){ + // assertNotNull(e); + assertEquals( 405,e.getResponse().getStatus()); + } + } + @Test + public void testDeleteFromWrongEndpoint(){ + try { + clientSetup.getRestClient() + .pathResource( clientSetup.getOrganizationName() + "/" + clientSetup.getAppName() ).delete( ); + + + }catch(NotAllowedException e){ + // assertNotNull(e); + assertEquals( 405,e.getResponse().getStatus()); + } + } + + + + public Organization createOrgPayload(String baseName,Map properties ){ + String orgName = baseName + UUIDUtils.newTimeUUID(); + return new Organization( orgName+ UUIDUtils.newTimeUUID(),orgName,orgName+"@usergrid",orgName,orgName, properties); + } +}