Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kafka-test): Add unit tests to driven adapter async event bus #607

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sh_dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
{
"name": "JACOCO_VERSION",
"packageName": "org.jacoco:jacoco-maven-plugin"
},
{
"name": "CLOUD_EVENTS_VERSION",
"packageName": "io.cloudevents:cloudevents-json-jackson"
}
],
"gradle": [
Expand Down
1 change: 1 addition & 0 deletions src/main/java/co/com/bancolombia/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class Constants {
public static final String RESILIENCE_4J_VERSION = "2.2.0";
public static final String BIN_STASH_VERSION = "1.2.6";
public static final String SPRING_DOC_OPENAPI_VERSION = "2.7.0";
public static final String CLOUD_EVENTS_VERSION = "4.0.1";
// gradle plugins
public static final String JACOCO_VERSION = "0.8.12";
public static final String SONAR_VERSION = "6.0.1.5171";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
dependencies {
implementation project(':model')
{{#eda}}
implementation 'io.cloudevents:cloudevents-json-jackson:{{CLOUD_EVENTS_VERSION}}'
{{/eda}}
{{#rabbitmq}}
implementation 'org.reactivecommons:async-commons-rabbit-starter:{{REACTIVE_COMMONS_VERSION}}'
{{/rabbitmq}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"driven-adapter/async-event-bus/events-gateway.java.mustache": "domain/model/src/main/java/{{packagePath}}/model/events/gateways/EventsGateway.java",
"driven-adapter/async-event-bus/reactive-events-gateway.java.mustache": "infrastructure/driven-adapters/async-event-bus/src/main/java/{{packagePath}}/events/ReactiveEventsGateway.java",
"driven-adapter/async-event-bus/reactive-direct-async-gateway.java.mustache": "infrastructure/driven-adapters/async-event-bus/src/main/java/{{packagePath}}/events/ReactiveDirectAsyncGateway.java",
"driven-adapter/async-event-bus/reactive-direct-async-gateway.unit.test.java.mustache": "infrastructure/driven-adapters/async-event-bus/src/test/java/{{packagePath}}/events/ReactiveDirectAsyncGatewayTest.java",
"driven-adapter/async-event-bus/reactive-events-gateway.unit.test.java.mustache": "infrastructure/driven-adapters/async-event-bus/src/test/java/{{packagePath}}/events/ReactiveEventsGatewayTest.java",
"driven-adapter/async-event-bus/build.gradle.mustache": "infrastructure/driven-adapters/async-event-bus/build.gradle"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package {{package}}.events;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.reactivecommons.async.api.DirectAsyncGateway;
import reactor.core.publisher.Mono;
{{#eda}}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.cloudevents.CloudEvent;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.jackson.JsonCloudEventData;

import java.net.URI;
import java.time.OffsetDateTime;
import java.util.UUID;
{{/eda}}
{{^eda}}
import org.reactivecommons.api.domain.Command;
import org.reactivecommons.async.api.AsyncQuery;
{{/eda}}

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

public class ReactiveDirectAsyncGatewayTest {

@Mock
private DirectAsyncGateway directAsyncGateway;
{{#eda}}
@Mock
private ObjectMapper objectMapper;
{{/eda}}

private ReactiveDirectAsyncGateway gateway;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
gateway = new ReactiveDirectAsyncGateway(directAsyncGateway{{#eda}}, objectMapper{{/eda}});
}

@Test
public void testRunRemoteJobSendsCommand() {{#eda}}throws Exception{{/eda}} {
Object command = new Object() {
public String toString() {
return "testCommand";
}
};
{{#eda}}
when(objectMapper.valueToTree(command)).thenReturn(mock(ObjectNode.class));
when(directAsyncGateway.sendCommand(any(CloudEvent.class), any(String.class))).thenReturn(Mono.empty());
{{/eda}}
{{^eda}}
when(directAsyncGateway.sendCommand(any(Command.class), any(String.class))).thenReturn(Mono.empty());
{{/eda}}

gateway.runRemoteJob(command).block();

{{#eda}}
ArgumentCaptor<CloudEvent> eventCaptor = ArgumentCaptor.forClass(CloudEvent.class);
verify(directAsyncGateway, times(1)).sendCommand(eventCaptor.capture(), eq(ReactiveDirectAsyncGateway.TARGET_NAME));
CloudEvent cloudEvent = eventCaptor.getValue();
{{/eda}}
{{^eda}}
ArgumentCaptor<Command> commandCaptor = ArgumentCaptor.forClass(Command.class);
verify(directAsyncGateway, times(1)).sendCommand(commandCaptor.capture(), eq(ReactiveDirectAsyncGateway.TARGET_NAME));
Command capturedCommand = commandCaptor.getValue();
assertEquals(ReactiveDirectAsyncGateway.SOME_COMMAND_NAME, capturedCommand.getName());
{{/eda}}
}

@Test
public void testRequestForRemoteDataSendsQuery() {{#eda}}throws JsonProcessingException{{/eda}} {
Object query = new Object() {
public String toString() {
return "testQuery";
}
};
{{#eda}}
ObjectNode mockNode = mock(ObjectNode.class);
when(objectMapper.valueToTree(query)).thenReturn(mockNode);
when(objectMapper.createObjectNode()).thenReturn(new ObjectMapper().createObjectNode());

CloudEvent mockCloudEvent = CloudEventBuilder.v1()
.withId(UUID.randomUUID().toString())
.withSource(URI.create("https://spring.io/foos"))
.withType(ReactiveDirectAsyncGateway.SOME_QUERY_NAME)
.withTime(OffsetDateTime.now())
.withData("application/json", JsonCloudEventData.wrap(objectMapper.createObjectNode().put("key", "value")))
.build();

when(directAsyncGateway.requestReply(any(CloudEvent.class), any(String.class), eq(CloudEvent.class)))
.thenReturn(Mono.just(mockCloudEvent));

gateway.requestForRemoteData(query).block();

ArgumentCaptor<CloudEvent> eventCaptor = ArgumentCaptor.forClass(CloudEvent.class);
verify(directAsyncGateway, times(1)).requestReply(eventCaptor.capture(), eq(ReactiveDirectAsyncGateway.TARGET_NAME), eq(CloudEvent.class));
CloudEvent cloudEvent = eventCaptor.getValue();
{{/eda}}
{{^eda}}
when(directAsyncGateway.requestReply(any(AsyncQuery.class), any(String.class), eq(Object.class))).thenReturn(Mono.just(new Object()));

gateway.requestForRemoteData(query).block();

ArgumentCaptor<AsyncQuery> queryCaptor = ArgumentCaptor.forClass(AsyncQuery.class);
verify(directAsyncGateway, times(1)).requestReply(queryCaptor.capture(), eq(ReactiveDirectAsyncGateway.TARGET_NAME), eq(Object.class));
AsyncQuery capturedQuery = queryCaptor.getValue();
assertEquals(ReactiveDirectAsyncGateway.SOME_QUERY_NAME, capturedQuery.getResource());
{{/eda}}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package {{package}}.events;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import reactor.core.publisher.Mono;
import org.reactivecommons.api.domain.DomainEventBus;

{{#eda}}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.cloudevents.CloudEvent;
{{/eda}}
{{^eda}}
import org.reactivecommons.api.domain.DomainEvent;
{{/eda}}
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ReactiveEventsGatewayTest {

{{#eda}}
@Mock
private DomainEventBus domainEventBus;

@Mock
private ObjectMapper objectMapper;

private ReactiveEventsGateway gateway;
{{/eda}}
{{^eda}}
@Mock
private DomainEventBus domainEventBus;

private ReactiveEventsGateway reactiveEventsGateway;
{{/eda}}

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
{{#eda}}
gateway = new ReactiveEventsGateway(domainEventBus, objectMapper);
when(domainEventBus.emit(any(CloudEvent.class))).thenReturn(Mono.empty());
{{/eda}}
{{^eda}}
reactiveEventsGateway = new ReactiveEventsGateway(domainEventBus);
{{/eda}}
}

@Test
public void testEmitLogsEvent() {
Object event = new Object() {
@Override
public String toString() {
return "testEvent";
}
};

{{#eda}}
when(objectMapper.valueToTree(event)).thenReturn(mock(ObjectNode.class));

gateway.emit(event).block();

verify(domainEventBus, times(1)).emit(any(CloudEvent.class));
{{/eda}}
{{^eda}}
when(domainEventBus.emit(any(DomainEvent.class))).thenReturn(Mono.empty());

reactiveEventsGateway.emit(event).block();

verify(domainEventBus, times(1)).emit(any(DomainEvent.class));
{{/eda}}
}

{{#eda}}
@Test
public void testEmitConstructsCloudEvent() {
Object event = new Object() {
public String toString() { return "testEvent"; }
};

when(objectMapper.valueToTree(event)).thenReturn(mock(ObjectNode.class));

gateway.emit(event).block();

ArgumentCaptor<CloudEvent> eventCaptor = ArgumentCaptor.forClass(CloudEvent.class);
verify(domainEventBus, times(1)).emit(eventCaptor.capture());

CloudEvent cloudEvent = eventCaptor.getValue();
assertEquals(ReactiveEventsGateway.SOME_EVENT_NAME, cloudEvent.getType());
assertEquals("https://reactive-commons.org/foos", cloudEvent.getSource().toString());
}
{{/eda}}

{{^eda}}
@Test
public void testEmitConstructsDomainEvent() {
Object event = new Object() {
@Override
public String toString() {
return "testEvent";
}
};

when(domainEventBus.emit(any(DomainEvent.class))).thenReturn(Mono.empty());

reactiveEventsGateway.emit(event).block();

ArgumentCaptor<DomainEvent> eventCaptor = ArgumentCaptor.forClass(DomainEvent.class);
verify(domainEventBus, times(1)).emit(eventCaptor.capture());

DomainEvent capturedEvent = eventCaptor.getValue();
assertEquals(ReactiveEventsGateway.SOME_EVENT_NAME, capturedEvent.getName());
assertEquals(event.toString(), capturedEvent.getData().toString());
}
{{/eda}}




}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
dependencies {
implementation project(':model')
implementation project(':usecase')
{{#eda}}
implementation 'io.cloudevents:cloudevents-json-jackson:{{CLOUD_EVENTS_VERSION}}'
{{/eda}}
{{#rabbitmq}}
implementation 'org.reactivecommons:async-commons-rabbit-starter:{{REACTIVE_COMMONS_VERSION}}'
{{/rabbitmq}}
Expand Down
Loading