Extending Data Hub Test Adapter

Data Hub Test Adapter can easily be extended to provide new or changed functionality. Here you find the steps for doing so.

Context

The Test Adapter JAR file, which can be found on the Solution Book, contains the original implementation. It can be used as a guide for your extension. If you have not already done so, read the topic The SAP Commerce Cloud Test Adapter Extension for a full overview of the adapter and its intended purpose. By extending the Data Hub Test Adapter, you can customize how the adapter outputs target items.

Procedure

  1. Provide a new implementation of the ImpexWriterFactory interface in a custom extension. Be sure to include a dependency to the original Data Hub Test Adapter JAR.
    /**
     * A factory for creating {@link com.hybris.datahub.test.ImpExWriter}s.
     */
    public interface ImpExWriterFactory
    	{
    	   /**
    	    * Creates new writer for a specific publication.
    	    * @param pub a publication the writer needs to be created for. All data written into the writer is
    	    * associated with this publication.
    	    * @return new writer just for the specified publication.
    	    */
    	   ImpExWriter createWriter(final TargetSystemPublication pub);
    	}
    
  2. Register your implementation as a Spring bean in the extension.xml file of your custom extension.
    The default implementation's configuration is as follows:
    <alias name="impExFileWriterFactory" alias="impExWriterFactory" />
    <bean name="impExFileWriterFactory" class="com.hybris.datahub.test.ImpExFileWriterFactory" init-method="initialize">
       <property name="baseDirectory" value="${test-adapter.file.writer.directory}" />
       <property name="cleanBaseDirectoryOnInit" value="${test-adapter.file.writer.directory.cleanup}" />
    </bean>
    
  3. Implement the ImpexWriter interface by providing details of how the content of the file should be written.

    Implement the following method, and the ImpexWriterFactory created in the previous step needs to return an instance of this class.

    /**
     * A writer for persisting exported data captured by the test adapter.
     */
    public interface ImpExWriter
    	{
    	   /**
    	    * Writes to the persistent storage. This method is convenient for writing a text "as is" without any modification done to the
    	    * original content.
    	    *
    	    * @param content content to write down into the storage.
    	    * @return a writer to use for further writing.
    	    * @throws IOException if this write failed.
    	    */
    	   ImpExWriter write(final String content) throws IOException;
    	}
    
  4. If required, change the naming strategy. To change the naming strategy, implement a subclass of ImpexFileWriter that overwrites the generateFileName method.
    protected Path generateFileName(final TargetSystemPublication pub)
    {
       return Paths.get(String.format("%s_%d-%d-%s.impex",
             pub.getTargetSystem().getTargetSystemName(), pub.getPublicationAction().getActionId(), pub.getPublicationId(),
             RandomStringUtils.randomAlphabetic(4)));
    }
    
  5. And a new factory class, or subclass, as described previously that returns an instance of the new writer.