Skip to content

Commit

Permalink
Updating_examples
Browse files Browse the repository at this point in the history
This illustrate the usage of funcions that returns not json objects
  • Loading branch information
fjtirado committed Aug 26, 2024
1 parent 7fa444c commit 906a97e
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,29 @@
"id": "expression",
"version": "1.0",
"name": "Workflow Expression example",
"constants" : {
"Dog" : {
"castellano" : "perro",
"leones": "perru",
"gallego" : "can",
"aragones" : "cocho",
"catalan" : "gos",
"vasco": "txakurra"
}
},
"dataInputSchema" : "schema/expression.json",
"description": "An example of how to use a JQ expression assignment",
"start": "squareState",
"extensions" : [ {
"extensionid": "workflow-output-schema",
"outputSchema": "schema/result.json"
}
],
"start": "max",
"functions": [
{
"name": "max",
"type": "expression",
"operation": "{max: .numbers | max_by(.x), min: .numbers | min_by(.y)}"
},
{
"name": "printMessage",
"type": "custom",
"operation": "sysout"
"name": "square",
"type": "expression",
"operation": ".number | sqrt"
}
],
"states": [
{
"name": "squareState",
"name": "max",
"type": "operation",
"actions": [
{
Expand All @@ -48,23 +38,22 @@
}
}
],
"transition": "finish"
"transition": "square"
},
{
"name": "finish",
"name": "square",
"type": "operation",
"stateDataFilter": {
"input": "{result: .number}"
"output": "{result}"
},
"actions": [
{
"name": "printAction",
"functionRef": {
"refName": "printMessage",
"arguments": {
"message": ".result"
"name": "square",
"functionRef": "square"
,
"actionDataFilter" : {
"results" : "{result: .}"
}
}
}
],
"end": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void testErrorRest() {
.post("/expression")
.then()
.statusCode(201)
.body("workflowdata.result", is(4))
.body("workflowdata.result", is(2.0f))
.body("workflowdata.number", nullValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
quarkus.native.native-image-xmx=8g
# OpenAPI Properties
quarkus.swagger-ui.always-include=true
quarkus.devservices.enabled=false

# OpenApi Client Properties
quarkus.rest-client.subtraction_yaml.url=http://localhost:8181
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
"functionRef": {
"refName": "multiplication",
"arguments": "{ leftElement: .difference, rightElement: .multiplyValue }"
},
"actionDataFilter" : {
"results" : "{product: .}"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,11 @@ paths:
responses:
"200":
description: OK
content:
application/json:
schema:
type: object
properties:
product:
format: float
type: number
content:
text/plain:
schema:
type: number
format: float
components:
schemas:
MultiplicationOperation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public class OperationsMockService implements QuarkusTestResourceLifecycleManage
public Map<String, String> start() {
multiplicationService =
this.startServer(8282,
"{ \"product\": 37.808 }");
"37.808", "text/plain");
subtractionService =
this.startServer(8181,
"{ \"difference\": 68.0 }");
"{ \"difference\": 68.0 }", "application/json");
return Collections.emptyMap();
}

Expand All @@ -58,13 +58,13 @@ public void stop() {
}
}

private WireMockServer startServer(final int port, final String response) {
private WireMockServer startServer(final int port, final String response, final String contentType) {
final WireMockServer server = new WireMockServer(port);
server.start();
server.stubFor(post(urlEqualTo("/"))
.withHeader(CloudEventExtensionConstants.PROCESS_ID, WireMock.matching(".*"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Content-Type", contentType)
.withBody(response)));
return server;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,14 @@
import io.quarkus.runtime.annotations.RegisterForReflection;

@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class OperationResource {

@POST
@APIResponseSchema(value = OperationResource.Result.class, responseDescription = "MultiplicationResult", responseCode = "200")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public Response doOperation(@NotNull MultiplicationOperation operation) {
return Response.ok(new Result(operation.getLeftElement() * operation.getRightElement())).build();
return Response.ok(operation.getLeftElement() * operation.getRightElement(), MediaType.TEXT_PLAIN).build();
}

@RegisterForReflection
public static final class Result {

float product;

public Result() {
}

public Result(float product) {
this.product = product;
}

public float getProduct() {
return product;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# quarkus.package.type=fast-jar
quarkus.native.native-image-xmx=8g
quarkus.swagger-ui.always-include=true
quarkus.devservices.enabled=false

# profile to pack this example into a container, to use it execute activate the maven container profile, -Dcontainer
%container.quarkus.container-image.build=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;
Expand All @@ -32,13 +33,14 @@ class OperationResourceIT {

@Test
void testRestExample() {
final OperationResource.Result result = given()
Response result = given()
.contentType(ContentType.JSON)
.when()
.body(new MultiplicationOperation(2, 2))
.post("/")
.then()
.statusCode(200).extract().as(OperationResource.Result.class);
assertThat(result.getProduct(), is(4f));
.response().contentType(ContentType.TEXT)
.statusCode(200)
.when()
.post("/");
assertThat(Float.parseFloat(result.asString()), is(4f));
}
}

0 comments on commit 906a97e

Please sign in to comment.