Skip to content

Commit

Permalink
Fixed $select not properly working on unitOfMeasurement sub-properties
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Jun 1, 2024
1 parent b51f060 commit 71eee9a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Fixed deep select for complex properties in GeoJSON and CSV output.
* Fixed automatic generation of Features when Feature and Location use different idTypes.
* Fixed GeoJSON output not containing geometries.
* Fixed $select not properly working on unitOfMeasurement sub-properties.


## Release version 2.3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq;

import com.fasterxml.jackson.databind.node.ArrayNode;
import de.fraunhofer.iosb.ilt.frostserver.model.ComplexValue;
import de.fraunhofer.iosb.ilt.frostserver.model.EntityType;
import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.model.core.EntitySet;
Expand Down Expand Up @@ -409,6 +410,13 @@ public void visit(PathElementCustomProperty element) {
resultObject = entityMap;
return;
}
} else if (inner instanceof ComplexValue cv) {
Object propertyValue = cv.getProperty(name);
Map<String, Object> entityMap = new HashMap<>();
entityName = name;
entityMap.put(entityName, propertyValue);
resultObject = entityMap;
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -516,6 +517,34 @@ void test20ResultQualityFilter() {
testFilterResults(sSrvc.dao(sMdl.etObservation), "resultQuality[0]/DQ_Result/code eq 2", getFromList(OBSERVATIONS, 1, 3, 5, 7, 9, 11));
}

@Test
void test21SelectUnit() {
LOGGER.info(" test21SelectUnit");
String urlString = serverSettings.getServiceUrl(version) + "/Datastreams(" + formatKeyValuesForUrl(DATASTREAMS.get(0)) + ")/unitOfMeasurement/symbol";
JsonNode json = getJsonObjectForResponse(urlString);
testResponseProperty(json, "symbol", DATASTREAMS.get(0).getProperty(EP_UNITOFMEASUREMENT).getSymbol(), urlString);

urlString = serverSettings.getServiceUrl(version) + "/Datastreams(" + formatKeyValuesForUrl(DATASTREAMS.get(0)) + ")/unitOfMeasurement/name";
json = getJsonObjectForResponse(urlString);
testResponseProperty(json, "name", DATASTREAMS.get(0).getProperty(EP_UNITOFMEASUREMENT).getName(), urlString);

urlString = serverSettings.getServiceUrl(version) + "/Datastreams(" + formatKeyValuesForUrl(DATASTREAMS.get(0)) + ")/unitOfMeasurement/definition";
json = getJsonObjectForResponse(urlString);
testResponseProperty(json, "definition", DATASTREAMS.get(0).getProperty(EP_UNITOFMEASUREMENT).getDefinition(), urlString);

urlString = serverSettings.getServiceUrl(version) + "/Datastreams(" + formatKeyValuesForUrl(DATASTREAMS.get(0)) + ")?$select=unitOfMeasurement/symbol";
json = getJsonObjectForResponse(urlString);
testResponseProperty(json, Arrays.asList("unitOfMeasurement", "symbol"), DATASTREAMS.get(0).getProperty(EP_UNITOFMEASUREMENT).getSymbol(), urlString);

urlString = serverSettings.getServiceUrl(version) + "/Datastreams(" + formatKeyValuesForUrl(DATASTREAMS.get(0)) + ")?$select=unitOfMeasurement/name";
json = getJsonObjectForResponse(urlString);
testResponseProperty(json, Arrays.asList("unitOfMeasurement", "name"), DATASTREAMS.get(0).getProperty(EP_UNITOFMEASUREMENT).getName(), urlString);

urlString = serverSettings.getServiceUrl(version) + "/Datastreams(" + formatKeyValuesForUrl(DATASTREAMS.get(0)) + ")?$select=unitOfMeasurement/definition";
json = getJsonObjectForResponse(urlString);
testResponseProperty(json, Arrays.asList("unitOfMeasurement", "definition"), DATASTREAMS.get(0).getProperty(EP_UNITOFMEASUREMENT).getDefinition(), urlString);
}

private JsonNode getJsonObjectForResponse(String urlString) {
// Ensure [ and ] are urlEncoded.
urlString = urlString.replaceAll(Pattern.quote("["), "%5B").replaceAll(Pattern.quote("]"), "%5D");
Expand All @@ -536,6 +565,21 @@ private JsonNode getJsonObjectForResponse(String urlString) {
return json;
}

private void testResponseProperty(JsonNode response, List<String> propertyNames, String expectedValue, String urlForError) {
JsonNode container = response;
final int size = propertyNames.size();
for (int idx = 0; idx < size - 1; idx++) {
String propertyName = propertyNames.get(idx);
JsonNode value = container.get(propertyName);
if (value == null || !value.isContainerNode()) {
fail("field '" + propertyName + "' is not a container node for request: " + urlForError);
return;
}
container = value;
}
testResponseProperty(container, propertyNames.get(size - 1), expectedValue, urlForError);
}

private void testResponseProperty(JsonNode response, String propertyName, String expectedValue, String urlForError) {
JsonNode value = response.get(propertyName);
if (value == null || !value.isTextual()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ protected PropertyFields<TableImpDatastreams> propertyFieldForUoM(final Field fi
uom.setSymbol(value);
}
case "definition" -> {
uom.setSymbol(value);
uom.setDefinition(value);
}
default -> {
// Do nothing.
Expand Down

0 comments on commit 71eee9a

Please sign in to comment.