This is an automated email from the ASF dual-hosted git repository.

tabish pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-protonj2.git


The following commit(s) were added to refs/heads/main by this push:
     new 240bffde PROTON-2698 Add some additional API for checking on single 
values
240bffde is described below

commit 240bffdefb0299f76ab1dd95ba3b798d45eda17b
Author: Timothy Bish <tabish...@gmail.com>
AuthorDate: Fri Apr 7 14:45:13 2023 -0400

    PROTON-2698 Add some additional API for checking on single values
    
    Allow a test to check that a single expected value exists in what could
    be a larger array of values such as offered or desired capabilities and
    in sasl mechanisms offered.  Also fix an issue where connected event on
    the peer client implementation was not getting triggered correctly in
    all cases.
---
 .../qpid/protonj2/test/driver/ScriptWriter.java    | 32 +++++++++++++++------
 .../driver/expectations/AttachExpectation.java     | 17 +++++++++++
 .../test/driver/expectations/BeginExpectation.java | 17 +++++++++++
 .../test/driver/expectations/OpenExpectation.java  | 33 ++++++++++++++++++++++
 .../expectations/SaslMechanismsExpectation.java    |  9 ++++++
 .../test/driver/netty/netty4/Netty4Client.java     |  8 ++----
 .../test/driver/netty/netty5/Netty5Client.java     |  7 +----
 .../protonj2/test/driver/ProtonTestClientTest.java | 28 ++++++++++++++++++
 8 files changed, 131 insertions(+), 20 deletions(-)

diff --git 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/ScriptWriter.java
 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/ScriptWriter.java
index bfb593a0..85281ec1 100644
--- 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/ScriptWriter.java
+++ 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/ScriptWriter.java
@@ -238,6 +238,14 @@ public abstract class ScriptWriter {
         return new AMQPHeaderInjectAction(getDriver(), header);
     }
 
+    public AMQPHeaderInjectAction remoteAMQPHeader() {
+        return new AMQPHeaderInjectAction(getDriver(), 
AMQPHeader.getAMQPHeader());
+    }
+
+    public AMQPHeaderInjectAction remoteSASLHeader() {
+        return new AMQPHeaderInjectAction(getDriver(), 
AMQPHeader.getSASLHeader());
+    }
+
     public OpenInjectAction remoteOpen() {
         return new OpenInjectAction(getDriver());
     }
@@ -320,7 +328,8 @@ public abstract class ScriptWriter {
 
     /**
      * Creates all the scripted elements needed for a successful SASL Anonymous
-     * connection.
+     * connection. This is generally used with a server type peer which will be
+     * accepting client connections.
      * <p>
      * For this exchange the SASL header is expected which is responded to 
with the
      * corresponding SASL header and an immediate SASL mechanisms frame that 
only
@@ -337,7 +346,8 @@ public abstract class ScriptWriter {
      * Creates all the scripted elements needed for a successful SASL Anonymous
      * connection. The provided set of mechanisms must contain the anonymous 
SASL
      * mechanism or an exception is thrown as otherwise the premise of this 
test
-     * method could not be met.
+     * method could not be met. This is generally used with a server type peer
+     * which will be accepting client connections.
      * <p>
      * For this exchange the SASL header is expected which is responded to 
with the
      * corresponding SASL header and an immediate SASL mechanisms frame that 
only
@@ -363,7 +373,8 @@ public abstract class ScriptWriter {
 
     /**
      * Creates all the scripted elements needed for a successful SASL Plain
-     * connection.
+     * connection. This is generally used with a server type peer which will be
+     * accepting client connections.
      * <p>
      * For this exchange the SASL header is expected which is responded to 
with the
      * corresponding SASL header and an immediate SASL mechanisms frame that 
only
@@ -385,7 +396,8 @@ public abstract class ScriptWriter {
      * Creates all the scripted elements needed for a successful SASL Plain
      * connection. The provided set of mechanisms must contain the plain SASL
      * mechanism or an exception is thrown as otherwise the premise of this 
test
-     * method could not be met.
+     * method could not be met. This is generally used with a server type peer
+     * which will be accepting client connections.
      * <p>
      * For this exchange the SASL header is expected which is responded to 
with the
      * corresponding SASL header and an immediate SASL mechanisms frame that 
only
@@ -415,7 +427,8 @@ public abstract class ScriptWriter {
 
     /**
      * Creates all the scripted elements needed for a successful SASL XOAUTH2
-     * connection.
+     * connection. This is generally used with a server type peer which will be
+     * accepting client connections.
      * <p>
      * For this exchange the SASL header is expected which is responded to 
with the
      * corresponding SASL header and an immediate SASL mechanisms frame that 
only
@@ -439,7 +452,8 @@ public abstract class ScriptWriter {
 
     /**
      * Creates all the scripted elements needed for a failed SASL Plain
-     * connection.
+     * connection. This is generally used with a server type peer which will be
+     * accepting client connections.
      * <p>
      * For this exchange the SASL header is expected which is responded to 
with the
      * corresponding SASL header and an immediate SASL mechanisms frame that 
only
@@ -456,7 +470,8 @@ public abstract class ScriptWriter {
 
     /**
      * Creates all the scripted elements needed for a failed SASL Plain
-     * connection.
+     * connection. This is generally used with a server type peer which will be
+     * accepting client connections.
      * <p>
      * For this exchange the SASL header is expected which is responded to 
with the
      * corresponding SASL header and an immediate SASL mechanisms frame that 
only
@@ -487,7 +502,8 @@ public abstract class ScriptWriter {
 
     /**
      * Creates all the scripted elements needed for a successful SASL EXTERNAL
-     * connection.
+     * connection. This is generally used with a server type peer which will be
+     * accepting client connections.
      * <p>
      * For this exchange the SASL header is expected which is responded to 
with the
      * corresponding SASL header and an immediate SASL mechanisms frame that 
only
diff --git 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/AttachExpectation.java
 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/AttachExpectation.java
index 3f1be105..6038afe2 100644
--- 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/AttachExpectation.java
+++ 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/AttachExpectation.java
@@ -20,6 +20,7 @@ import static org.hamcrest.CoreMatchers.anyOf;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.collection.ArrayMatching.hasItemInArray;
 
 import java.nio.ByteBuffer;
 import java.util.Map;
@@ -348,6 +349,14 @@ public class AttachExpectation extends 
AbstractExpectation<Attach> {
         return 
withOfferedCapabilities(equalTo(TypeMapper.toSymbolArray(offeredCapabilities)));
     }
 
+    public AttachExpectation withOfferedCapability(Symbol offeredCapability) {
+        return withOfferedCapabilities(hasItemInArray(offeredCapability));
+    }
+
+    public AttachExpectation withOfferedCapability(String offeredCapability) {
+        return 
withOfferedCapabilities(hasItemInArray(Symbol.valueOf(offeredCapability)));
+    }
+
     public AttachExpectation withDesiredCapabilities(Symbol... 
desiredCapabilities) {
         return withDesiredCapabilities(equalTo(desiredCapabilities));
     }
@@ -356,6 +365,14 @@ public class AttachExpectation extends 
AbstractExpectation<Attach> {
         return 
withDesiredCapabilities(equalTo(TypeMapper.toSymbolArray(desiredCapabilities)));
     }
 
+    public AttachExpectation withDesiredCapability(Symbol desiredCapability) {
+        return withDesiredCapabilities(hasItemInArray(desiredCapability));
+    }
+
+    public AttachExpectation withDesiredCapability(String desiredCapability) {
+        return 
withDesiredCapabilities(hasItemInArray(Symbol.valueOf(desiredCapability)));
+    }
+
     public AttachExpectation withPropertiesMap(Map<Symbol, Object> properties) 
{
         return withProperties(equalTo(properties));
     }
diff --git 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/BeginExpectation.java
 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/BeginExpectation.java
index ef7e0c9b..620cf643 100644
--- 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/BeginExpectation.java
+++ 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/BeginExpectation.java
@@ -20,6 +20,7 @@ import static org.hamcrest.CoreMatchers.anyOf;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.collection.ArrayMatching.hasItemInArray;
 
 import java.nio.ByteBuffer;
 import java.util.Map;
@@ -166,6 +167,14 @@ public class BeginExpectation extends 
AbstractExpectation<Begin> {
         return withOfferedCapabilities(equalTo(offeredCapabilities));
     }
 
+    public BeginExpectation withOfferedCapability(Symbol offeredCapability) {
+        return withOfferedCapabilities(hasItemInArray(offeredCapability));
+    }
+
+    public BeginExpectation withOfferedCapability(String offeredCapability) {
+        return 
withOfferedCapabilities(hasItemInArray(Symbol.valueOf(offeredCapability)));
+    }
+
     public BeginExpectation withDesiredCapabilities(String... 
desiredCapabilities) {
         return 
withDesiredCapabilities(equalTo(TypeMapper.toSymbolArray(desiredCapabilities)));
     }
@@ -174,6 +183,14 @@ public class BeginExpectation extends 
AbstractExpectation<Begin> {
         return withDesiredCapabilities(equalTo(desiredCapabilities));
     }
 
+    public BeginExpectation withDesiredCapability(Symbol desiredCapability) {
+        return withDesiredCapabilities(hasItemInArray(desiredCapability));
+    }
+
+    public BeginExpectation withDesiredCapability(String desiredCapability) {
+        return 
withDesiredCapabilities(hasItemInArray(Symbol.valueOf(desiredCapability)));
+    }
+
     public BeginExpectation withPropertiesMap(Map<Symbol, Object> properties) {
         return withProperties(equalTo(properties));
     }
diff --git 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/OpenExpectation.java
 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/OpenExpectation.java
index afdda58b..ffac3cb6 100644
--- 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/OpenExpectation.java
+++ 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/OpenExpectation.java
@@ -18,6 +18,7 @@ package org.apache.qpid.protonj2.test.driver.expectations;
 
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.collection.ArrayMatching.hasItemInArray;
 
 import java.nio.ByteBuffer;
 import java.util.Map;
@@ -166,6 +167,14 @@ public class OpenExpectation extends 
AbstractExpectation<Open> {
         return withOutgoingLocales(equalTo(outgoingLocales));
     }
 
+    public OpenExpectation withOutgoingLocale(String outgoingLocale) {
+        return 
withOutgoingLocales(hasItemInArray(Symbol.valueOf(outgoingLocale)));
+    }
+
+    public OpenExpectation withOutgoingLocale(Symbol outgoingLocale) {
+        return withOutgoingLocales(hasItemInArray(outgoingLocale));
+    }
+
     public OpenExpectation withIncomingLocales(String... incomingLocales) {
         return 
withIncomingLocales(equalTo(TypeMapper.toSymbolArray(incomingLocales)));
     }
@@ -174,6 +183,14 @@ public class OpenExpectation extends 
AbstractExpectation<Open> {
         return withIncomingLocales(equalTo(incomingLocales));
     }
 
+    public OpenExpectation withIncomingLocale(String incomingLocale) {
+        return 
withIncomingLocales(hasItemInArray(Symbol.valueOf(incomingLocale)));
+    }
+
+    public OpenExpectation withIncomingLocale(Symbol incomingLocale) {
+        return withIncomingLocales(hasItemInArray(incomingLocale));
+    }
+
     public OpenExpectation withOfferedCapabilities(String... 
offeredCapabilities) {
         return 
withOfferedCapabilities(equalTo(TypeMapper.toSymbolArray(offeredCapabilities)));
     }
@@ -182,6 +199,14 @@ public class OpenExpectation extends 
AbstractExpectation<Open> {
         return withOfferedCapabilities(equalTo(offeredCapabilities));
     }
 
+    public OpenExpectation withOfferedCapability(Symbol offeredCapability) {
+        return withOfferedCapabilities(hasItemInArray(offeredCapability));
+    }
+
+    public OpenExpectation withOfferedCapability(String offeredCapability) {
+        return 
withOfferedCapabilities(hasItemInArray(Symbol.valueOf(offeredCapability)));
+    }
+
     public OpenExpectation withDesiredCapabilities(String... 
desiredCapabilities) {
         return 
withDesiredCapabilities(equalTo(TypeMapper.toSymbolArray(desiredCapabilities)));
     }
@@ -190,6 +215,14 @@ public class OpenExpectation extends 
AbstractExpectation<Open> {
         return withDesiredCapabilities(equalTo(desiredCapabilities));
     }
 
+    public OpenExpectation withDesiredCapability(Symbol desiredCapability) {
+        return withDesiredCapabilities(hasItemInArray(desiredCapability));
+    }
+
+    public OpenExpectation withDesiredCapability(String desiredCapability) {
+        return 
withDesiredCapabilities(hasItemInArray(Symbol.valueOf(desiredCapability)));
+    }
+
     public OpenExpectation withPropertiesMap(Map<Symbol, Object> properties) {
         return withProperties(equalTo(properties));
     }
diff --git 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/SaslMechanismsExpectation.java
 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/SaslMechanismsExpectation.java
index 0065d9b3..5848d549 100644
--- 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/SaslMechanismsExpectation.java
+++ 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/expectations/SaslMechanismsExpectation.java
@@ -17,6 +17,7 @@
 package org.apache.qpid.protonj2.test.driver.expectations;
 
 import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.collection.ArrayMatching.hasItemInArray;
 
 import org.apache.qpid.protonj2.test.driver.AMQPTestDriver;
 import org.apache.qpid.protonj2.test.driver.codec.ListDescribedType;
@@ -47,6 +48,14 @@ public class SaslMechanismsExpectation extends 
AbstractExpectation<SaslMechanism
         return withSaslServerMechanisms(equalTo(mechanisms));
     }
 
+    public SaslMechanismsExpectation withSaslServerMechanism(String 
mechanisms) {
+        return 
withSaslServerMechanisms(hasItemInArray(Symbol.valueOf(mechanisms)));
+    }
+
+    public SaslMechanismsExpectation withSaslServerMechanism(Symbol 
mechanisms) {
+        return withSaslServerMechanisms(hasItemInArray(mechanisms));
+    }
+
     //----- Matcher based with methods for more complex validation
 
     public SaslMechanismsExpectation withSaslServerMechanisms(Matcher<?> m) {
diff --git 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/netty/netty4/Netty4Client.java
 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/netty/netty4/Netty4Client.java
index 30a0207e..1db99dc2 100644
--- 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/netty/netty4/Netty4Client.java
+++ 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/netty/netty4/Netty4Client.java
@@ -266,6 +266,7 @@ public final class Netty4Client implements NettyClient {
             if (!isSecure()) {
                 if (!options.isUseWebSockets()) {
                     handleConnected(context.channel());
+                    context.fireChannelActive();
                 }
             } else {
                 SslHandler sslHandler = 
context.pipeline().get(SslHandler.class);
@@ -385,12 +386,6 @@ public final class Netty4Client implements NettyClient {
     protected ChannelHandler getClientHandler() {
         return new SimpleChannelInboundHandler<ByteBuf>() {
 
-            @Override
-            public void channelActive(ChannelHandlerContext ctx) throws 
Exception {
-                connectedRunnable.run();
-                ctx.fireChannelActive();
-            }
-
             @Override
             protected void channelRead0(ChannelHandlerContext ctx, ByteBuf 
input) throws Exception {
                 LOG.trace("AMQP Test Client Channel read: {}", input);
@@ -482,6 +477,7 @@ public final class Netty4Client implements NettyClient {
         channel = connectedChannel;
         connected.set(true);
         connectedLatch.countDown();
+        connectedRunnable.run();
     }
 
     protected void handleTransportFailure(Channel failedChannel, Throwable 
cause) {
diff --git 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/netty/netty5/Netty5Client.java
 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/netty/netty5/Netty5Client.java
index 61134119..bc8a0925 100644
--- 
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/netty/netty5/Netty5Client.java
+++ 
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/netty/netty5/Netty5Client.java
@@ -385,12 +385,6 @@ public final class Netty5Client implements NettyClient {
     protected ChannelHandler getClientHandler() {
         return new SimpleChannelInboundHandler<Buffer>() {
 
-            @Override
-            public void channelActive(ChannelHandlerContext ctx) throws 
Exception {
-                connectedRunnable.run();
-                ctx.fireChannelActive();
-            }
-
             @Override
             protected void messageReceived(ChannelHandlerContext ctx, Buffer 
input) throws Exception {
                 LOG.trace("AMQP Test Client Channel read: {}", input);
@@ -482,6 +476,7 @@ public final class Netty5Client implements NettyClient {
         channel = connectedChannel;
         connected.set(true);
         connectedLatch.countDown();
+        connectedRunnable.run();
     }
 
     protected void handleTransportFailure(Channel failedChannel, Throwable 
cause) {
diff --git 
a/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/ProtonTestClientTest.java
 
b/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/ProtonTestClientTest.java
index 3027fe91..d15e9396 100644
--- 
a/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/ProtonTestClientTest.java
+++ 
b/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/ProtonTestClientTest.java
@@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import java.net.URI;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.qpid.protonj2.test.driver.codec.security.SaslCode;
 import org.apache.qpid.protonj2.test.driver.codec.transport.AMQPHeader;
 import org.apache.qpid.protonj2.test.driver.utils.TestPeerTestsBase;
 import org.junit.jupiter.api.Test;
@@ -196,4 +197,31 @@ class ProtonTestClientTest extends TestPeerTestsBase {
             peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
         }
     }
+
+    @Test
+    public void 
testClientPeerCanBeScriptedToConnectAndThenInitiateSASLAnonymousAuthentication()
 throws Exception {
+        try (ProtonTestServer peer = new ProtonTestServer()) {
+            peer.expectSASLAnonymousConnect("PLAIN", "ANONYMOUS");
+            peer.start();
+
+            URI remoteURI = peer.getServerURI();
+
+            ProtonTestClient client = new ProtonTestClient();
+
+            client.remoteSASLHeader().queue();
+            client.expectSASLHeader();
+            client.expectSaslMechanisms().withSaslServerMechanism("ANONYMOUS");
+            client.remoteSaslInit().withMechanism("ANONYMOUS").queue();
+            client.expectSaslOutcome().withCode(SaslCode.OK);
+            client.remoteAMQPHeader().queue();
+            client.connect(remoteURI.getHost(), remoteURI.getPort());
+
+            client.waitForScriptToComplete(5, TimeUnit.SECONDS);
+            client.close();
+
+            LOG.info("Test started, peer listening on: {}", remoteURI);
+
+            peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org
For additional commands, e-mail: commits-h...@qpid.apache.org

Reply via email to