Contact Us 1-800-596-4880

Mock Data Using a DataWeave Library

logo cloud IDE Cloud IDE

logo desktop IDE Desktop IDE

Open Beta Release: The cloud IDE is in open beta. Any use of Anypoint Code Builder in its beta state is subject to the applicable beta services terms and conditions, available from the IDE.

The following tutorial shows you how to develop an integration, import the Mock Data Generators Library from Exchange, and use the custom library to return a random value from the library.

Before You Begin

Before getting started, ensure you:

Create a Mule Application

  1. In Anypoint Code Builder, open the Command Palette by pressing Ctrl+shift+p (Mac) or Cmd+shift+p (Windows), and type MuleSoft: Develop an Integration.

  2. Select the command.

  3. Configure your integration using the following values:

    Develop an Integration form, with Empty Project selected
    1. Under Project Name, provide the name *mock-dw-library.

    2. Under Project Location, click Browse, and then select your home directory.

    3. Under Create, select Empty Project.

    4. Select a Mule runtime and Java version.

      You can select any of the supported Mule runtime and Java versions. The IDE saves your version settings to the project’s mule-artifact.json file. For information about version support and automated Mule patch updates, see Mule and Java version support. To set default Mule runtime and Java versions for the projects you create, see Version Settings for Mule, Java, and Connectors.

  4. Click Create Project.

Create a Flow

Start by configuring a flow with an HTTP listener that exposes a root path (/):

  1. Between the <mule> and </mule> elements, create an HTTP Listener global configuration:

    <http:listener-config name="HTTP_Listener_config" >
      <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>
  2. After the </http:listener-config> element, add a flow and an HTTP Listener element:

    <flow name="receive-request" doc:name="Flow" >
      <http:listener path="/" config-ref="HTTP_Listener_config" doc:name="Listener" doc:id="bbzemf" />
    </flow>

Import the DataWeave Library

  1. Open the Command Palette by pressing Ctrl+Shift+p (Windows) or Cmd+Shift+p (Mac), and provide the following command:

    MuleSoft: Import Asset from Exchange
  2. Select DataWeave Library.

  3. Start typing DataWeave, and press Return to list assets that match your search.

  4. Select DataWeave Mock Data Generators Library.

  5. Select version 1.0.0.

Anypoint Code Builder displays a message indicating that the dependency was successfully added.

Use the DataWeave Library

  1. After the <http:listener> element, add a new line and create a Set Payload processor:

    <flow name="receive-request" doc:name="Flow" >
      <http:listener path="/" config-ref="HTTP_Listener_config" doc:name="Listener" doc:id="bbzemf" />
      <set-payload value="#[]" doc:name="Set Payload" doc:id="ee6848" />
    </flow>
  2. Add the following DataWeave script inside the value element of the set-payload element:

    %dw 2.0
    output application/json
    import * from mocks::DataGenerators
    ---
    
    
    mocks::DataGenerators::randomCity()

Review your XML code:

<flow name="receive-request" doc:name="Flow" >
  <http:listener path="/" config-ref="HTTP_Listener_config" doc:name="Listener" doc:id="bbzemf" />
  <set-payload
    doc:name="Set Payload"
    value="#[%dw 2.0 output application/json import * from mocks::DataGenerators --- mocks::DataGenerators::randomCity()]"
    />
</flow>

Import the HTTP Connector Library

  1. Open the Command Palette by pressing Ctrl+Shift+p (Windows) or Cmd+Shift+p (Mac), and provide the following command:

    MuleSoft: Import Asset from Exchange
  2. Select Connector.

  3. Start typing HTTP, and press Return to list assets that match your search.

  4. Select HTTP Connector - Mule 4.

  5. Select the latest version.

Anypoint Code Builder displays a message indicating that the dependency was successfully added.

Deploy the Application

  1. Open the Command Palette by pressing Ctrl+Shift+p (Windows) or Cmd+Shift+p (Mac), and provide the following command:

    MuleSoft: Deploy to CloudHub
  2. Anypoint Code Builder prompts that a deployment configuration file has been created and opens it for you to review:

    {
      "runtime": "4.4.0", (1)
      "workerSize": 0.1,
      "applicationName": "mock-dw-library", (2)
      "workers": 1,
      "autoStart": true
    }
    1 The runtime version is the Mule runtime version you select when creating the integration.

    For information about version support and automated Mule patch updates, see Mule and Java version support.

    2 The applicationName property defines the part of the URL that you use to access your application on CloudHub.

    The name must be unique across all applications deployed to CloudHub.

  3. After reviewing the deployment configuration, select Deploy to deploy the application to CloudHub.

  4. Select your preferred environment.

    Anypoint Code Builder packages the application and deploys it to CloudHub.

  5. After deployment completes, select Open in Runtime Manager.

  6. Select your application from the application list.
    The domain name of the application varies, but it follows a similar structure such as: mock-dw-library.eu-s1.cloudhub.io.

  7. Open your preferred REST client and make a GET request to the domain name as displayed in Runtime Manager.

  8. Your application now returns a random city every time you reach the endpoint.