Skip to content

Commit

Permalink
KSQL-12544 | Update numeric overflow exception tests (#134) (#10450)
Browse files Browse the repository at this point in the history
* Updated numeric overflow exception tests (#134)
  • Loading branch information
hrishabhg authored Sep 30, 2024
1 parent c89fa37 commit f699ed6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ You can set up IntelliJ for CheckStyle. First install the CheckStyle IDEA plugin
URL: https://raw.githubusercontent.com/confluentinc/common/master/build-tools/src/main/resources/checkstyle/checkstyle.xml
Ignore invalid certs: true

- (Optional) Make the new configuration active.
- Make the new configuration active.

- Highlight the newly added 'Confluent Checks' and click the edit button (pencil icon).

- Set properties to match the `checkstyle/checkstyle.properties` file in the repo.
- [!IMP] Set properties to match the `checkstyle/checkstyle.properties` file in the repo.

'Confluent Checks' will now be available in the CheckStyle tool window in the IDE and will auto-highlight issues in the code editor.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package io.confluent.ksql.test.model;

import static org.hamcrest.Matchers.instanceOf;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.confluent.ksql.test.tools.exceptions.InvalidFieldException;
import io.confluent.ksql.test.tools.exceptions.KsqlExpectedException;
Expand All @@ -26,13 +28,16 @@ public final class ExpectedExceptionNode {

private final Optional<String> type;
private final Optional<String> message;
private final Optional<String> cause;

ExpectedExceptionNode(
@JsonProperty("type") final String type,
@JsonProperty("message") final String message
@JsonProperty("message") final String message,
@JsonProperty("cause") final String cause
) {
this.type = Optional.ofNullable(type);
this.message = Optional.ofNullable(message);
this.cause = Optional.ofNullable(cause);

if (!this.type.isPresent() && !this.message.isPresent()) {
throw new MissingFieldException("expectedException.type or expectedException.message");
Expand All @@ -47,6 +52,7 @@ public Matcher<Throwable> build() {
.ifPresent(expectedException::expect);

message.ifPresent(expectedException::expectMessage);
cause.ifPresent(c -> expectedException.expectCause(instanceOf(parseThrowable(c))));
return expectedException.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

/**
* Helper class for testing exceptions.
*/
@SuppressFBWarnings("NM_CLASS_NOT_EXCEPTION")
public class KsqlExpectedException {
public final List<Matcher<?>> matchers = new ArrayList<>();
private Matcher<Throwable> expectedCause;

public static KsqlExpectedException none() {
return new KsqlExpectedException();
Expand All @@ -52,6 +56,14 @@ public void expectMessage(final Matcher<String> matcher) {
matchers.add(UnloggedMessageMatcher.hasMessage(matcher));
}

public void expectCause(final Matcher<Throwable> causeMatcher) {
this.expectedCause = causeMatcher;
}

/**
* Matcher that matches the message of a {@link KsqlStatementException} or its unlogged message.
* @param <T> the type of the exception
*/
public static class UnloggedMessageMatcher<T extends Throwable> extends TypeSafeMatcher<T> {
private final Matcher<String> matcher;

Expand Down Expand Up @@ -92,6 +104,21 @@ public static <T extends Throwable> Matcher<T> hasMessage(final Matcher<String>

@SuppressWarnings("unchecked")
public Matcher<Throwable> build() {
return allOf((List)new ArrayList<>(matchers));
final List<Matcher<?>> allMatchers = new ArrayList<>(matchers);
if (expectedCause != null) {
allMatchers.add(new TypeSafeMatcher<Throwable>() {
@Override
protected boolean matchesSafely(final Throwable item) {
return expectedCause.matches(item.getCause());
}

@Override
public void describeTo(final Description description) {
description.appendText("exception with cause ");
expectedCause.describeTo(description);
}
});
}
return allOf((List) allMatchers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@
{"topic": "S2", "key": 100,"value": {"SUM": "801.7"}}
],
"expectedException": {
"type": "io.confluent.ksql.function.KsqlFunctionException",
"message": "Numeric field overflow"
"type": "org.apache.kafka.streams.errors.StreamsException",
"message": "Numeric field overflow",
"cause": "io.confluent.ksql.function.KsqlFunctionException"
}
},
{
Expand Down Expand Up @@ -167,8 +168,9 @@
{"topic": "S2", "key": 100,"value": {"SUM": "50.9"}}
],
"expectedException": {
"type": "io.confluent.ksql.function.KsqlFunctionException",
"message": "Numeric field overflow"
"type": "org.apache.kafka.streams.errors.StreamsException",
"message": "Numeric field overflow",
"cause": "io.confluent.ksql.function.KsqlFunctionException"
}
},
{
Expand Down

0 comments on commit f699ed6

Please sign in to comment.