Skip to content

Commit 337f81b

Browse files
authored
Example: For refactor / JSON parse log (#2690)
* feat(dispatcher): add production mode support for error handling and extend URI validation tests - Introduced `productionRouter` in `DummyTestRouter` to enable production mode configuration. - Updated `DispatchingInterceptor` to adjust error messages in production mode. - Expanded `DispatchingInterceptorTest` with production-specific URI validation. * refactor(orchestration): simplify for-loop logic, improve JSON response formatting, and enhance test validation
1 parent 92e3423 commit 337f81b

3 files changed

Lines changed: 34 additions & 33 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static HashMap<String, Object> createParameterBindings(Router router, Exc
8383
params.put("headers", headerMap);
8484
if (includeJsonObject) {
8585
try {
86-
log.info("Parsing body as JSON for scripting plugins");
86+
log.debug("Parsing body as JSON for scripting plugins");
8787
params.put("json", om.readValue(readInputStream(msg.getBodyAsStreamDecoded()), Map.class));
8888
} catch (Exception e) {
8989
log.warn("Can't parse body as JSON", e);
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
# yaml-language-server: $schema=https://www.membrane-api.io/v7.0.6.json
2+
23
api:
34
port: 2000
45
flow:
56
- request:
6-
# Fetch the complete list of products (limit set to avoid pagination)
77
- call:
8-
url: https://api.predic8.de/shop/v2/products?limit=1000
9-
- setProperty:
10-
name: products
11-
value: ${$.products}
12-
language: jsonpath
8+
url: https://api.predic8.de/shop/v2/products?limit=7
139
# Iterate over each product to get additional details (price)
1410
- for:
15-
in: property.products
11+
in: $.products
12+
language: jsonpath
1613
flow:
1714
- call:
1815
url: https://api.predic8.de/shop/v2/products/${property.it['id']}
19-
- setProperty:
20-
name: price
21-
value: ${$.price}
22-
language: jsonpath
16+
language: spel
2317
- groovy:
24-
src: property.it.price = property.price
18+
src: |
19+
property.result = property.result ?: []
20+
property.result.add(
21+
[ "name": property.it.name,
22+
"price": fn.jsonPath('$.price')
23+
])
2524
# Render a simplified JSON response with only product name and price
2625
- template:
2726
contentType: application/json
2827
pretty: true
2928
src: |
3029
{
31-
"products": [
32-
<% property.products.eachWithIndex { p, idx -> %>
33-
{
34-
"name": "<%= p.name %>",
35-
"price": "<%= p.price %>"
36-
}<%= idx < property.products.size() - 1 ? ',' : '' %>
37-
<% } %>
30+
"products": [
31+
<% property.result.eachWithIndex { p, idx -> %>
32+
{
33+
"name": "${p.name}",
34+
"price": ${p.price}
35+
}${idx < property.result.size() - 1 ? ',' : ''}
36+
<% } %>
3837
]
3938
}
40-
- return: {}
39+
- return:
40+
status: 200
41+

distribution/src/test/java/com/predic8/membrane/examples/withinternet/test/orchestration/ForLoopExampleTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
package com.predic8.membrane.examples.withinternet.test.orchestration;
1616

17-
import com.predic8.membrane.examples.util.AbstractSampleMembraneStartStopTestcase;
18-
import org.junit.jupiter.api.Test;
17+
import com.predic8.membrane.examples.util.*;
18+
import org.junit.jupiter.api.*;
1919

20-
import static io.restassured.RestAssured.given;
21-
import static org.hamcrest.CoreMatchers.containsString;
20+
import static io.restassured.RestAssured.*;
21+
import static org.hamcrest.Matchers.*;
2222

2323
public class ForLoopExampleTest extends AbstractSampleMembraneStartStopTestcase {
2424

@@ -27,18 +27,18 @@ protected String getExampleDirName() {
2727
return "orchestration/for-loop";
2828
}
2929

30-
// @formatter:off
3130
@Test
32-
void testCall() {
31+
void call() {
32+
// @formatter:off
3333
given()
3434
.when()
3535
.get("http://localhost:2000")
3636
.then()
37-
.body(
38-
containsString("\"products\""),
39-
containsString("\"name\""),
40-
containsString("\"price\"")
41-
).statusCode(200);
37+
.body("products[0].name", notNullValue())
38+
.body("products[0].price", instanceOf(Number.class))
39+
.body("products.size()", greaterThan(2))
40+
.statusCode(200);
41+
// @formatter:on
4242
}
43-
// @formatter:on
43+
4444
}

0 commit comments

Comments
 (0)