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

[CDR-1397] Add Execution info to MetaData #594

Merged
Merged
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Note: version releases in the 0.x.y range may introduce breaking changes.

## [unreleased]
### Added
### Added
- Added EHRbase AQL `MeataData` debug execution data ([594](https://github.com/ehrbase/openEHR_SDK/pull/594))
### Changed
- Removed OpenEhrClient::getFolder (use directoryCrudEndpoint()::getFolder instead) ([#588](https://github.com/ehrbase/openEHR_SDK/pull/588))
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public interface AdditionalProperty<T> extends Function<Object, T> {
*/
String getName();

@FunctionalInterface
interface BooleanProperty extends MetaData.AdditionalProperty<Boolean> {

@Override
default Boolean apply(Object o) {
return o instanceof Boolean ? (Boolean) o : null;
}
}

@FunctionalInterface
interface IntegerProperty extends AdditionalProperty<Integer> {

Expand All @@ -60,6 +69,25 @@ default Integer apply(Object o) {
}
}

@FunctionalInterface
interface StringProperty extends MetaData.AdditionalProperty<String> {

@Override
default String apply(Object o) {
return o instanceof String ? (String) o : null;
}
}

@FunctionalInterface
interface JSONProperty extends MetaData.AdditionalProperty<Map<String, Object>> {

@SuppressWarnings("unchecked")
@Override
default Map<String, Object> apply(Object o) {
return o instanceof Map ? (Map<String, Object>) o : null;
}
}

/**
* Extracted from the query <code>fetch</code> parameter or from the server default.
* <pre>
Expand All @@ -80,6 +108,21 @@ default Integer apply(Object o) {
* Size of the returned rows.
*/
IntegerProperty resultSize = () -> "resultsize";

/**
* ehrbase execution-option result - dry_run status
*/
BooleanProperty dryRun = () -> "_dry_run";

/**
* ehrbase execution-option result - executed SQL
*/
StringProperty executedSQL = () -> "_executed_sql";

/**
* ehrbase execution-option result - query plan json
*/
JSONProperty queryPlan = () -> "_query_plan";
}

@JsonProperty(value = "_href")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.ehrbase.openehr.sdk.response.dto.util.DTOFixtures;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -89,6 +90,41 @@ void serializedJSON() throws JsonProcessingException {
}""");
}

@Test
void serializedJSONWithExecutionData() throws JsonProcessingException {

MetaData metaData = new MetaData();
metaData.setHref("https://example.com/subpath/ehrbase/rest/openehr/v1/query/aql");
metaData.setType(MetaData.RESULTSET);
metaData.setSchemaVersion("1.0.4");
metaData.setCreated(OffsetDateTime.parse("2017-08-19T12:30:00.568+02:00"));
metaData.setGenerator("DIPS.OpenEhr.ResultSets.Serialization.Json.ResultSetJsonWriter (5.0.0.0)");
metaData.setExecutedAql("SELECT e/ehr_id/value FROM EHR e");
// debug info
metaData.setAdditionalProperty(MetaData.AdditionalProperty.dryRun, true);
metaData.setAdditionalProperty(MetaData.AdditionalProperty.executedSQL, "SELECT TRUE");
metaData.setAdditionalProperty(MetaData.AdditionalProperty.queryPlan, Map.of("key", "value"));

String json = objectMapper.writeValueAsString(metaData);

assertThat(json)
.isEqualToNormalizingWhitespace(
"""
{
"_href" : "https://example.com/subpath/ehrbase/rest/openehr/v1/query/aql",
"_type" : "RESULTSET",
"_schema_version" : "1.0.4",
"_created" : "2017-08-19T12:30:00.568+02:00",
"_generator" : "DIPS.OpenEhr.ResultSets.Serialization.Json.ResultSetJsonWriter (5.0.0.0)",
"_executed_aql" : "SELECT e/ehr_id/value FROM EHR e",
"_dry_run" : true,
"_executed_sql" : "SELECT TRUE",
"_query_plan" : {
"key" : "value"
}
}""");
}

@Test
void deserializedJSONMinimal() throws JsonProcessingException {

Expand Down Expand Up @@ -148,6 +184,40 @@ void deserializeJSON() throws JsonProcessingException {
assertEquals(20, metaData.getAdditionalProperty(MetaData.AdditionalProperty.resultSize));
}

@Test
void deserializeJSONWithExecutionData() throws JsonProcessingException {

MetaData metaData = objectMapper.readValue(
"""
{
"_href" : "https://example.com/ehrbase/rest/openehr/v1/query/aql",
"_type" : "RESULTSET",
"_schema_version" : "1.0.4",
"_created" : "2017-08-19T00:25:47.568+02:00",
"_generator" : "DIPS.OpenEhr.ResultSets.Serialization.Json.ResultSetJsonWriter (5.0.0.0)",
"_executed_aql" : "SELECT e/ehr_id/value FROM EHR e",
"_dry_run" : true,
"_executed_sql" : "SELECT TRUE",
"_query_plan" : {
"key" : "value"
}
}""",
MetaData.class);

assertEquals("https://example.com/ehrbase/rest/openehr/v1/query/aql", metaData.getHref());
assertEquals(MetaData.RESULTSET, metaData.getType());
assertEquals("1.0.4", metaData.getSchemaVersion());
assertEquals(
OffsetDateTime.parse("2017-08-19T00:25:47.568+02:00").atZoneSameInstant(ZoneOffset.UTC),
metaData.getCreated().atZoneSameInstant(ZoneOffset.UTC));
assertEquals(
"DIPS.OpenEhr.ResultSets.Serialization.Json.ResultSetJsonWriter (5.0.0.0)", metaData.getGenerator());
assertEquals("SELECT e/ehr_id/value FROM EHR e", metaData.getExecutedAql());
assertEquals(true, metaData.getAdditionalProperty(MetaData.AdditionalProperty.dryRun));
assertEquals("SELECT TRUE", metaData.getAdditionalProperty(MetaData.AdditionalProperty.executedSQL));
assertEquals(Map.of("key", "value"), metaData.getAdditionalProperty(MetaData.AdditionalProperty.queryPlan));
}

@Test
void deserializeRealWorldExample() throws IOException {

Expand Down