DineshS
Tera Guru

Being new to ATF myself, I was able to get the inbound ReST integrations configured on ATF. It wasn't hard given the tutorials and docs. However, I couldn't find anything on how to use ATF for outbound integrations.

As I started to figure out bits and pieces, I thought to share with the community. I am sure the experts out here will be able to improvise this further and in turn help me too.

The flow of the test at a high level is to invoke a ReST API, verify if the HTTP status code is as expected. 

A - Decide on the outbound API to connect

You could do this with any ReST API, I chose the Bitcoin Price Index(BPI) mentioned here, because it rhymed with API,had a new generation touch and was easy to demonstrate 🙂

B - Create Required Step Configurations

We will need few new ATF Step Configurations to be created.

Navigate to ATF -> Administration -> Step Configurations. Click on New

1. Create the step config to invoke the outbound ReST API

find_real_file.png

It's a good idea to think of the input and output variable you need upfront, as we need to use them in the script.

find_real_file.png

find_real_file.png

find_real_file.png

The script is derived from this doc link(Direct RESTMessageV2 example)

(function executeStep(inputs, outputs, stepResult, timeout) {
    try {
        var reqMessage = new sn_ws.RESTMessageV2();
        //reqMessage.setAuthenticationProfile("basic", "sys_id of the profile"); //TO DO: parameterise
        reqMessage.setHttpMethod(inputs.u_http_method);
        reqMessage.setEndpoint(inputs.u_endpoint);
        reqMessage.setRequestBody(inputs.u_request_body);
        for (var name in inputs) {
            reqMessage.setRequestHeader(name, inputs.u_http_header);
        }
        reqMessage.setHttpTimeout(10000); //In milliseconds. Wait at most 10 seconds for response from http request. TO DO: parameterise
        response = reqMessage.execute();
        outputs.u_response_body = response.haveError() ? response.getErrorMessage() : response.getBody();
        outputs.u_http_status = response.getStatusCode();
        stepResult.setOutputMessage(response.getBody());
        stepResult.setSuccess();
    } catch (ex) {
        outputs.u_response_body = ex.getMessage();
        outputs.u_http_status = '500';
        stepResult.setFailed();
    }
}(inputs, outputs, stepResult, timeout));

2. Step Config to Compare values retrieved from the API against expected values

Create a step configuration to compare two values, given the comparison operator. This could be enhanced to include additional operators such as 'contains', 'starts with' etc to introspect the API response 

find_real_file.png

Again, it's a good idea to think of the input and output variable you need upfront, as we need to use them in the script.

 find_real_file.png

For the operator, I tried reusing something that's already provided by ServiceNow. I don't know if this is the right table or not. The outcome I wanted to achieve was to get a list of standard operators available out of the box. You will notice this later when we use this step configuration within the test.

find_real_file.png

Alternatively, one could choose to use the Variables Choice List option, which helps you to provide a list of valid values that the variable(u_operator in this case) can have.

find_real_file.png

The step execution script is as given below. I have included only couple of operators for now. I am hoping you can improvise and share with the community.

(function executeStep(inputs, outputs, stepResult, timeout) {
	outputs.u_issuccess = 1;
	if(inputs.u_operator == '=' && inputs.u_value1 == inputs.u_value2){
		stepResult.setSuccess();
	} else if(inputs.u_operator == '!=' && inputs.u_value1 != inputs.u_value2){
		stepResult.setSuccess();
	} else {
		outputs.u_issuccess = 0;
		stepResult.setFailed();
		stepResult.setOutputMessage('Either operator is unsupported or operation returned false!');
	}
}(inputs, outputs, stepResult, timeout));

C - Create the ATF Test Case using the Step Configurations from above

Now that we have the step configurations, it is time to put them to work. 

Let us create a test named OutboundBPI(Bitcoin Price Index, not a typo for API) with two Exclusive Parameters, one for API endpoint and other for HTTP success code(eg: 200) that we expect the API call to return.

find_real_file.png

Now create the test steps. In my case I have two main steps, one for calling the outbound API and another for comparing if the HTTP return code was as expected. Ignore the Log steps as I have used them for debugging purposes

This is how the step configurations will show, while adding the steps

find_real_file.png 

Notice how the input variables we added show up on the test step for Outbound ReST API

find_real_file.png

And the one for Compare Values step

find_real_file.png

Remember, I mentioned about the values for the operator ? Here's how the list will look like. Remember to code for the operators that you are planning to use from this list.

find_real_file.png

Here's the summary view of the test steps created

find_real_file.png

Now let us run the test and see the result !

find_real_file.png

 PS : Log steps(steps 2 and 4) above are just for debugging purposes

Great ! Bitcoin Price Index API is working! Happy Days !!!

 

PS: Please share your valuable feedback on the post, how to improve, if it was readable, useful etc

Comments
Eli7
Tera Expert

This is very useful. Good job documenting this and thank you for sharing with the community!

Version history
Last update:
‎02-28-2022 07:14 AM
Updated by: