Test your plug-in

It is strongly recommended to implement tests to validate the behavior that you have developed before deploying it to a Semarchy xDM instance.

This page provides instructions to implement automated tests when developing a Semarchy xDM plug-in. It explains how to create a JUnit test case for the enricher plug-in that you previously created.

Create a test project

To create a test project:

  1. Select File > New > Other

  2. In the Wizards filter, enter fragment. The list shows the Fragment Project wizard.

  3. Select the Fragment Project wizard and then click Next.

  4. In the Project Name, enter the name for your project. For example: com.acme.phoneStandardizerTests. Leave the other fields as is.

  5. Click Next. In the Content page, modify the following:

    • Version: Version of your test plug-in fragment (for example: 1.0.0)

    • Name: Visible Name for your test plug-in fragment (for Example: Phone Standardizer Test)

    • Provider: Your company’s name (for Example, ACME Corp.)

  6. In the Host Plug-in section, use the Browse… button to select the Plug-in ID.

  7. In the Select a Plug-in filter, enter the name of the Semarchy xDM plug-in you want to validate with your tests. For example com.acme.phoneStandardizer.

  8. Select the Semarchy xDM plug-in from the list. For example, select com.acme.phoneStandardizer

  9. Click Finish. A new project is created.

  10. In the project editor, select the Dependencies Tab.

  11. In the Required Plug-ins section, click the Add… button.

  12. In the Select a Plug-in filter, enter org.junit4. Select the org.junit4 plug-in in the list of matching items and then click OK.

  13. Press Control+S (or Command+S on macOS) to save the editor.

Create a test case

To create a test case:

  1. In the Package Explorer, select the test project (com.acme.phoneStandardizerTests), right click and then select File > New > Other.

  2. In the Wizards filter, enter junit. The list shows the JUnit Test Case wizard. Select it from the list and then click Next.

  3. In the JUnit Test Case wizard, enter the following:

    • Package: Name of the package containing the class for your test. For example: com.acme.phonestandardizer.tests

    • Name: Name of the test class. For example: TestPhoneFrance.

    • Class under test: Use the browse button to select the class you want to test with this test case. For example: com.acme.phonestandardizer.IntlPhoneStandardizer

  4. Click Next. Select the methods you want to test. Typically, the transform method for an enricher or the isValid method for a validation.

  5. Click Finish. The code editor for your test case opens.

In addition to the transform and isValid methods, you may also test other methods such as setUp and tearDown to verify the plug-in behavior during its entire lifecycle.

Implement the test case

The test case checks the transformation of several phone numbers. It checks that the transformation provides expected results with valid, invalid or null phone numbers.

Download the full source code of the TestPhoneFrance.java test case.

The rest of this section describes the key methods implemented in the test code.

The checkPhoneTransform method tests the behavior of the transformer. It takes three parameters:

  • The NULLIFYONERROR parameter

  • an INPUTPHONE input phone number

  • an expected STANDARDIZEDPHONE output phone number

The method:

  1. Defines the mapped inputs, outputs and parameters for the test case.

  2. Instantiates an rowTransformerExecutor object. This object enables testing a row transformer.
    Note that this object is created using:

    • The list of mapped inputs and outputs.

    • The ID of the enricher/validation set when declaring the endpoint, that is com.acme.phoneStandardizer.intlPhoneStandardizer.

  3. Sets up the plug-in using the mapped parameters.

  4. Creates a list containing one input DataRow containing an INPUTPHONE input field value.

  5. Runs the transform method for the rowTransformerExecutor to execute the enricher.

  6. Compares (using the JUnit assertEquals method) the value of the STANDARDIZEDPHONE field of the output IDataRow with the expected standardized phone.

  7. Finally, uses the tearDown method on the rowTransformerExecutor.

private void checkPhoneTransform(Boolean nullifyOnError, String inputPhone, String expectedOutputPhone) {

	// Define the mapped inputs.
	Set<String> mappedInputs = new HashSet<String>();
	mappedInputs.add("INPUTPHONE");

	// Define the mapped outputs.
	Set<String> mappedOuputs = new HashSet<String>();
	mappedOuputs.add("STANDARDIZEDPHONE");

	// Define the mapped parameters
	Map<String,Object> parameters = new HashMap<String, Object>();
	parameters.put("NULLIFYONERROR", nullifyOnError);

	// Create the rowTransformerExecutor
	RowTransformerExecutor rowTransformerExecutor = new RowTransformerExecutor("com.acme.phoneStandardizer.intlPhoneStandardizer",null,mappedInputs,mappedOuputs);

	// Setting up the plug-in.
	rowTransformerExecutor.setUp(parameters);

	try{

		// Creating a new data row with the test phone number
		IDataRow inDataRow = new DataRow();
		((DataRow) inDataRow).setValue("INPUTPHONE", inputPhone);

		// Inserting this data row in a list of data rows.
		List<IDataRow> inDataRowList = new ArrayList<IDataRow>();
		inDataRowList.add(inDataRow);

		// Enriching the list of input data rows.
		List<IDataRow> outDataRowList = rowTransformerExecutor.transform(inDataRowList);

		// Testing the resulting phone number.
		assertEquals(expectedOutputPhone, outDataRowList.get(0).getValue("STANDARDIZEDPHONE"));

	} finally {

		// Tearing down the plug-in.
		rowTransformerExecutor.tearDown();
	}
}

This method is used in various test cases, as shown below.

@Test public void testTransformNullPhones() {
	checkPhoneTransform(true, null, null);
	checkPhoneTransform(false, null, null);
}

@Test public void testTransformBadPhones() {
	checkPhoneTransform(true, "abcd", null);
	checkPhoneTransform(false, "abcd", "abcd");
	checkPhoneTransform(true, "64169710", null);
	checkPhoneTransform(false, "64169710", "64169710");
	checkPhoneTransform(true, "111164169710", null);
	checkPhoneTransform(false, "111164169710", "111164169710");
	checkPhoneTransform(true, "+44664169710", null);
	checkPhoneTransform(false, "+44664169710", "+44664169710");
	checkPhoneTransform(true, "1664169710", null);
	checkPhoneTransform(false, "1664169710", "1664169710");
}
The unit tests only include French phone numbers, as it is what the transformer supports. Valid French Phone numbers have 10 digits, starting with a zero. They may be written with non-numeric characters that will be ignored. They may be prefixed with the +33 country code.

Run the test case

To run the test case:

  1. Save the test case code.

  2. Select the test project, right-click and select Run As > JUnit Plug-in Test. The JUnit view opens and shows the execution results for the test case.

The test cases should display no error.