Skip to content

Commit 5a3d293

Browse files
authored
Add unit tests for ScriptingUtils JSON handling and improve exception logging (#2956)
1 parent b6622e0 commit 5a3d293

2 files changed

Lines changed: 77 additions & 21 deletions

File tree

core/src/main/java/com/predic8/membrane/core/lang/ScriptingUtils.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,29 @@
1616

1717
package com.predic8.membrane.core.lang;
1818

19-
import com.fasterxml.jackson.databind.*;
20-
import com.predic8.membrane.core.exchange.*;
21-
import com.predic8.membrane.core.http.*;
22-
import com.predic8.membrane.core.interceptor.Interceptor.*;
23-
import com.predic8.membrane.core.lang.groovy.*;
24-
import com.predic8.membrane.core.openapi.serviceproxy.*;
25-
import com.predic8.membrane.core.openapi.util.*;
26-
import com.predic8.membrane.core.router.*;
27-
import com.predic8.membrane.core.util.text.*;
28-
import org.slf4j.*;
29-
30-
import java.util.*;
31-
import java.util.function.*;
32-
33-
import static com.predic8.membrane.core.interceptor.Interceptor.Flow.*;
34-
import static com.predic8.membrane.core.openapi.util.UriTemplateMatcher.*;
35-
import static com.predic8.membrane.core.util.FileUtil.*;
36-
import static com.predic8.membrane.core.util.URLParamUtil.*;
37-
import static java.util.Collections.*;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.predic8.membrane.core.exchange.Exchange;
21+
import com.predic8.membrane.core.http.HeaderMap;
22+
import com.predic8.membrane.core.http.LazyCookieMap;
23+
import com.predic8.membrane.core.interceptor.Interceptor.Flow;
24+
import com.predic8.membrane.core.lang.groovy.GroovyBuiltInFunctions;
25+
import com.predic8.membrane.core.lang.groovy.PathParametersMap;
26+
import com.predic8.membrane.core.openapi.serviceproxy.APIProxy;
27+
import com.predic8.membrane.core.openapi.util.PathDoesNotMatchException;
28+
import com.predic8.membrane.core.router.Router;
29+
import com.predic8.membrane.core.util.text.SerializationFunction;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
36+
import static com.predic8.membrane.core.interceptor.Interceptor.Flow.REQUEST;
37+
import static com.predic8.membrane.core.interceptor.Interceptor.Flow.RESPONSE;
38+
import static com.predic8.membrane.core.openapi.util.UriTemplateMatcher.matchTemplate;
39+
import static com.predic8.membrane.core.util.FileUtil.readInputStream;
40+
import static com.predic8.membrane.core.util.URLParamUtil.getParams;
41+
import static java.util.Collections.emptyMap;
3842

3943
public class ScriptingUtils {
4044

@@ -92,9 +96,9 @@ public static Map<String, Object> createParameterBindings(Router router, Exchang
9296
if (includeJsonObject) {
9397
try {
9498
log.debug("Parsing body as JSON for scripting plugins");
95-
params.put("json", om.readValue(readInputStream(msg.getBodyAsStreamDecoded()), Map.class));
99+
params.put("json", om.readValue(readInputStream(msg.getBodyAsStreamDecoded()), Object.class));
96100
} catch (Exception e) {
97-
log.warn("Can't parse body as JSON", e);
101+
log.info("Can't parse body as JSON: {}", e.getMessage());
98102
}
99103
}
100104
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.predic8.membrane.core.lang;
2+
3+
import com.predic8.membrane.core.router.DefaultRouter;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.junit.jupiter.api.Nested;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.net.URISyntaxException;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
import static com.predic8.membrane.core.http.Request.get;
13+
import static com.predic8.membrane.core.interceptor.Interceptor.Flow.REQUEST;
14+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
15+
import static org.junit.jupiter.api.Assertions.assertNotNull;
16+
17+
class ScriptingUtilsTest {
18+
19+
@Nested
20+
class json {
21+
22+
@Test
23+
void map() throws URISyntaxException {
24+
assertInstanceOf(Map.class, createBinding("""
25+
{"foo":1}""").get("json"));
26+
}
27+
28+
@Test
29+
void array() throws URISyntaxException {
30+
assertInstanceOf(List.class, createBinding("[1,2,3]").get("json"));
31+
}
32+
33+
@Test
34+
void string() throws URISyntaxException {
35+
assertInstanceOf(String.class, createBinding("""
36+
"foo"
37+
""").get("json"));
38+
}
39+
40+
@Test
41+
void number() throws URISyntaxException {
42+
assertInstanceOf(Integer.class, createBinding("7").get("json"));
43+
}
44+
45+
private static @NotNull Map<String, Object> createBinding(String json) throws URISyntaxException {
46+
var bindings = ScriptingUtils.createParameterBindings(new DefaultRouter(), get("/foo").json(json).buildExchange(), REQUEST, true, null);
47+
assertNotNull(bindings);
48+
return bindings;
49+
}
50+
}
51+
52+
}

0 commit comments

Comments
 (0)