Search in sources :

Example 11 with SerializationPolicy

use of com.google.gwt.user.server.rpc.SerializationPolicy in project pentaho-platform by pentaho.

the class AbstractGwtRpcTest method testInvokeServiceClassLogsAndThrowsIfRPCThrowsSerializationException.

@Test
public void testInvokeServiceClassLogsAndThrowsIfRPCThrowsSerializationException() throws NoSuchMethodException {
    ServiceClassWhichDoesNotImplementInterface target = new ServiceClassWhichDoesNotImplementInterface();
    Method targetMethod = target.getClass().getMethod("method", String.class);
    ClassLoader targetClassLoader = mock(ClassLoader.class);
    Method serviceSpecialMethod = ServiceInterface.class.getMethod("method", String.class);
    Object[] rpcParameters = new Object[] { "id1" };
    SerializationPolicy policy = mock(SerializationPolicy.class);
    RPCRequest rpcRequest = new RPCRequest(serviceSpecialMethod, rpcParameters, policy, 0);
    HttpServletRequest httpRequestMock = mock(HttpServletRequest.class);
    TestGwtRpc gwtRpcSpy = spy(new TestGwtRpc(httpRequestMock));
    doReturn(target).when(gwtRpcSpy).getTarget();
    doReturn(targetClassLoader).when(gwtRpcSpy).getTargetClassLoader();
    doReturn(rpcRequest).when(gwtRpcSpy).getRequest();
    SerializationException error = new SerializationException();
    try (MockedStatic<RPC> rpc = Mockito.mockStatic(RPC.class)) {
        rpc.when(() -> RPC.invokeAndEncodeResponse(target, targetMethod, rpcParameters, policy)).thenThrow(error);
        try {
            gwtRpcSpy.invoke();
            fail();
        } catch (GwtRpcProxyException ex) {
            assertEquals(error, ex.getCause());
            verify(loggerMock).error(nullable(String.class), eq(error));
        }
    }
}
Also used : SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) SerializationException(com.google.gwt.user.client.rpc.SerializationException) RPC(com.google.gwt.user.server.rpc.RPC) Method(java.lang.reflect.Method) HttpServletRequest(javax.servlet.http.HttpServletRequest) RPCRequest(com.google.gwt.user.server.rpc.RPCRequest) GwtRpcProxyException(org.pentaho.platform.web.servlet.GwtRpcProxyException)

Example 12 with SerializationPolicy

use of com.google.gwt.user.server.rpc.SerializationPolicy in project pentaho-platform by pentaho.

the class AbstractGwtRpcTest method testInvokeRunsInTargetClassLoader.

@Test
public void testInvokeRunsInTargetClassLoader() throws NoSuchMethodException, SerializationException {
    ServiceClassWhichDoesNotImplementInterface target = new ServiceClassWhichDoesNotImplementInterface();
    Method targetMethod = target.getClass().getMethod("method", String.class);
    ClassLoader targetClassLoader = mock(ClassLoader.class);
    Method serviceMethod = ServiceInterface.class.getMethod("method", String.class);
    Object[] rpcParameters = new Object[] { "id1" };
    SerializationPolicy policy = mock(SerializationPolicy.class);
    RPCRequest rpcRequest = new RPCRequest(serviceMethod, rpcParameters, policy, 0);
    HttpServletRequest httpRequestMock = mock(HttpServletRequest.class);
    String response = "rpc response";
    TestGwtRpc gwtRpcSpy = spy(new TestGwtRpc(httpRequestMock));
    doReturn(target).when(gwtRpcSpy).getTarget();
    doReturn(targetMethod).when(gwtRpcSpy).getTargetMethod(target.getClass(), rpcRequest);
    doReturn(targetClassLoader).when(gwtRpcSpy).getTargetClassLoader();
    doReturn(rpcRequest).when(gwtRpcSpy).getRequest();
    // Stub invokeCore with:
    doAnswer((Answer<String>) invocationOnMock -> {
        ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
        assertEquals(targetClassLoader, currentClassLoader);
        return response;
    }).when(gwtRpcSpy).invokeCore(target, targetMethod, rpcRequest);
    gwtRpcSpy.invoke();
    verify(gwtRpcSpy).invokeCore(target, targetMethod, rpcRequest);
}
Also used : SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) HttpServletRequest(javax.servlet.http.HttpServletRequest) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ServletException(javax.servlet.ServletException) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) RunWith(org.junit.runner.RunWith) GwtRpcProxyException(org.pentaho.platform.web.servlet.GwtRpcProxyException) Mockito.spy(org.mockito.Mockito.spy) Function(java.util.function.Function) RPCServletUtils(com.google.gwt.user.server.rpc.RPCServletUtils) Answer(org.mockito.stubbing.Answer) Mockito.doThrow(org.mockito.Mockito.doThrow) HttpServletRequest(javax.servlet.http.HttpServletRequest) RPC(com.google.gwt.user.server.rpc.RPC) RPCRequest(com.google.gwt.user.server.rpc.RPCRequest) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Mockito.doAnswer(org.mockito.Mockito.doAnswer) SerializationPolicyLoader(com.google.gwt.user.server.rpc.SerializationPolicyLoader) Assert.fail(org.junit.Assert.fail) GwtRpcUtil(org.pentaho.platform.web.gwt.rpc.impl.GwtRpcUtil) ParseException(java.text.ParseException) RemoteService(com.google.gwt.user.client.rpc.RemoteService) SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) Method(java.lang.reflect.Method) Mockito.doReturn(org.mockito.Mockito.doReturn) SerializationException(com.google.gwt.user.client.rpc.SerializationException) MalformedURLException(java.net.MalformedURLException) ThrowingSupplier(org.pentaho.platform.web.gwt.rpc.util.ThrowingSupplier) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) MockedStatic(org.mockito.MockedStatic) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) Nullable(edu.umd.cs.findbugs.annotations.Nullable) Log(org.apache.commons.logging.Log) ServletContext(javax.servlet.ServletContext) LogFactory(org.apache.commons.logging.LogFactory) org.junit(org.junit) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.reset(org.mockito.Mockito.reset) Assert.assertEquals(org.junit.Assert.assertEquals) IncompatibleRemoteServiceException(com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException) InputStream(java.io.InputStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) RPCRequest(com.google.gwt.user.server.rpc.RPCRequest) Method(java.lang.reflect.Method) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 13 with SerializationPolicy

use of com.google.gwt.user.server.rpc.SerializationPolicy in project pentaho-platform by pentaho.

the class AbstractGwtRpcTest method testGetSerializationPolicyCallsLoadSerializationPolicyWithModulePathAndStrongName.

@Test
public void testGetSerializationPolicyCallsLoadSerializationPolicyWithModulePathAndStrongName() {
    String moduleBaseURL = "http://localhost:8080/pentaho/content/data-access/resources/gwt/";
    String appContextPath = "/pentaho";
    String moduleContextPath = "/content/data-access/resources/gwt/";
    String strongName = "ABCDF12345";
    HttpServletRequest httpRequestMock = mock(HttpServletRequest.class);
    SerializationPolicy policyMock = mock(SerializationPolicy.class);
    TestGwtRpc gwtRpcSpy = spy(new TestGwtRpc(httpRequestMock));
    doReturn(appContextPath).when(gwtRpcSpy).getAppContextPath();
    doReturn(policyMock).when(gwtRpcSpy).loadSerializationPolicy(moduleContextPath, strongName);
    SerializationPolicy result = gwtRpcSpy.getSerializationPolicy(moduleBaseURL, strongName);
    assertEquals(policyMock, result);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 14 with SerializationPolicy

use of com.google.gwt.user.server.rpc.SerializationPolicy in project pentaho-platform by pentaho.

the class GwtRpcSerializationPolicyCacheTest method testGetSerializationPolicyWithNormalKeyIsSupported.

@Test
public void testGetSerializationPolicyWithNormalKeyIsSupported() {
    GwtRpcSerializationPolicyCache cache = new GwtRpcSerializationPolicyCache();
    SerializationPolicy policy = mock(SerializationPolicy.class);
    testSerializationPolicyIsNewAndCreated(cache, "url", "abc", policy);
    testSerializationPolicyIsInCache(cache, "url", "abc", policy);
}
Also used : SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) IGwtRpcSerializationPolicyCache(org.pentaho.platform.web.gwt.rpc.IGwtRpcSerializationPolicyCache) Test(org.junit.Test)

Example 15 with SerializationPolicy

use of com.google.gwt.user.server.rpc.SerializationPolicy in project pentaho-platform by pentaho.

the class AbstractGwtRpc method getSerializationPolicyCore.

@NonNull
private SerializationPolicy getSerializationPolicyCore(@Nullable String moduleBaseURL, @Nullable String strongName) {
    if (moduleBaseURL == null) {
        logger.error(Messages.getInstance().getErrorString("GwtRpcPluginProxyServlet.ERROR_0004_MALFORMED_URL", ""));
        return getDefaultSerializationPolicy();
    }
    String modulePath;
    try {
        modulePath = new URL(moduleBaseURL).getPath();
    } catch (MalformedURLException ex) {
        logger.error(Messages.getInstance().getErrorString("GwtRpcPluginProxyServlet.ERROR_0004_MALFORMED_URL", moduleBaseURL), ex);
        return getDefaultSerializationPolicy();
    }
    String appContextPath = getAppContextPath();
    modulePath = GwtRpcUtil.scrubWebAppRoot(modulePath, appContextPath);
    if (!modulePath.startsWith(appContextPath)) {
        logger.error(Messages.getInstance().getErrorString("GwtRpcPluginProxyServlet.ERROR_0004_MALFORMED_URL", moduleBaseURL));
        return getDefaultSerializationPolicy();
    }
    String moduleContextPath = modulePath.substring(appContextPath.length());
    SerializationPolicy serializationPolicy = loadSerializationPolicy(moduleContextPath, strongName);
    return serializationPolicy != null ? serializationPolicy : getDefaultSerializationPolicy();
}
Also used : SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

SerializationPolicy (com.google.gwt.user.server.rpc.SerializationPolicy)34 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 IOException (java.io.IOException)11 InputStream (java.io.InputStream)11 Test (org.junit.Test)9 RPC (com.google.gwt.user.server.rpc.RPC)5 RPCRequest (com.google.gwt.user.server.rpc.RPCRequest)5 SerializationPolicyLoader (com.google.gwt.user.server.rpc.SerializationPolicyLoader)5 Method (java.lang.reflect.Method)5 URL (java.net.URL)5 PentahoSystem (org.pentaho.platform.engine.core.system.PentahoSystem)5 PluginUtil (org.pentaho.platform.plugin.services.pluginmgr.PluginUtil)5 MalformedURLException (java.net.MalformedURLException)4 GwtRpcProxyException (org.pentaho.platform.web.servlet.GwtRpcProxyException)4 NonNull (edu.umd.cs.findbugs.annotations.NonNull)3 ParseException (java.text.ParseException)3 IPluginResourceLoader (org.pentaho.platform.api.engine.IPluginResourceLoader)3 SerializationException (com.google.gwt.user.client.rpc.SerializationException)2 SerializationPolicyProvider (com.google.gwt.user.server.rpc.SerializationPolicyProvider)2