Author: Arpit Singh

Problem Faced

Last week, I received a minor Change Requirement from the Client to add a field in the API Specs (RAML).

The requirement was critical and so I quickly made the raml change in code, pushed it to Github Repo, and got it merged to Dev. 

What I missed was: updating the same change in the corresponding API in exchange.

Though I pushed those changes to exchange the next day, I got another Change Request and while publishing the raml change to Exchange, I remembered what I missed.

By the way, we do have a checklist to be followed for every new CR that is raised, but the change was so minor and critical, that I neglected the Checklist!

What is Raml Sync Verifier Plugin?

The custom plugin:

  • Checks if the Raml in API implementation project is in Sync with the Raml in Exchange
  • Gives you options to pass various configurations
  • Fails the build if the ramls are not in sync with each other (This configuration can be turned off)

Why is it required?

The RAML published to Exchange and the Raml in your API implementation should be in SYNC for a particular environment. 

There are plenty of good reasons for that: 

  • Maintaining consistency of code
  • Version control
  • Reusing Fragments and canonicals 

Note: Adding the api id (From API Manager) and Autodiscovery will not make the raml code in sync with exchange.  

I hate making the same mistake twice (At least when it comes to Tech Stuff!), so I created a very simple custom maven plugin which during the build process checks if the ramls are in sync or not.

Source Code Link

I will be describing briefly about the Plugin code and the complete code with example Mule app can be found at GitHub Repo.

Pre-Requisites:

  • Knowledge about developing Maven Custom Plugin
  • Knowledge about Mule 4 apps and Anypoint Platform
  • Anypoint Studio Installed 
  • Maven installed 
  • Eclipse installed
  • Valid AnyPoint Credentials
  • Have developed a simple Mule app with a basic API spec in design center using above credentials

About the Code

Project Structure

The Plugin has just one Mojo. A Java mojo consists simply of a single class representing one plugins goal.

Exchange API Used 

About VerifyRamlMojo

  • Goal Name: verify-raml
  • Default Phase: TEST
  • Parameters Matrix

Behind the Scene Logic

As mentioned in the Properties Matrix, you are required to pass the appropriate values of the parameters. The GitHub repo also has a sample Mule app using the plugin, so you can see the usage of the plugin there as well.

Flow Diagram

The flow diagram can also be found at draw.io.

Steps

  • Using Cloudhub GraphQL API: https://anypoint.mulesoft.com/graph/api/v1/graphql, get the URL to download the RAML from Exchange
  • Sample Response
  • Download and unzip the file in a temp directory
  • Compare the unzipped folder with the folder present in the project (default src/main/resources/api/)
  • Using Files.walkFileTree(one, new SimpleFileVisitor<Path>()
  • Comparison is done byte by byte in size, but can be expanded as per your requirements
  • If difference in size is detected, then detailed logs are displayed in the build console
getLog().error("=== comparing: " + file + " to " + fileInOther + " === Difference in Bytes : "   +(otherBytes.length - theseBytes.length));
getLog().error(file + " is not equal to " + fileInOther);

Note: You might face an issue while importing the GitHub code in Eclipse. Please mark the goal descriptor as ignored.


Testing the plugin

Installing the custom maven plugin in your local system

1.   Clone the custom plugin code from GitHub Repo

2.   Open command prompt and browse to the cloned project Root Dir

3.   Run mvn clean install

Testing the maven plugin in a sample Mule App

1.   Clone the samle mule app from GitHub repo

2.   Open command prompt and browse to the cloned project Root Dir

3.   Run mvn clean test.

The plugin will be executed during the test phase based on the defaultPhase Configuration in the Mojo.

@Mojo(name = "verify-raml", defaultPhase = LifecyclePhase.TEST)
Notes on the sample Mule App
  • You should include the plugin with its required configurations in order to use the plugin. 
  • You can check out the usage in the pom.xml of the sample app.
  • I have excluded the credentials from my pom.xml and I would recommend creating a simple API in the design center using your anypoint credentials and test out the plugin.

Test Runs

First Run

Raml in exchange is in sync with the raml in sample-mule-app

mvn clean test 

The plugin will be executed during the test phase based on the defaultPhase Configuration in the Mojo.

@Mojo(name = "verify-raml", defaultPhase = LifecyclePhase.TEST)

Second Run

Raml in exchange is not synced with the raml in sample-mule-app. 

I manually added another endpoint in raml of sample-mule-app.

The output on running mvn clean test.

Leave a Comment