### Question:
ProcessEngineEventsAggregator extends BaseCommandContextEventsAggregator<CloudRuntimeEvent<?,?>, MessageProducerCommandContextCloseListener> { @Override public void add(CloudRuntimeEvent<?, ?> element) { CommandContext commandContext = getCurrentCommandContext(); String executionId = resolveExecutionId(element); ExecutionContext executionContext = resolveExecutionContext(commandContext, executionId); if(executionContext != null) { ExecutionContextInfoAppender executionContextInfoAppender = createExecutionContextInfoAppender(executionContext); CloudRuntimeEventImpl<?,?> event = CloudRuntimeEventImpl.class.cast(element); element = executionContextInfoAppender.appendExecutionContextInfoTo(event); } super.add(element); } ProcessEngineEventsAggregator(MessageProducerCommandContextCloseListener closeListener); @Override void add(CloudRuntimeEvent<?, ?> element);  }### Answer:
@Test public void addShouldRegisterCloseListenerWhenItIsMissing() { given(commandContext.hasCloseListener(MessageProducerCommandContextCloseListener.class)).willReturn(false); eventsAggregator.add(event); verify(commandContext).addCloseListener(closeListener); }
@Test public void addShouldNotRegisterCloseListenerWhenItIsAlreadyRegistered() { given(commandContext.hasCloseListener(MessageProducerCommandContextCloseListener.class)).willReturn(true); eventsAggregator.add(event); verify(commandContext, never()).addCloseListener(closeListener); }
@Test public void addShouldAddTheEventEventToTheEventAttributeListWhenTheAttributeAlreadyExists() { ArrayList<CloudRuntimeEvent<?,?>> currentEvents = new ArrayList<>(); given(commandContext.getGenericAttribute(MessageProducerCommandContextCloseListener.PROCESS_ENGINE_EVENTS)).willReturn(currentEvents); eventsAggregator.add(event); assertThat(currentEvents).containsExactly(event); verify(commandContext, never()).addAttribute(eq(MessageProducerCommandContextCloseListener.PROCESS_ENGINE_EVENTS), any()); }
@Test public void addShouldCreateAnewListAndRegisterItAsAttributeWhenTheAttributeDoesNotExist() { given(commandContext.getGenericAttribute(MessageProducerCommandContextCloseListener.PROCESS_ENGINE_EVENTS)).willReturn(null); eventsAggregator.add(event); verify(commandContext).addAttribute(eq(MessageProducerCommandContextCloseListener.PROCESS_ENGINE_EVENTS), eventsCaptor.capture()); assertThat(eventsCaptor.getValue()).containsExactly(event); }