< Previous | Next >

Task 3: Retrieving an OpenAPI description and calling the service in a generated client

You get an OpenAPI description of your HTDS, and use it to generate a Java client. Then, you test the Java client with a JUnit class.

About this task

You download an OpenAPI description of your HTDS, and copy it to the tutorial directory. Then, you use the description to generate a Java client. You add custom code to the Java client to call your decision service with input parameters. If you have an installation of Maven, you can run the Java client test.

Step 1: Downloading and replacing files

About this task

You download the YAML description of your decision service, and copy it to the sample directory.

Procedure

  1. In the Navigator pane of the Explorer tab in Rule Execution Server, expand the RuleApps.
  2. Expand /PreTradeChecksRuleApp/1.0 RuleApp, and click PreTradeChecks/1.0.
  3. On the Ruleset View page, click Retrieve HTDS Description File.
  4. Select REST in the Service protocol type field.
  5. Select OpenAPI - YAML.
  6. Select the Decision trace information option, and then click Download.
    Image shows the HTDS dialog.
  7. Save the file by renaming it pretradecheck.yaml and replacing the following file: <InstallDir>/executionserver/samples/decisionservice/data/pretradecheck.yaml.

Step 2: Generating the Java client

About this task

You generate a Java client that is based on the downloaded YAML file by using swagger-codegen-cli.jar.

Procedure

  1. Open the Samples Console perspective in Rule Designer.
  2. In the Samples Commands view, expand Samples Commands > Rule Execution Server > Executing a hosted transparent decision service.
  3. Double-click generate.openapi.client. You can then use the open_api_client project generated in <InstallDir>/executionserver/samples/decisionservice/open_api_client. This directory is referred to as <OpenAPI_Client_Dir> in the next steps.

    This command uses the swagger-codegen-cli.jar file as follows:

    java -jar swagger-codegen-cli-2.2.0.jar generate -i <path_to_data>/pretradecheck.yaml -l java --library jersey2 -o <target_project_name>

    The swagger-codegen-cli-2.2.0.jar can be downloaded from the Swagger website or found in <InstallDir>/executionserver/samples/decisionservice/lib.

Step 3: Adding a test to the Java client

About this task

You add a JUnit test to the generated client by using a command that copies a Java file that is provided for this task.

Procedure

  1. Again in the Samples Commands view of the Samples Console, expand Samples Commands > Rule Execution Server > Executing a hosted transparent decision service.
  2. Double-click add.openapi.unit.test.

    This command copies the SimpleApiTest Java class to <OpenAPI_Client_Dir>/src/test/java/io/swagger/client/api/SimpleApiTest.java.

    In the predefined Java class, a test is defined with the appropriate input parameters for customer and order, and an initialization of the TraceFilter to get the number of fired rules.

    You can open the SimpleApiTest.java file to see the initialized values:

    ...
    /*            
    * Initialize customer values            
    */           
    Customer customer = new Customer();           
    customer.setName("John Smith");           
    customer.setOwner("George Smith@IBM");           
    ...           
    customer.setBondPosition((float) 800);           
    customer.setFuturPosition((float) 800);           
    customer.setStockPosition((float) 800);          
    customer.setCurrency(CurrencyEnum.EURO);                      
    /*            
    * Initialize order values            
    */           
    Order order = new Order();
    order.setStock("Company X");           
    order.setAmount(90);           
    order.setAction(ActionEnum.BUY);          
    order.setSecurityType(SecurityTypeEnum.STOCK);           
    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");            
    /*            
    * Initialize trace filter values            
    */           
    TraceFilter traceFilter = new TraceFilter();          
    traceFilter.setInfoTotalRulesFired(true);           
    ...           
    traceFilter.setNone(true);                      
    request.traceFilter_(traceFilter);           
    request.setCustomerParameter(customer);           
    request.setOrderParameter(order);           
    ...           
    try {                
    result = api.callDecisionServiceOperation(request);                
    System.out.println(result);               
    assertNotNull(result.getReportParameter());                
    assertEquals("an_id",
    result.getReportParameter().getId());                
    ...                
    assertEquals(StatusEnum.BLOCKED, result.getReportParameter().getStatus());                
    assertEquals(ReasonsEnum.BLACK_LIST_STOCKS, result.getReportParameter().getReasons());           
    } catch (ApiException e) {               
     System.err.println("Exception when calling DefaultApi#executeRuleset");                
    e.printStackTrace();           
    }           
    assertNotNull(result);    
    }
    }

Step 4: (Optional) Running the Java client test

This step is optional because you must have Maven 3.0 or higher installed, and Java 8 in your PATH environment variable.

About this task

You run your JUnit test and check the results.

Procedure

  1. Open a command line on your computer.
  2. Go to <OpenAPI_Client_Dir>, type mvn clean test, and press Enter.

    You get output similar to the output from running io.swagger.client.api.DefaultApiTest:

    ...    
    decisionTrace_: class DecisionTrace {
    ...        
    totalRulesFired: 4    
    reportParameter: class Order {     
    ...        
    status: BLOCKED        
    reasons: BLACK_LIST_STOCKS
    ...
    ]
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    ...

What to do next

In the next task, you continue working in the Rule Execution Server console. You can optionally run your decision service through IBM API Connect, or you can skip the next task and retrieve a WSDL description and call the decision service with a generated client.
< Previous | Next >