IdentityProviderAwareProcessListener extends DefaultProcessEventListener { public void beforeProcessStarted(final ProcessStartedEvent event) { resolveIdentityProvider(); if (identityProvider != null) { final WorkflowProcessInstance wpi = (WorkflowProcessInstance) event.getProcessInstance(); final String name = identityProvider.getName(); wpi.setVariable("initiator", name); wpi.getMetaData().put("OwnerId", name); } } IdentityProviderAwareProcessListener(final KieSession kieSession); void beforeProcessStarted(final ProcessStartedEvent event);  }
@Test public void testSetUser() { final IdentityProvider identityProvider = mock(IdentityProvider.class); final String userId = "userId"; when(identityProvider.getName()).thenReturn(userId); when(environment.get("IdentityProvider")).thenReturn(identityProvider); final WorkflowProcessInstance processInstance = mock(WorkflowProcessInstance.class); final HashMap<String, Object> metaData = new HashMap<>(); when(processInstance.getMetaData()).thenReturn(metaData); final ProcessStartedEvent event = new ProcessStartedEventImpl(processInstance, mock(KieRuntime.class )); listener.beforeProcessStarted(event); assertEquals(userId, metaData.get("OwnerId")); verify(processInstance).setVariable("initiator", userId); }
@Test public void testUserNotSet() { final WorkflowProcessInstance processInstance = mock(WorkflowProcessInstance.class); final HashMap<String, Object> metaData = new HashMap<>(); when(processInstance.getMetaData()).thenReturn(metaData); final ProcessStartedEvent event = new ProcessStartedEventImpl(processInstance, mock(KieRuntime.class)); listener.beforeProcessStarted(event); assertTrue(metaData.isEmpty()); verify(processInstance, never()).setVariable(anyString(), anyString()); }