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