Search in sources :

Example 6 with SerializationPolicy

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

the class PluginGwtRpcTest method testLoadSerializationPolicyWhenPluginResource.

@Test
public void testLoadSerializationPolicyWhenPluginResource() throws MalformedURLException {
    String pathInfo = "/serviceName";
    String pluginId = "data-access";
    String moduleContextPath = "/content/data-access/resources/gwt/";
    String strongName = "ABC";
    String policyFilename = strongName + ".gwt.rpc";
    URL policy1Url = new URL("file:///resources/gwt/a/" + policyFilename);
    try (MockedStatic<PluginUtil> pluginUtilMock = mockStatic(PluginUtil.class)) {
        pluginUtilMock.when(() -> PluginUtil.getPluginIdFromPath(moduleContextPath)).thenReturn(pluginId);
        // ----
        // There is no configured SP bean.
        pentahoSystemMock = mockStatic(PentahoSystem.class);
        pentahoSystemMock.when(() -> PentahoSystem.get(eq(SerializationPolicy.class), eq(null), anyMap())).thenReturn(null);
        // ---
        ClassLoader pluginClassLoader = mock(ClassLoader.class);
        when(PluginUtil.getClassLoaderForService(moduleContextPath)).thenReturn(pluginClassLoader);
        // ---
        // More than one serialization policy resource found.
        IPluginResourceLoader resLoaderMock = mock(IPluginResourceLoader.class);
        when(resLoaderMock.findResources(pluginClassLoader, policyFilename)).thenReturn(Collections.singletonList(policy1Url));
        when(PentahoSystem.get(eq(IPluginResourceLoader.class), eq(null))).thenReturn(resLoaderMock);
        // ---
        InputStream pluginPolicyInputStreamMock = mock(InputStream.class);
        // Spying requires a non-final class. Lambda expression is not suitable.
        @SuppressWarnings("Convert2Lambda") ThrowingSupplier<InputStream, IOException> pluginPolicyInputStreamSupplierSpy = spy(new ThrowingSupplier<InputStream, IOException>() {

            @Override
            public InputStream get() {
                return pluginPolicyInputStreamMock;
            }
        });
        // ---
        SerializationPolicy pluginPolicyMock = mock(SerializationPolicy.class);
        try (MockedStatic<AbstractGwtRpc> abstractGwtRpcMock = mockStatic(AbstractGwtRpc.class)) {
            abstractGwtRpcMock.when(() -> AbstractGwtRpc.loadSerializationPolicyFromInputStream(pluginPolicyInputStreamSupplierSpy, policyFilename)).thenReturn(pluginPolicyMock);
            // ---
            HttpServletRequest httpRequestMock = setupHttpRequest(pathInfo);
            PluginGwtRpc gwtRpcSpy = spy(new PluginGwtRpc(httpRequestMock));
            doReturn(pluginPolicyInputStreamSupplierSpy).when(gwtRpcSpy).getInputStreamSupplier(policy1Url);
            // ---
            SerializationPolicy result = gwtRpcSpy.loadSerializationPolicy(moduleContextPath, strongName);
            // ---
            assertEquals(pluginPolicyMock, result);
            verify(loggerMock, times(0)).error(nullable(String.class));
            verify(loggerMock, times(0)).warn(nullable(String.class));
        }
    }
}
Also used : SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) InputStream(java.io.InputStream) IOException(java.io.IOException) PentahoSystem(org.pentaho.platform.engine.core.system.PentahoSystem) URL(java.net.URL) IPluginResourceLoader(org.pentaho.platform.api.engine.IPluginResourceLoader) HttpServletRequest(javax.servlet.http.HttpServletRequest) PluginUtil(org.pentaho.platform.plugin.services.pluginmgr.PluginUtil) Test(org.junit.Test)

Example 7 with SerializationPolicy

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

the class PluginGwtRpcTest method testLoadSerializationPolicyLogsWarningAndReturnsFirstIfMoreThanOnePluginResource.

@Test
public void testLoadSerializationPolicyLogsWarningAndReturnsFirstIfMoreThanOnePluginResource() throws MalformedURLException {
    String pathInfo = "/serviceName";
    String pluginId = "data-access";
    String moduleContextPath = "/content/data-access/resources/gwt/";
    String strongName = "ABC";
    String policyFilename = strongName + ".gwt.rpc";
    URL policy1Url = new URL("file:///resources/gwt/a/" + policyFilename);
    URL policy2Url = new URL("file:///resources/gwt/b/" + policyFilename);
    try (MockedStatic<PluginUtil> pluginUtilMock = mockStatic(PluginUtil.class)) {
        pluginUtilMock.when(() -> PluginUtil.getPluginIdFromPath(moduleContextPath)).thenReturn(pluginId);
        // ----
        // There is no configured SP bean.
        pentahoSystemMock = mockStatic(PentahoSystem.class);
        pentahoSystemMock.when(() -> PentahoSystem.get(eq(SerializationPolicy.class), eq(null), anyMap())).thenReturn(null);
        // ---
        ClassLoader pluginClassLoader = mock(ClassLoader.class);
        when(PluginUtil.getClassLoaderForService(moduleContextPath)).thenReturn(pluginClassLoader);
        // ---
        // More than one serialization policy resource found.
        IPluginResourceLoader resLoaderMock = mock(IPluginResourceLoader.class);
        when(resLoaderMock.findResources(pluginClassLoader, policyFilename)).thenReturn(Arrays.asList(policy1Url, policy2Url));
        when(PentahoSystem.get(eq(IPluginResourceLoader.class), eq(null))).thenReturn(resLoaderMock);
        // ---
        InputStream pluginPolicyInputStreamMock = mock(InputStream.class);
        // Spying requires a non-final class. Lambda expression is not suitable.
        @SuppressWarnings("Convert2Lambda") ThrowingSupplier<InputStream, IOException> pluginPolicyInputStreamSupplierSpy = spy(new ThrowingSupplier<InputStream, IOException>() {

            @Override
            public InputStream get() {
                return pluginPolicyInputStreamMock;
            }
        });
        // ---
        SerializationPolicy pluginPolicyMock = mock(SerializationPolicy.class);
        try (MockedStatic<AbstractGwtRpc> abstractGwtRpcMock = mockStatic(AbstractGwtRpc.class)) {
            abstractGwtRpcMock.when(() -> AbstractGwtRpc.loadSerializationPolicyFromInputStream(pluginPolicyInputStreamSupplierSpy, policyFilename)).thenReturn(pluginPolicyMock);
            // ---
            HttpServletRequest httpRequestMock = setupHttpRequest(pathInfo);
            PluginGwtRpc gwtRpcSpy = spy(new PluginGwtRpc(httpRequestMock));
            doReturn(pluginPolicyInputStreamSupplierSpy).when(gwtRpcSpy).getInputStreamSupplier(policy1Url);
            // ---
            SerializationPolicy result = gwtRpcSpy.loadSerializationPolicy(moduleContextPath, strongName);
            // ---
            assertEquals(pluginPolicyMock, result);
            verify(loggerMock, times(1)).warn(nullable(String.class));
        }
    }
}
Also used : SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) InputStream(java.io.InputStream) IOException(java.io.IOException) PentahoSystem(org.pentaho.platform.engine.core.system.PentahoSystem) URL(java.net.URL) IPluginResourceLoader(org.pentaho.platform.api.engine.IPluginResourceLoader) HttpServletRequest(javax.servlet.http.HttpServletRequest) PluginUtil(org.pentaho.platform.plugin.services.pluginmgr.PluginUtil) Test(org.junit.Test)

Example 8 with SerializationPolicy

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

the class PluginGwtRpcTest method testLoadSerializationPolicyLogsErrorAndReturnsNullIfNoPluginResource.

@Test
public void testLoadSerializationPolicyLogsErrorAndReturnsNullIfNoPluginResource() {
    String pathInfo = "/serviceName";
    String pluginId = "data-access";
    String moduleContextPath = "/content/data-access/resources/gwt/";
    String strongName = "ABC";
    String serializationPolicyFilename = strongName + ".gwt.rpc";
    try (MockedStatic<PluginUtil> pluginUtilMock = mockStatic(PluginUtil.class)) {
        pluginUtilMock.when(() -> PluginUtil.getPluginIdFromPath(moduleContextPath)).thenReturn(pluginId);
        // ----
        // There is no configured SP bean.
        pentahoSystemMock = mockStatic(PentahoSystem.class);
        pentahoSystemMock.when(() -> PentahoSystem.get(eq(SerializationPolicy.class), eq(null), anyMap())).thenReturn(null);
        // ---
        ClassLoader pluginClassLoader = mock(ClassLoader.class);
        when(PluginUtil.getClassLoaderForService(moduleContextPath)).thenReturn(pluginClassLoader);
        // ---
        // No serialization policy resource found.
        IPluginResourceLoader resLoaderMock = mock(IPluginResourceLoader.class);
        when(resLoaderMock.findResources(pluginClassLoader, serializationPolicyFilename)).thenReturn(Collections.emptyList());
        when(PentahoSystem.get(eq(IPluginResourceLoader.class), eq(null))).thenReturn(resLoaderMock);
        // ---
        HttpServletRequest httpRequestMock = setupHttpRequest(pathInfo);
        PluginGwtRpc gwtRpc = new PluginGwtRpc(httpRequestMock);
        SerializationPolicy result = gwtRpc.loadSerializationPolicy(moduleContextPath, strongName);
        assertNull(result);
        verify(loggerMock, times(1)).error(nullable(String.class));
    }
}
Also used : SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) HttpServletRequest(javax.servlet.http.HttpServletRequest) PluginUtil(org.pentaho.platform.plugin.services.pluginmgr.PluginUtil) PentahoSystem(org.pentaho.platform.engine.core.system.PentahoSystem) IPluginResourceLoader(org.pentaho.platform.api.engine.IPluginResourceLoader) Test(org.junit.Test)

Example 9 with SerializationPolicy

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

the class SystemGwtRpcTest method testLoadSerializationPolicy.

// endregion
// region Serialization Policy
@Test
public void testLoadSerializationPolicy() {
    String servletPath = "/ws";
    String pathInfo = "/gwt/serviceName";
    String moduleContextPath = "/mantle/";
    String strongName = "ABC";
    String policyFilePath = moduleContextPath + strongName + ".gwt.rpc";
    // ---
    InputStream systemPolicyInputStreamMock = mock(InputStream.class);
    ServletContext servletContextMock = mock(ServletContext.class);
    when(servletContextMock.getResourceAsStream(policyFilePath)).thenReturn(systemPolicyInputStreamMock);
    // ---
    SerializationPolicy systemPolicyMock = mock(SerializationPolicy.class);
    try (MockedStatic<AbstractGwtRpc> rpc = Mockito.mockStatic(AbstractGwtRpc.class)) {
        rpc.when(() -> AbstractGwtRpc.loadSerializationPolicyFromInputStream(any(), eq(policyFilePath))).thenAnswer((Answer<SerializationPolicy>) invocationOnMock -> {
            @SuppressWarnings("unchecked") ThrowingSupplier<InputStream, IOException> inputStreamSupplier = (ThrowingSupplier<InputStream, IOException>) invocationOnMock.getArguments()[0];
            InputStream inputStream = inputStreamSupplier.get();
            assertEquals(systemPolicyInputStreamMock, inputStream);
            return systemPolicyMock;
        });
        // ---
        HttpServletRequest httpRequestMock = setupHttpRequest(servletPath, pathInfo);
        SystemGwtRpc gwtRpcSpy = spy(new SystemGwtRpc(httpRequestMock));
        doReturn(servletContextMock).when(gwtRpcSpy).getServletContext();
        // ---
        SerializationPolicy result = gwtRpcSpy.loadSerializationPolicy(moduleContextPath, strongName);
        // ---
        assertEquals(systemPolicyMock, result);
        rpc.verify(() -> AbstractGwtRpc.loadSerializationPolicyFromInputStream(any(), eq(policyFilePath)), times(1));
    }
}
Also used : SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) ThrowingSupplier(org.pentaho.platform.web.gwt.rpc.util.ThrowingSupplier) Test(org.junit.Test) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) GwtRpcProxyException(org.pentaho.platform.web.servlet.GwtRpcProxyException) Mockito.spy(org.mockito.Mockito.spy) ApplicationContext(org.springframework.context.ApplicationContext) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) MockedStatic(org.mockito.MockedStatic) HttpServletRequest(javax.servlet.http.HttpServletRequest) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) ServletContext(javax.servlet.ServletContext) SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) Mockito.mock(org.mockito.Mockito.mock) HttpServletRequest(javax.servlet.http.HttpServletRequest) InputStream(java.io.InputStream) ServletContext(javax.servlet.ServletContext) ThrowingSupplier(org.pentaho.platform.web.gwt.rpc.util.ThrowingSupplier) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with SerializationPolicy

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

the class AbstractGwtRpcTest method testGetSerializationPolicyReplacesWebAppRootToken.

@Test
public void testGetSerializationPolicyReplacesWebAppRootToken() {
    String moduleBaseURL = "http://localhost:8080/WEBAPP_ROOT//content/data-access/resources/gwt/";
    String modulePath = "/WEBAPP_ROOT//content/data-access/resources/gwt/";
    String modulePathScrubbed = "/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);
    try (MockedStatic<GwtRpcUtil> gwtRpcUtil = Mockito.mockStatic(GwtRpcUtil.class)) {
        gwtRpcUtil.when(() -> GwtRpcUtil.scrubWebAppRoot(modulePath, appContextPath)).thenReturn(modulePathScrubbed);
        gwtRpcSpy.getSerializationPolicy(moduleBaseURL, strongName);
        gwtRpcUtil.verify(() -> GwtRpcUtil.scrubWebAppRoot(modulePath, appContextPath));
        GwtRpcUtil.scrubWebAppRoot(modulePath, appContextPath);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SerializationPolicy(com.google.gwt.user.server.rpc.SerializationPolicy) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GwtRpcUtil(org.pentaho.platform.web.gwt.rpc.impl.GwtRpcUtil)

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