Skip to content

Commit

Permalink
fix: Expressions languages are more complex than we thought
Browse files Browse the repository at this point in the history
  • Loading branch information
Delawen committed Mar 8, 2023
1 parent eb77cb9 commit 422b6f9
Show file tree
Hide file tree
Showing 34 changed files with 899 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ spec:
completion-mode: MANUAL
steps:
- throttle:
caller-runs-when-rejected: false
constant: '5'
executor-service: myExecutor
reject-execution: false
async-delayed: true
caller-runs-when-rejected: false
reject-execution: false
- sample:
description:
text: Message Sampler
Expand All @@ -58,29 +58,29 @@ spec:
simple: ${body}
async-delayed: true
- routing-slip:
uri-delimiter: '|'
simple: ${body}
uri-delimiter: '|'
- choice:
when:
- simple: '{{?foo}}'
steps:
- dynamic-router:
simple: ${body}
- set-header:
name: bar
simple: foo
name: bar
- simple: '{{?bar}}'
steps:
- set-property:
name: property
simple: bar
name: property
- threads:
pool-size: 8
- split:
tokenize: ','
steps:
- remove-property:
name: property
tokenize: ','
- marshal:
json:
library: Gson
Expand All @@ -92,9 +92,9 @@ spec:
- simple: '{{?baz}}'
steps:
- recipient-list:
stop-on-exception: true
constant: direct:a,direct:b
parallel-processing: true
stop-on-exception: true
- remove-headers:
exclude-pattern: toExclude
pattern: toRemove
Expand Down Expand Up @@ -124,14 +124,14 @@ spec:
logging-level: INFO
log-name: yaml
- service-call:
blacklist-service-filter:
servers:
- service2@host1
name: sc
static-service-discovery:
servers:
- service1@host1
- service1@host2
blacklist-service-filter:
servers:
- service2@host1
otherwise:
steps:
- remove-header:
Expand Down Expand Up @@ -192,8 +192,8 @@ spec:
logging-level: INFO
log-name: yaml
- idempotent-consumer:
idempotent-repository: myRepo
simple: ${header.id}
idempotent-repository: myRepo
steps:
- set-body:
simple: ola ke ase
Expand Down
5 changes: 5 additions & 0 deletions camel-route-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kaoto.backend.api.service.step.parser.camelroute;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -11,11 +12,14 @@
import javax.inject.Inject;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import io.kaoto.backend.api.metadata.catalog.StepCatalog;
import io.kaoto.backend.api.service.deployment.generator.camelroute.CamelRouteDeploymentGeneratorService;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@QuarkusTest
class CamelRouteStepParserServiceTest {
Expand Down Expand Up @@ -62,4 +66,38 @@ void deepParse() throws IOException {
assertThat(yaml).isEqualToNormalizingNewlines(route);
assertTrue(camelRouteDeploymentGeneratorService.supportedCustomResources().isEmpty());
}


@ParameterizedTest
@ValueSource(strings = {"route.yaml", "route2.yaml", "route3.yaml"})
void deepParseParametrized(String file) throws IOException {
var route = new String(this.getClass().getResourceAsStream(file).readAllBytes(),
StandardCharsets.UTF_8);
assertTrue(camelRouteStepParserService.appliesTo(route));
var steps = camelRouteStepParserService.deepParse(route);
assertTrue(camelRouteDeploymentGeneratorService.appliesTo(steps.getSteps()));
assertTrue(steps.getParameters() == null || steps.getParameters().isEmpty());
assertTrue(steps.getMetadata() == null || steps.getMetadata().isEmpty());
assertFalse(steps.getSteps().isEmpty());

var yaml = camelRouteDeploymentGeneratorService.parse(steps.getSteps(), steps.getMetadata(),
steps.getParameters());
assertThat(yaml).isEqualToNormalizingNewlines(route);
assertTrue(camelRouteDeploymentGeneratorService.supportedCustomResources().isEmpty());
}


@Test
@Disabled("Until we support full expressions in conditionals")
void compareCamelAndKebabCases() throws IOException {
var route = new String(this.getClass().getResourceAsStream("route2.yaml").readAllBytes(),
StandardCharsets.UTF_8);
var routeb = new String(this.getClass().getResourceAsStream("route2b.yaml").readAllBytes(),
StandardCharsets.UTF_8);

var steps = camelRouteStepParserService.deepParse(route);
var stepsb = camelRouteStepParserService.deepParse(routeb);

assertEquals(steps, stepsb);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
- from:
uri: timer:trigger-gitter-streaming
parameters:
repeatCount: '1'
steps:
- log:
message: Opening connection to Gitter...
- set-header:
name: Authorization
expression:
simple:
expression: Bearer {{token}}
- set-header:
simple:
expression: ${body}
result-type: long
- to:
uri: https://gitter.ems.host/_matrix/client/v3/sync
- log:
message: Gitter HTTP Streaming started
- loop:
expression:
simple:
expression: 'true'
do-while: true
steps:
- remove-headers:
pattern: '*'
- set-property:
name: next_batch
expression:
jsonpath:
expression: $.next_batch
- set-header:
name: Authorization
expression:
simple:
expression: Bearer {{token}}
- to-d:
uri: https://gitter.ems.host/_matrix/client/v3/sync?timeout=30000&filter={"account_data":{"not_types":["*"]},"room":{"account_data":{"not_types":["*"]},"rooms":["{{room}}"],"timeline":{"limit":1,"types":["m.room.message"]},"state":{"not_types":["*"]}}
}&since=${exchangeProperty.next_batch}&disableStreamCache=true
- convert-body-to:
type: String
- choice:
when:
- jsonpath: $['rooms']
steps:
- set-property:
name: backup-response
expression:
simple:
expression: ${body}
- set-header:
name: CamelJsltString
expression:
constant:
expression: |-
let event = [for (.rooms.join)
.value.timeline.events[0]] {
"fromUser": {
"username": replace($event[0].sender,"@|-(.*)",""),
"displayName": replace($event[0].sender,"@|-(.*)","")
},
"text": $event[0].content.body,
"sent": format-time(replace($event[0].origin_server_ts, "[\\s\\S]{3}\\z", ""), "yyyy-MM-dd'T'HH:mm:ss.SSSX"),
}
- to:
uri: jslt:dummy
parameters:
allowTemplateFromHeader: 'true'
- wire-tap:
uri: kamelet:sink
- set-body:
expression:
simple:
expression: ${exchangeProperty.backup-response}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
- from:
uri: "timer:trigger-gitter-streaming"
parameters:
repeatCount: 1
steps:
- log:
message: Opening connection to Gitter...
- setHeader:
name: Authorization
expression:
simple:
expression: "Bearer {{token}}"
- set-header:
name: test
simple:
expression: ${body}
result-type: long
- to:
uri: >-
https://gitter.ems.host/_matrix/client/v3/sync?filter={"account_data":{"not_types":["*"]},"room":{"account_data":{"not_types":["*"]},"rooms":["{{room}}"],"timeline":{"limit":1,"types":["m.room.message"]},"state":{"not_types":["*"]}
} }
- log:
message: Gitter HTTP Streaming started
- loop:
doWhile: true
expression:
simple:
expression: true
steps:
- removeHeaders:
pattern: "*"
- setProperty:
name: next_batch
expression:
jsonpath:
expression: $.next_batch
- setHeader:
name: Authorization
expression:
simple:
expression: "Bearer {{token}}"
- toD:
uri: >-
https://gitter.ems.host/_matrix/client/v3/sync?timeout=30000&filter={"account_data":{"not_types":["*"]},"room":{"account_data":{"not_types":["*"]},"rooms":["{{room}}"],"timeline":{"limit":1,"types":["m.room.message"]},"state":{"not_types":["*"]}
} }&since=${exchangeProperty.next_batch}&disableStreamCache=true
- convertBodyTo:
type: String
- when:
expression:
jsonpath:
suppressExceptions: true
expression: "$['rooms']"
steps:
- setProperty:
name: backup-response
expression:
simple:
expression: "${body}"
- setHeader:
name: CamelJsltString
expression:
constant:
expression: >-
let event = [for (.rooms.join)
.value.timeline.events[0]] {
"fromUser": {
"username": replace($event[0].sender,"@|-(.*)",""),
"displayName": replace($event[0].sender,"@|-(.*)","")
},
"text": $event[0].content.body,
"sent": format-time(replace($event[0].origin_server_ts, "[\\s\\S]{3}\\z", ""), "yyyy-MM-dd'T'HH:mm:ss.SSSX"),
}
- to: "jslt:dummy?allowTemplateFromHeader=true"
- wire-tap:
uri: "kamelet:sink"
- setBody:
expression:
simple:
expression: "${exchangeProperty.backup-response}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- from:
uri: direct:set-body
steps:
- set-body:
expression:
simple:
expression: Hello from yaml route set-body
- log:
message: Opening connection to Gitter...
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class KamelPopulator {
public static final String JQ = "jq";
public static final String CONSTANT = "constant";
public static final String NAME = "name";
public static final String EXPRESSION = "expression";
public static final String CAMEL_APACHE_ORG_KAMELET_ICON = "camel.apache.org/kamelet.icon";
private final String group = "camel.apache.org";

Expand Down Expand Up @@ -469,19 +470,21 @@ public List<FlowStep> processSteps(final Branch b) {


public static Expression getExpression(final Step step) {
Expression expression = new Expression(null, null, null, null, null);
Expression expression = new Expression();
for (Parameter p : step.getParameters()) {
if (p.getValue() == null) {
continue;
}
if (NAME.equalsIgnoreCase(p.getId())) {
expression.setName(p.getValue().toString());
expression.setName(String.valueOf(p.getValue()));
} else if (SIMPLE.equalsIgnoreCase(p.getId())) {
expression.setSimple(p.getValue().toString());
expression.setSimple(p.getValue());
} else if (JQ.equalsIgnoreCase(p.getId())) {
expression.setJq(p.getValue().toString());
expression.setJq(String.valueOf(p.getValue()));
} else if (CONSTANT.equalsIgnoreCase(p.getId())) {
expression.setConstant(p.getValue().toString());
expression.setConstant(String.valueOf(p.getValue()));
} else if (EXPRESSION.equalsIgnoreCase(p.getId())) {
expression.setExpression((Expression) p.getValue());
}
}
return expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class KameletRepresenter extends Representer {

public static final String SIMPLE = "simple";
public static final String JQ = "jq";
public static final String JSONPATH = "jsonpath";
public static final String CONSTANT = "constant";
public static final String STEPS = "steps";
public static final String PARAMETERS = "parameters";
Expand Down Expand Up @@ -382,6 +383,8 @@ private Node representConditionBlock(final Object data) {
properties.put(SIMPLE, step.getSimple());
} else if (step.getJq() != null && !step.getJq().isEmpty()) {
properties.put(JQ, step.getJq());
} else if (step.getJsonpath() != null && !step.getJsonpath().isEmpty()) {
properties.put(JSONPATH, step.getJsonpath());
}
return representMapping(getTag(data.getClass(), Tag.MAP), properties,
DumperOptions.FlowStyle.AUTO);
Expand Down
Loading

0 comments on commit 422b6f9

Please sign in to comment.