changeset 1188:223dbbdd7e40

SoapValueExtractor
author Devel 2
date Wed, 17 Jun 2020 15:26:33 +0200
parents 013fb325d040
children 7f70d5ddbde9
files stress-tester/src/main/java/com/passus/st/extractor/ContentExtractorUtils.java stress-tester/src/main/java/com/passus/st/extractor/SoapValueExtractor.java stress-tester/src/test/java/com/passus/st/extractor/SoapValueExtractorTest.java
diffstat 3 files changed, 164 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/extractor/ContentExtractorUtils.java	Wed Jun 17 15:21:15 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/extractor/ContentExtractorUtils.java	Wed Jun 17 15:26:33 2020 +0200
@@ -3,7 +3,6 @@
 import com.passus.commons.Assert;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public class ContentExtractorUtils {
@@ -43,6 +42,9 @@
                 case "regexp":
                     contentReplacer = new RegexValueExtractor(expr);
                     break;
+                case "soap":
+                    contentReplacer = new SoapValueExtractor(expr);
+                    break;
                 default:
                     throw new IllegalArgumentException("Invalid replacer rule '" + rule + "'. Invalid replacer type '" + extractorType + "'.");
             }
@@ -89,6 +91,9 @@
                 case "regexp":
                     contentExtractor = new RegexValueExtractor(expr);
                     break;
+                case "soap":
+                    contentExtractor = new SoapValueExtractor(expr);
+                    break;
                 default:
                     throw new IllegalArgumentException("Invalid extractor rule '" + rule + "'. Invalid extractor type '" + extractorType + "'.");
             }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/extractor/SoapValueExtractor.java	Wed Jun 17 15:26:33 2020 +0200
@@ -0,0 +1,112 @@
+package com.passus.st.extractor;
+
+import org.apache.commons.io.input.CharSequenceInputStream;
+import org.apache.commons.lang3.StringUtils;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import javax.xml.soap.*;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+public class SoapValueExtractor implements ContentExtractor, ContentReplacer {
+
+    private MessageFactory factory;
+
+    private final String method;
+
+    private final String parameter;
+
+    public SoapValueExtractor(String expresion) {
+        try {
+            factory = MessageFactory.newInstance();
+        } catch (SOAPException e) {
+
+        }
+
+        String[] parts = StringUtils.split(expresion, ".");
+        if (parts.length != 2) {
+            throw new IllegalArgumentException("Invalid expression.");
+        }
+
+        this.method = parts[0];
+        this.parameter = parts[1];
+    }
+
+    private SOAPMessage parse(CharSequence content) throws IOException, SOAPException {
+        try (CharSequenceInputStream is = new CharSequenceInputStream(content, "UTF-8")) {
+            return factory.createMessage(new MimeHeaders(), is);
+        }
+    }
+
+    private Element findElement(NodeList childNodes, String name) throws IOException, SOAPException {
+        String elemName = ":" + name;
+        for (int i = 0; i < childNodes.getLength(); i++) {
+            Node node = childNodes.item(i);
+            if (node instanceof Element) {
+                Element elem = (Element) node;
+                if (elem.getTagName().endsWith(elemName)) {
+                    return elem;
+                }
+            }
+        }
+
+        return null;
+    }
+
+    private Element findElement(CharSequence content) throws IOException, SOAPException {
+        SOAPMessage message = parse(content);
+        return findElement(message);
+    }
+
+    private Element findElement(SOAPMessage message) throws IOException, SOAPException {
+        SOAPBody soapBody = message.getSOAPBody();
+        NodeList childNodes = soapBody.getChildNodes();
+        Element element = findElement(childNodes, method);
+        if (element == null) {
+            return null;
+        }
+
+        element = findElement(element.getChildNodes(), parameter);
+        if (element == null) {
+            return null;
+        }
+
+        return element;
+    }
+
+    @Override
+    public CharSequence extract(CharSequence content) throws IOException {
+        try {
+            Element element = findElement(content);
+            if (element == null) {
+                return null;
+            }
+
+            return element.getTextContent();
+        } catch (Exception e) {
+            throw new IOException(e.getMessage(), e);
+        }
+    }
+
+    @Override
+    public CharSequence replace(CharSequence content, CharSequence value) throws IOException {
+        try {
+            SOAPMessage message = parse(content);
+            Element element = findElement(message);
+            if (element == null) {
+                return null;
+            }
+
+            element.setTextContent(value.toString());
+            try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+                message.writeTo(baos);
+                return baos.toString("UTF-8");
+            }
+        } catch (Exception e) {
+            throw new IOException(e.getMessage(), e);
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/extractor/SoapValueExtractorTest.java	Wed Jun 17 15:26:33 2020 +0200
@@ -0,0 +1,46 @@
+package com.passus.st.extractor;
+
+import org.testng.annotations.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class SoapValueExtractorTest {
+
+    public static String soap() {
+        return "<soap:Envelope xmlns:mrns0=\"http://sdp.SOMETHING.com/mapping/TSO\" xmlns:sdp=\"http://sdp.SOMETHING.com.tr/mapping/generated\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
+                + "<soap:Header>"
+                + " <sdp:token>"
+                + "     <sdp:sessionId>1</sdp:sessionId>"
+                + " </sdp:token>"
+                + " <sdp:transaction-list>"
+                + "     <sdp:transaction-id>" + 11 + "</sdp:transaction-id>"
+                + " </sdp:transaction-list>"
+                + "</soap:Header>"
+                + "<soap:Body>"
+                + " <sdp:SendSMSInput>"
+                + "     <sdp:EXPIRY_DATE>date</sdp:EXPIRY_DATE>"
+                + "     <sdp:MESSAGE_CLASS>0</sdp:MESSAGE_CLASS>"
+                + " </sdp:SendSMSInput>"
+                + "</soap:Body>"
+                + "</soap:Envelope>";
+    }
+
+    @Test
+    public void testExtract() throws Exception {
+        String soap = soap();
+
+        SoapValueExtractor extractor = new SoapValueExtractor("SendSMSInput.EXPIRY_DATE");
+        CharSequence value = extractor.extract(soap);
+        assertEquals("date", value);
+    }
+
+    @Test
+    public void testExtractReplace() throws Exception {
+        String soap = soap();
+
+        SoapValueExtractor extractor = new SoapValueExtractor("SendSMSInput.EXPIRY_DATE");
+        CharSequence replaced = extractor.replace(soap, "date2");
+        CharSequence value = extractor.extract(replaced);
+        assertEquals("date2", value);
+    }
+}
\ No newline at end of file