Skip to content

Commit

Permalink
Updating_examples (apache#2005)
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 authored and rgdoliveira committed Sep 5, 2024
1 parent 07ec421 commit 4658b6b
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This example contains a simple workflow service that illustrate JQ expression usage.
The service is described using JSON format as defined in the
[CNCF Serverless Workflow specification](https://github.com/serverlessworkflow/specification).
The service accepts an array of complex numbers (x being the real coordinate and y the imaginary one) and return the max real coordinate.
The service accepts an array of complex numbers (x being the real coordinate and y the imaginary one) and return the square of the max real coordinate.


## Installing and Running
Expand Down Expand Up @@ -84,19 +84,14 @@ curl -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' -d
```


In Quarkus you should see the log message printed:

```text
4
```

And the returned data will be something similar to

```json
{
"id": "9f30a25e-61d4-4e80-bc7c-eb04db51564c",
"workflowdata": {
"result": 4
"result": 2.0
}
}
```
Expand Down
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" : {
"toStateData": ".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 4658b6b

Please sign in to comment.