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));
}
}
}
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));
}
}
}
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));
}
}
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));
}
}
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);
}
}
Aggregations