Search in sources :

Example 26 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class StreamSenderTest method testStreamSenderMessageWithDeliveryAnnotations.

@Test
public void testStreamSenderMessageWithDeliveryAnnotations() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.remoteFlow().withLinkCredit(10).queue();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Sender test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort()).openFuture().get();
        // Populate delivery annotations
        final Map<String, Object> deliveryAnnotations = new HashMap<>();
        deliveryAnnotations.put("da1", 1);
        deliveryAnnotations.put("da2", 2);
        deliveryAnnotations.put("da3", 3);
        StreamSender sender = connection.openStreamSender("test-queue");
        StreamSenderMessage message = sender.beginMessage(deliveryAnnotations);
        final byte[] payload = new byte[] { 0, 1, 2, 3, 4, 5 };
        HeaderMatcher headerMatcher = new HeaderMatcher(true);
        headerMatcher.withDurable(true);
        headerMatcher.withPriority((byte) 1);
        headerMatcher.withTtl(65535);
        headerMatcher.withFirstAcquirer(true);
        headerMatcher.withDeliveryCount(2);
        PropertiesMatcher propertiesMatcher = new PropertiesMatcher(true);
        propertiesMatcher.withMessageId("ID:12345");
        propertiesMatcher.withUserId("user".getBytes(StandardCharsets.UTF_8));
        propertiesMatcher.withTo("the-management");
        propertiesMatcher.withSubject("amqp");
        propertiesMatcher.withReplyTo("the-minions");
        propertiesMatcher.withCorrelationId("abc");
        propertiesMatcher.withContentEncoding("application/json");
        propertiesMatcher.withContentType("gzip");
        propertiesMatcher.withAbsoluteExpiryTime(123);
        propertiesMatcher.withCreationTime(1);
        propertiesMatcher.withGroupId("disgruntled");
        propertiesMatcher.withGroupSequence(8192);
        propertiesMatcher.withReplyToGroupId("/dev/null");
        DeliveryAnnotationsMatcher daMatcher = new DeliveryAnnotationsMatcher(true);
        daMatcher.withEntry("da1", Matchers.equalTo(1));
        daMatcher.withEntry("da2", Matchers.equalTo(2));
        daMatcher.withEntry("da3", Matchers.equalTo(3));
        MessageAnnotationsMatcher maMatcher = new MessageAnnotationsMatcher(true);
        maMatcher.withEntry("ma1", Matchers.equalTo(1));
        maMatcher.withEntry("ma2", Matchers.equalTo(2));
        maMatcher.withEntry("ma3", Matchers.equalTo(3));
        ApplicationPropertiesMatcher apMatcher = new ApplicationPropertiesMatcher(true);
        apMatcher.withEntry("ap1", Matchers.equalTo(1));
        apMatcher.withEntry("ap2", Matchers.equalTo(2));
        apMatcher.withEntry("ap3", Matchers.equalTo(3));
        EncodedDataMatcher bodyMatcher = new EncodedDataMatcher(payload);
        TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
        payloadMatcher.setHeadersMatcher(headerMatcher);
        payloadMatcher.setDeliveryAnnotationsMatcher(daMatcher);
        payloadMatcher.setMessageAnnotationsMatcher(maMatcher);
        payloadMatcher.setPropertiesMatcher(propertiesMatcher);
        payloadMatcher.setApplicationPropertiesMatcher(apMatcher);
        payloadMatcher.setMessageContentMatcher(bodyMatcher);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withPayload(payloadMatcher).withMore(false).accept();
        // Populate all Header values
        message.durable(true);
        assertEquals(true, message.durable());
        message.priority((byte) 1);
        assertEquals(1, message.priority());
        message.timeToLive(65535);
        assertEquals(65535, message.timeToLive());
        message.firstAcquirer(true);
        assertTrue(message.firstAcquirer());
        message.deliveryCount(2);
        assertEquals(2, message.deliveryCount());
        // Populate message annotations
        assertFalse(message.hasAnnotations());
        assertFalse(message.hasAnnotation("ma1"));
        message.annotation("ma1", 1);
        assertTrue(message.hasAnnotation("ma1"));
        assertEquals(1, message.annotation("ma1"));
        message.annotation("ma2", 2);
        assertEquals(2, message.annotation("ma2"));
        message.annotation("ma3", 3);
        assertEquals(3, message.annotation("ma3"));
        assertTrue(message.hasAnnotations());
        // Populate all Properties values
        message.messageId("ID:12345");
        assertEquals("ID:12345", message.messageId());
        message.userId("user".getBytes(StandardCharsets.UTF_8));
        assertArrayEquals("user".getBytes(StandardCharsets.UTF_8), message.userId());
        message.to("the-management");
        assertEquals("the-management", message.to());
        message.subject("amqp");
        assertEquals("amqp", message.subject());
        message.replyTo("the-minions");
        assertEquals("the-minions", message.replyTo());
        message.correlationId("abc");
        assertEquals("abc", message.correlationId());
        message.contentEncoding("application/json");
        assertEquals("application/json", message.contentEncoding());
        message.contentType("gzip");
        assertEquals("gzip", message.contentType());
        message.absoluteExpiryTime(123);
        assertEquals(123, message.absoluteExpiryTime());
        message.creationTime(1);
        assertEquals(1, message.creationTime());
        message.groupId("disgruntled");
        assertEquals("disgruntled", message.groupId());
        message.groupSequence(8192);
        assertEquals(8192, message.groupSequence());
        message.replyToGroupId("/dev/null");
        assertEquals("/dev/null", message.replyToGroupId());
        // Populate message application properties
        assertFalse(message.hasProperties());
        assertFalse(message.hasProperty("ma1"));
        message.property("ap1", 1);
        assertEquals(1, message.property("ap1"));
        assertTrue(message.hasProperty("ap1"));
        message.property("ap2", 2);
        assertEquals(2, message.property("ap2"));
        message.property("ap3", 3);
        assertEquals(3, message.property("ap3"));
        assertTrue(message.hasProperties());
        OutputStream stream = message.body();
        stream.write(payload);
        stream.close();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        assertNotNull(message.tracker());
        Wait.assertTrue(() -> message.tracker().settlementFuture().isDone());
        assertTrue(message.tracker().settlementFuture().get().settled());
        sender.closeAsync().get(10, TimeUnit.SECONDS);
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : HeaderMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.HeaderMatcher) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) DeliveryAnnotationsMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.DeliveryAnnotationsMatcher) MessageAnnotationsMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.MessageAnnotationsMatcher) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Connection(org.apache.qpid.protonj2.client.Connection) EncodedDataMatcher(org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher) TransferPayloadCompositeMatcher(org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher) URI(java.net.URI) ApplicationPropertiesMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.ApplicationPropertiesMatcher) StreamSender(org.apache.qpid.protonj2.client.StreamSender) StreamSenderMessage(org.apache.qpid.protonj2.client.StreamSenderMessage) Client(org.apache.qpid.protonj2.client.Client) PropertiesMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.PropertiesMatcher) ApplicationPropertiesMatcher(org.apache.qpid.protonj2.test.driver.matchers.messaging.ApplicationPropertiesMatcher) Test(org.junit.jupiter.api.Test)

Example 27 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class StreamSenderTest method testOpenStreamSenderWithLinCapabilities.

@Test
public void testOpenStreamSenderWithLinCapabilities() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.SENDER.getValue()).withTarget().withCapabilities("queue").and().respond();
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("StreamSender test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        StreamSenderOptions senderOptions = new StreamSenderOptions();
        senderOptions.targetOptions().capabilities("queue");
        StreamSender sender = connection.openStreamSender("test-queue", senderOptions);
        sender.openFuture().get();
        sender.close();
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) StreamSender(org.apache.qpid.protonj2.client.StreamSender) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) StreamSenderOptions(org.apache.qpid.protonj2.client.StreamSenderOptions) Test(org.junit.jupiter.api.Test)

Example 28 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class TransactionsTest method testExceptionOnCommitWhenCoordinatorRejectsDischarge.

@Test
public void testExceptionOnCommitWhenCoordinatorRejectsDischarge() throws Exception {
    final String errorMessage = "Transaction aborted due to timeout";
    final byte[] txnId1 = new byte[] { 0, 1, 2, 3 };
    final byte[] txnId2 = new byte[] { 1, 1, 2, 3 };
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(4).queue();
        peer.expectDeclare().accept(txnId1);
        peer.expectDischarge().withFail(false).withTxnId(txnId1).reject(TransactionErrors.TRANSACTION_TIMEOUT.toString(), "Transaction aborted due to timeout");
        peer.expectDeclare().accept(txnId2);
        peer.expectDischarge().withFail(true).withTxnId(txnId2).accept();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        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());
        Session session = connection.openSession().openFuture().get();
        session.beginTransaction();
        try {
            session.commitTransaction();
            fail("Commit should have failed after link closed.");
        } catch (ClientTransactionRolledBackException expected) {
            // Expect this to time out.
            String message = expected.getMessage();
            assertTrue(message.contains(errorMessage));
        }
        session.beginTransaction();
        session.rollbackTransaction();
        session.closeAsync();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientTransactionRolledBackException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionRolledBackException) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 29 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class TransactionsTest method testBeginTransactionAndClose.

/**
 * Create a transaction and then close the Session which result in the remote rolling back
 * the transaction by default so the client doesn't manually roll it back itself.
 *
 * @throws Exception
 */
@Test
public void testBeginTransactionAndClose() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectDeclare().accept();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        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());
        Session session = connection.openSession().openFuture().get();
        session.beginTransaction();
        session.closeAsync();
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 30 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class TransactionsTest method testExceptionOnCommitWhenCoordinatorLinkClosedAfterDischargeSent.

@Test
public void testExceptionOnCommitWhenCoordinatorLinkClosedAfterDischargeSent() throws Exception {
    final String errorMessage = "CoordinatorLinkClosed-breadcrumb";
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectDeclare().accept();
        peer.expectDischarge();
        peer.remoteDetach().withClosed(true).withErrorCondition(AmqpError.RESOURCE_DELETED.toString(), errorMessage).queue();
        peer.expectDetach();
        peer.expectCoordinatorAttach().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectDeclare().accept();
        peer.expectDischarge().accept();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        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());
        Session session = connection.openSession().openFuture().get();
        session.beginTransaction();
        try {
            session.commitTransaction();
            fail("Commit should have failed after link closed.");
        } catch (ClientTransactionRolledBackException expected) {
            // Expect this to time out.
            String message = expected.getMessage();
            assertTrue(message.contains(errorMessage));
        }
        session.beginTransaction();
        session.rollbackTransaction();
        session.closeAsync();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientTransactionRolledBackException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionRolledBackException) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Aggregations

Connection (org.apache.qpid.protonj2.client.Connection)368 Client (org.apache.qpid.protonj2.client.Client)367 URI (java.net.URI)354 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)352 Test (org.junit.jupiter.api.Test)250 Session (org.apache.qpid.protonj2.client.Session)166 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)112 Sender (org.apache.qpid.protonj2.client.Sender)89 Receiver (org.apache.qpid.protonj2.client.Receiver)79 ExecutionException (java.util.concurrent.ExecutionException)72 StreamReceiver (org.apache.qpid.protonj2.client.StreamReceiver)65 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)60 StreamSender (org.apache.qpid.protonj2.client.StreamSender)49 StreamDelivery (org.apache.qpid.protonj2.client.StreamDelivery)46 TransferPayloadCompositeMatcher (org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher)44 Tracker (org.apache.qpid.protonj2.client.Tracker)41 StreamSenderMessage (org.apache.qpid.protonj2.client.StreamSenderMessage)38 ReceiverOptions (org.apache.qpid.protonj2.client.ReceiverOptions)34 SenderOptions (org.apache.qpid.protonj2.client.SenderOptions)33 OutputStream (java.io.OutputStream)31