camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [camel] branch master updated: [CAMEL-11257] Added test for Asynchronous MDN contruction
Date Wed, 06 Jun 2018 06:47:21 GMT
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new f86c496  [CAMEL-11257] Added test for Asynchronous MDN contruction
f86c496 is described below

commit f86c4963673fa20851bf4796424c9f4fea1c43c6
Author: William Collins <punkhornsw@gmail.com>
AuthorDate: Tue Jun 5 11:12:39 2018 -0400

    [CAMEL-11257] Added test for Asynchronous MDN contruction
---
 .../component/as2/api/AS2ServerConnection.java     |  2 +
 .../AS2MessageDispositionNotificationEntity.java   |  3 +-
 .../component/as2/api/util/HttpMessageUtils.java   | 14 ++++--
 .../camel/component/as2/api/AS2MessageTest.java    | 52 +++++++++++++++++++---
 4 files changed, 61 insertions(+), 10 deletions(-)

diff --git a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2ServerConnection.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2ServerConnection.java
index 3b383fe..e9f340d 100644
--- a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2ServerConnection.java
+++ b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2ServerConnection.java
@@ -140,6 +140,8 @@ public class AS2ServerConnection {
                 while (!Thread.interrupted()) {
 
                     this.httpService.handleRequest(this.serverConnection, context);
+                    
+                    // Send asynchronous MDN if any.
 
                 }
             } catch (final ConnectionClosedException ex) {
diff --git a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/AS2MessageDispositionNotificationEntity.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/AS2MessageDispositionNotificationEntity.java
index dd933d0..1f984ba 100644
--- a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/AS2MessageDispositionNotificationEntity.java
+++ b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/AS2MessageDispositionNotificationEntity.java
@@ -219,8 +219,7 @@ public class AS2MessageDispositionNotificationEntity extends MimeEntity
{
             if (dispositionModifier != null) {
                 as2Disposition = as2Disposition + "/" + dispositionModifier.toString();
             }
-            Header as2DispositionField = new BasicHeader(AS2_DISPOSITION,
-                    dispositionMode.toString() + ";" + dispositionType.toString());
+            Header as2DispositionField = new BasicHeader(AS2_DISPOSITION, as2Disposition);
             canonicalOutstream.writeln(as2DispositionField.toString());
 
             if (failureFields != null) {
diff --git a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/HttpMessageUtils.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/HttpMessageUtils.java
index 133de6b..1563c7a 100644
--- a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/HttpMessageUtils.java
+++ b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/HttpMessageUtils.java
@@ -23,6 +23,7 @@ import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpException;
 import org.apache.http.HttpMessage;
+import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;
 import org.apache.http.io.SessionInputBuffer;
 import org.apache.http.util.Args;
@@ -48,12 +49,19 @@ public final class HttpMessageUtils {
         }
     }
 
-    public static <T> T getEntity(HttpMessage request, Class<T> type) {
-        if (request instanceof HttpEntityEnclosingRequest) {
-            HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
+    public static <T> T getEntity(HttpMessage message, Class<T> type) {
+        Args.notNull(message, "message");
+        Args.notNull(type, "type");
+        if (message instanceof HttpEntityEnclosingRequest) {
+            HttpEntity entity = ((HttpEntityEnclosingRequest)message).getEntity();
             if (entity != null && type.isInstance(entity)) {
                 return type.cast(entity);
             }
+        } else if (message instanceof HttpResponse) {
+            HttpEntity entity = ((HttpResponse)message).getEntity();
+            if (entity != null && type.isInstance(entity)) {
+                type.cast(entity);
+            }
         }
         return null;
     }
diff --git a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/AS2MessageTest.java
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/AS2MessageTest.java
index 78ec28a..533018f 100644
--- a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/AS2MessageTest.java
+++ b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/AS2MessageTest.java
@@ -24,9 +24,13 @@ import java.security.Security;
 import java.security.cert.Certificate;
 import java.security.cert.X509Certificate;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
+import org.apache.camel.component.as2.api.entity.AS2DispositionModifier;
 import org.apache.camel.component.as2.api.entity.AS2DispositionType;
+import org.apache.camel.component.as2.api.entity.AS2MessageDispositionNotificationEntity;
 import org.apache.camel.component.as2.api.entity.ApplicationEDIEntity;
 import org.apache.camel.component.as2.api.entity.ApplicationEDIFACTEntity;
 import org.apache.camel.component.as2.api.entity.ApplicationPkcs7SignatureEntity;
@@ -34,8 +38,11 @@ import org.apache.camel.component.as2.api.entity.DispositionMode;
 import org.apache.camel.component.as2.api.entity.DispositionNotificationMultipartReportEntity;
 import org.apache.camel.component.as2.api.entity.MimeEntity;
 import org.apache.camel.component.as2.api.entity.MultipartSignedEntity;
+import org.apache.camel.component.as2.api.entity.TextPlainEntity;
 import org.apache.camel.component.as2.api.util.EntityUtils;
 import org.apache.camel.component.as2.api.util.HttpMessageUtils;
+import org.apache.camel.component.as2.api.util.MicUtils;
+import org.apache.camel.component.as2.api.util.MicUtils.ReceivedContentMic;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpException;
@@ -70,6 +77,7 @@ import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -117,7 +125,9 @@ public class AS2MessageTest {
     private static final String FROM = "mrAS@example.org";
     private static final String CLIENT_FQDN = "client.example.org";
     private static final String SERVER_FQDN = "server.example.org";
+    private static final String REPORTING_UA = "Server Responding with MDN";
     private static final String DISPOSITION_NOTIFICATION_TO = "mrAS@example.org";
+    private static final String DISPOSITION_NOTIFICATION_OPTIONS = "signed-receipt-protocol=optional,pkcs7-signature;
signed-receipt-micalg=optional,sha1";
     private static final String[] SIGNED_RECEIPT_MIC_ALGORITHMS = new String[] {"sha1", "md5"};
 
     private static final HttpDateGenerator DATE_GENERATOR = new HttpDateGenerator();
@@ -407,25 +417,57 @@ public class AS2MessageTest {
         AS2AsynchronousMDNManager mdnManager = new AS2AsynchronousMDNManager(AS2_VERSION,
USER_AGENT, CLIENT_FQDN,
                 certList.toArray(new X509Certificate[0]), signingKP.getPrivate());
 
-        // Create plain edi request message
+        // Create plain edi request message to acknowledge
         ApplicationEDIEntity ediEntity = EntityUtils.createEDIEntity(EDI_MESSAGE,
                 ContentType.create(AS2MediaType.APPLICATION_EDIFACT, AS2Charset.US_ASCII),
null, false);
         HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST",
REQUEST_URI);
         HttpMessageUtils.setHeaderValue(request, AS2Header.AS2_TO, AS2_NAME);
+        String originalMessageId = Util.createMessageId(SERVER_FQDN);
+        HttpMessageUtils.setHeaderValue(request, AS2Header.MESSAGE_ID, originalMessageId);
+        HttpMessageUtils.setHeaderValue(request, AS2Header.DISPOSITION_NOTIFICATION_OPTIONS,
DISPOSITION_NOTIFICATION_OPTIONS);
         EntityUtils.setMessageEntity(request, ediEntity);
 
+        // Create response for MDN creation.
         HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
         String httpdate = DATE_GENERATOR.getCurrentDate();
         response.setHeader(HTTP.DATE_HEADER, httpdate);
+        response.setHeader(AS2Header.SERVER, REPORTING_UA);
 
         // Create a receipt for edi message
+        Map<String, String> extensionFields = new HashMap<String, String>();
+        extensionFields.put("Original-Recipient", "rfc822;" + AS2_NAME);
+        AS2DispositionModifier dispositionModifier = AS2DispositionModifier.createWarning("AS2
is cool!");
+        String[] failureFields = new String[] {"failure-field-1" };
+        String[] errorFields = new String[] {"error-field-1"};
+        String[] warningFields = new String[] {"warning-field-1"};
         DispositionNotificationMultipartReportEntity mdn = new DispositionNotificationMultipartReportEntity(request,
-                response, DispositionMode.AUTOMATIC_ACTION_MDN_SENT_AUTOMATICALLY, AS2DispositionType.PROCESSED,
null,
-                null, null, null, null, null, "boundary", true);
+                response, DispositionMode.AUTOMATIC_ACTION_MDN_SENT_AUTOMATICALLY, AS2DispositionType.PROCESSED,
dispositionModifier,
+                failureFields, errorFields, warningFields, 
+                extensionFields, null, "boundary", true);
 
         // Send MDN
-        mdnManager.send(mdn, TARGET_HOST, TARGET_PORT, REQUEST_URI, SUBJECT, FROM, AS2_NAME,
AS2_NAME);
-
+        HttpCoreContext httpContext = mdnManager.send(mdn, TARGET_HOST, TARGET_PORT, REQUEST_URI,
SUBJECT, FROM, AS2_NAME, AS2_NAME);
+        HttpRequest mndRequest = httpContext.getRequest();
+        DispositionNotificationMultipartReportEntity reportEntity = HttpMessageUtils.getEntity(mndRequest,
DispositionNotificationMultipartReportEntity.class);
+        assertNotNull("Request does not contain resport", reportEntity);
+        assertEquals("Report entity contains invalid number of parts", 2, reportEntity.getPartCount());
+        assertTrue("Report first part is not text entity", reportEntity.getPart(0) instanceof
TextPlainEntity);
+        assertTrue("Report second part is not MDN entity", reportEntity.getPart(1) instanceof
AS2MessageDispositionNotificationEntity);
+        AS2MessageDispositionNotificationEntity mdnEntity = (AS2MessageDispositionNotificationEntity)
reportEntity.getPart(1);
+        assertEquals("Unexpected value for Reporting UA", REPORTING_UA, mdnEntity.getReportingUA());
+        assertEquals("Unexpected value for Final Recipient", AS2_NAME, mdnEntity.getFinalRecipient());
+        assertEquals("Unexpected value for Original Message ID", originalMessageId, mdnEntity.getOriginalMessageId());
+        assertEquals("Unexpected value for Disposition Mode", DispositionMode.AUTOMATIC_ACTION_MDN_SENT_AUTOMATICALLY,
mdnEntity.getDispositionMode());
+        assertEquals("Unexpected value for Disposition Type", AS2DispositionType.PROCESSED,
mdnEntity.getDispositionType());
+        assertEquals("Unexpected value for Disposition Modifier", dispositionModifier, mdnEntity.getDispositionModifier());
+        assertArrayEquals("Unexpected value for Failure Fields", failureFields, mdnEntity.getFailureFields());
+        assertArrayEquals("Unexpected value for Error Fields", errorFields, mdnEntity.getErrorFields());
+        assertArrayEquals("Unexpected value for Warning Fields", warningFields, mdnEntity.getWarningFields());
+        assertEquals("Unexpected value for Extension Fields", extensionFields, mdnEntity.getExtensionFields());
+        ReceivedContentMic expectedMic = MicUtils.createReceivedContentMic(request);
+        ReceivedContentMic mdnMic = mdnEntity.getReceivedContentMic();
+        assertEquals("Unexpected value for Recieved Content Mic", expectedMic.getEncodedMessageDigest(),
mdnMic.getEncodedMessageDigest());
+        LOG.debug(Util.printMessage(mndRequest));
     }
     
 }

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.

Mime
View raw message