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 b45180b2 PROTON-2845 Add API for message sections that uses Matcher
types
b45180b2 is described below
commit b45180b2df666938c0f1136573cbf49769e9a40f
Author: Timothy Bish <[email protected]>
AuthorDate: Mon Aug 26 18:13:17 2024 -0400
PROTON-2845 Add API for message sections that uses Matcher types
Allow message body section to have a matcher used in a simple API for
the message matcher in the TransferMatcher API.
---
.../qpid/protonj2/client/impl/MessageSendTest.java | 2 +-
.../matchers/transport/TransferMessageMatcher.java | 75 ++++++----------------
.../driver/matchers/types/EncodedDataMatcher.java | 20 ++++++
.../protonj2/test/driver/SenderHandlingTest.java | 70 ++++++++++++++++++++
4 files changed, 109 insertions(+), 58 deletions(-)
diff --git
a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/MessageSendTest.java
b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/MessageSendTest.java
index 2a3c8291..717a40cb 100644
---
a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/MessageSendTest.java
+++
b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/MessageSendTest.java
@@ -1361,7 +1361,7 @@ class MessageSendTest extends ImperativeClientTestCase {
session.openReceiver("dummy").openFuture().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
-
peer.expectTransfer().withMessage().withMessageFormat(0).withData(null);
+
peer.expectTransfer().withMessage().withMessageFormat(0).withData((byte[])
null);
peer.expectDetach().respond();
peer.expectClose().respond();
diff --git
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/matchers/transport/TransferMessageMatcher.java
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/matchers/transport/TransferMessageMatcher.java
index 66972c20..24d63289 100644
---
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/matchers/transport/TransferMessageMatcher.java
+++
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/matchers/transport/TransferMessageMatcher.java
@@ -22,6 +22,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
import org.apache.qpid.protonj2.test.driver.codec.primitives.Binary;
import org.apache.qpid.protonj2.test.driver.codec.primitives.Symbol;
@@ -312,75 +313,35 @@ public class TransferMessageMatcher extends
TypeSafeMatcher<ByteBuffer> {
}
public TransferMessageMatcher withSequence(List<?> sequence) {
- final EncodedAmqpSequenceMatcher matcher = new
EncodedAmqpSequenceMatcher(sequence, footersMatcher != null);
-
- if (!bodySectionMatchers.isEmpty()) {
- bodySectionMatchers.get(bodySectionMatchers.size() -
1).setAllowTrailingBytes(true);
- }
-
- bodySectionMatchers.add(matcher);
+ return withBodySection(new EncodedAmqpSequenceMatcher(sequence,
footersMatcher != null));
+ }
- return this;
+ public TransferMessageMatcher withSequence(Matcher<?> sequenceMatcher) {
+ return withBodySection(new EncodedAmqpSequenceMatcher(sequenceMatcher,
footersMatcher != null));
}
public TransferMessageMatcher withData(byte[] payload) {
- final EncodedDataMatcher matcher = new EncodedDataMatcher(payload,
footersMatcher != null);
-
- if (headersMatcher != null) {
- headersMatcher.getInnerMatcher().setAllowTrailingBytes(true);
- }
- if (deliveryAnnotationsMatcher != null) {
-
deliveryAnnotationsMatcher.getInnerMatcher().setAllowTrailingBytes(true);
- }
- if (messageAnnotationsMatcher != null) {
-
messageAnnotationsMatcher.getInnerMatcher().setAllowTrailingBytes(true);
- }
- if (propertiesMatcher != null) {
- propertiesMatcher.getInnerMatcher().setAllowTrailingBytes(true);
- }
- if (applicationPropertiesMatcher != null) {
-
applicationPropertiesMatcher.getInnerMatcher().setAllowTrailingBytes(true);
- }
-
- if (!bodySectionMatchers.isEmpty()) {
- bodySectionMatchers.get(bodySectionMatchers.size() -
1).setAllowTrailingBytes(true);
- }
-
- bodySectionMatchers.add(matcher);
+ return withBodySection(new EncodedDataMatcher(payload, footersMatcher
!= null));
+ }
- return this;
+ public TransferMessageMatcher withData(Matcher<?> payloadMatcher) {
+ return withBodySection(new EncodedDataMatcher(payloadMatcher,
footersMatcher != null));
}
public TransferMessageMatcher withValue(Object value) {
- final EncodedAmqpValueMatcher matcher = new
EncodedAmqpValueMatcher(value, footersMatcher != null);
-
- if (headersMatcher != null) {
- headersMatcher.getInnerMatcher().setAllowTrailingBytes(true);
- }
- if (deliveryAnnotationsMatcher != null) {
-
deliveryAnnotationsMatcher.getInnerMatcher().setAllowTrailingBytes(true);
- }
- if (messageAnnotationsMatcher != null) {
-
messageAnnotationsMatcher.getInnerMatcher().setAllowTrailingBytes(true);
- }
- if (propertiesMatcher != null) {
- propertiesMatcher.getInnerMatcher().setAllowTrailingBytes(true);
- }
- if (applicationPropertiesMatcher != null) {
-
applicationPropertiesMatcher.getInnerMatcher().setAllowTrailingBytes(true);
- }
-
- if (!bodySectionMatchers.isEmpty()) {
- bodySectionMatchers.get(bodySectionMatchers.size() -
1).setAllowTrailingBytes(true);
- }
-
- bodySectionMatchers.add(matcher);
+ return withBodySection(new EncodedAmqpValueMatcher(value,
footersMatcher != null));
+ }
- return this;
+ public TransferMessageMatcher withValue(Matcher<?> valueMatcher) {
+ return withBodySection(new EncodedAmqpValueMatcher(valueMatcher,
footersMatcher != null));
}
public TransferMessageMatcher withValidBodySection() {
- final EncodedAnyBodySectionMatcher matcher = new
EncodedAnyBodySectionMatcher(footersMatcher != null);
+ return withBodySection(new EncodedAnyBodySectionMatcher(footersMatcher
!= null));
+ }
+
+ protected TransferMessageMatcher withBodySection(EncodedBodySectionMatcher
matcher) {
+ Objects.requireNonNull(matcher, "Body section matcher cannot be null");
if (headersMatcher != null) {
headersMatcher.getInnerMatcher().setAllowTrailingBytes(true);
diff --git
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/matchers/types/EncodedDataMatcher.java
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/matchers/types/EncodedDataMatcher.java
index 70ac3b29..5a71a0ef 100644
---
a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/matchers/types/EncodedDataMatcher.java
+++
b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/matchers/types/EncodedDataMatcher.java
@@ -26,6 +26,7 @@ import
org.apache.qpid.protonj2.test.driver.codec.primitives.Binary;
import org.apache.qpid.protonj2.test.driver.codec.primitives.Symbol;
import org.apache.qpid.protonj2.test.driver.codec.primitives.UnsignedLong;
import org.hamcrest.Description;
+import org.hamcrest.Matcher;
public class EncodedDataMatcher extends EncodedAmqpTypeMatcher {
@@ -48,6 +49,14 @@ public class EncodedDataMatcher extends
EncodedAmqpTypeMatcher {
this(expectedValue, false);
}
+ /**
+ * @param expectedValue
+ * the value that is expected to be IN the received {@link Data}
+ */
+ public EncodedDataMatcher(Matcher<?> expectedValue) {
+ this(expectedValue, false);
+ }
+
/**
* @param expectedValue
* the value that is expected to be IN the received {@link Data}
@@ -70,6 +79,17 @@ public class EncodedDataMatcher extends
EncodedAmqpTypeMatcher {
super(DESCRIPTOR_SYMBOL, DESCRIPTOR_CODE, expectedValue,
permitTrailingBytes);
}
+ /**
+ * @param expectedValue
+ * the value that is expected to be IN the received {@link Data}
+ * @param permitTrailingBytes
+ * if it is permitted for bytes to be left in the Binary after
+ * consuming the {@link AmqpValue}
+ */
+ public EncodedDataMatcher(Matcher<?> expectedValue, boolean
permitTrailingBytes) {
+ super(DESCRIPTOR_SYMBOL, DESCRIPTOR_CODE, expectedValue,
permitTrailingBytes);
+ }
+
@Override
protected boolean matchesSafely(ByteBuffer receivedBinary) {
return super.matchesSafely(receivedBinary);
diff --git
a/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/SenderHandlingTest.java
b/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/SenderHandlingTest.java
index 3ea157aa..51ac8019 100644
---
a/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/SenderHandlingTest.java
+++
b/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/SenderHandlingTest.java
@@ -27,6 +27,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.qpid.protonj2.test.driver.codec.transport.AMQPHeader;
import org.apache.qpid.protonj2.test.driver.utils.TestPeerTestsBase;
+import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.slf4j.Logger;
@@ -1058,4 +1059,73 @@ class SenderHandlingTest extends TestPeerTestsBase {
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
+
+ @Test
+ public void
testMatchValueBodySectionMatcherExpectationUsingGivenMatchers() throws
Exception {
+ try (ProtonTestServer peer = new ProtonTestServer();
+ ProtonTestClient client = new ProtonTestClient()) {
+
+ peer.expectAMQPHeader().respondWithAMQPHeader();
+ peer.expectOpen().respond();
+ peer.expectBegin().respond();
+ peer.expectAttach().ofSender().respond().withHandle(42);
+ peer.remoteFlow().withLinkCredit(1).queue();
+ // Script a full message using the inject API
+ peer.expectTransfer().withMessage().withMessageFormat(1)
+
.withProperties().withCorrelationId("test").and()
+
.withDeliveryAnnotations().also()
+
.withApplicationProperties().and()
+ .withMessageAnnotations().also()
+
.withHeader().withDurability(true).and()
+
.withValue(Matchers.any(String.class))
+ .withFooters();
+ peer.expectTransfer().withMessage().withMessageFormat(1)
+
.withProperties().withCorrelationId("test").and()
+
.withDeliveryAnnotations().also()
+
.withApplicationProperties().and()
+ .withMessageAnnotations().also()
+
.withHeader().withDurability(true).and()
+
.withValue(Matchers.containsStringIgnoringCase("1,2,3"))
+ .withFooters();
+ peer.start();
+
+ URI remoteURI = peer.getServerURI();
+
+ LOG.info("Test started, peer listening on: {}", remoteURI);
+
+ client.connect(remoteURI.getHost(), remoteURI.getPort());
+ client.expectAMQPHeader();
+ client.expectOpen();
+ client.expectBegin();
+ client.expectAttach().ofReceiver().withHandle(42);
+ client.expectFlow().withLinkCredit(1).withHandle(42);
+ client.remoteTransfer().withMessage().withMessageFormat(1)
+
.withHeader().withDurability(true).also()
+
.withApplicationProperties().withProperty("ap", "pa").also()
+
.withDeliveryAnnotations().withAnnotation("da", "ad").also()
+
.withProperties().withCorrelationId("test").also()
+
.withMessageAnnotations().withAnnotation("ma", "am").also()
+
.withFooter().withFooter("footer", "1").also()
+ .withBody().withValue("test
1").also()
+ .queue();
+ client.remoteTransfer().withMessage().withMessageFormat(1)
+
.withHeader().withDurability(true).also()
+
.withApplicationProperties().withProperty("ap", "pa").also()
+
.withDeliveryAnnotations().withAnnotation("da", "ad").also()
+
.withProperties().withCorrelationId("test").also()
+
.withMessageAnnotations().withAnnotation("ma", "am").also()
+
.withFooter().withFooter("footer", "1").also()
+ .withBody().withValue("test
1,2,3").also()
+ .queue();
+
+ // Now start and then await the remote grant of credit and out
send of a transfer
+ client.remoteHeader(AMQPHeader.getAMQPHeader()).now();
+ client.remoteOpen().now();
+ client.remoteBegin().now();
+ client.remoteAttach().ofSender().withHandle(2).now();
+
+ client.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]