Cúram REST API unit testing

Cúram REST resources are HTTP URL endpoints, so you can take a number of different approaches to test your REST APIs.

It is good practice to develop unit tests for REST APIs, and JUnit is one example tool that provides an approach for such testing.

In addition to JUnit, it can be worth considering the following open source libraries to make writing automated tests simpler:
  • Jackson for JSON handling
  • Apache HTTP Client.

In the absence of these libraries, simple tests can be performed that uses the Java Standard Edition alone, for example:


  /**
   * Tests the JSON response when the /asc_cases resource is executed for
   * concern_role_id=24011 and the _limit parameter is set to 1.
   * 
   * @throws Exception
   */
  public void test_json_response() throws Exception {

    final URL url = new URL(
      "http://localhost:9080/Rest/v1/asc_cases?concern_role_id=24011&_limit=1");
    final HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    // Verify the response code is what is expected
    assertEquals(200, conn.getResponseCode());

    final StringBuilder responseBuilder = new StringBuilder();
    final BufferedReader in = new BufferedReader(new InputStreamReader(
      conn.getInputStream()));

    while (in.ready()) {
      responseBuilder.append(in.readLine());
    }

    // Get the expected response from a flat file
    final String responseFileLocation = "C:\\";
    final String responseFile = "Response.txt";
    final String file = responseFileLocation + responseFile;

    final BufferedReader fileBufferedReader = new BufferedReader(
        new FileReader(file));
    final String expectedResponse = fileBufferedReader.readLine();
     
    // Check if the expected response is what was retrieved
    assertEquals(expectedResponse, responseBuilder.toString());
  }