Continuous Delivery – OPA and Postman, Newman and Jenkins #2

Continuous Development – OPA and Postman, Newman and Jenkins #2

Following on from the previous post in this series, we now have Oracle Policy Automation and Postman playing nicely together. In this chapter we will use  some scripts in our Collection, both to streamline the authentication process and to provide us with some feedback about the execution of our collection.

So, first things first let’s add some scripts to our Postman Collection. In Postman there are two sorts of scripts, which you can think of as “before” and “after” scripts. For example you might run a script to prepare some data, or to authenticate your user with Oracle Policy Automation before you actually run a request. Similarly, after the test has run, you might investigate an output attribute from Oracle Policy Automation, or look at the response time of your request and compare it to others. That sort of thing. Let’s look at some useful examples. And just before we continue, remember that “before” and “after” scripts are available both at the Collection level as well as the individual Request level.

  1. Authenticating before the Collection is run

This is a common need  – to make sure the Collection can actually run without having to manually go and authenticate if your 1800 seconds are up. Of course more sophisticated scripts could store the current expiry and check to see if authentication is needed but this is just a simple example of how to authenticate. For example, you could paste this into your Pre-Request Script or your Pre-Collection Script:

var myVar = "?grant_type=client_credentials&client_id=YOURUSER&client_secret=YOURPASSWORD";
console.log("Updating Token");
pm.sendRequest({
url: "http://YOURSERVER/determinations-server/batch/auth" + myVar,
method: 'POST'
}, function (err, res) {
pm.environment.set("OAuth_Token", res.json().access_token);
});

This requires you also to set up a variable in your Environment in Postman called OAuth_Token, and also to ensure that your Collection knows to use it. Follow these steps.

  1. Create the Environment Variable by clicking the Eye icon and then Edit. Leave the value blank, your script will populate it automatically, Ignore the other variables in the screenshot they are not used by this simple script.
  2. Add the Script to manage the Variable.
  3. Add a reference to the new variable in Authentication.

So in pictures, first the Environment Variable to be added:

Postman Variables 3

And the Collection or Request Pre-Request Script Tab:

Postman Script Before

And in the Authentication Tab (note the double braces):

Postman Variable Usage

Now we can launch the Collection and know that the authentication token will be acquired as well, without having to remember to do it manually.

This is the first step towards our automation platform being truly useful. But before we do that, let’s add a couple more things to our Collection. We will add a test script that actually tests something. In my example Project, one of the outputs is the duration of the trip between the two stations. Let’s say that for whatever purpose, we consider a journey of greater than 20 minutes to be a functional failure. So we want to write an “after” script that looks at that for us. And let’s also say that we want to look at the response time and ensure that it meets our internal guidelines. So we might have a script like this:

function standardDeviation(values, avg) {
var squareDiffs = values.map(value => Math.pow(value - avg, 2));
return Math.sqrt(average(squareDiffs));
}

function average(data) {
return data.reduce((sum, value)=>sum + value) / data.length;
}

if (responseCode.code === 200 || responseCode.code === 201) {
response_array = globals['response_times'] ? JSON.parse(globals['response_times']) : []
response_array.push(responseTime)
postman.setGlobalVariable("response_times", JSON.stringify(response_array))

response_average = average(response_array);
postman.setGlobalVariable('response_average', response_average)

response_std = standardDeviation(response_array, response_average)
postman.setGlobalVariable('response_std', response_std)
}

This script uses three global variables that you create in the same way as environment variables and one of them is an array, representing the different times you run your request for example. In pictures:

Add an “after” Test Script:

Add the Variables :

Run the Collection or Script and observe the variables are evolving:

And let’s add another test, this time to check the journey duration is not too long (paste this after or before the existing “after script).

As you can see above, you are able to access the output of the request using JSON and then you can pose a condition. In the example above, the test has failed because I chose two stations that are very far apart. This is getting rather interesting. The next part is to use some data that makes our requests a bit more useful and to automate them. See you soon  for the next part!

Kudos to the following pages :

How to Automate OAuth2 in Postman

A Postman script to calculate average response times

Richard Napier

Author: Richard Napier

Richard Napier joined Siebel Systems in 1999 and took up the role of managing the nascent Siebel University in Southern Europe. He subsequently was Director of Business Development and Education for InFact Group (now part of Business & Decisions) for 8 years. He now runs his Consulting and mentoring company, On Demand Consulting & Education Ltd and has run ODCE since 2010. Owner of the OPA Hub, he also is Co-Founder of the Siebel Hub.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Worldwide
Logo by Southpaw Projects LLC