ActionRunner implements Callable<Boolean> { public Boolean call() throws ActionInvocationException { final String workItemName = ActionUtil.extractName( params ); try { final ExecutionResult result = callImpl(); if ( result.isSuccess() ) { WorkItemLifecycleEventUtil.publish( workItemName, params, WorkItemLifecyclePhase.SUCCEEDED ); } else { WorkItemLifecycleEventUtil.publish( workItemName, params, WorkItemLifecyclePhase.FAILED ); } return result.updateRequired(); } catch ( final Throwable t ) { synchronized ( lock ) { lock.notifyAll(); } WorkItemLifecycleEventUtil.publish( workItemName, params, WorkItemLifecyclePhase.FAILED, t.toString() ); throw new ActionInvocationException( Messages.getInstance().getActionFailedToExecute( actionBean .getClass().getName() ), t ); } } ActionRunner( final IAction actionBean, final String actionUser, final Map<String, Serializable> params, final
    IBackgroundExecutionStreamProvider streamProvider ); Boolean call();  }
@Test public void testCallInvokesExecute() throws Exception { Map<String, Serializable> paramsMap = createMapWithUserLocale(); IAction actionBeanSpy = Mockito.spy( new TestAction() ); ActionRunner actionRunner = new ActionRunner( actionBeanSpy, "actionUser", paramsMap, null ); actionRunner.call(); Mockito.verify( actionBeanSpy ).execute(); Assert.assertTrue( actionBeanSpy.isExecutionSuccessful() ); }
@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(); }
@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 ) ); }
@Test public void testCallThrowsException() throws Exception { Map<String, Serializable> paramsMap = createMapWithUserLocale(); IAction actionBeanSpy = Mockito.spy( new TestAction() ); IBackgroundExecutionStreamProvider mockStreamProvider = Mockito.mock( IBackgroundExecutionStreamProvider.class ); when( mockStreamProvider.getInputStream() ).thenThrow( new Exception( "something went wrong" ) ); ActionRunner actionRunner = new ActionRunner( actionBeanSpy, "actionUser", paramsMap, mockStreamProvider ); exception.expect( ActionInvocationException.class ); actionRunner.call(); }