1. Overview
  2. Step 1: Full test
  3. Step 2: Add MUnit to your API
  4. Summary

Lab 3: Run and Debug the API

Overview

In this lab we’ll run through an end to end test, and add unit testing coverage.

Step 1: Full test

In this step, you will go through a use case of creating a product, making an update and finally deleting it. These instructions will assume you are using POSTMAN, but the API Console can be used directly within Anypoint Studio. Along the way you’ll check the state of the product. You will need to:

  • Create a product.

  • Check for its existence.

  • Perform an update.

  • Check the change has been applied.

  • Delete the product.

  • Confirm the product is no longer available.

The following instructions will guide you through the process:

  1. Right click the application. Select Run As > Mule Application.

  2. The application will start running, and after a few seconds the API console will be available for us to test out the integration.

  3. Open up POSTMAN (or an equivalent).

  4. Start by creating a product. Select the POST method and set the body using the example from the RAML specification, or the example below:

         {
         "id": 1,
         "name": "ELMO",
         "description": "Playskool sesame street play all day Elmo",
         "productNumber": "UUID34234923934293",
         "manufactured": true,
         "safetyStockLevel": 540,
         "standardCost": 78,
         "listPrice": 99.9,
         "size": "200x300",
         "sizeUnitMeasureCode": "in",
         "weightUnitMeasureCode": "gr",
         "weight": 400,
         "daysToManufacture": 10,
         "categories": [
             "playskool",
             "sesame street"
         ],
         "stock": 2800,
         "colors": [
             "blue",
             "red",
             "green"
         ],
         "images": [
             "/wcsstore/HTSStorefrontAssetStore/Attachment/HomePage/Product eSpots/Play-All-Day-Elmo.png"
         ]
         }
    

    The productNumber must be unique, so if this record has already been created, try setting a different random value, or ask you instructor to provide a value by querying the database directly to check for existing records.

  5. Set the endpoint to be http://localhost:8081/api/product, and add a Content-Type header with value application/json.

  6. Review the mock body of the operation, which in this case, contains the name, description, category, etc. of the product to be created, and click the Send button.

  7. If everything was successful, you should receive a 201 - Created response code, and a response body that contains the new product ID.

    Take note of this ID. We will use it in the following steps.

  8. Open a new tab for the GET method on the /product/{id} resource.

  9. Enter the ID from the product you created before, and click Send.

    If everyhing goes Ok, you should get a 200 status response code, with the body of the product you created before.

  10. You have created a product and confirmed it exists. Now update it. Open a new tab and select PUT.

  11. Again, specify the product ID to update in the URL (set the url to http://localhost:8081/api/product/{id}, which is the same ID from before, and take a moment to check how you are changing the product (pick a minor update or your choice). Set a Content-Type header with value application/json.

  12. After taking note of your update click Send.

  13. If everything goes ok, you should get a 204 status response code and no body.

    After you update the information, perform a GET operation again under the /product/{id} resource, specifying the ID of your product, and you should obtain the updated information.

  14. To end with your basic operations, delete the product. Cretae a new tab and select DELETE as the HTTP verb. Set the url to http://localhost:8081/api/product/{id} with your product id.

    If everything goes Ok, you should get a 204 status response code and no body.

  15. To confirm we have deleted it, perform a GET operation under the /product/{id} resource, specifying the ID of your product. You should get no information. In fact, as the product and resource do not exist, you should get a 404 status response code, and the message “Resource not found”, which is what we want.

    Stop the Mule runtime.

Step 2: Add MUnit to your API

MUnit is an application testing framework that allows you to easily build automated tests for your integrations and APIs. It provides a full suite of integration and unit test capabilities, and is fully integrated with Maven and Surefire for integration with your continuous testing and deployment environment.

In this step you will use the MUnit for RAML feature to create a test for a specific flow.

The following instructions will guide you through the process.

  1. Inside api-product.xml file, right click on the “APIKit Router” component and select “Create Test Suite for api.xml from API Specification”.

    A new sapi-products-db-apikit-test.xml file will be generated within the folder src/test/munit and a viewer will open.

    Let’s focus on the test flow called “get:/product:api-config-200-application/json-FlowTest”. We are going to mock the Database invocation, so you do not have to depend on external resources.

  2. Go to the palette and select the Mock When operation from the MUnit Tools component.

  3. Drag & drop in the Behavior section

  4. Click on the Mock component configuration.

  5. In the General Panel complete with the following data

    • Processor: db:select

    • In the Then return section, select Payload tab

  6. Complete the field Value with this representation of a product simulating the Database response (press function button first):

     %dw 2.0
     output application/json
     ---
        
     [{
         id: 1,
         name: "ELMO",
         description: "Playskool sesame street play all day Elmo",
         product_number: "UUID34234923934293",
         manufactured: true,
         safety_stock_level: 540,
         standard_cost: 78,
         list_price: 99.9,
         size: "200x300",
         size_unit_measure_code: "in",
         weight_unit_measure_code: "gr",
         weight: 400,
         days_to_manufacture: 10,
         categories: "playskool, sesame street",
         stock: 2800,
         colors: "blue, red, green",
         images: "/wcsstore/HTSStorefrontAssetStore/Attachment/HomePage/Product_eSpots/Play-All-Day-Elmo.png",
         created_date: now(),
         modified_date: now()
     }, 
     {
         id: 2,
         name: "ELMO",
         description: "Playskool sesame street play all day Elmo",
         product_number: "UUID34234923934293",
         manufactured: true,
         safety_stock_level: 540,
         standard_cost: 78,
         list_price: 99.9,
         size: "200x300",
         size_unit_measure_code: "in",
         weight_unit_measure_code: "gr",
         weight: 400,
         days_to_manufacture: 10,
         categories: "playskool, sesame street",
         stock: 2800,
         colors: "blue, red, green",
         images: "/wcssatore/HTSStorefrontAssetStore/Attachment/HomePage/Product_eSpots/Play-All-Day-Elmo.png",
         created_date: now(),
         modified_date: now()
     }]
    
  7. Now complete the field Media-Type with the value application/java

  8. The final configuration for the Then return section should look like this:

  9. Check the assert called “Assert That - Payload is Expected”, adding a simple expression that checks that the result is the expected one. In this case you can simply check that the “id” that comes in the JSON response equals “1” (which is what you set it to in the mocked response) extracting it with a DataWeave inline expression.

    Set these attributes on the component:

    • Expression: #[payload[0].id]

    • Is: #[MunitTools::equalTo(1)]

    • Failure Message: The response payload is not correct!

  10. To run the test, right click on the flow name and select “Run MUnit test”. This will open the MUnit Tab and shows the execution.

  11. If everything goes OK, you will see the test executed successfully.

If you have any problems, call over the instructor to assist.

You can download a complete project from exchange.

Summary

In this module we ran through a full set of tests, and added unit testing into the API.

We saw how easy it is to use MUnit to provide unit test coverage for the API.

See the MUnit documentation for more information.

Congratulations! You have completed Lab 3.

Please proceed to the optional Lab 4