This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ehrbase/feature/NUM-1095_validate_aql
feat(NUM-1095): Validate aql query
- Loading branch information
Showing
6 changed files
with
183 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
21 changes: 21 additions & 0 deletions
21
src/main/java/org/ehrbase/aqleditor/dto/aql/QueryValidationResponse.java
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,21 @@ | ||
package org.ehrbase.aqleditor.dto.aql; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@JsonInclude(Include.NON_NULL) | ||
public class QueryValidationResponse { | ||
|
||
private boolean valid; | ||
private String message; | ||
private String startLine; | ||
private String startColumn; | ||
private String error; | ||
|
||
} |
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
109 changes: 109 additions & 0 deletions
109
src/test/java/org.ehrbase.aqleditor/AqlServiceTest.java
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,109 @@ | ||
package org.ehrbase.aqleditor; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsStringIgnoringCase; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.hamcrest.core.IsNull.notNullValue; | ||
import static org.hamcrest.core.IsNull.nullValue; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.ehrbase.aqleditor.dto.aql.QueryValidationResponse; | ||
import org.ehrbase.aqleditor.dto.aql.Result; | ||
import org.ehrbase.aqleditor.service.AqlEditorAqlService; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class AqlServiceTest { | ||
|
||
@InjectMocks | ||
private AqlEditorAqlService aqlService; | ||
|
||
private final String INVALID_QUERY = "Select TOP 13 FORWARD where (o0/data[at0001]/events[at0at0001]/events[at0002]/data[at0003]/items[at0004]/value/magnitude < 1.1)"; | ||
private final String VALID_QUERY = "Select TOP 13 FORWARD o0/data[at0001]/events[at0002]/data[at0003]/items[at0004]/value/magnitude as Systolic__magnitude, e/ehr_id/value as ehr_id from EHR e contains OBSERVATION o0[openEHR-EHR-OBSERVATION.sample_blood_pressure.v1] where (o0/data[at0001]/events[at0002]/data[at0003]/items[at0004]/value/magnitude >= $magnitude and o0/data[at0001]/events[at0002]/data[at0003]/items[at0004]/value/magnitude < 1.1)"; | ||
|
||
@Test | ||
public void shouldCorrectlyValidateAql1() { | ||
Result aql = Result.builder().q("invalid aql").build(); | ||
QueryValidationResponse response = aqlService.validateAql(aql); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.isValid(), is(false)); | ||
|
||
assertThat(response.getStartColumn(), is("0")); | ||
assertThat(response.getStartLine(), is("1")); | ||
assertThat(response.getMessage(), | ||
containsStringIgnoringCase("AQL Parse exception: line 1: char 0")); | ||
assertThat(response.getError(), | ||
containsStringIgnoringCase("mismatched input 'invalid' expecting SELECT")); | ||
} | ||
|
||
@Test | ||
public void shouldCorrectlyValidateAql2() { | ||
|
||
Result aql = Result.builder().q(INVALID_QUERY).build(); | ||
QueryValidationResponse response = aqlService.validateAql(aql); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.isValid(), is(false)); | ||
|
||
assertThat(response.getStartColumn(), is("23")); | ||
assertThat(response.getStartLine(), is("1")); | ||
assertThat(response.getMessage(), | ||
containsStringIgnoringCase("AQL Parse exception: line 1: char 23")); | ||
assertThat(response.getError(), | ||
containsStringIgnoringCase("mismatched input 'where' expecting {DISTINCT, FUNCTION_IDENTIFIER, EXTENSION_IDENTIFIER, IDENTIFIER, INTEGER, STRING}")); | ||
} | ||
|
||
@Test | ||
public void shouldCorrectlyValidateAql3() { | ||
Result aql = Result.builder().q(VALID_QUERY).build(); | ||
QueryValidationResponse response = aqlService.validateAql(aql); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.isValid(), is(true)); | ||
assertThat(response.getMessage(), containsStringIgnoringCase("Query is valid")); | ||
} | ||
|
||
@Test | ||
public void shouldCorrectlyBuildErrorResponseWhenLineAndCharMissing() { | ||
|
||
QueryValidationResponse response = aqlService.buildResponse("AQL Parse exception:"); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.isValid(), is(false)); | ||
|
||
assertThat(response.getStartColumn(), nullValue()); | ||
assertThat(response.getStartLine(), nullValue()); | ||
assertThat(response.getMessage(), | ||
containsStringIgnoringCase("AQL Parse exception:")); | ||
} | ||
|
||
@Test | ||
public void shouldCorrectlyBuildErrorResponseWhenEmpty() { | ||
|
||
QueryValidationResponse response = aqlService.buildResponse(StringUtils.EMPTY); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.isValid(), is(false)); | ||
|
||
assertThat(response.getStartColumn(), nullValue()); | ||
assertThat(response.getStartLine(), nullValue()); | ||
assertThat(response.getMessage(), nullValue()); | ||
} | ||
|
||
@Test | ||
public void shouldCorrectlyBuildErrorResponseWhenNull() { | ||
|
||
QueryValidationResponse response = aqlService.buildResponse(null); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.isValid(), is(false)); | ||
|
||
assertThat(response.getStartColumn(), nullValue()); | ||
assertThat(response.getStartLine(), nullValue()); | ||
assertThat(response.getMessage(), nullValue()); | ||
} | ||
} |