Skip to Content

Hi all,
This is my first Blog on SAP Blog.

It’s a simple blog that explains how to test a java mapping directly on Eclipse or any other IDE.

Till NW 7.0 it’s easy to debug with XML files on system, as we can directly able to convert File to InputStream and pass that a parameter into excecute method of StreamTransformation interface. But as StreamTransformation interface is deprecated and AbstractTransformation class has been implemented with NW 7.1.

As transform method of AbstractTransformation only accept TransformationInput as parameter, it’s difficult to pass offline XML file as payload and test it.

Here we will use a very simple approach to make sure our java mapping will work fine offline.

  1. Method getInputPayload() of TransformationInput object will return payload as InputPayload.
  2. Method getInputStream of InputPayload object will return payload as InputStream.
  3. Now it’s same as of previous Java mapping, we just need to create another method with any name in our JM and pass it as parameter, here we used execute method to do that.
  4. In execute method we are able to write any logic that we want and build output xml.
  5. Now, as we already know we need to change OutputStream to payload.
  6. Follow it : – getOutputStream -> getOutputPayload -> TransformationOutput and we have TransformationOutput object.
  7. That’s it.

Here is a simple code, that just push whole source XML into a target node.

 

package com.sapss.testme;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;

/**
 * @author i24989 (Anoop Rai)
 * @since 19-11-2017
 * @version 1.0
 */

public class TestJavaMapping extends AbstractTransformation {

	TransformationInput transInput = null;
	TransformationOutput transOutput = null;

	public static void main(String[] args) throws StreamTransformationException, FileNotFoundException,
			TransformerConfigurationException, TransformerFactoryConfigurationError {
		TestJavaMapping pm = new TestJavaMapping();

		FileInputStream fin = new FileInputStream("C:\\Users\\154151.XML");
		FileOutputStream fout = new FileOutputStream("C:\\Users\\OFFLINEOUT.xml");

		pm.execute(fin, fout);

	}

	public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {
		this.execute(in.getInputPayload().getInputStream(), out.getOutputPayload().getOutputStream());

	}

	public void execute(InputStream in, OutputStream out) throws StreamTransformationException {

		try {
			DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document doc = db.newDocument();

			Element outRoot = doc.createElement("SAP_TestPayload");
			outRoot.setAttribute("xmlns", "http://testme.org/");

			Element piXMLInput = doc.createElement("PIXMLInput");

			BufferedReader bfr = new BufferedReader(new InputStreamReader(in, "utf-8"));
			String line;
			String contentLine = "";
			while ((line = bfr.readLine()) != null) {
				contentLine = contentLine + line.trim();
			}
			
			piXMLInput.setTextContent(contentLine);
			outRoot.appendChild(piXMLInput);
			doc.appendChild(outRoot);

			Transformer trns = TransformerFactory.newInstance().newTransformer();
			DOMSource targetMsg = new DOMSource(doc);
			StreamResult stRslt = new StreamResult(out);
			trns.transform(targetMsg, stRslt);

		} catch (ParserConfigurationException e) {
			throw new StreamTransformationException("Unable to Parse Input XML message: " + e.getMessage());
		} catch (UnsupportedEncodingException e) {
			throw new StreamTransformationException(
					"Unsupportable input (XML UTF-8 Only) or blank : " + e.getMessage());
		} catch (IOException e) {
			throw new StreamTransformationException("Unable to read xml lines or it may be blank: " + e.getMessage());
		} catch (TransformerConfigurationException e) {
			throw new StreamTransformationException("Unable to build output XML message: " + e.getMessage());
		} catch (TransformerFactoryConfigurationError e) {
			throw new StreamTransformationException("Unable to build output XML message: " + e.getMessage());
		} catch (TransformerException e) {
			throw new StreamTransformationException("Unable to build output XML message: " + e.getMessage());
		}
		try {
			out.flush();
		} catch (IOException e) {
			throw new StreamTransformationException("Unable to flush output XML message: " + e.getMessage());
		}

	}

}

 

Regards,

Anoop Kumar Rai

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Fatih Pense

    Hi Anoop,

    I think using relative paths while testing is better. Create a test folder in Eclipse.

    Like “test/case1/154151/input.xml” instead of “C:\\Users\\154151.XML”

    Thank you for your contribution.

    Regards,

    Fatih

    (2) 

Leave a Reply