### Question:
MessageProducerCommandContextCloseListener implements CommandContextCloseListener { @Override public void closed(CommandContext commandContext) { List<CloudRuntimeEvent<?, ?>> events = commandContext.getGenericAttribute(PROCESS_ENGINE_EVENTS); if (events != null && !events.isEmpty()) { CloudRuntimeEvent<?, ?>[] payload = events.stream() .filter(CloudRuntimeEventImpl.class::isInstance) .map(CloudRuntimeEventImpl.class::cast) .map(runtimeBundleInfoAppender::appendRuntimeBundleInfoTo) .toArray(CloudRuntimeEvent<?, ?>[]::new); Message<CloudRuntimeEvent<?, ?>[]> message = messageBuilderChainFactory.create(null) .withPayload(payload) .build(); producer.auditProducer().send(message); } } MessageProducerCommandContextCloseListener(ProcessEngineChannels producer,
            MessageBuilderChainFactory<ExecutionContext> messageBuilderChainFactory,
            RuntimeBundleInfoAppender runtimeBundleInfoAppender ); @Override void closed(CommandContext commandContext); @Override void closing(CommandContext commandContext); @Override void afterSessionsFlush(CommandContext commandContext); @Override void closeFailure(CommandContext commandContext); static final String PROCESS_ENGINE_EVENTS; }### Answer:
@Test public void closedShouldSendEventsRegisteredOnTheCommandContext() { processEngineEventsAggregator.add(event); given(commandContext.getGenericAttribute(MessageProducerCommandContextCloseListener.PROCESS_ENGINE_EVENTS)) .willReturn(Collections.singletonList(event)); closeListener.closed(commandContext); verify(auditChannel).send(messageArgumentCaptor.capture()); assertThat(messageArgumentCaptor.getValue() .getPayload()).containsExactly(event); CloudRuntimeEvent<?, ?>[] result = messageArgumentCaptor.getValue().getPayload(); assertThat(result).hasSize(1); assertThat(result[0].getProcessInstanceId()).isEqualTo(MOCK_PROCESS_INSTANCE_ID); assertThat(result[0].getParentProcessInstanceId()).isEqualTo(MOCK_PARENT_PROCESS_INSTANCE_ID); assertThat(result[0].getBusinessKey()).isEqualTo(MOCK_BUSINESS_KEY); assertThat(result[0].getProcessDefinitionId()).isEqualTo(MOCK_PROCESS_DEFINITION_ID); assertThat(result[0].getProcessDefinitionKey()).isEqualTo(MOCK_PROCESS_DEFINITION_KEY); assertThat(result[0].getProcessDefinitionVersion()).isEqualTo(MOCK_PROCESS_DEFINITION_VERSION); assertThat(result[0].getAppName()).isEqualTo(APP_NAME); assertThat(result[0].getServiceName()).isEqualTo(SPRING_APP_NAME); assertThat(result[0].getServiceType()).isEqualTo(SERVICE_TYPE); assertThat(result[0].getServiceVersion()).isEqualTo(SERVICE_VERSION); }
@Test public void closedShouldDoNothingWhenRegisteredEventsIsNull() { given(commandContext.getGenericAttribute(MessageProducerCommandContextCloseListener.PROCESS_ENGINE_EVENTS)) .willReturn(null); closeListener.closed(commandContext); verify(auditChannel, never()).send(any()); }
@Test public void closedShouldDoNothingWhenRegisteredEventsIsEmpty() { given(commandContext.getGenericAttribute(MessageProducerCommandContextCloseListener.PROCESS_ENGINE_EVENTS)) .willReturn(Collections.emptyList()); closeListener.closed(commandContext); verify(auditChannel, never()).send(any()); }
@Test public void closedShouldSendMessageHeadersWithExecutionContext() { given(commandContext.getGenericAttribute(MessageProducerCommandContextCloseListener.PROCESS_ENGINE_EVENTS)) .willReturn(Collections.singletonList(event)); closeListener.closed(commandContext); verify(auditChannel).send(messageArgumentCaptor.capture()); assertThat(messageArgumentCaptor.getValue() .getHeaders()).containsEntry("routingKey", MOCK_ROUTING_KEY) .containsEntry("messagePayloadType",LORG_ACTIVITI_CLOUD_API_MODEL_SHARED_EVENTS_CLOUD_RUNTIME_EVENT) .containsEntry("appName", APP_NAME) .containsEntry("serviceName",SPRING_APP_NAME) .containsEntry("serviceType", SERVICE_TYPE) .containsEntry("serviceVersion", SERVICE_VERSION) .containsEntry("serviceFullName",SPRING_APP_NAME); }