Automate API stubbing using WireMock on Oracle Cloud

Abhinav Shroff
Oracle Developers
Published in
7 min readJan 4, 2018

This blog will help you understand how Oracle Developer Cloud Service can be used to automate stubbing an API for testing. It will demonstrate how WireMock installation and API stub configuration can be automated using Oracle Developer Cloud Service and Application Container Cloud Service.

What is WireMock?

WireMock is a simulator for HTTP-based APIs. Some might consider it a service virtualization tool or a mock server. It enables us to continue development and testing when an API we depend on doesn’t exist or isn’t complete. It supports testing of edge cases and failure modes that the real API won’t reliably produce. And because it’s fast it can reduce our build time from hours down to minutes. Below is the url, where you can find more information on WireMock.

http://wiremock.org

Why Automate API simulation?

With Microservice architecture becoming popular to achieve shorter development lifecycle for a component, automation in DevOps becomes crucial. Part of the automation which has a huge impact on both the quality and the time lines of delivery is Test Automation. In case of dependency on services which are yet to be developed or have access to, the Test Automation cannot be achieved unless someone codes a mock service up. Here WireMock can be of great help to us. It has a standalone mock API server, where you can setup your mock API without writing a single line of code and with Oracle Developer Cloud, you can make this happen in an automated fashion, as part of your CI & CD pipeline.

Technology Stack Used:

1. Oracle Developer Cloud Service: Cloud DevOps platform

2. Oracle Application Container Cloud Service: Cloud Deployment

3. Grunt: Build Tool

4. WireMock: API Simulator

Note: We will be using the Git cli to push the code to the hosted Git repository on Oracle Developer Cloud Service.

Project structure for WireMock setup:

You will need to download the WireMock standalone jar from this link. Rest of the files will have to be created. The purpose of the files and the code/script required for each of these is discussed in detail, below in the blog.

To push the code to the Git repository hosted on Oracle developer Cloud Service, make sure you have Git CLI installed on your development machine. Open the command prompt, navigate to the Wiremock artifacts folder and then start by initializing Git repository. This can be done by using the command:

git init

as show in the screenshot below. Post that add the files to the git repository by using the command:

git add <file name>

Refer the screen shot below. Make sure you add all the files to the Git repository. Then commit the added files all at once by using the command:

Git commit –m “First Commit”

Now get the Git repository URL from the Project tab of Developer Cloud Service as show below in the screenshot:

Now, add the remote repository using the command:

Git remote add origin <DevCS Git repository URL>

And then use:

‘git push origin master’ command to push the code to the master branch of the DevCS hosted Git repository, as shown in the screenshot below.

Description of the files in the Project:

wiremock-standalone-2.12.0.jar

This is a jar file, which is to be deployed on Oracle Application Container Cloud and run as standalone WireMock server. The WiremMock standalone jar file can be downloaded from this link.

manifest.json

This is the most crucial file for us to be able to deploy WireMock standalone jar on Oracle Application Container. It contains the Java version(configured as 8) to be used and the command to execute the Jar file. Below is the snippet for reference:

{"runtime": {"majorVersion": "8"},"command": "java -jar wiremock-standalone-2.12.0.jar","notes": "Mock Service"}

package.json

Since Grunt is a Nodejs based build tool and it requires some dependencies to execute the build. Hence we need a package.json to configure the Nodejs module dependencies required for the Grunt to execute. Below is the snippet for reference:

{"name": "WireMock","version": "0.0.1","private": true,"scripts": {"start": "java -jar wiremock-standalone-2.12.0.jar"},"dependencies": {"grunt": "^0.4.5","grunt-contrib-compress": "^1.3.0","grunt-hook": "^0.3.1","load-grunt-tasks": "^3.5.2"}}

Gruntfile.js

In this JavaScript file we configure the Grunt task, which will be executed by the Grunt build tool on the build server. Grunt would help us in building a zip file with the WireMock jar and the manifest.json to deploy on Oracle Application Container Cloud. This file also contains the declaration of the source to be zipped by the Grunt task. Below is the snippet for reference:

module.exports = function(grunt) {require('load-grunt-tasks')(grunt);grunt.initConfig({compress: {main: {options: {archive: 'wiremock.zip',pretty: true},expand: true,cwd: './',src: ['wiremock-standalone-2.12.0.jar','./manifest.json'],dest: './'}}});grunt.registerTask('default', ['compress']);};

Developer Cloud Build Job Configuration to build and deploy WireMock jar

Below are the screenshots for the build job configuration for the WiremockInstall build job, which will perform Grunt build to build a zip file and deploy it on Oracle Application Container Cloud Service. Also create another build job WiremockConfigure as a place holder for

As this job needs to be present for the Post build configurations od WiremockInstall build job.

Give a name of your choice to the build job. For the purpose of this blog I have named it as ‘WiremockInstall’. You can leave the JDK version to default in the dropdown as shown in the screenshot below:

Select the repository in which the WireMock jar and other deployment related files have been uploaded.

Here we set the SCM polling as the trigger. This ensures that, every time we upload code to the Git repository, it will trigger the ‘WiremockInstall’ build job.

Configure the Nodejs version in the dropdown as shown below in the screenshot.

Add Execute Shell build step from the drop down and use the npm install command to install the Grunt dependencies before executing the grunt command.

In the Post Build tab configure the build job WiremockConfigure as ‘Jobs to Build’, also check the ‘Archive the Artifacts’, to archive the zip getting generated, as shown below.

WireMock Deployment

Now go to the Deploy tab, click on the New Configurations to configure the deployment of the zip file generated by the build job.

On successful deployment you can click on the ‘wiremock’ link on the top to get the WireMock server URL.

WiremockConfigure Build Job

This build job will execute the HTTP API of the deployed WireMock server on Application Container Cloud to configure a mock API.

Name the build job as ‘WiremockConfigure’. You can leave other fields with default values.

Now set the trigger as WiremockInstall as shown below.

Use the Execute Shell build step and add the below curl command. You would need to have the URL for the WireMock Server deployed as mentioned above in the blog.

curl -X POST — data ‘{ “request”: { “url”: “/get/this”, “method”: “GET” }, “response”: { “status”: 200, “body”: “Here it is!\n” }}’ <WireMock server url>/__admin/mappings/new

Here the resource configured for the request would be /get/this.

This will configure the mock service, which can be used both for development and testing of the code which requires a service to be accessed. Below is the anatomy of the URL, that you can use to access the mock service.

<WireMock server url>/get/this

Below screenshot depicts the output of the URL in the browser.

Happy Coding!

**The views expressed in this post are my own and do not necessarily reflect the views of Oracle

--

--