Skip to content

Commit

Permalink
Added tests and docs for the simple language apache#2533
Browse files Browse the repository at this point in the history
  • Loading branch information
aldettinger committed Apr 29, 2021
1 parent cc32c2f commit 2c93ab6
Show file tree
Hide file tree
Showing 11 changed files with 468 additions and 0 deletions.
44 changes: 44 additions & 0 deletions docs/modules/ROOT/pages/reference/extensions/core.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,50 @@ Check the xref:user-guide/index.adoc[User guide] for more information about writ

== Additional Camel Quarkus configuration

=== Simple language

==== Using the OGNL notation
When using the OGNL notation from the simple language, the `camel-quarkus-bean` extension should be used.

For instance, the expression below is accessing the `getAddress()` method on the message body of type `Client`.
[source,java]
---
simple("${body.address}")
---

In such a situation, one should take an additional dependency on the camel-quarkus-bean extension xref:{cq-camel-components}::bean-component.adoc[as described here].
Note that in native mode, some classes may need to be registered for reflection. In the example above, the `Client` class
needs to be link:https://quarkus.io/guides/writing-native-applications-tips#registering-for-reflection[registered for reflection].

==== Using dynamic type resolution in native mode
When dynamically resolving a type from simple expressions like `${mandatoryBodyAs(TYPE)}` or `${body} is TYPE`, it may be needed to register some classes for reflection manually.

For instance, the simple expressions below is dynamically resolving the type `java.nio.ByteBuffer` at runtime:
[source,java]
---
simple("${body} is 'java.nio.ByteBuffer'")
---

As such, the class `java.nio.ByteBuffer` needs to be link:https://quarkus.io/guides/writing-native-applications-tips#registering-for-reflection[registered for reflection].

==== Using the simple language with classpath resources in native mode
Beyond standard usages, a trick is needed when using the simple language with classpath resources in native mode. In such a situation, one needs to explicitly embed the resources in the native executable by specifying the `include-patterns` option.

For instance, the route below would load a simple script from a classpath resource named _mysimple.txt_:
[source,java]
----
from("direct:start").transform().simple("resource:classpath:mysimple.txt");
----

In order to work in native mode the `include-patterns` configuration should be set. For instance, in the `application.properties` file as below :
[source,properties]
----
quarkus.camel.native.resources.include-patterns = *.txt
----

More information about selecting resources for inclusion in the native executable could be found at xref:user-guide/native-mode.adoc#embedding-resource-in-native-executable[Embedding resource in native executable].


[width="100%",cols="80,5,15",options="header"]
|===
| Configuration property | Type | Default
Expand Down
42 changes: 42 additions & 0 deletions extensions-core/core/runtime/src/main/doc/configuration.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=== Simple language

==== Using the OGNL notation
When using the OGNL notation from the simple language, the `camel-quarkus-bean` extension should be used.

For instance, the expression below is accessing the `getAddress()` method on the message body of type `Client`.
[source,java]
---
simple("${body.address}")
---

In such a situation, one should take an additional dependency on the camel-quarkus-bean extension xref:{cq-camel-components}::bean-component.adoc[as described here].
Note that in native mode, some classes may need to be registered for reflection. In the example above, the `Client` class
needs to be link:https://quarkus.io/guides/writing-native-applications-tips#registering-for-reflection[registered for reflection].

==== Using dynamic type resolution in native mode
When dynamically resolving a type from simple expressions like `${mandatoryBodyAs(TYPE)}` or `${body} is TYPE`, it may be needed to register some classes for reflection manually.

For instance, the simple expressions below is dynamically resolving the type `java.nio.ByteBuffer` at runtime:
[source,java]
---
simple("${body} is 'java.nio.ByteBuffer'")
---

As such, the class `java.nio.ByteBuffer` needs to be link:https://quarkus.io/guides/writing-native-applications-tips#registering-for-reflection[registered for reflection].

==== Using the simple language with classpath resources in native mode
Beyond standard usages, a trick is needed when using the simple language with classpath resources in native mode. In such a situation, one needs to explicitly embed the resources in the native executable by specifying the `include-patterns` option.

For instance, the route below would load a simple script from a classpath resource named _mysimple.txt_:
[source,java]
----
from("direct:start").transform().simple("resource:classpath:mysimple.txt");
----

In order to work in native mode the `include-patterns` configuration should be set. For instance, in the `application.properties` file as below :
[source,properties]
----
quarkus.camel.native.resources.include-patterns = *.txt
----

More information about selecting resources for inclusion in the native executable could be found at xref:user-guide/native-mode.adoc#embedding-resource-in-native-executable[Embedding resource in native executable].
117 changes: 117 additions & 0 deletions integration-tests/simple/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-tests</artifactId>
<version>1.9.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>camel-quarkus-integration-test-simple</artifactId>
<name>Camel Quarkus :: Integration Tests :: Simple :: Tests</name>
<description>The camel quarkus integration tests for the simple language</description>

<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-direct</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-bean</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>

<!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-bean-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-direct-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.language.simple;

import java.nio.ByteBuffer;

import io.quarkus.runtime.annotations.RegisterForReflection;

/**
* Manually register some third party classes for reflection.
*/
public class ReflectionConfigurations {

// Needed as "${mandatoryBodyAs(String).OGNL}" reflectively invokes method 'String.toUpperCase()'
@RegisterForReflection(targets = String.class, fields = false)
public class StringReflectionConfiguration {
}

// Needed as "${body} is TYPE" reflectively resolves 'java.nio.ByteBuffer'
// at runtime
@RegisterForReflection(targets = ByteBuffer.class, fields = false, methods = false)
public class ByteBufferReflectionConfiguration {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.language.simple;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.camel.ProducerTemplate;

@Path("/simple")
@ApplicationScoped
public class SimpleResource {

@Inject
ProducerTemplate template;

@Path("/filter")
@GET
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String filter(boolean premium) {
return template.requestBodyAndHeader("direct:filter-simple", "NOT-PREMIUM", "premium", premium, String.class);
}

@Path("/transform")
@GET
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String transform(String user) {
return template.requestBodyAndHeader("direct:transform-simple", null, "user", user, String.class);
}

@Path("/resource")
@GET
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String resource(String body) {
return template.requestBody("direct:resource-simple", body, String.class);
}

@Path("/mandatoryBodyAs")
@GET
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String mandatoryBodyAs(byte[] body) {
return template.requestBody("direct:mandatoryBodyAs-simple", body, String.class);
}

@Path("/bodyIs")
@GET
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String mandatoryBodyAs(String body) {
if ("A body of type String".equals(body)) {
return template.requestBody("direct:bodyIs-simple", "STRING", String.class);
} else {
return template.requestBody("direct:bodyIs-simple", ByteBuffer.wrap(body.getBytes(StandardCharsets.UTF_8)),
String.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.language.simple;

import org.apache.camel.builder.RouteBuilder;

public class SimpleRoutes extends RouteBuilder {

@Override
public void configure() {
from("direct:filter-simple").filter().simple("${in.header.premium} == true").setBody(constant("PREMIUM"));

from("direct:transform-simple").transform().simple("Hello ${in.header.user} !");

from("direct:resource-simple").transform().simple("resource:classpath:mysimple.txt");

from("direct:mandatoryBodyAs-simple").filter().simple("${mandatoryBodyAs(String).toUpperCase()} == 'GOLD'")
.setBody(constant("PREMIUM"));

from("direct:bodyIs-simple").filter().simple("${body} is 'java.nio.ByteBuffer'").setBody(constant("BYTE_BUFFER"));
}

}
17 changes: 17 additions & 0 deletions integration-tests/simple/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements. See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License. You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------
quarkus.camel.native.resources.include-patterns = *.txt
1 change: 1 addition & 0 deletions integration-tests/simple/src/main/resources/mysimple.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The name is ${body}

0 comments on commit 2c93ab6

Please sign in to comment.