Skip to content

Commit

Permalink
Merge pull request #218 from AxonFramework/fix-null-query-name-npe
Browse files Browse the repository at this point in the history
Fixed NPE when query is null
  • Loading branch information
lfgcampos authored Nov 29, 2021
2 parents 3b94cd9 + afe037e commit 9838b26
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.stream.Stream;

import static org.axonframework.common.BuilderUtils.assertNonNull;
import static org.axonframework.common.ObjectUtils.nullSafeTypeOf;
import static org.axonframework.messaging.GenericMessage.asMessage;

/**
Expand All @@ -56,6 +57,21 @@ public class TracingQueryGateway implements QueryGateway {
private final QueryGateway delegate;
private final MessageTagBuilderService messageTagBuilderService;

/**
* Instantiate a {@link TracingQueryGateway} based on the fields contained in the {@link Builder}.
* <p>
* Will assert that the {@link Tracer} and delegate {@link QueryGateway} are not {@code null}, and will throw an
* {@link AxonConfigurationException} if they are.
*
* @param builder the {@link Builder} used to instantiate a {@link TracingQueryGateway} instance
*/
protected TracingQueryGateway(Builder builder) {
builder.validate();
this.tracer = builder.tracer;
this.delegate = builder.buildDelegateQueryGateway();
this.messageTagBuilderService = builder.messageTagBuilderService;
}

/**
* Instantiate a Builder to be able to create a {@link TracingQueryGateway}.
* <p>
Expand All @@ -72,26 +88,11 @@ public static Builder builder() {
return new Builder();
}

/**
* Instantiate a {@link TracingQueryGateway} based on the fields contained in the {@link Builder}.
* <p>
* Will assert that the {@link Tracer} and delegate {@link QueryGateway} are not {@code null}, and will throw an
* {@link AxonConfigurationException} if they are.
*
* @param builder the {@link Builder} used to instantiate a {@link TracingQueryGateway} instance
*/
protected TracingQueryGateway(Builder builder) {
builder.validate();
this.tracer = builder.tracer;
this.delegate = builder.buildDelegateQueryGateway();
this.messageTagBuilderService = builder.messageTagBuilderService;
}

@Override
public <R, Q> CompletableFuture<R> query(String queryName, Q query, ResponseType<R> responseType) {
QueryMessage<?, R> queryMessage = new GenericQueryMessage<>(asMessage(query), queryName, responseType);
return getWithSpan(
"query_" + SpanUtils.messageName(query.getClass(), queryName),
"query_" + SpanUtils.messageName(nullSafeTypeOf(query), queryName),
queryMessage,
(childSpan) -> delegate.query(queryName, queryMessage, responseType)
.whenComplete((r, e) -> {
Expand All @@ -109,7 +110,7 @@ public <R, Q> Stream<R> scatterGather(String queryName,
TimeUnit timeUnit) {
QueryMessage<?, R> queryMessage = new GenericQueryMessage<>(asMessage(query), queryName, responseType);
return getWithSpan(
"scatterGather_" + SpanUtils.messageName(query.getClass(), queryName),
"scatterGather_" + SpanUtils.messageName(nullSafeTypeOf(query), queryName),
queryMessage,
(childSpan) -> delegate.scatterGather(queryName, queryMessage, responseType, timeout, timeUnit)
.onClose(() -> {
Expand All @@ -129,7 +130,7 @@ public <Q, I, U> SubscriptionQueryResult<I, U> subscriptionQuery(String queryNam
asMessage(query), queryName, initialResponseType, updateResponseType
);
return getWithSpan(
"subscriptionQuery_" + SpanUtils.messageName(query.getClass(), queryName),
"subscriptionQuery_" + SpanUtils.messageName(nullSafeTypeOf(query), queryName),
queryMessage,
(childSpan) -> {
SubscriptionQueryResult<I, U> subscriptionQueryResult = delegate.subscriptionQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ void testQuery_query() throws ExecutionException, InterruptedException {
assertNull(scopeManager.activeSpan(), "There should be no activeSpan");
}

@Test
void testQuery_nullQuery() {
//noinspection unchecked
when(mockQueryBus.query(any(QueryMessage.class)))
.thenReturn(CompletableFuture.completedFuture(answer1));
assertDoesNotThrow(() -> testSubject.query("query", null, String.class));
}

@Test
void testScatterGather() {
//noinspection unchecked
Expand Down Expand Up @@ -162,6 +170,15 @@ void testScatterGather() {
assertNull(scopeManager.activeSpan(), "There should be no activeSpan");
}

@Test
void testScatterGather_nullQuery() {
assertDoesNotThrow(() -> testSubject.scatterGather("query",
null,
ResponseTypes.instanceOf(String.class),
1L,
TimeUnit.MILLISECONDS));
}

@Test
void testSubscriptionQuery() {
String initial = "initial";
Expand Down Expand Up @@ -237,6 +254,16 @@ void testSubscriptionQueryResults() {
assertNull(scopeManager.activeSpan(), "There should be no activeSpan");
}

@Test
void testSubscriptionQuery_nullQuery() {
when(mockQueryBus.subscriptionQuery(any(), anyInt()))
.thenReturn(createSubscriptionQueryResult("initial", "update"));
assertDoesNotThrow(() -> testSubject.subscriptionQuery("query",
null,
instanceOf(String.class),
instanceOf(String.class)));
}

private <I, U> SubscriptionQueryResult createSubscriptionQueryResult(I initial, U... updates) {
return new DefaultSubscriptionQueryResult<>(
Mono.just(GenericQueryResponseMessage.asResponseMessage(initial)),
Expand Down

0 comments on commit 9838b26

Please sign in to comment.