Skip to content

Commit

Permalink
PROTON-2802 Make scripting message payload of a transfer simpler
Browse files Browse the repository at this point in the history
Provide a simpler API for scripting the expected message payload that
will accompany an incoming transfer and provide some updates to match
that API on the remote inject of transfer with message payloads.
  • Loading branch information
tabish121 committed Mar 6, 2024
1 parent e77fd98 commit cf36674
Show file tree
Hide file tree
Showing 8 changed files with 1,264 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,16 @@ public void testSendMessageWithHeaderValuesPopulated() throws Exception {
// Gates send on remote flow having been sent and received
session.openReceiver("dummy").openFuture().get();

HeaderMatcher headerMatcher = new HeaderMatcher(true);
headerMatcher.withDurable(true);
headerMatcher.withPriority((byte) 1);
headerMatcher.withTtl(65535);
headerMatcher.withFirstAcquirer(true);
headerMatcher.withDeliveryCount(2);
EncodedAmqpValueMatcher bodyMatcher = new EncodedAmqpValueMatcher("Hello World");
TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
payloadMatcher.setHeadersMatcher(headerMatcher);
payloadMatcher.setMessageContentMatcher(bodyMatcher);

peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectTransfer().withMessageFormat(0).withPayload(payloadMatcher).accept();
peer.expectTransfer().withMessage().withMessageFormat(0)
.withHeader()
.withDurability(true)
.withPriority((byte) 1)
.withTimeToLive(65535)
.withFirstAcquirer(true)
.withDeliveryCount(2)
.also()
.withValue("Hello World");
peer.expectDetach().respond();
peer.expectClose().respond();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ public FooterBuilder withFooter() {
return new FooterBuilder();
}

public MessageBuilder withMessage() {
return new MessageBuilder();
}

private Header getOrCreateHeader() {
if (header == null) {
header = new Header();
Expand Down Expand Up @@ -479,6 +483,11 @@ public PropertiesBuilder withReplyToGroupId(String value) {

public final class ApplicationPropertiesBuilder extends SectionBuilder {

public ApplicationPropertiesBuilder withProperty(String key, Object value) {
getOrCreateApplicationProperties().setApplicationProperty(key, value);
return this;
}

public ApplicationPropertiesBuilder withApplicationProperty(String key, Object value) {
getOrCreateApplicationProperties().setApplicationProperty(key, value);
return this;
Expand Down Expand Up @@ -540,7 +549,12 @@ public BodySectionBuilder withDescribed(DescribedType described) {

public final class FooterBuilder extends SectionBuilder {

public FooterBuilder withFooter(Object key, Object value) {
public FooterBuilder withFooter(String key, Object value) {
getOrCreateFooter().setFooterProperty(Symbol.valueOf(key), value);
return this;
}

public FooterBuilder withFooter(Symbol key, Object value) {
getOrCreateFooter().setFooterProperty(key, value);
return this;
}
Expand Down Expand Up @@ -666,6 +680,42 @@ public TransactionalStateBuilder withModified(boolean failed, boolean undelivera
}
}

public final class MessageBuilder extends SectionBuilder {

public MessageBuilder withMessageFormat(int format) {
TransferInjectAction.this.withMessageFormat(format);
return this;
}

public HeaderBuilder withHeader() {
return TransferInjectAction.this.withHeader();
}

public DeliveryAnnotationsBuilder withDeliveryAnnotations() {
return TransferInjectAction.this.withDeliveryAnnotations();
}

public MessageAnnotationsBuilder withMessageAnnotations() {
return TransferInjectAction.this.withMessageAnnotations();
}

public PropertiesBuilder withProperties() {
return TransferInjectAction.this.withProperties();
}

public ApplicationPropertiesBuilder withApplicationProperties() {
return TransferInjectAction.this.withApplicationProperties();
}

public BodySectionBuilder withBody() {
return TransferInjectAction.this.withBody();
}

public FooterBuilder withFooter() {
return TransferInjectAction.this.withFooter();
}
}

private static byte[] generateUniqueDeliveryTag() {
final byte[] tag = new byte[Long.BYTES + Long.BYTES];
final UUID uuid = UUID.randomUUID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.qpid.protonj2.test.driver.codec.transport.Transfer;
import org.apache.qpid.protonj2.test.driver.matchers.transactions.TransactionalStateMatcher;
import org.apache.qpid.protonj2.test.driver.matchers.transport.TransferMatcher;
import org.apache.qpid.protonj2.test.driver.matchers.transport.TransferMessageMatcher;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

Expand Down Expand Up @@ -285,6 +286,10 @@ public TransferExpectation withPayload(byte[] buffer) {
return this;
}

public TransferMessageMatcher withMessage() {
return (TransferMessageMatcher) (this.payloadMatcher = new TransferMessageMatcher(this));
}

//----- Matcher based with methods for more complex validation

public TransferExpectation withHandle(Matcher<?> m) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public abstract class AbstractMessageSectionMatcher<T extends AbstractMessageSec
private final Map<Object, Matcher<?>> fieldMatchers;
private Map<Object, Object> receivedFields;

private final boolean allowTrailingBytes;
private boolean allowTrailingBytes;

protected AbstractMessageSectionMatcher(UnsignedLong numericDescriptor, Symbol symbolicDescriptor, Map<Object, Matcher<?>> fieldMatchers, boolean expectTrailingBytes) {
this.numericDescriptor = numericDescriptor;
Expand All @@ -50,6 +50,14 @@ protected AbstractMessageSectionMatcher(UnsignedLong numericDescriptor, Symbol s
this.allowTrailingBytes = expectTrailingBytes;
}

public void setAllowTrailingBytes(boolean allowTrailingBytes) {
this.allowTrailingBytes = allowTrailingBytes;
}

public boolean isAllowTrailngBytes() {
return allowTrailingBytes;
}

protected Map<Object, Matcher<?>> getMatchers() {
return fieldMatchers;
}
Expand Down
Loading

0 comments on commit cf36674

Please sign in to comment.