Red Hat
Jul 5, 2018
by Christina Lin
This is part one of my two-article series that demonstrates the approach of implementing contract-first API design using Apicurioand Red Hat Fuse.

It covers how to create an OpenAPI standard document as the contract between API providers and consumers using Apicurio. It also shows how to quickly create mock tests using Red Hat Fuse.

There are two common approaches of creating these APIs.
  • Code First
  • Contract First
Coming from a old time ESB developer, these are not new. We have been doing this forever. Before, it was the WSDL that define the contract of the service. we were doing a lot more code first, for me it's simply because it's much easier for me to write couple of Java classes and generate the WSDL for my consumer. 


It's often pretty straightforward if the consumer of your application has finalized how they want the service to be like. But you and I all know this is not often the case. So I had to go back to my code, and make the changes accordingly and pray I did not break anything in the way.

Of course there is always preliminary discussion, but small changes are inevitable, and it's taking the toll of developer to grind through the process of getting everything right.

Thanks to the new OpenAPI Specification, business user, citizen user/developer can use it to negotiate and maybe rung a couple of pre-test with the consumer before it gets handed over to developer. This design approach is called Contract First. It has become more and more popular as it avoids developer wasting time while in the process of negotiating how the service should be provided.

Obviously there are many ways to do Contract First API . I am going to demonstrate how this can be done using Apicurio and Fuse. I will be using Apicurio to define APIs, and automatically generates the Fuse project for quick testing purpose.

Theme: Customer Service API


We will be providing customer service to our consumer. For the sake of this demo, we will only start with retrieving and creating customer service.

Defining Application with OpenAPI Specification in Apicurio

Apicurio is a web-based Open Source tool to design OpenAPI specification. Go to https://www.apicur.io/

If you don't already have an account, please register for one.


After registering, you will be redirected to the main page of Apicurio, so after discussing with the consumer on what they wish to have for the customer service, we can start creating the contract by clicking on "Create New API".



Basically, what you need to do is pretty simple

  • Create the API(Service)
  • Create the data definitions (if any) 
  • Add Paths, and define parameters, operations and return responses to the path

Enter the name of the service, and would be nice to add some of the description to it so it's easier for people to pick up what everything means.


Set the customer definition, show the detail of info we are going exchange. 



Add and define the properties and their data type.


We can start adding the Paths to the doc, 

Adding Parameters and define it's type.






And responses too, (if your path needs it)


Once we are done, it's time to export the API Standard documents.



Generating Fuse Project from Standard API Doc


Go to JBoss Developer Studio, create a new Fuse project by right click on navigation panel, select New and Fuse Integration project.



Provide a name to the project,

We are going to do this in a microservices approach, so select the most popular runtime - Spring Boot, and we will be running this on Cloud Platform - OpenShift.


Select the Spring DSL template.


You will have a sample Fuse project,




Add the generated API Spec doc to directory: src/spec/



Go to the pom.xml file, place the following in it,

<plugins>
....
<plugin>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-restdsl-swagger-plugin</artifactId>
  <version>2.21.0</version>
  <configuration>
    <specificationUri>src/spec/MyCustomer.json</specificationUri>
    <fileName>camel-rest.xml</fileName>
    <outputDirectory>src/main/resources/spring</outputDirectory>    
  </configuration>

</plugin>
....
</plugin>

Generate the XML by running following in command line tool,

  mvn camel-restdsl-swagger:generate-xml



You will find a newly generated camel context named "camel-rest.xml", and it has all the Path implementation in Camel.



Copy everything in between "rests", into the original "camel-context.xml" and place in-between "camelContext". and add the following rest configuration on top of the rest component.

<restConfiguration apiContextPath="api-docs" bindingMode="auto"
            component="undertow" contextPath="/customer"
            enableCORS="true" port="8080">
   <apiProperty key="cors" value="true"/>
   <apiProperty key="api.title" value="Customer Service"/>
   <apiProperty key="api.version" value="1.0.0"/>

</restConfiguration>




Delete the generated "camel-rest.xml". 

Mocking the APIs with Camel

We will mock the return result by adding a constant defined bean in the camel context.
Add a beans.xml under src/main.resource/spring (Right click on the folder, select New->beans.xml File)




Add the follow code snap to the beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
    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

    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<util:list id="CustomerList" list-class="java.util.ArrayList">
   <ref bean="Customer"/>
</util:list>
<util:map id="Customer" map-class="java.util.HashMap">
   <entry key="name" value="Christina"/>
   <entry key="age" value="28"/>
   <entry key="contact" value="765483921"/>

</util:map>
</beans>

Add the camel route in the to "camel-context.xml", the first one returns a mock customer data, the second one takes the input of a customer info.

<route id="rest1-route">
  <from id="restone" uri="direct:rest1"/>
  <setBody id="route-setBody1">
    <simple>bean:CustomerList?method=get(0)</simple>
  </setBody>
</route>
<route id="rest2-route">
  <from id="resttwo" uri="direct:rest2"/>
  <log id="input-log" message=">>> ${body}"/>
    <setBody id="route-setBody2">
      <simple>Customer created</simple>
    </setBody>

</route>

And in the src/main/java/org/mycompany/Application.java added the new beans.xml file.

@ImportResource({"classpath:spring/camel-context.xml",
"classpath:spring/beans.xml"})

public class Application {

Now, it's time to setup the dependency libraries into pom.xml.


<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
    
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-undertow-starter</artifactId>
</dependency> 
    
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-jackson-starter</artifactId>
</dependency> 
    
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-swagger-java-starter</artifactId>

</dependency>

and remove the tomcat dependency (we are using undertow now)

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Finally, it's time to test. run the follow in command line:

  mvn sprint-boot:run

And two endpoint will be exposed for testing.

Christina Laptop$ curl http://YOURIP:8080/customer/id/123
{"name":"Christina","age":"28","contact":"765483921"}

Christina Laptop$ curl --header "Content-Type: application/json"   --request PUT   --data '{"name":"Christina","age":28,"contact":"765483921"}'   http://YOURIP:8080/customer/add

"Customer created"

You are now ready for the consumer to start testing the APIs.
Next part I will take you through how to actually implement the API, expose it on Cloud and manage it.

Original Post