Example test case for a REST API GET operation
You can generate a test case for a REST API GET operation, as shown in this example.
Before you begin
About this task
The following example code shows a test case generated for a GET operation, in which an ID is specified using a path parameter:
package test;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import com.ibm.integration.test.v1.NodeSpy;
import com.ibm.integration.test.v1.SpyObjectReference;
import com.ibm.integration.test.v1.TestMessageAssembly;
import com.ibm.integration.test.v1.exception.TestException;
import static com.ibm.integration.test.v1.Matchers.*;
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static org.hamcrest.MatcherAssert.assertThat;
public class CustomerDatabaseV1_getCustomer_subflow_0001_IntegrationTest {
/*
* CustomerDatabaseV1_getCustomer_subflow_0001_IntegrationTest
* Integration test generated by IBM App Connect Enterprise Toolkit 12.0.0.0 on 26-Apr-2021 14:28:46
*/
@Test
public void CustomerDatabaseV1_getCustomer_subflow_TestCase_001() throws TestException {
// Define the SpyObjectReference
SpyObjectReference nodeReference = new SpyObjectReference().application("CustomerDatabaseV1")
.messageFlow("gen.CustomerDatabaseV1").node("getCustomer (Implementation)");
// Initialise a NodeSpy
NodeSpy nodeSpy = new NodeSpy(nodeReference);
// Declare a new TestMessageAssembly object for the message being sent into the node
TestMessageAssembly inputMessageAssembly = new TestMessageAssembly();
// Variables used for setting parameters in Local Environment
String customerId = "0";
// Add Local Environment to Message Assembly for HTTP
inputMessageAssembly.localEnvironmentPath("HTTP.Input.Path.Segment[0]").setValue("customerdb");
inputMessageAssembly.localEnvironmentPath("HTTP.Input.Path.Segment[1]").setValue("v1");
inputMessageAssembly.localEnvironmentPath("HTTP.Input.Path.Segment[2]").setValue("customers");
inputMessageAssembly.localEnvironmentPath("HTTP.Input.Path.Segment[3]").setValue(customerId);
// Add Local Environment to Message Assembly for REST
inputMessageAssembly.localEnvironmentPath("REST.Input.Method").setValue("GET");
inputMessageAssembly.localEnvironmentPath("REST.Input.Operation").setValue("getCustomer");
inputMessageAssembly.localEnvironmentPath("REST.Input.Path").setValue("/customerdb/v1/customers/" + customerId);
inputMessageAssembly.localEnvironmentPath("REST.Input.URI")
.setValue("http://localhost/customerdb/v1/customers/" + customerId);
inputMessageAssembly.localEnvironmentPath("REST.Input.Parameters.customerId").setValue(customerId);
// Call the message flow node with the Message Assembly
nodeSpy.evaluate(inputMessageAssembly, true, "Input");
// Assert the number of times that the node is called
assertThat(nodeSpy, nodeCallCountIs(1));
// Assert the terminal propagate count for the message
assertThat(nodeSpy, terminalPropagateCountIs("Output", 1));
/* Compare Output Message 1 at output terminal Output */
try {
TestMessageAssembly actualMessageAssembly = null;
String actualOutputData = null;
String expectedOutputData = null;
// Get the TestMessageAssembly object for the actual propagated message
actualMessageAssembly = nodeSpy.propagatedMessageAssembly("Output", 1);
// Assert output message body data
// Get the string containing the actual data that was propagated from the node
actualOutputData = actualMessageAssembly.getMessageBodyAsString();
// Create an InputStream from the expected resource
String expectedResourcePath = "/getCustomer_subflow_output_data.json";
InputStream resourceStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(expectedResourcePath);
if (resourceStream == null) {
throw new TestException("Unable to locate resource: " + expectedResourcePath);
}
byte[] buffer = new byte[resourceStream.available()];
resourceStream.read(buffer);
// Get the expected output data using output data file
expectedOutputData = new String(buffer, StandardCharsets.UTF_8);
// Assert that the actual data matches the expected data
assertThat(actualOutputData, jsonEquals(expectedOutputData));
} catch (Exception ex) {
throw new TestException("Failed to compare with expected message", ex);
}
}
}
The output file
/getCustomer_subflow_output_data.json
is generated from the Open
API definition for the REST API. It contains the following
string:{"id":0,"firstname":"string","lastname":"string","address":"string"}
The output message assembly is generated from
/getCustomer_subflow_output_data.json
. It is generated using the following
code:
// Assert output message body data
// Get the string containing the actual data that was propagated from the node
actualOutputData = actualMessageAssembly.getMessageBodyAsJSON();