Search in sources :

Example 1 with AmqpValidator

use of org.apache.activemq.transport.amqp.client.AmqpValidator in project activemq-artemis by apache.

the class AmqpSecurityTest method testSendMessageFailsOnAnonymousRelayWhenNotAuthorizedToSendToAddress.

@Test(timeout = 60000)
public void testSendMessageFailsOnAnonymousRelayWhenNotAuthorizedToSendToAddress() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    AmqpClient client = createAmqpClient(guestUser, guestPass);
    client.setValidator(new AmqpValidator() {

        @Override
        public void inspectDeliveryUpdate(Sender sender, Delivery delivery) {
            DeliveryState state = delivery.getRemoteState();
            if (!delivery.remotelySettled()) {
                markAsInvalid("delivery is not remotely settled");
            }
            if (state instanceof Rejected) {
                Rejected rejected = (Rejected) state;
                if (rejected.getError() == null || rejected.getError().getCondition() == null) {
                    markAsInvalid("Delivery should have been Rejected with an error condition");
                } else {
                    ErrorCondition error = rejected.getError();
                    if (!error.getCondition().equals(AmqpError.UNAUTHORIZED_ACCESS)) {
                        markAsInvalid("Should have been tagged with unauthorized access error");
                    }
                }
            } else {
                markAsInvalid("Delivery should have been Rejected");
            }
            latch.countDown();
        }
    });
    AmqpConnection connection = client.connect();
    try {
        AmqpSession session = connection.createSession();
        AmqpSender sender = session.createAnonymousSender();
        AmqpMessage message = new AmqpMessage();
        message.setAddress(getQueueName());
        message.setMessageId("msg" + 1);
        message.setText("Test-Message");
        try {
            sender.send(message);
            fail("Should not be able to send, message should be rejected");
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            sender.close();
        }
        assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));
        connection.getStateInspector().assertValid();
    } finally {
        connection.close();
    }
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) CountDownLatch(java.util.concurrent.CountDownLatch) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) AmqpValidator(org.apache.activemq.transport.amqp.client.AmqpValidator) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) Sender(org.apache.qpid.proton.engine.Sender) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Delivery(org.apache.qpid.proton.engine.Delivery) Test(org.junit.Test)

Example 2 with AmqpValidator

use of org.apache.activemq.transport.amqp.client.AmqpValidator in project activemq-artemis by apache.

the class AmqpSendReceiveTest method testDeliveryDelayOfferedWhenRequested.

@Test(timeout = 60000)
public void testDeliveryDelayOfferedWhenRequested() throws Exception {
    AmqpClient client = createAmqpClient();
    client.setValidator(new AmqpValidator() {

        @Override
        public void inspectOpenedResource(Sender sender) {
            Symbol[] offered = sender.getRemoteOfferedCapabilities();
            if (!contains(offered, AmqpSupport.DELAYED_DELIVERY)) {
                markAsInvalid("Broker did not indicate it support delayed message delivery");
            }
        }
    });
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    AmqpSender sender = session.createSender(getQueueName(), new Symbol[] { AmqpSupport.DELAYED_DELIVERY });
    assertNotNull(sender);
    connection.getStateInspector().assertValid();
    sender.close();
    connection.close();
}
Also used : AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) Sender(org.apache.qpid.proton.engine.Sender) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AmqpValidator(org.apache.activemq.transport.amqp.client.AmqpValidator) Test(org.junit.Test)

Example 3 with AmqpValidator

use of org.apache.activemq.transport.amqp.client.AmqpValidator in project activemq-artemis by apache.

the class AmqpTransactionTest method testUnsettledTXMessageGetTransactedDispostion.

@Test(timeout = 30000)
public void testUnsettledTXMessageGetTransactedDispostion() throws Exception {
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    assertNotNull(session);
    AmqpSender sender = session.createSender(getQueueName());
    AmqpMessage message = new AmqpMessage();
    message.setText("Test-Message");
    sender.send(message);
    AmqpReceiver receiver = session.createReceiver(getQueueName());
    receiver.setStateInspector(new AmqpValidator() {

        @Override
        public void inspectDeliveryUpdate(Sender sender, Delivery delivery) {
            if (delivery.remotelySettled()) {
                LOG.info("Receiver got delivery update for: {}", delivery);
                if (!(delivery.getRemoteState() instanceof TransactionalState)) {
                    markAsInvalid("Transactionally acquire work no tagged as being in a transaction.");
                } else {
                    TransactionalState txState = (TransactionalState) delivery.getRemoteState();
                    if (!(txState.getOutcome() instanceof Accepted)) {
                        markAsInvalid("Transaction state lacks any outcome");
                    } else if (txState.getTxnId() == null) {
                        markAsInvalid("Transaction state lacks any TX Id");
                    }
                }
                if (!(delivery.getLocalState() instanceof TransactionalState)) {
                    markAsInvalid("Transactionally acquire work no tagged as being in a transaction.");
                } else {
                    TransactionalState txState = (TransactionalState) delivery.getLocalState();
                    if (!(txState.getOutcome() instanceof Accepted)) {
                        markAsInvalid("Transaction state lacks any outcome");
                    } else if (txState.getTxnId() == null) {
                        markAsInvalid("Transaction state lacks any TX Id");
                    }
                }
                TransactionalState localTxState = (TransactionalState) delivery.getLocalState();
                TransactionalState remoteTxState = (TransactionalState) delivery.getRemoteState();
                if (!localTxState.getTxnId().equals(remoteTxState)) {
                    markAsInvalid("Message not enrolled in expected transaction");
                }
            }
        }
    });
    session.begin();
    assertTrue(session.isInTransaction());
    receiver.flow(1);
    AmqpMessage received = receiver.receive(2, TimeUnit.SECONDS);
    assertNotNull(received);
    received.accept(false);
    session.commit();
    sender.getStateInspector().assertValid();
    connection.close();
}
Also used : AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) Sender(org.apache.qpid.proton.engine.Sender) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) AmqpReceiver(org.apache.activemq.transport.amqp.client.AmqpReceiver) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) Delivery(org.apache.qpid.proton.engine.Delivery) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) AmqpValidator(org.apache.activemq.transport.amqp.client.AmqpValidator) TransactionalState(org.apache.qpid.proton.amqp.transaction.TransactionalState) Test(org.junit.Test)

Example 4 with AmqpValidator

use of org.apache.activemq.transport.amqp.client.AmqpValidator in project activemq-artemis by apache.

the class AmqpTransactionTest method testSentTransactionalMessageIsSettleWithTransactionalDisposition.

@Test(timeout = 30000)
public void testSentTransactionalMessageIsSettleWithTransactionalDisposition() throws Exception {
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    assertNotNull(session);
    AmqpSender sender = session.createSender(getQueueName());
    sender.setStateInspector(new AmqpValidator() {

        @Override
        public void inspectDeliveryUpdate(Sender sender, Delivery delivery) {
            if (delivery.remotelySettled()) {
                DeliveryState state = delivery.getRemoteState();
                if (state instanceof TransactionalState) {
                    LOG.debug("Remote settled with TX state: {}", state);
                } else {
                    LOG.warn("Remote settled with non-TX state: {}", state);
                    markAsInvalid("Remote did not settled with TransactionState.");
                }
            }
        }
    });
    session.begin();
    assertTrue(session.isInTransaction());
    AmqpMessage message = new AmqpMessage();
    message.setText("Test-Message");
    sender.send(message);
    session.commit();
    sender.getStateInspector().assertValid();
    connection.close();
}
Also used : AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) Sender(org.apache.qpid.proton.engine.Sender) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) Delivery(org.apache.qpid.proton.engine.Delivery) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) AmqpValidator(org.apache.activemq.transport.amqp.client.AmqpValidator) TransactionalState(org.apache.qpid.proton.amqp.transaction.TransactionalState) Test(org.junit.Test)

Example 5 with AmqpValidator

use of org.apache.activemq.transport.amqp.client.AmqpValidator in project activemq-artemis by apache.

the class AmqpInboundConnectionTest method testBrokerContainerId.

@Test(timeout = 60000)
public void testBrokerContainerId() throws Exception {
    AmqpClient client = createAmqpClient();
    assertNotNull(client);
    client.setValidator(new AmqpValidator() {

        @Override
        public void inspectOpenedResource(Connection connection) {
            if (!BROKER_NAME.equals(connection.getRemoteContainer())) {
                markAsInvalid("Broker did not send the expected container ID");
            }
        }
    });
    AmqpConnection connection = addConnection(client.connect());
    try {
        assertNotNull(connection);
        connection.getStateInspector().assertValid();
    } finally {
        connection.close();
    }
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) Connection(org.apache.qpid.proton.engine.Connection) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) AmqpValidator(org.apache.activemq.transport.amqp.client.AmqpValidator) Test(org.junit.Test)

Aggregations

AmqpClient (org.apache.activemq.transport.amqp.client.AmqpClient)21 AmqpConnection (org.apache.activemq.transport.amqp.client.AmqpConnection)21 AmqpValidator (org.apache.activemq.transport.amqp.client.AmqpValidator)21 Test (org.junit.Test)21 AmqpSession (org.apache.activemq.transport.amqp.client.AmqpSession)15 AmqpReceiver (org.apache.activemq.transport.amqp.client.AmqpReceiver)8 Receiver (org.apache.qpid.proton.engine.Receiver)8 Map (java.util.Map)7 AmqpSender (org.apache.activemq.transport.amqp.client.AmqpSender)7 Sender (org.apache.qpid.proton.engine.Sender)7 Connection (org.apache.qpid.proton.engine.Connection)6 HashMap (java.util.HashMap)5 Source (org.apache.qpid.proton.amqp.messaging.Source)5 ErrorCondition (org.apache.qpid.proton.amqp.transport.ErrorCondition)5 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)4 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)4 Delivery (org.apache.qpid.proton.engine.Delivery)4 Symbol (org.apache.qpid.proton.amqp.Symbol)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 DescribedType (org.apache.qpid.proton.amqp.DescribedType)2