Search in sources :

Example 1 with PluginUtil

use of org.pentaho.platform.plugin.services.pluginmgr.PluginUtil in project pentaho-platform by pentaho.

the class PluginGwtRpcTest method testLoadSerializationPolicyLogsErrorAndReturnsNullIfNoPluginClassLoader.

@Test
public void testLoadSerializationPolicyLogsErrorAndReturnsNullIfNoPluginClassLoader() {
    String pathInfo = "/serviceName";
    String pluginId = "data-access";
    String moduleContextPath = "/content/data-access/resources/gwt/";
    String strongName = "ABC";
    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);
        // ---
        when(PluginUtil.getClassLoaderForService(moduleContextPath)).thenReturn(null);
        // ---
        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) Test(org.junit.Test)

Example 2 with PluginUtil

use of org.pentaho.platform.plugin.services.pluginmgr.PluginUtil 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 3 with PluginUtil

use of org.pentaho.platform.plugin.services.pluginmgr.PluginUtil 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 4 with PluginUtil

use of org.pentaho.platform.plugin.services.pluginmgr.PluginUtil 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 5 with PluginUtil

use of org.pentaho.platform.plugin.services.pluginmgr.PluginUtil in project pentaho-platform by pentaho.

the class PluginGwtRpcTest method testLoadSerializationPolicySupportsPluginBean.

// endregion
// region Serialization Policy
@Test
public void testLoadSerializationPolicySupportsPluginBean() {
    String pathInfo = "/serviceName";
    String pluginId = "data-access";
    String moduleContextPath = "/content/data-access/resources/gwt/";
    String strongName = "ABC";
    // ---
    try (MockedStatic<PluginUtil> pluginUtilMock = mockStatic(PluginUtil.class)) {
        pluginUtilMock.when(() -> PluginUtil.getPluginIdFromPath(moduleContextPath)).thenReturn(pluginId);
        // ---
        SerializationPolicy pluginPolicyMock = mock(SerializationPolicy.class);
        pentahoSystemMock = mockStatic(PentahoSystem.class);
        pentahoSystemMock.when(() -> PentahoSystem.get(eq(SerializationPolicy.class), eq(null), anyMap())).thenAnswer(invocationOnMock -> {
            Map<String, String> props = (Map<String, String>) invocationOnMock.getArguments()[2];
            if (pluginId.equals(props.get("plugin"))) {
                return pluginPolicyMock;
            }
            fail();
            return mock(SerializationPolicy.class);
        });
        // ---
        HttpServletRequest httpRequestMock = setupHttpRequest(pathInfo);
        PluginGwtRpc gwtRpc = new PluginGwtRpc(httpRequestMock);
        SerializationPolicy result = gwtRpc.loadSerializationPolicy(moduleContextPath, strongName);
        assertEquals(pluginPolicyMock, result);
    }
}
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) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

SerializationPolicy (com.google.gwt.user.server.rpc.SerializationPolicy)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Test (org.junit.Test)5 PentahoSystem (org.pentaho.platform.engine.core.system.PentahoSystem)5 PluginUtil (org.pentaho.platform.plugin.services.pluginmgr.PluginUtil)5 IPluginResourceLoader (org.pentaho.platform.api.engine.IPluginResourceLoader)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Map (java.util.Map)1 ArgumentMatchers.anyMap (org.mockito.ArgumentMatchers.anyMap)1