Skip to content

Commit

Permalink
Add test coverage for ProcessDeployedEvents listener
Browse files Browse the repository at this point in the history
  • Loading branch information
igdianov committed Oct 31, 2024
1 parent 67320b4 commit 8fd77ff
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ public ProcessDefinitionsSyncService(
public List<String> syncProcessDefinitions(SyncCloudProcessDefinitionsPayload payload) {
List<String> excludedProcessDefinitionIds = payload.getExcludedProcessDefinitionIds();

return toProcessDeployedEventList(excludedProcessDefinitionIds)
return queryProcessDefinitionsStream(excludedProcessDefinitionIds)
.map(processDefinitions -> processDefinitions.stream().map(this::toProcessDeployedEvent).toList())
.map(ProcessDeployedEvents::new)
.peek(applicationEventPublisher::publishEvent)
.flatMap(it -> it.getProcessDeployedEvents().stream())
.map(ProcessDeployedEvent::getProcessDefinitionId)
.toList();
}

private Stream<List<ProcessDeployedEvent>> toProcessDeployedEventList(List<String> excludedProcessDefinitionIds) {
private Stream<List<ProcessDefinition>> queryProcessDefinitionsStream(List<String> excludedProcessDefinitionIds) {
final AtomicInteger counter = new AtomicInteger();

return repositoryService
Expand All @@ -74,8 +75,7 @@ private Stream<List<ProcessDeployedEvent>> toProcessDeployedEventList(List<Strin
.collect(Collectors.groupingBy(it -> counter.getAndIncrement() / 10))
.values()
.stream()
.map(converter::from)
.map(processDefinitions -> processDefinitions.stream().map(this::toProcessDeployedEvent).toList());
.map(converter::from);
}

private ProcessDeployedEvent toProcessDeployedEvent(ProcessDefinition processDefinition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.assertj.core.api.Assertions.tuple;
import static org.awaitility.Awaitility.await;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -30,11 +31,13 @@
import org.activiti.api.process.model.ProcessDefinition;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.model.events.ProcessDeployedEvent;
import org.activiti.api.process.model.payloads.ResumeProcessPayload;
import org.activiti.api.process.model.payloads.SetProcessVariablesPayload;
import org.activiti.api.process.model.payloads.SignalPayload;
import org.activiti.api.process.model.payloads.StartProcessPayload;
import org.activiti.api.process.model.payloads.SuspendProcessPayload;
import org.activiti.api.runtime.event.impl.ProcessDeployedEvents;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.model.payloads.ClaimTaskPayload;
Expand All @@ -56,9 +59,11 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.context.event.EventListener;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.PagedModel;
Expand All @@ -81,6 +86,7 @@
TaskRestTemplate.class,
MessageClientStreamConfiguration.class,
TestChannelBinderConfiguration.class,
CommandEndpointIT.TestProcessDeployedEventsListener.class,
}
)
@ContextConfiguration(initializers = { KeycloakContainerApplicationInitializer.class })
Expand All @@ -107,6 +113,17 @@ public class CommandEndpointIT {

private Map<String, String> processDefinitionIds = new HashMap<>();

private static List<ProcessDeployedEvents> processDeployedEvents = new ArrayList<>();

@TestComponent
static class TestProcessDeployedEventsListener {

@EventListener
void on(ProcessDeployedEvents event) {
processDeployedEvents.add(event);
}
}

private static final String PROCESS_DEFINITIONS_URL = "/v1/process-definitions";
private static final String PROCESS_INSTANCES_RELATIVE_URL = "/v1/process-instances";
private static final String TASKS_URL = "/v1/tasks";
Expand Down Expand Up @@ -205,6 +222,7 @@ public void eventBasedStartProcessTests() throws Exception {
@Test
public void syncCloudProcessDefinitionsTest() {
streamHandler.resetSyncProcessDefinitionsAck();
processDeployedEvents.clear();

var payload = new SyncCloudProcessDefinitionsPayload();

Expand All @@ -221,11 +239,23 @@ public void syncCloudProcessDefinitionsTest() {
.extracting(SyncCloudProcessDefinitionsResult::getEntity)
.asInstanceOf(InstanceOfAssertFactories.LIST)
.contains(processDefinitionIds.values().toArray());

assertThat(processDeployedEvents).isNotEmpty();

assertThat(
processDeployedEvents
.stream()
.flatMap(it -> it.getProcessDeployedEvents().stream())
.map(ProcessDeployedEvent::getProcessDefinitionId)
.toList()
)
.contains(processDefinitionIds.values().toArray(String[]::new));
}

@Test
public void syncCloudProcessDefinitionsRuntimeGatewayTest() {
streamHandler.resetSyncProcessDefinitionsAck();
processDeployedEvents.clear();

var payload = new SyncCloudProcessDefinitionsPayload();

Expand All @@ -237,6 +267,17 @@ public void syncCloudProcessDefinitionsRuntimeGatewayTest() {
.extracting(SyncCloudProcessDefinitionsResult::getEntity)
.asInstanceOf(InstanceOfAssertFactories.LIST)
.contains(processDefinitionIds.values().toArray());

assertThat(processDeployedEvents).isNotEmpty();

assertThat(
processDeployedEvents
.stream()
.flatMap(it -> it.getProcessDeployedEvents().stream())
.map(ProcessDeployedEvent::getProcessDefinitionId)
.toList()
)
.contains(processDefinitionIds.values().toArray(String[]::new));
}

@Test
Expand Down

0 comments on commit 8fd77ff

Please sign in to comment.