Skip to content

Commit

Permalink
fix evaluate list error (#123)
Browse files Browse the repository at this point in the history
Co-authored-by: zhiyaoPCCW <[email protected]>
  • Loading branch information
zhiyaoPCCW and zhiyaoPCCW authored Nov 12, 2024
1 parent 12cdedc commit 403c2ad
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,11 @@ public Object evaluate(String expression) {
}

public <T> T evaluate(String expression, Class<T> clazz) {
log.info("Evaluating expression: {}", expression);
try {
return expressionParser
.parseExpression(expression, parserContext)
.getValue(evaluationContext, clazz);
} catch (Exception ex) {
log.error("Error evaluating expression: {}", expression, ex);
return null;
}
}
Expand Down Expand Up @@ -143,9 +141,9 @@ public static Object evaluateObject(
return expression;
}
} catch (Exception e) {
log.warn("spel parse object error!", e);
log.warn("spel parse object error: {}", e.getMessage());
}
return evaluate(JsonToolkit.toJson(expression), context, String.class);
return evaluate(String.valueOf(expression), context, String.class);
}

private static Object parseStr(String str, Map<String, Object> context, Boolean postRequest) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.consoleconnect.kraken.operator.gateway;

import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;

import com.consoleconnect.kraken.operator.core.exception.KrakenException;
import com.consoleconnect.kraken.operator.core.model.UnifiedAsset;
import com.consoleconnect.kraken.operator.core.model.facet.ComponentAPITargetFacets;
Expand All @@ -14,6 +18,7 @@
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -197,4 +202,14 @@ void test_parse_to_list() throws IOException {
String s1 = calculateBasedOnResponseBody(JsonToolkit.toJson(o), context);
Assertions.assertNotNull(s1);
}

@Test
@SneakyThrows
void givenMapExpression_whenEvaluate_thenReturnSuccess() {
String s = readFileToString("mockData/address.list.mock.json");
Object evaluate =
SpELEngine.evaluate(JsonToolkit.fromJson(s, Object.class), new HashMap<>(), true);
assertThat(
String.valueOf(evaluate), hasJsonPath("$.alternateGeographicAddress[0].tags", hasSize(1)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"validationResult": "partial",
"alternateGeographicAddress": [
{
"id": "mock-id",
"@type": "FieldedAddress",
"country": "AU",
"city": "Sydney",
"locality": "12345",
"name": "Sydney location",
"tags": [
"offnet"
]
}
],
"provideAlternative": true,
"submittedGeographicAddress": {
"@type": "FieldedAddress",
"country": "AU",
"city": "Sydney",
"postcode": "60027",
"postcodeExtension": "string",
"streetName": "string",
"streetType": "VIA",
"streetSuffix": "string",
"streetNr": "12",
"streetNrSuffix": "string",
"streetNrLast": "string",
"streetNrLastSuffix": "string",
"locality": "e7539c5fe66f9e4ce5b08ab7",
"stateOrProvince": "ANKONA"
}
}

0 comments on commit 403c2ad

Please sign in to comment.