-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(kafka-test): Add unit tests to driven adapter async event bus (#607)
* upgrade version to 3.20.3 version [skip ci] * feat(build): Add unit tests for reactive gateways * feat(build): Add CloudEvents dependency for EDA support * Update templates, revert some changes --------- Co-authored-by: Release Bot <[email protected]> Co-authored-by: AndresFelipe11 <[email protected]>
- Loading branch information
1 parent
69ac464
commit 6b73a1a
Showing
7 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/resources/driven-adapter/async-event-bus/build.gradle.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...rces/driven-adapter/async-event-bus/reactive-direct-async-gateway.unit.test.java.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
.../resources/driven-adapter/async-event-bus/reactive-events-gateway.unit.test.java.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} | ||
|
||
|
||
|
||
|
||
} |
3 changes: 3 additions & 0 deletions
3
src/main/resources/entry-point/async-event-handler/build.gradle.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters