Search in sources :

Example 1 with FormException

use of org.alfresco.repo.forms.FormException in project alfresco-remote-api by Alfresco.

the class RepositoryContainerTest method testHideExceptions.

public void testHideExceptions() throws Exception {
    final String messageFormException = "Failed to persist field 'prop_cm_name'";
    final String messageAuthenticationException = "Authentication failed for Web Script";
    RepositoryContainer repoContainer = (RepositoryContainer) getServer().getApplicationContext().getBean("webscripts.container");
    RepositoryContainer repoContainerMock = Mockito.spy(repoContainer);
    Class<?>[] defaultConfiguration = repoContainerMock.getNotPublicExceptions();
    try {
        List<Class<?>> testExceptoins = new LinkedList<Class<?>>();
        testExceptoins.add(SQLException.class);
        repoContainerMock.setNotPublicExceptions(testExceptoins);
        // case: AlfrescoRuntimeException with SQLException cause
        Mockito.doAnswer(new Answer<Object>() {

            public Object answer(InvocationOnMock invocation) {
                throw new AlfrescoRuntimeException("AlfrescoRuntimeException", new SQLException("SQLException"));
            }
        }).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
        try {
            repoContainerMock.executeScript(null, null, null);
        } catch (Exception e) {
            assertNull("SQLException cause should be hidden for client", ExceptionStackUtil.getCause(e, new Class[] { SQLException.class }));
            assertTrue("Details should be in the server logs.", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
        }
        // case: AlfrescoRuntimeException with NOT SQLException cause
        Mockito.doAnswer(new Answer<Object>() {

            public Object answer(InvocationOnMock invocation) {
                throw new AlfrescoRuntimeException("AlfrescoRuntimeException", new NullPointerException());
            }
        }).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
        try {
            repoContainerMock.executeScript(null, null, null);
        } catch (Exception e) {
            assertNotNull("NullPointerException cause should be visible for client", ExceptionStackUtil.getCause(e, new Class[] { NullPointerException.class }));
            assertFalse("Details should be available for client", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
        }
        // case: RuntimeException with SQLException cause
        Mockito.doAnswer(new Answer<Object>() {

            public Object answer(InvocationOnMock invocation) {
                throw new RuntimeException("AlfrescoRuntimeException", new SQLException("SQLException"));
            }
        }).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
        try {
            repoContainerMock.executeScript(null, null, null);
        } catch (Exception e) {
            assertNull("SQLException cause should be hidden for client", ExceptionStackUtil.getCause(e, new Class[] { SQLException.class }));
            assertTrue("Details should be in the server logs.", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
        }
        // case: FormException
        Mockito.doAnswer(new Answer<Object>() {

            public Object answer(InvocationOnMock invocation) {
                throw new FormException(messageFormException);
            }
        }).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
        try {
            repoContainerMock.executeScript(null, null, null);
        } catch (Exception e) {
            assertTrue("FormException should be visible for client", e instanceof FormException);
            assertFalse("Details should be available for client", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
            assertTrue("Actual message should be available for client", e.getMessage().contains(messageFormException));
        }
        // case: WebScriptException
        Mockito.doAnswer(new Answer<Object>() {

            public Object answer(InvocationOnMock invocation) {
                throw new WebScriptException(HttpServletResponse.SC_UNAUTHORIZED, messageAuthenticationException);
            }
        }).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
        try {
            repoContainerMock.executeScript(null, null, null);
        } catch (Exception e) {
            assertTrue("WebScriptException should be visible for client", e instanceof WebScriptException);
            assertFalse("Details should be available for client", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
            assertTrue("Actual message should be available for client", e.getMessage().contains(messageAuthenticationException));
        }
    } finally {
        repoContainerMock.setNotPublicExceptions(Arrays.asList(defaultConfiguration));
    }
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) SQLException(java.sql.SQLException) LinkedList(java.util.LinkedList) SQLException(java.sql.SQLException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) FormException(org.alfresco.repo.forms.FormException) FormException(org.alfresco.repo.forms.FormException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) Authenticator(org.springframework.extensions.webscripts.Authenticator)

Aggregations

SQLException (java.sql.SQLException)1 LinkedList (java.util.LinkedList)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 FormException (org.alfresco.repo.forms.FormException)1 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Authenticator (org.springframework.extensions.webscripts.Authenticator)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)1 WebScriptResponse (org.springframework.extensions.webscripts.WebScriptResponse)1