Skip to content

Tutorial Talend Component Kit 2: Testing your own component

Simon Neidig edited this page Dec 11, 2019 · 2 revisions

With every software development the often unpopular testing is part of it.

In the case of the "Talend Component Kit ", however, this is not quite as unpopular as it allows debugging and thus a pleasant development. With the integration of the components directly into "Talend Open Studio for Data Integration " a debugging in "Intellij IDEA " is unfortunately not possible. However, the test environment also allows you to create your own jobs in a simple way directly in the code!

After the second part you can:

  • explain the test class structure.
  • use the SimpleConditionRule.
  • create your own test jobs and debug them

1. Already generated test classes

Because we selected the extensions "Apache Beam " and "Talend Component Kit Testing " during project creation, the following test structure has already been created and set up for us. Initially, the focus of the tutorial will be on testing an input component using the "Talend Component Kit Testing ".

Abb. 1: Projekt-Baum mit Pointer auf dem zunächst betrachteten Input-Test

The tests are written in the file "CompanyInputMapperTest ", or in our case "OdiSysInputMapperTest " (in the following "Company " is replaced by "OdiSys "), under the path "src → test → java → de.odisys.talend.components → source ". The automatically generated Java class should look like this:

public class OdiSysInputMapperTest {

    @ClassRule
    public static final SimpleComponentRule COMPONENT_FACTORY = new 						SimpleComponentRule("de.odisys.talend.components");

    @Test
    @Ignore("You need to complete this test")
    public void produce() throws IOException {

        // Source configuration
        // Setup your component configuration for the test here
        final OdiSysInputMapperConfiguration configuration =  new OdiSysInputMapperConfiguration()
                                                                            /* .setDataset() */;

        // We create the component mapper instance using the configuration filled above
        final Mapper mapper = COMPONENT_FACTORY.createMapper(OdiSysInputMapper.class, configuration);

        // Collect the source as a list
        assertEquals(asList(/* TODO - give the expected data */), COMPONENT_FACTORY.collectAsList(Record.class, mapper));
    }
}

The "SimpleComponentRule " declared in it is explained in section 2, the first test method including job pipeline is implemented in section 3.

2. The SimpleComponentRule

The so-called "SimpleComponentRule " is declared directly at the beginning of the test class with the annotation "@ClassRule" as final attribute as follows. It is important that the constructor is given the name of the package of the created components as parameter. This has already been done for the automatically generated classes.

@ClassRule
public static final SimpleComponentRule COMPONENT_FACTORY = new SimpleComponentRule("de.odisys.talend.components");

When the rule is initialized, a Component Manager is created. This provides two "Mock " classes, i.e. classes that serve as templates.

  • the "Emitter ", to mimic an input component
  • the "Collector ", to imitate an output component.

In the job pipeline, they can later be created and used with "test://emitter ", or "test://collector ". The usage is explained in the next section as an example for the collector.

3. creating a job pipeline

Using the class "Job " from "org.talend.sdk.component.runtime.manager.chain.Job " it is possible to **create **with Talend-Jobs with simple coding and execute them in the test. In general such a job is structured as follows. After "Job.components() " the components are created, after ".connections() " the connections between the jobs are drawn. With ".build() " a build of the job is generated, which can then be executed with ".run() ".

Job.components()
  .component("a", ...)    
  .component("b", ...)
  .connections()
  .from("a").to("b")
  .build()
  .run();

The creation of components works as follows:

...
  .component("Variablenname", "Komponentenfamilie://Komponentenname?" + Konfiguration als Querystring)
...

In the configuration as query string, a previously created configuration is converted into a string and thus made available for the test component. In the following example, the Jira input component created in the following tutorial is tested:

 @Test
    public void produce() throws IOException {
        // Define your DataStore
        DataStore dataStore = new DataStore("http://your.domain/jira", "username", "password");

        // Define your DataSet
        final DataSet dataSet = new DataSet(dataStore, "Jira-Board-Name", "In Progress");

        // Define your Configuration and add the DataSet created previous
        final OdiSysInputMapperConfiguration configuration =  new OdiSysInputMapperConfiguration();
        configuration.setDataset(dataSet);

        // Create the uriConfig that could be added to the end of the component name
        final String uriConfig = SimpleFactory.configurationByExample()
                .forInstance(configuration)
                .configured().toQueryString();

        // create a Mapper with the SimpleComponentRule-Factory, defined as @ClassRule and the configuration created previous
        final Mapper mapper = COMPONENT_FACTORY.createMapper(OdiSysInputMapper.class, configuration);

        // create the job-pipeline
        Job.components()
          .component("OdiSysInput", "OdiSys-Component://OdiSysInput?" + uriConfig)
          .component("collector", "test://collector")
          .connections()
          .from("OdiSysInput").to("collector")
          .build()
          .run();

        // extract the result of the job-run
        final List<Record> result = COMPONENT_FACTORY.collectAsList(Record.class, mapper);
        ReadContext json = JsonPath.parse(result.get(0).getString("data"));
        List<String> issues = json.read("$");

        assertEquals(2, issues.size());
    }

After the test job has been executed, you can use "COMPONENT_FACTORY.collectAsList(Record.class, mapper) " to Output generated by the input component are captured. Once it has been captured, it can be processed further using Java operations and tests executed on it.

The tests created in this way can be executed in "Intellij IDEA " Debugger by clicking on the little green "Play Button"!