Friday, 18 December 2020

Using POSTMAN for entities testing in D365

POSTMAN for entity testing

At https://github.com/Microsoft/Dynamics-AX-Integration, Microsoft provides sample code for consuming services. However, there are many scenarios where the other endpoint in an integration might not use a Microsoft stack. Even when the other endpoint does use, for example, the Open Data Protocol (OData) client code that Microsoft makes available, you might find it useful to perform the following actions:

  • Explore and analyze how an interaction's messages are constructed.
  • Test the response of a service to a well-known request.
  • Determine how exceptions will appear to the other endpoint.

Many frequently used tools that will help you perform these actions are available. This topic isn't an endorsement of any tool. Although it provides examples that use some frequently used software utilities, the principles should broadly apply to other, similar tools.

Prerequisites

Before you can test a service by using an external application, you must register the application in Microsoft Azure, and in Finance and Operations.

For details, see:

Query OData by using Postman

Postman (https://www.getpostman.com/postman) is a tool that is often used to interact with RESTful services (such as OData) in scenarios that involve the development and testing of application programming interfaces (APIs). This procedure isn't an endorsement of Postman, and other similar tools are available. However, we are using Postman to illustrate the concepts and messages that are involved when you use OAuth to authenticate with Azure AD, and then make OData requests to and receive responses from the application.

  1. Start Postman.

  2. In the upper-right corner, select the gear button, and then select Manage environments to create or update an environment.

  3. Enter a name for the environment, and then select Bulk Edit.

  4. Enter key-value pairs as shown in the following table. Enter one pair per line, and separate the key and value by using a colon (:).

    TABLE 1
    KeyValue
    tenant_idThe Azure tenant ID that you looked up during the setup of prerequisites
    client_idThe Azure AD application ID that you registered during the setup of prerequisites
    client_secretThe secret key that you generated during application registration during the setup of prerequisites
    grant_typeclient_credentials
    resourceThe base URL of the instance without the trailing '/'
  5. To verify that the key-value pairs can be parsed correctly, select Key-Value Edit, and review the results.

  6. Close the environment page.

  7. In the field to the left of the gear and eye buttons, select the new or updated environment.

  8. To retrieve an Azure AD token, create a POST request that has a URL in the format https://login.microsoftonline.com/[tenant ID]/oauth2/token.

    You can use a URL parameter that refers to the tenant_id environment variable, such as https://login.microsoftonline.com/:tenant_id/oauth2/token.

    Retrieve an Azure AD token

  9. On the Body tab, add body elements as request parameters that refer to the environment variables that you created earlier. Select Bulk Edit, enter the keys from the previous table, enter a colon (:), and then enter the key name again but enclose it in double braces ({{}}). Enter one request parameter per line. For example, enter grant_type:{{grant_type}}. Here is an example.

    Using POSTMAN for entities testing in D365,Dynamics 365 entity postman,postman entity soap authentication,SOAPUI,token,third party test entity in D365,ODATA postmant entity testing,

  10. On the Tests tab, create a test that validates that the response is reasonable, and that stores the returned authorization token in an environment variable. Here is an example.

    C#
    var json = JSON.parse(responseBody);
    tests["Get Azure AD Token"] = !json.error && responseBody !== '' && responseBody !== '{}' && json.access_token !== '';
    postman.setEnvironmentVariable("bearerToken", json.access_token);
    
  11. Select Save, enter a name and collection for the request, and then select Save again.

  12. Select Send to make the authorization request. The Body tab should now contain an Azure AD token together with other response details.

    Azure AD token

  13. Because of the test code, the token is now in an environment variable. You can see that the token is an environment variable by selecting the Environment quick look button (the eye button).

    Environment quick look

  14. Create a request to perform create, read, update, or delete (CRUD) operations on the desired data entity via the OData service. Create the URL according to your requirements. For more information, see Open Data Protocol (OData). You might find it useful to parameterize the request by using a variable that is stored in the environment, as shown earlier. The following example of a GET query uses a Customer Account parameter. The query returns name and address details for the customer account that is specified in the environment variable. Note that special characters must be correctly URL-encoded.

    Console
    https://[Finance and Operations instance URL]/data/Customers?$format=json&$filter=CustomerAccount%20eq%20%27{{custAccount}}%27&$select=CustomerAccount,Name,AddressDescription,FullPrimaryAddress
    
  15. Add an Authorization header that refers to the authorization token that was retrieved earlier and stored in the bearerToken environment variable. The token must be prefixed by Bearer in the header.

    Bearer token

  16. Create a test to help validate the response. The following example tests that non-empty, JSON-formatted data is returned in the response body.

    C#
    var json = JSON.parse(responseBody);
    tests["Get customer info"] = !json.error && responseBody !== '' && responseBody !== '{}';
    
  17. Save and send the request, and then verify the result. You must ensure that the user account being used is set to a default company that has data. Alternatively, you can also specify cross-company=true as the query parameter in the OData request.

    Result

In our example, we have now successfully authenticated and then used the OData service to read a customer record.

Above content is taken from https://docs.microsoft.com/

Thanks,

Vikas Mehta

1 comment:

AZURE INTERVIEW QUESTIONS AND ANSWERS

AZURE INTERVIEW QUESTIONS AND ANSWERES 2021 2. What is cloud computing? Explanation:  It is the use of servers on the internet to “store...