Search in sources :

Example 1 with ISecurityHelper

use of org.pentaho.platform.api.engine.ISecurityHelper in project pentaho-platform by pentaho.

the class ActionRunnerTest method testCallWithStreamProvider.

@Test
public void testCallWithStreamProvider() throws Exception {
    Map<String, Serializable> paramsMap = createMapWithUserLocale();
    IAction actionBeanSpy = Mockito.spy(new TestAction());
    IBackgroundExecutionStreamProvider mockStreamProvider = Mockito.mock(IBackgroundExecutionStreamProvider.class);
    InputStream mockInputStream = Mockito.mock(InputStream.class);
    OutputStream mockOutputStream = Mockito.mock(OutputStream.class);
    when(mockStreamProvider.getInputStream()).thenReturn(mockInputStream);
    String mockOutputPath = "/someUser/someOutput";
    when(mockStreamProvider.getOutputPath()).thenReturn(mockOutputPath);
    when(mockStreamProvider.getOutputStream()).thenReturn(mockOutputStream);
    ISecurityHelper mockSecurityHelper = Mockito.mock(ISecurityHelper.class);
    SecurityHelper.setMockInstance(mockSecurityHelper);
    when(mockSecurityHelper.runAsUser(Mockito.anyString(), Mockito.any())).thenReturn(mockOutputPath);
    PowerMockito.mockStatic(PentahoSystem.class);
    IUnifiedRepository mockRepository = Mockito.mock(IUnifiedRepository.class);
    when(PentahoSystem.get(isA(IUnifiedRepository.class.getClass()), Mockito.any())).thenReturn(mockRepository);
    IAuthorizationPolicy mockAuthorizationPolicy = Mockito.mock(IAuthorizationPolicy.class);
    when(PentahoSystem.get(isA(IAuthorizationPolicy.class.getClass()), Mockito.any())).thenReturn(mockAuthorizationPolicy);
    when(mockAuthorizationPolicy.isAllowed(SchedulerOutputPathResolver.SCHEDULER_ACTION_NAME)).thenReturn(true);
    String repoId = "SOME_REPO_ID";
    Map<String, Serializable> dummyMetaData = new HashMap<>();
    dummyMetaData.put(RepositoryFile.SCHEDULABLE_KEY, true);
    when(mockRepository.getFileMetadata(repoId)).thenReturn(dummyMetaData);
    RepositoryFile mockRepoFile = Mockito.mock(RepositoryFile.class);
    when(mockRepoFile.isFolder()).thenReturn(true);
    when(mockRepoFile.getId()).thenReturn(repoId);
    ActionRunner actionRunner = new ActionRunner(actionBeanSpy, "actionUser", paramsMap, mockStreamProvider);
    actionRunner.call();
    Mockito.verify(actionBeanSpy).execute();
}
Also used : IBackgroundExecutionStreamProvider(org.pentaho.platform.api.scheduler2.IBackgroundExecutionStreamProvider) Serializable(java.io.Serializable) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) IAction(org.pentaho.platform.api.action.IAction) HashMap(java.util.HashMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) TestAction(org.pentaho.platform.util.bean.TestAction) ISecurityHelper(org.pentaho.platform.api.engine.ISecurityHelper) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with ISecurityHelper

use of org.pentaho.platform.api.engine.ISecurityHelper in project pentaho-platform by pentaho.

the class ActionRunnerTest method testCallWithStreamProviderAndVarargsAction.

@Test
public void testCallWithStreamProviderAndVarargsAction() throws Exception {
    Map<String, Serializable> paramsMap = createMapWithUserLocale();
    TestVarArgsAction testVarArgsAction = new TestVarArgsAction();
    IBackgroundExecutionStreamProvider mockStreamProvider = Mockito.mock(IBackgroundExecutionStreamProvider.class);
    InputStream mockInputStream = Mockito.mock(InputStream.class);
    OutputStream mockOutputStream = Mockito.mock(OutputStream.class);
    when(mockStreamProvider.getInputStream()).thenReturn(mockInputStream);
    String mockOutputPath = "/someUser/someOutput";
    when(mockStreamProvider.getOutputPath()).thenReturn(mockOutputPath);
    when(mockStreamProvider.getOutputStream()).thenReturn(mockOutputStream);
    ISecurityHelper mockSecurityHelper = Mockito.mock(ISecurityHelper.class);
    SecurityHelper.setMockInstance(mockSecurityHelper);
    when(mockSecurityHelper.runAsUser(Mockito.anyString(), Mockito.any())).thenReturn(mockOutputPath);
    PowerMockito.mockStatic(PentahoSystem.class);
    IUnifiedRepository mockRepository = Mockito.mock(IUnifiedRepository.class);
    when(PentahoSystem.get(isA(IUnifiedRepository.class.getClass()), Mockito.any())).thenReturn(mockRepository);
    IAuthorizationPolicy mockAuthorizationPolicy = Mockito.mock(IAuthorizationPolicy.class);
    when(PentahoSystem.get(isA(IAuthorizationPolicy.class.getClass()), Mockito.any())).thenReturn(mockAuthorizationPolicy);
    when(mockAuthorizationPolicy.isAllowed(SchedulerOutputPathResolver.SCHEDULER_ACTION_NAME)).thenReturn(true);
    String repoId = "SOME_REPO_ID";
    Map<String, Serializable> dummyMetaData = new HashMap<>();
    dummyMetaData.put(RepositoryFile.SCHEDULABLE_KEY, true);
    when(mockRepository.getFileMetadata(repoId)).thenReturn(dummyMetaData);
    RepositoryFile mockRepoFile = Mockito.mock(RepositoryFile.class);
    when(mockRepoFile.isFolder()).thenReturn(true);
    when(mockRepoFile.getId()).thenReturn(repoId);
    ActionRunner actionRunner = new ActionRunner(testVarArgsAction, "actionUser", paramsMap, mockStreamProvider);
    actionRunner.call();
    assertThat(testVarArgsAction.isExecuteWasCalled(), is(true));
}
Also used : IBackgroundExecutionStreamProvider(org.pentaho.platform.api.scheduler2.IBackgroundExecutionStreamProvider) Serializable(java.io.Serializable) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) HashMap(java.util.HashMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ISecurityHelper(org.pentaho.platform.api.engine.ISecurityHelper) TestVarArgsAction(org.pentaho.platform.engine.services.actions.TestVarArgsAction) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with ISecurityHelper

use of org.pentaho.platform.api.engine.ISecurityHelper in project pentaho-platform by pentaho.

the class SchedulerServiceTest method testGetJobInfo.

@Test
public void testGetJobInfo() throws Exception {
    String jobId = "jobId";
    Job mockJob = mock(Job.class);
    doReturn(mockJob).when(schedulerService).getJob(jobId);
    ISecurityHelper mockSecurityHelper = mock(ISecurityHelper.class);
    doReturn(mockSecurityHelper).when(schedulerService).getSecurityHelper();
    IPentahoSession mockPentahoSession = mock(IPentahoSession.class);
    doReturn(mockPentahoSession).when(schedulerService).getSession();
    String sessionName = "sessionName";
    doReturn(sessionName).when(mockPentahoSession).getName();
    doReturn(sessionName).when(mockJob).getUserName();
    Map<String, Serializable> mockJobParams = mock(Map.class);
    doReturn(mockJobParams).when(mockJob).getJobParams();
    Set<String> jobParamsKeyset = new HashSet<>();
    doReturn(jobParamsKeyset).when(mockJobParams).keySet();
    String jobParamKey = "key";
    jobParamsKeyset.add(jobParamKey);
    String value = "value";
    String[] testArray = new String[] { value };
    doReturn(testArray).when(mockJobParams).get(jobParamKey);
    // Test 1
    doReturn(true).when(schedulerService).canAdminister(mockPentahoSession);
    Job testJob = schedulerService.getJobInfo(jobId);
    assertEquals(mockJob, testJob);
    // Test 2
    doReturn(false).when(schedulerService).canAdminister(mockPentahoSession);
    testJob = schedulerService.getJobInfo(jobId);
    assertEquals(mockJob, testJob);
    verify(mockJobParams, times(2)).put(eq(jobParamKey), any(Serializable.class));
    verify(schedulerService, times(2)).getJob(jobId);
    verify(schedulerService, times(2)).getSession();
    verify(mockPentahoSession, times(2)).getName();
    verify(mockJob, times(2)).getUserName();
    verify(mockJob, times(6)).getJobParams();
    verify(mockJobParams, times(2)).keySet();
    verify(mockJobParams, times(2)).get(jobParamKey);
    verify(schedulerService, times(2)).canAdminister(null);
}
Also used : Serializable(java.io.Serializable) ISecurityHelper(org.pentaho.platform.api.engine.ISecurityHelper) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Matchers.anyString(org.mockito.Matchers.anyString) Job(org.pentaho.platform.api.scheduler2.Job) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with ISecurityHelper

use of org.pentaho.platform.api.engine.ISecurityHelper in project pentaho-platform by pentaho.

the class PentahoXmlaServletTest method testMakeContentFinderHandlesXmlaEnablement.

@Test
public void testMakeContentFinderHandlesXmlaEnablement() throws Exception {
    ISecurityHelper securityHelper = mock(ISecurityHelper.class);
    SecurityHelper.setMockInstance(securityHelper);
    when(securityHelper.runAsSystem(any((Callable.class)))).thenReturn(DATASOURCE_XML);
    Document content = XmlDom4JHelper.getDocFromString(new PentahoXmlaServlet().makeContentFinder("fakeurl").getContent(), new PentahoEntityResolver());
    assertEquals(2, content.selectNodes("/DataSources/DataSource/Catalogs/Catalog").size());
    assertNotNull(content.selectNodes("/DataSources/DataSource/Catalogs/Catalog[@name='EnabledCatalog']"));
    assertNotNull(content.selectNodes("/DataSources/DataSource/Catalogs/Catalog[@name='FoodMart']"));
}
Also used : ISecurityHelper(org.pentaho.platform.api.engine.ISecurityHelper) Document(org.dom4j.Document) PentahoEntityResolver(org.pentaho.platform.engine.services.solution.PentahoEntityResolver) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with ISecurityHelper

use of org.pentaho.platform.api.engine.ISecurityHelper in project pentaho-platform by pentaho.

the class PentahoXmlaServletTest method createConnectionFactory.

@Test
public void createConnectionFactory() throws Exception {
    ISecurityHelper securityHelper = mock(ISecurityHelper.class);
    SecurityHelper.setMockInstance(securityHelper);
    when(securityHelper.runAsSystem(any((Callable.class)))).thenReturn(DATASOURCE_XML);
    IMondrianCatalogService catalogService = mock(MondrianCatalogHelper.class);
    MondrianCatalog mondrianCatalog = mock(MondrianCatalog.class);
    when(mondrianCatalog.getDataSourceInfo()).thenReturn("DataSource=foo");
    doReturn(mondrianCatalog).when(catalogService).getCatalog(anyString(), anyObject());
    PowerMockito.mockStatic(DriverManager.class);
    when(DriverManager.getConnection(anyString(), anyObject())).thenReturn(mock(RolapConnection.class));
    PentahoSystem.registerObject(catalogService);
    PentahoXmlaServlet xmlaServlet = spy(new PentahoXmlaServlet());
    XmlaHandler.ConnectionFactory connectionFactory = xmlaServlet.createConnectionFactory(mock(ServletConfig.class));
    Properties properties = new Properties();
    properties.put("DataSource", "bogus");
    try {
        connectionFactory.getConnection("SampleData", "SampleData", "baz", properties);
    } catch (MondrianException exception) {
    // ignored
    }
    try {
        connectionFactory.getConnection("SampleData", "SampleData", "baz", properties);
    } catch (MondrianException exception) {
    // ignored
    }
    // We verify that only one Catalog Locator is created for multiple requests
    verify(xmlaServlet, times(1)).makeCatalogLocator(anyObject());
}
Also used : RolapConnection(mondrian.rolap.RolapConnection) ISecurityHelper(org.pentaho.platform.api.engine.ISecurityHelper) MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) XmlaHandler(mondrian.xmla.XmlaHandler) ServletConfig(javax.servlet.ServletConfig) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) Properties(java.util.Properties) MondrianException(mondrian.olap.MondrianException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ISecurityHelper (org.pentaho.platform.api.engine.ISecurityHelper)8 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Serializable (java.io.Serializable)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 HashMap (java.util.HashMap)2 MondrianException (mondrian.olap.MondrianException)2 Matchers.anyString (org.mockito.Matchers.anyString)2 IAuthorizationPolicy (org.pentaho.platform.api.engine.IAuthorizationPolicy)2 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)2 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)2 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)2 IBackgroundExecutionStreamProvider (org.pentaho.platform.api.scheduler2.IBackgroundExecutionStreamProvider)2 Job (org.pentaho.platform.api.scheduler2.Job)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 ServletConfig (javax.servlet.ServletConfig)1 ServletException (javax.servlet.ServletException)1