citrusframework / citrusframework/citrus

Custom SqlResultSetScriptValidator for validating with callback

Open
#619 6 comments 1 reaction 0 assignees View on GitHub
bug prio-medium
Dominant language
Java
Stars
485
Forks
155
Avg merge
4d 22h
Merged PRs (30d)
6

Description

**Citrus Version**
2.8.0

**Question**
I would like to understand what is the default way of using the SQL validation with the Java DSL. Additionally I would like to have type ensurance of the result set (with POJOs). Therefore I wanted not to do a groovy based validation script. Instead I decided to use the custom validator approach (see: https://github.com/citrusframework/citrus/blob/citrus-2.8.0/modules/citrus-java-dsl/src/main/java/com/consol/citrus/dsl/builder/ExecuteSQLQueryBuilder.java#L240) via the following snippet:

```
designer
.query(myDbDatasource)
.sqlResource(resource)
.validator(new SqlResultSetScriptValidator() {
@Override
public void validateSqlResultSet(List> resultSet,
ScriptValidationContext validationContext, TestContext context)
throws ValidationException {
assert false;
}
});
```

**What I've tried so far**
Unfortunately this does not work, where work in this case means: the test are green, which should not be the case because of the `assert false`. Instead what works is the following snippet:

```
designer
.query(myDbDatasource)
.sqlResource(resource)
.validateScript("assert true == true", "groovy")
.validator(new SqlResultSetScriptValidator() {
@Override
public void validateSqlResultSet(List> resultSet,
ScriptValidationContext validationContext, TestContext context)
throws ValidationException {
assert false;
}
});
```

It seems that I either don't fully understand the usage of the `validator()` method, or there is some hidden dependency that the validator is only used in the context of the script validation. Perhaps I'm also missing the point: What is the default way of programmatically validate the result set (ideally not only with type `List>` but rather as it can be defined in the `HttpClientResponseActionBuilder` with the `validationCallback` like this:

```
.validationCallback(new JsonMappingValidationCallback(MyJsonResultPojo.class, objectMapper) {
@Override
public void validate(
MyJsonResultPojo myJsonResultPojo,
Map map,
TestContext testContext
) {
assertThat(myJsonResultPojo.getMyAttribute()).isEqualTo(123);
}
});
```

I mean I could do it with groovy (and I love groovy :)) but honestly it is super hard to debug when the execution of the script is somewhere hidden within the framework. Therefore I would prefer proper Java POJOs

**Additional information**
The identified piece of code that causes this situation is here: https://github.com/citrusframework/citrus/blob/citrus-2.8.0/modules/citrus-core/src/main/java/com/consol/citrus/actions/ExecuteSQLQueryAction.java#L242

Thanks & Bye,
Mario

Contributor guide

Open the contributing guide

Research direction

Start by reading ExecuteSQLQueryBuilder.java around the validator configuration and ExecuteSQLQueryAction.java around the validation logic identified in the report. Trace why the custom SqlResultSetScriptValidator is only invoked with script validation, then determine the intended callback or POJO-validation behavior. Done means the Java DSL supports the requested programmatic result-set validation and its behavior is covered by the project’s existing validation approach.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, sql
Domain
databases, testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.