### Question:
ProcessStartedEventHandler implements QueryEventHandler { @Override public void handle(CloudRuntimeEvent<?, ?> event) { CloudProcessStartedEvent startedEvent = (CloudProcessStartedEvent) event; String processInstanceId = startedEvent.getEntity().getId(); LOGGER.debug("Handling start of process Instance " + processInstanceId); Optional<ProcessInstanceEntity> findResult = processInstanceRepository.findById(processInstanceId); ProcessInstanceEntity processInstanceEntity = findResult.orElseThrow( () -> new QueryException("Unable to find process instance with the given id: " + processInstanceId)); if (ProcessInstance.ProcessInstanceStatus.CREATED.equals(processInstanceEntity.getStatus())) { processInstanceEntity.setStatus(ProcessInstance.ProcessInstanceStatus.RUNNING); processInstanceEntity.setName(startedEvent.getEntity().getName()); processInstanceEntity.setLastModified(new Date(startedEvent.getTimestamp())); processInstanceRepository.save(processInstanceEntity); } } ProcessStartedEventHandler(ProcessInstanceRepository processInstanceRepository); @Override void handle(CloudRuntimeEvent<?, ?> event); @Override String getHandledEvent();  }### Answer:
@Test public void handleShouldUpdateProcessInstanceStatusToRunningAndUpdateInstanceName() { CloudProcessStartedEvent event = buildProcessStartedEvent(); ProcessInstanceEntity currentProcessInstanceEntity = mock(ProcessInstanceEntity.class); given(currentProcessInstanceEntity.getStatus()).willReturn(ProcessInstance.ProcessInstanceStatus.CREATED); given(processInstanceRepository.findById(event.getEntity().getId())).willReturn(Optional.of(currentProcessInstanceEntity)); handler.handle(event); verify(processInstanceRepository).save(currentProcessInstanceEntity); verify(currentProcessInstanceEntity).setStatus(ProcessInstance.ProcessInstanceStatus.RUNNING); verify(currentProcessInstanceEntity).setName(event.getEntity().getName()); }
@Test public void handleShouldIgnoreEventIfProcessInstanceIsAlreadyInRunningStatus() { CloudProcessStartedEvent event = buildProcessStartedEvent(); ProcessInstanceEntity currentProcessInstanceEntity = mock(ProcessInstanceEntity.class); given(currentProcessInstanceEntity.getStatus()).willReturn(ProcessInstance.ProcessInstanceStatus.RUNNING); given(processInstanceRepository.findById(event.getEntity().getId())).willReturn(Optional.of(currentProcessInstanceEntity)); handler.handle(event); verify(processInstanceRepository, never()).save(currentProcessInstanceEntity); verify(currentProcessInstanceEntity, never()).setStatus(ProcessInstance.ProcessInstanceStatus.RUNNING); }
@Test public void handleShouldThrowExceptionWhenRelatedProcessInstanceIsNotFound() { CloudProcessStartedEvent event = buildProcessStartedEvent(); given(processInstanceRepository.findById("200")).willReturn(Optional.empty()); expectedException.expect(QueryException.class); expectedException.expectMessage("Unable to find process instance with the given id: "); handler.handle(event); }