Skip to content

Commit

Permalink
Migrate xml-to-json policy to use new mediation context
Browse files Browse the repository at this point in the history
  • Loading branch information
pubudu91 committed Dec 13, 2022
1 parent 7f1656b commit 34bdbe3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion mediation-xml-to-json/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "choreo"
name = "mediation.xml_to_json"
version = "0.3.1"
version = "0.4.0"
distribution = "2201.2.3"
keywords=["choreo-apim-mediation-policy", "choreo-apim-mediation-request-flow", "choreo-apim-mediation-response-flow"]

Expand Down
7 changes: 5 additions & 2 deletions mediation-xml-to-json/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,18 @@ modules = [
[[package]]
org = "choreo"
name = "mediation"
version = "0.6.1"
version = "0.7.0"
dependencies = [
{org = "ballerina", name = "regex"}
]
modules = [
{org = "choreo", packageName = "mediation", moduleName = "mediation"}
]

[[package]]
org = "choreo"
name = "mediation.xml_to_json"
version = "0.3.1"
version = "0.4.0"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "mime"},
Expand Down
13 changes: 11 additions & 2 deletions mediation-xml-to-json/tests/utest_policy.bal
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import ballerina/http;
import ballerina/test;
import choreo/mediation;

xml originalPayload = xml `<greeting><name>John Doe</name><message>Hello World!</message></greeting>`;
json expPayload = {"greeting": {"name": "John Doe", "message": "Hello World!"}};
Expand All @@ -24,15 +25,15 @@ json expPayload = {"greeting": {"name": "John Doe", "message": "Hello World!"}};
public function testRequestFlowSingleInstance() {
http:Request req = new;
req.setXmlPayload(originalPayload);
http:Response|false|error|() result = xmlToJsonIn({httpMethod: "get", resourcePath: "/greet"}, req);
http:Response|false|error|() result = xmlToJsonIn(createContext("get", "/greet"), req);
assertResult(result, req.getJsonPayload(), expPayload);
}

@test:Config {}
public function testResponseFlowSingleInstance() {
http:Response res = new;
res.setXmlPayload(originalPayload);
http:Response|false|error|() result = xmlToJsonOut({httpMethod: "get", resourcePath: "/greet"}, new, res);
http:Response|false|error|() result = xmlToJsonOut(createContext("get", "/greet"), new, res);
assertResult(result, res.getJsonPayload(), expPayload);
}

Expand All @@ -47,3 +48,11 @@ function assertResult(http:Response|false|error|() result, json|http:ClientError

test:assertEquals(payload, expVal);
}

function createContext(string httpMethod, string resPath) returns mediation:Context {
mediation:ResourcePath originalPath = checkpanic mediation:createImmutableResourcePath(resPath);
mediation:Context originalCtx =
mediation:createImmutableMediationContext(httpMethod, originalPath.pathSegments(), {}, {});
mediation:ResourcePath mutableResPath = checkpanic mediation:createMutableResourcePath(resPath);
return mediation:createMutableMediationContext(originalCtx, mutableResPath.pathSegments(), {}, {});
}

0 comments on commit 34bdbe3

Please sign in to comment.