Search in sources :

Example 1 with Target

use of org.apache.qpid.protonj2.test.driver.codec.messaging.Target in project qpid-protonj2 by apache.

the class StreamReceiverTest method tryReadReceiverTarget.

private void tryReadReceiverTarget(boolean attachResponse) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue());
        peer.expectFlow();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        StreamReceiverOptions options = new StreamReceiverOptions().openTimeout(150, TimeUnit.MILLISECONDS);
        StreamReceiver receiver = connection.openStreamReceiver("test-receiver", options);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        if (attachResponse) {
            peer.expectDetach().respond();
            peer.expectEnd().respond();
            peer.respondToLastAttach().later(10);
        } else {
            peer.expectDetach();
            peer.expectEnd();
        }
        if (attachResponse) {
            assertNotNull(receiver.target(), "Remote should have responded with a Target value");
        } else {
            try {
                receiver.target();
                fail("Should failed to get remote source due to no attach response");
            } catch (ClientException ex) {
                LOG.debug("Caught expected exception from blocking call", ex);
            }
        }
        try {
            receiver.closeAsync().get();
        } catch (ExecutionException ex) {
            LOG.debug("Caught unexpected exception from close call", ex);
            fail("Should not fail to close when connection not closed and detach sent");
        }
        peer.expectClose().respond();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) StreamReceiverOptions(org.apache.qpid.protonj2.client.StreamReceiverOptions) Connection(org.apache.qpid.protonj2.client.Connection) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI)

Example 2 with Target

use of org.apache.qpid.protonj2.test.driver.codec.messaging.Target in project qpid-protonj2 by apache.

the class ReceiverTest method tryReadReceiverTarget.

private void tryReadReceiverTarget(boolean attachResponse) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue());
        peer.expectFlow();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        ConnectionOptions options = new ConnectionOptions().openTimeout(100);
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), options);
        connection.openFuture().get();
        Session session = connection.openSession();
        session.openFuture().get();
        Receiver receiver = session.openReceiver("test-receiver");
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        if (attachResponse) {
            peer.expectDetach().respond();
            peer.respondToLastAttach().later(10);
        } else {
            peer.expectDetach();
        }
        if (attachResponse) {
            assertNotNull(receiver.target(), "Remote should have responded with a Target value");
        } else {
            try {
                receiver.target();
                fail("Should failed to get remote source due to no attach response");
            } catch (ClientException ex) {
                LOG.debug("Caught expected exception from blocking call", ex);
            }
        }
        try {
            receiver.closeAsync().get();
        } catch (ExecutionException ex) {
            LOG.debug("Caught unexpected exception from close call", ex);
            fail("Should not fail to close when connection not closed and detach sent");
        }
        peer.expectClose().respond();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) Receiver(org.apache.qpid.protonj2.client.Receiver) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session)

Example 3 with Target

use of org.apache.qpid.protonj2.test.driver.codec.messaging.Target in project qpid-protonj2 by apache.

the class ScriptWriter method respondToLastAttach.

/**
 * Creates a Attach response for the last link Attach that was received and fills in the Attach
 * fields based on values from the remote.  The caller can further customize the Attach that is
 * emitted by using the various with methods to assign values to the fields in the Attach.
 *
 * @return a new {@link AttachInjectAction} that can be queued or sent immediately.
 *
 * @throws IllegalStateException if no Attach has yet been received from the remote.
 */
public AttachInjectAction respondToLastAttach() {
    AttachInjectAction response = new AttachInjectAction(getDriver());
    final SessionTracker session = getDriver().sessions().getLastRemotelyOpenedSession();
    final LinkTracker link = session.getLastRemotelyOpenedLink();
    if (link == null) {
        throw new IllegalStateException("Cannot create response to Attach before one has been received.");
    }
    if (link.isLocallyAttached()) {
        throw new IllegalStateException("Cannot create response to Attach since a local Attach was already sent.");
    }
    // Populate the response using data in the locally opened link, script can override this after return.
    response.onChannel(link.getSession().getLocalChannel());
    response.withName(link.getName());
    response.withRole(link.getRole());
    response.withSndSettleMode(link.getRemoteSenderSettleMode());
    response.withRcvSettleMode(link.getRemoteReceiverSettleMode());
    if (link.getRemoteSource() != null) {
        response.withSource(new Source(link.getRemoteSource()));
        if (Boolean.TRUE.equals(link.getRemoteSource().getDynamic())) {
            response.withSource().withAddress(UUID.randomUUID().toString());
        }
    }
    if (link.getRemoteTarget() != null) {
        response.withTarget(new Target(link.getRemoteTarget()));
        if (Boolean.TRUE.equals(link.getRemoteTarget().getDynamic())) {
            response.withTarget().withAddress(UUID.randomUUID().toString());
        }
    }
    if (link.getRemoteCoordinator() != null) {
        response.withTarget(new Coordinator(link.getRemoteCoordinator()));
    }
    if (response.getPerformative().getInitialDeliveryCount() == null) {
        if (link.isSender()) {
            response.withInitialDeliveryCount(0);
        }
    }
    return response;
}
Also used : Target(org.apache.qpid.protonj2.test.driver.codec.messaging.Target) AttachInjectAction(org.apache.qpid.protonj2.test.driver.actions.AttachInjectAction) Coordinator(org.apache.qpid.protonj2.test.driver.codec.transactions.Coordinator) Source(org.apache.qpid.protonj2.test.driver.codec.messaging.Source)

Example 4 with Target

use of org.apache.qpid.protonj2.test.driver.codec.messaging.Target in project qpid-protonj2 by apache.

the class DetachLastCoordinatorInjectAction method beforeActionPerformed.

@Override
protected void beforeActionPerformed(AMQPTestDriver driver) {
    LinkTracker tracker = driver.sessions().getLastOpenedCoordinator();
    if (tracker == null) {
        throw new AssertionError("Cannot send coordinator detach as scripted, no active coordinator found.");
    }
    onChannel(tracker.getSession().getLocalChannel().intValue());
    if (!tracker.isLocallyAttached()) {
        AttachInjectAction attach = new AttachInjectAction(driver);
        attach.onChannel(onChannel());
        attach.withName(tracker.getName());
        attach.withSource(tracker.getRemoteSource());
        if (tracker.getRemoteTarget() != null) {
            attach.withTarget(tracker.getRemoteTarget());
        } else {
            attach.withTarget(tracker.getRemoteCoordinator());
        }
        if (tracker.isSender()) {
            attach.withRole(Role.SENDER);
            // the action will not override an explicitly null source.
            if (getPerformative().getError() != null) {
                attach.withNullSource();
            }
        } else {
            attach.withRole(Role.RECEIVER);
            // the action will not override an explicitly null target.
            if (getPerformative().getError() != null) {
                if (getPerformative().getError() != null) {
                    attach.withNullTarget();
                }
            }
        }
        attach.perform(driver);
    }
    getPerformative().setHandle(tracker.getHandle());
}
Also used : LinkTracker(org.apache.qpid.protonj2.test.driver.LinkTracker)

Example 5 with Target

use of org.apache.qpid.protonj2.test.driver.codec.messaging.Target in project qpid-protonj2 by apache.

the class AttachExpectation method handleAttach.

// ----- Handle the performative and configure response is told to respond
@Override
public void handleAttach(int frameSize, Attach attach, ByteBuf payload, int channel, AMQPTestDriver context) {
    super.handleAttach(frameSize, attach, payload, channel, context);
    final UnsignedShort remoteChannel = UnsignedShort.valueOf(channel);
    final SessionTracker session = driver.sessions().getSessionFromRemoteChannel(remoteChannel);
    if (session == null) {
        throw new AssertionError(String.format("Received Attach on channel [%s] that has no matching Session for that remote channel. ", remoteChannel));
    }
    final LinkTracker link = session.handleRemoteAttach(attach);
    if (response != null) {
        // to say otherwise by the test.
        if (response.onChannel() == BeginInjectAction.CHANNEL_UNSET) {
            response.onChannel(link.getSession().getLocalChannel());
        }
        // Populate the fields of the response with defaults if non set by the test script
        if (response.getPerformative().getHandle() == null) {
            response.withHandle(session.findFreeLocalHandle());
        }
        if (response.getPerformative().getName() == null) {
            response.withName(attach.getName());
        }
        if (response.getPerformative().getRole() == null) {
            response.withRole(Boolean.TRUE.equals(attach.getRole()) ? Role.SENDER : Role.RECEIVER);
        }
        if (response.getPerformative().getSenderSettleMode() == null) {
            response.withSndSettleMode(SenderSettleMode.valueOf(attach.getSenderSettleMode()));
        }
        if (response.getPerformative().getReceiverSettleMode() == null) {
            response.withRcvSettleMode(ReceiverSettleMode.valueOf(attach.getReceiverSettleMode()));
        }
        if (response.getPerformative().getSource() == null && !response.isNullSourceRequired()) {
            response.withSource(attach.getSource());
            if (attach.getSource() != null && Boolean.TRUE.equals(attach.getSource().getDynamic())) {
                attach.getSource().setAddress(UUID.randomUUID().toString());
            }
        }
        if (rejecting) {
            if (Boolean.FALSE.equals(attach.getRole())) {
                // Sender attach so response should have null target
                response.withNullTarget();
            } else {
                // Receiver attach so response should have null source
                response.withNullSource();
            }
        }
        if (response.getPerformative().getTarget() == null && !response.isNullTargetRequired()) {
            if (attach.getTarget() != null) {
                if (attach.getTarget() instanceof Target) {
                    Target target = (Target) attach.getTarget();
                    response.withTarget(target);
                    if (target != null && Boolean.TRUE.equals(target.getDynamic())) {
                        target.setAddress(UUID.randomUUID().toString());
                    }
                } else {
                    Coordinator coordinator = (Coordinator) attach.getTarget();
                    response.withTarget(coordinator);
                }
            }
        }
        if (response.getPerformative().getInitialDeliveryCount() == null) {
            Role role = Role.valueOf(response.getPerformative().getRole());
            if (role == Role.SENDER) {
                response.withInitialDeliveryCount(0);
            }
        }
    // Other fields are left not set for now unless test script configured
    }
}
Also used : Role(org.apache.qpid.protonj2.test.driver.codec.transport.Role) Target(org.apache.qpid.protonj2.test.driver.codec.messaging.Target) SessionTracker(org.apache.qpid.protonj2.test.driver.SessionTracker) Coordinator(org.apache.qpid.protonj2.test.driver.codec.transactions.Coordinator) LinkTracker(org.apache.qpid.protonj2.test.driver.LinkTracker) UnsignedShort(org.apache.qpid.protonj2.test.driver.codec.primitives.UnsignedShort)

Aggregations

Target (org.apache.qpid.protonj2.types.messaging.Target)24 Test (org.junit.jupiter.api.Test)18 Source (org.apache.qpid.protonj2.types.messaging.Source)11 Symbol (org.apache.qpid.protonj2.types.Symbol)10 ProtonBuffer (org.apache.qpid.protonj2.buffer.ProtonBuffer)8 Connection (org.apache.qpid.protonj2.engine.Connection)8 Engine (org.apache.qpid.protonj2.engine.Engine)8 Session (org.apache.qpid.protonj2.engine.Session)8 ProtonTestConnector (org.apache.qpid.protonj2.test.driver.ProtonTestConnector)8 InputStream (java.io.InputStream)6 Receiver (org.apache.qpid.protonj2.engine.Receiver)6 Coordinator (org.apache.qpid.protonj2.types.transactions.Coordinator)6 URI (java.net.URI)5 HashMap (java.util.HashMap)5 ProtonBufferInputStream (org.apache.qpid.protonj2.buffer.ProtonBufferInputStream)5 Client (org.apache.qpid.protonj2.client.Client)5 Connection (org.apache.qpid.protonj2.client.Connection)5 Sender (org.apache.qpid.protonj2.engine.Sender)4 Target (org.apache.qpid.protonj2.test.driver.codec.messaging.Target)4 ArrayList (java.util.ArrayList)3