Mindmajix

Testing logic – the most-used pattern

Testing Logic is probably the most-used job design in Talend programming, and is used to ensure that a snippet of new code is not influenced by external factors within a large and complex job. This simple recipe shows how this can easily be achieved.

Getting ready

Open the jo_cook_ch10_0140_logicTest job.

How to do it…

The steps for testing logic are as follows:

  • In tFixedFlowInput, tick the box labeled Use Inline Table.
  • Add the values, as shown in the following screenshot:

talend 26

  • In the tMap, add a new field to the output named ageCheckValid, and populate it with the following code:
customer.age >= 21 && customer.country.equals("UK") ? true : customer.age >= 18 && !customer.country.equals("UK") ? true : false
  • Run the job to see the results of the test.

How it works…

In this example, we are testing an age limit; 21 or over is valid for the UK, 18 or over is valid for the rest of the world.

In tFixedFlowInput, we defined a set of test values that would prove that the logic test in tMap is working correctly.

The tLogRow component, then allows us to see the inputs and the corresponding results.

There’s more…

This example is great for testing new rules, and especially for testing new code routines prior to adding to a complex job. It is quick to create, and the tFixedFlowInput component allows us to build a controlled set of test data that should test all return values, thus giving us confidence in the correctness of the new code or code routine.

Killing a job from within tJavaRow

User Case

There are multiple ways to stop a job in Talend. You could use “if” trigger statements combined with “tDie”, some components have a die on error tick boxes in their settings. Here, let us see one of the ways to kill/terminate a Talend Studio job based on an input file row value.

Let’s say you are black boxing a Talend job for a client and you know based on experience that the client does not always provide the most accurate input files. Some of the rows that must be NOT NULL for the job to run successfully aren’t always provided. Unfortunately you cannot substitute those empty rows with dummy data (according to your job logic), so you have to kill the job and inform the client that something went wrong, so that the person running the job will be able to fix the issue.

Validation Process

  1. Create a new Job;
  2. Add 3 components
    • “tFileInputDelimited”;
    • “tJavaRow”;
    • “tLogRow”;
    output_row.columnName1= input_row.columnName1; output_row.columnName2 = input_row.columnName2; output_row.columnName3 = input_row.columnName3; if (output_row.columnName4 == null) { System.out.println("Error: " + output_row.columnName4 + " is null"); System.exit(99); } else { output_row.columnName4 = input_row.columnName4; } output_row.columnName5 = input_row.columnName5;
  3. Create a Delimited file (.csv) and make sure that for one of the columns the value is null;
  4. In “tJavaRow” add code similar to:

output_row.columnName1= input_row.columnName1; output_row.columnName2 = input_row.columnName2; output_row.columnName3 = input_row.columnName3; if (output_row.columnName4 == null) { System.out.println("Error: " + output_row.columnName4 + " is null"); System.exit(99); } else { output_row.columnName4 = input_row.columnName4; } output_row.columnName5 = input_row.columnName5;

Note: columnName[1,2,3,4,5] – the names of the columns you’ve specified in your Delimited file. If row value equals to null for a particular column, then print a message to the console log and terminate the job.

  1. Run the job (if in your delimited file, the column that you are evaluating has a null, the job will be terminated);

This simple approach can reduce the amount of Talend components used in the Job while providing a way to evaluate the input file.


0 Responses on Testing logic – the most-used pattern"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.