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

Feature/unit-tests-da-aeb #598

Closed
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [v3.20.3](https://github.com/bancolombia/scaffold-clean-architecture/tree/v3.20.3) (2024-11-25)

[Full Changelog](https://github.com/bancolombia/scaffold-clean-architecture/compare/v3.20.2...v3.20.3)

**Merged pull requests:**

- fix\(build\): Update commons templates [\#590](https://github.com/bancolombia/scaffold-clean-architecture/pull/590) ([juparog](https://github.com/juparog))

## [v3.20.2](https://github.com/bancolombia/scaffold-clean-architecture/tree/v3.20.2) (2024-11-21)

[Full Changelog](https://github.com/bancolombia/scaffold-clean-architecture/compare/v3.20.1...v3.20.2)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package=co.com.bancolombia
systemProp.version=3.20.2
systemProp.version=3.20.3
simulateRest=true
systemProp.sonar.gradle.skipCompile=true
2 changes: 1 addition & 1 deletion src/main/java/co/com/bancolombia/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class Constants {
public static final String JACOCO_VERSION = "0.8.12";
public static final String SONAR_VERSION = "5.1.0.4882";
public static final String COBERTURA_VERSION = "4.0.0";
public static final String PLUGIN_VERSION = "3.20.2";
public static final String PLUGIN_VERSION = "3.20.3";
public static final String DEPENDENCY_CHECK_VERSION = "11.1.0";
public static final String PITEST_VERSION = "1.15.0";
// custom
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:4.0.1'
{{/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:4.0.1'
{{/eda}}
{{#rabbitmq}}
implementation 'org.reactivecommons:async-commons-rabbit-starter:{{REACTIVE_COMMONS_VERSION}}'
{{/rabbitmq}}
Expand Down