4 changed files with 385 additions and 0 deletions
@ -0,0 +1,139 @@ |
|||||
|
# Mocking Services in Camel |
||||
|
|
||||
|
## Goals |
||||
|
|
||||
|
In this example, we will mock the an API ([api.github.com](https://api.github.com/)). |
||||
|
This is very useful if the API is billed per usage or not available. |
||||
|
|
||||
|
## The mocked service |
||||
|
|
||||
|
We will mock the following API calls (you can try them in a terminal): |
||||
|
``` |
||||
|
curl -D - https://api.github.com/ |
||||
|
curl -D - https://api.github.com/users/nmasse-itix |
||||
|
curl -D - https://api.github.com/users/lbroudoux |
||||
|
curl -D - https://api.github.com/users/xxx-dummy-missing-xxx |
||||
|
curl -D - https://api.github.com/orgs/microcks |
||||
|
curl -D - https://api.github.com/orgs/xxx-dummy-missing-xxx |
||||
|
``` |
||||
|
|
||||
|
## How this project has been created |
||||
|
|
||||
|
Generate a new Maven project using the `camel-archetype-java` archetype: |
||||
|
``` |
||||
|
mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-spring -DarchetypeVersion=2.21.1 -DinteractiveMode=false -DgroupId=fr.itix.camel-examples -DartifactId=mocking -Dversion=1.0-SNAPSHOT -Dpackage=fr.itix.camel.mocking |
||||
|
``` |
||||
|
|
||||
|
As usual, enter the created directory and remove the provided examples: |
||||
|
``` |
||||
|
cd mocking |
||||
|
rm -rf src/data |
||||
|
``` |
||||
|
|
||||
|
You should have such repository structure: |
||||
|
``` |
||||
|
$ find . -type f |
||||
|
pom.xml |
||||
|
src/main/resources/META-INF/spring/camel-context.xml |
||||
|
src/main/resources/log4j2.properties |
||||
|
``` |
||||
|
|
||||
|
Since we will use the `jetty` Camel component to listen for HTTP requests, |
||||
|
according to [the documentation](http://camel.apache.org/jetty.html), you will |
||||
|
have to add the specified dependency to your `pom.xml`. In the `<dependencies>` |
||||
|
section, add: |
||||
|
```xml |
||||
|
<dependency> |
||||
|
<groupId>org.apache.camel</groupId> |
||||
|
<artifactId>camel-jetty</artifactId> |
||||
|
</dependency> |
||||
|
``` |
||||
|
|
||||
|
Replace the content of `camel-context.xml` with [this file](src/main/resources/META-INF/spring/camel-context.xml). |
||||
|
|
||||
|
## How to test it |
||||
|
|
||||
|
Each time you changed something in your `camel-context.xml`, you will have |
||||
|
to re-run Camel with: |
||||
|
``` |
||||
|
mvn install camel:run |
||||
|
``` |
||||
|
|
||||
|
If you followed the instructions, you should have something like this: |
||||
|
``` |
||||
|
[INFO] --- camel-maven-plugin:2.21.1:run (default-cli) @ mocking --- |
||||
|
[INFO] You can skip tests from the command line using: mvn camel:run -Dmaven.test.skip=true |
||||
|
[INFO] Using org.apache.camel.spring.Main to initiate a CamelContext |
||||
|
[INFO] Starting Camel ... |
||||
|
Jun 05, 2018 11:08:24 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh |
||||
|
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2311c071: startup date [Tue Jun 05 11:08:24 CEST 2018]; root of context hierarchy |
||||
|
Jun 05, 2018 11:08:24 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions |
||||
|
INFO: Loading XML bean definitions from file [/Users/nmasse/git/perso/Camel-Examples/mocking/target/classes/META-INF/spring/camel-context.xml] |
||||
|
[pache.camel.spring.Main.main()] SpringCamelContext INFO Apache Camel 2.21.1 (CamelContext: camel-1) is starting |
||||
|
[pache.camel.spring.Main.main()] ManagedManagementStrategy INFO JMX is enabled |
||||
|
[pache.camel.spring.Main.main()] DefaultTypeConverter INFO Type converters loaded (core: 194, classpath: 8) |
||||
|
[pache.camel.spring.Main.main()] SpringCamelContext INFO StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html |
||||
|
[pache.camel.spring.Main.main()] log INFO Logging initialized @4192ms to org.eclipse.jetty.util.log.Slf4jLog |
||||
|
[pache.camel.spring.Main.main()] Server INFO jetty-9.4.6.v20170531 |
||||
|
[pache.camel.spring.Main.main()] ContextHandler INFO Started o.e.j.s.ServletContextHandler@24af2e09{/,null,AVAILABLE} |
||||
|
[pache.camel.spring.Main.main()] AbstractConnector INFO Started ServerConnector@3ac5035f{HTTP/1.1,[http/1.1]}{0.0.0.0:8080} |
||||
|
[pache.camel.spring.Main.main()] Server INFO Started @4290ms |
||||
|
[pache.camel.spring.Main.main()] SpringCamelContext INFO Route: route1 started and consuming from: jetty:http://0.0.0.0:8080?matchOnUriPrefix=true |
||||
|
[pache.camel.spring.Main.main()] SpringCamelContext INFO Route: route2 started and consuming from: direct://get_orgs_microcks |
||||
|
[pache.camel.spring.Main.main()] SpringCamelContext INFO Route: route3 started and consuming from: direct://get_users_nmasse_itix |
||||
|
[pache.camel.spring.Main.main()] SpringCamelContext INFO Route: route4 started and consuming from: direct://get_users_lbroudoux |
||||
|
[pache.camel.spring.Main.main()] SpringCamelContext INFO Route: route5 started and consuming from: direct://not_found |
||||
|
[pache.camel.spring.Main.main()] SpringCamelContext INFO Route: route6 started and consuming from: direct://get_metadata |
||||
|
[pache.camel.spring.Main.main()] SpringCamelContext INFO Total 6 routes, of which 6 are started |
||||
|
[pache.camel.spring.Main.main()] SpringCamelContext INFO Apache Camel 2.21.1 (CamelContext: camel-1) started in 0.397 seconds |
||||
|
Jun 05, 2018 11:08:25 AM org.springframework.context.support.DefaultLifecycleProcessor start |
||||
|
INFO: Starting beans in phase 2147483646 |
||||
|
``` |
||||
|
|
||||
|
From another terminal you can verify that the mock is working and matches the mocked API: |
||||
|
``` |
||||
|
$ curl -D - http://localhost:8080/ |
||||
|
HTTP/1.1 200 OK |
||||
|
Content-Type: application/json |
||||
|
Accept: */* |
||||
|
breadcrumbId: ID-nmasse-OSX-local-1528189705427-0-9 |
||||
|
User-Agent: curl/7.54.0 |
||||
|
Transfer-Encoding: chunked |
||||
|
Server: Jetty(9.4.6.v20170531) |
||||
|
|
||||
|
{ |
||||
|
"organization_url": "https://api.github.com/orgs/{org}", |
||||
|
"user_url": "https://api.github.com/users/{user}" |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
You can try with the other URLs: |
||||
|
``` |
||||
|
curl -D - http://localhost:8080/users/nmasse-itix |
||||
|
curl -D - http://localhost:8080/users/lbroudoux |
||||
|
curl -D - http://localhost:8080/users/xxx-dummy-missing-xxx |
||||
|
curl -D - http://localhost:8080/orgs/microcks |
||||
|
curl -D - http://localhost:8080/orgs/xxx-dummy-missing-xxx |
||||
|
``` |
||||
|
|
||||
|
On the other console, you should see the requests coming in the log: |
||||
|
``` |
||||
|
[ qtp183376222-18] route1 INFO GET / |
||||
|
[ qtp183376222-19] route1 INFO GET /users/nmasse-itix |
||||
|
[ qtp183376222-20] route1 INFO GET /users/lbroudoux |
||||
|
[ qtp183376222-21] route1 INFO GET /users/xxx-dummy-missing-xxx |
||||
|
[ qtp183376222-22] route1 INFO GET /orgs/microcks |
||||
|
[ qtp183376222-23] route1 INFO GET /orgs/xxx-dummy-missing-xxx |
||||
|
``` |
||||
|
|
||||
|
Note: you will have to press Ctrl-C to exit this example! |
||||
|
|
||||
|
## References |
||||
|
|
||||
|
- [Jetty](http://camel.apache.org/jetty.html) |
||||
|
- [HTTP Component](http://camel.apache.org/http.html) (message header names) |
||||
|
- [Header Names for Spring DSL](https://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html#HTTP_PATH) |
||||
|
- [Match on URI prefix](http://camel.apache.org/how-do-i-let-jetty-match-wildcards.html) |
||||
|
- [Simple Expressions Syntax](http://camel.apache.org/simple.html) |
||||
|
- [Constant Syntax](http://camel.apache.org/constant.html) |
||||
|
- [The Log EIP](http://camel.apache.org/logeip.html) |
||||
@ -0,0 +1,102 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<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/maven-v4_0_0.xsd"> |
||||
|
|
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<groupId>fr.itix.camel-examples</groupId> |
||||
|
<artifactId>mocking</artifactId> |
||||
|
<packaging>jar</packaging> |
||||
|
<version>1.0-SNAPSHOT</version> |
||||
|
|
||||
|
<name>A Camel Spring Route</name> |
||||
|
|
||||
|
<properties> |
||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> |
||||
|
</properties> |
||||
|
|
||||
|
<dependencyManagement> |
||||
|
<dependencies> |
||||
|
<!-- Camel BOM --> |
||||
|
<dependency> |
||||
|
<groupId>org.apache.camel</groupId> |
||||
|
<artifactId>camel-parent</artifactId> |
||||
|
<version>2.21.1</version> |
||||
|
<scope>import</scope> |
||||
|
<type>pom</type> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
</dependencyManagement> |
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.apache.camel</groupId> |
||||
|
<artifactId>camel-core</artifactId> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.apache.camel</groupId> |
||||
|
<artifactId>camel-spring</artifactId> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.apache.camel</groupId> |
||||
|
<artifactId>camel-jetty</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<!-- logging --> |
||||
|
<dependency> |
||||
|
<groupId>org.apache.logging.log4j</groupId> |
||||
|
<artifactId>log4j-api</artifactId> |
||||
|
<scope>runtime</scope> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.apache.logging.log4j</groupId> |
||||
|
<artifactId>log4j-core</artifactId> |
||||
|
<scope>runtime</scope> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.apache.logging.log4j</groupId> |
||||
|
<artifactId>log4j-slf4j-impl</artifactId> |
||||
|
<scope>runtime</scope> |
||||
|
</dependency> |
||||
|
|
||||
|
<!-- testing --> |
||||
|
<dependency> |
||||
|
<groupId>org.apache.camel</groupId> |
||||
|
<artifactId>camel-test-spring</artifactId> |
||||
|
<scope>test</scope> |
||||
|
</dependency> |
||||
|
|
||||
|
</dependencies> |
||||
|
|
||||
|
<build> |
||||
|
<defaultGoal>install</defaultGoal> |
||||
|
|
||||
|
<plugins> |
||||
|
<plugin> |
||||
|
<groupId>org.apache.maven.plugins</groupId> |
||||
|
<artifactId>maven-compiler-plugin</artifactId> |
||||
|
<version>3.7.0</version> |
||||
|
<configuration> |
||||
|
<source>1.8</source> |
||||
|
<target>1.8</target> |
||||
|
</configuration> |
||||
|
</plugin> |
||||
|
<plugin> |
||||
|
<groupId>org.apache.maven.plugins</groupId> |
||||
|
<artifactId>maven-resources-plugin</artifactId> |
||||
|
<version>3.0.2</version> |
||||
|
<configuration> |
||||
|
<encoding>UTF-8</encoding> |
||||
|
</configuration> |
||||
|
</plugin> |
||||
|
|
||||
|
<!-- allows the route to be ran via 'mvn camel:run' --> |
||||
|
<plugin> |
||||
|
<groupId>org.apache.camel</groupId> |
||||
|
<artifactId>camel-maven-plugin</artifactId> |
||||
|
<version>2.21.1</version> |
||||
|
</plugin> |
||||
|
</plugins> |
||||
|
</build> |
||||
|
|
||||
|
</project> |
||||
@ -0,0 +1,137 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<beans xmlns="http://www.springframework.org/schema/beans" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation=" |
||||
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
||||
|
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> |
||||
|
|
||||
|
<camelContext xmlns="http://camel.apache.org/schema/spring"> |
||||
|
|
||||
|
<!-- Our main Camel route that listen to HTTP requests and dispatch them |
||||
|
-- to other routes based on the method and path. --> |
||||
|
<route> |
||||
|
<!-- Listen to HTTP requests on port 8080, and accept requests on all paths |
||||
|
-- (this is the matchOnUriPrefix=true parameter). --> |
||||
|
<from uri="jetty:http://0.0.0.0:8080?matchOnUriPrefix=true"/> |
||||
|
|
||||
|
<!-- Generate an access log (method + path) for debugging purposes. --> |
||||
|
<log message="${in.header.CamelHttpMethod} /${in.header.CamelHttpPath}" /> |
||||
|
|
||||
|
<!-- Since all responses will be JSON, we can already set the Content-Type. --> |
||||
|
<setHeader headerName="Content-Type"> |
||||
|
<constant>application/json</constant> |
||||
|
</setHeader> |
||||
|
|
||||
|
<!-- Dispatch the requests to other camel routes. --> |
||||
|
<choice> |
||||
|
<when> |
||||
|
<simple>${in.header.CamelHttpMethod} == 'GET'</simple> |
||||
|
|
||||
|
<!-- Dispatch all GET requests, based on the path. --> |
||||
|
<choice> |
||||
|
<!-- Note: the path does NOT include the first '/' ! --> |
||||
|
<when> |
||||
|
<!-- GET / --> |
||||
|
<simple>${in.header.CamelHttpPath} == ''</simple> |
||||
|
<to uri="direct:get_metadata"/> |
||||
|
</when> |
||||
|
<when> |
||||
|
<!-- GET /users/nmasse-itix --> |
||||
|
<simple>${in.header.CamelHttpPath} == 'users/nmasse-itix'</simple> |
||||
|
<to uri="direct:get_users_nmasse_itix"/> |
||||
|
</when> |
||||
|
<when> |
||||
|
<!-- GET /users/lbroudoux --> |
||||
|
<simple>${in.header.CamelHttpPath} == 'users/lbroudoux'</simple> |
||||
|
<to uri="direct:get_users_lbroudoux"/> |
||||
|
</when> |
||||
|
<when> |
||||
|
<!-- GET /orgs/microcks --> |
||||
|
<simple>${in.header.CamelHttpPath} == 'orgs/microcks'</simple> |
||||
|
<to uri="direct:get_orgs_microcks"/> |
||||
|
</when> |
||||
|
|
||||
|
<!-- If path does not match, return a 404 error. --> |
||||
|
<otherwise> |
||||
|
<to uri="direct:not_found"/> |
||||
|
</otherwise> |
||||
|
</choice> |
||||
|
</when> |
||||
|
|
||||
|
<!-- If method does not match, return a 404 error. --> |
||||
|
<otherwise> |
||||
|
<to uri="direct:not_found"/> |
||||
|
</otherwise> |
||||
|
</choice> |
||||
|
</route> |
||||
|
|
||||
|
<!-- GET /orgs/microcks --> |
||||
|
<route> |
||||
|
<from uri="direct:get_orgs_microcks"/> |
||||
|
<setBody> |
||||
|
<constant><![CDATA[{ |
||||
|
"login": "microcks", |
||||
|
"id": 11051048, |
||||
|
"description": "", |
||||
|
"name": "Microcks", |
||||
|
"blog": "http://microcks.github.io", |
||||
|
"type": "Organization" |
||||
|
} |
||||
|
]]></constant> |
||||
|
</setBody> |
||||
|
</route> |
||||
|
|
||||
|
<!-- GET /users/nmasse-itix --> |
||||
|
<route> |
||||
|
<from uri="direct:get_users_nmasse_itix"/> |
||||
|
<setBody> |
||||
|
<constant><![CDATA[{ |
||||
|
"login": "nmasse-itix", |
||||
|
"id": 7472564, |
||||
|
"type": "User", |
||||
|
"site_admin": false, |
||||
|
"name": "Nicolas MASSE" |
||||
|
}]]></constant> |
||||
|
</setBody> |
||||
|
</route> |
||||
|
|
||||
|
<!-- GET /users/lbroudoux --> |
||||
|
<route> |
||||
|
<from uri="direct:get_users_lbroudoux"/> |
||||
|
<setBody> |
||||
|
<constant><![CDATA[{ |
||||
|
"login": "lbroudoux", |
||||
|
"id": 1538635, |
||||
|
"type": "User", |
||||
|
"site_admin": false, |
||||
|
"name": "Laurent Broudoux" |
||||
|
}]]></constant> |
||||
|
</setBody> |
||||
|
</route> |
||||
|
|
||||
|
<!-- GET / --> |
||||
|
<route> |
||||
|
<from uri="direct:get_metadata"/> |
||||
|
<setBody> |
||||
|
<constant><![CDATA[{ |
||||
|
"organization_url": "https://api.github.com/orgs/{org}", |
||||
|
"user_url": "https://api.github.com/users/{user}" |
||||
|
}]]></constant> |
||||
|
</setBody> |
||||
|
</route> |
||||
|
|
||||
|
<!-- 404 Not Found --> |
||||
|
<route> |
||||
|
<from uri="direct:not_found"/> |
||||
|
<setHeader headerName="CamelHttpResponseCode"> |
||||
|
<constant>404</constant> |
||||
|
</setHeader> |
||||
|
<setBody> |
||||
|
<constant><![CDATA[{ |
||||
|
"message": "Not Found", |
||||
|
"documentation_url": "https://developer.github.com/v3" |
||||
|
}]]></constant> |
||||
|
</setBody> |
||||
|
</route> |
||||
|
</camelContext> |
||||
|
</beans> |
||||
@ -0,0 +1,7 @@ |
|||||
|
|
||||
|
appender.out.type = Console |
||||
|
appender.out.name = out |
||||
|
appender.out.layout.type = PatternLayout |
||||
|
appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n |
||||
|
rootLogger.level = INFO |
||||
|
rootLogger.appenderRef.out.ref = out |
||||
Loading…
Reference in new issue