Search in sources :

Example 21 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project hono by eclipse.

the class ProtonBasedCommandTest method testFromRoutedCommandMessageSucceeds.

/**
 * Verifies that a command can be created from a valid message representing a routed command
 * message with a <em>via</em> property containing a gateway identifier.
 */
@Test
public void testFromRoutedCommandMessageSucceeds() {
    final String gatewayId = "gw-1";
    final String targetDeviceId = "4711";
    final String replyToId = "the-reply-to-id";
    final String correlationId = "the-correlation-id";
    final Map<String, Object> applicationProperties = new HashMap<>();
    applicationProperties.put("via", gatewayId);
    final Message message = mock(Message.class);
    when(message.getAddress()).thenReturn(String.format("%s/%s/%s", CommandConstants.COMMAND_ENDPOINT, Constants.DEFAULT_TENANT, "4711"));
    when(message.getApplicationProperties()).thenReturn(new ApplicationProperties(applicationProperties));
    when(message.getSubject()).thenReturn("doThis");
    when(message.getCorrelationId()).thenReturn(correlationId);
    when(message.getReplyTo()).thenReturn(String.format("%s/%s/%s/%s", CommandConstants.NORTHBOUND_COMMAND_RESPONSE_ENDPOINT, Constants.DEFAULT_TENANT, "4711", replyToId));
    final ProtonBasedCommand cmd = ProtonBasedCommand.fromRoutedCommandMessage(message);
    assertTrue(cmd.isValid());
    assertThat(cmd.getName()).isEqualTo("doThis");
    assertThat(cmd.getTenant()).isEqualTo(Constants.DEFAULT_TENANT);
    assertThat(cmd.getGatewayOrDeviceId()).isEqualTo(gatewayId);
    assertThat(cmd.getGatewayId()).isEqualTo(gatewayId);
    assertThat(cmd.getDeviceId()).isEqualTo(targetDeviceId);
    assertThat(cmd.getReplyToId()).isEqualTo(String.format("%s/%s", targetDeviceId, replyToId));
    assertThat(cmd.getCorrelationId()).isEqualTo(correlationId);
    assertFalse(cmd.isOneWay());
}
Also used : Message(org.apache.qpid.proton.message.Message) HashMap(java.util.HashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Test(org.junit.jupiter.api.Test)

Example 22 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project hono by eclipse.

the class VertxBasedAmqpProtocolAdapterTest method testUploadCommandResponseWithoutPayloadSucceeds.

/**
 * Verify that the AMQP adapter forwards command responses that do not contain a payload downstream.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUploadCommandResponseWithoutPayloadSucceeds(final VertxTestContext ctx) {
    // GIVEN an AMQP adapter
    givenAnAdapter(properties);
    final CommandResponseSender responseSender = givenACommandResponseSenderForAnyTenant();
    when(responseSender.sendCommandResponse(any(TenantObject.class), any(RegistrationAssertion.class), any(CommandResponse.class), (SpanContext) any())).thenReturn(Future.succeededFuture());
    // which is enabled for the test tenant
    final TenantObject tenantObject = givenAConfiguredTenant(TEST_TENANT_ID, true);
    // WHEN an unauthenticated device publishes a command response
    final String replyToAddress = String.format("%s/%s/%s", getCommandResponseEndpoint(), TEST_TENANT_ID, Commands.getDeviceFacingReplyToId("test-reply-id", TEST_DEVICE, MessagingType.amqp));
    final Map<String, Object> propertyMap = new HashMap<>();
    propertyMap.put(MessageHelper.APP_PROPERTY_STATUS, 200);
    final ApplicationProperties props = new ApplicationProperties(propertyMap);
    final Message message = getFakeMessage(replyToAddress, null);
    message.setCorrelationId("correlation-id");
    message.setApplicationProperties(props);
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    adapter.onMessageReceived(AmqpContext.fromMessage(delivery, message, span, null)).onComplete(ctx.succeeding(ok -> {
        ctx.verify(() -> {
            // THEN the adapter forwards the command response message downstream
            verify(responseSender).sendCommandResponse(eq(tenantObject), any(RegistrationAssertion.class), any(CommandResponse.class), (SpanContext) any());
            // and reports the forwarded message
            verify(metrics).reportCommand(eq(Direction.RESPONSE), eq(TEST_TENANT_ID), eq(tenantObject), eq(ProcessingOutcome.FORWARDED), eq(0), any());
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ProtonConnection(io.vertx.proton.ProtonConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ProcessingOutcome(org.eclipse.hono.service.metric.MetricsTags.ProcessingOutcome) MessagingType(org.eclipse.hono.util.MessagingType) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) CommandContext(org.eclipse.hono.client.command.CommandContext) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) EventConstants(org.eclipse.hono.util.EventConstants) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) Mockito.mock(org.mockito.Mockito.mock) VertxTestContext(io.vertx.junit5.VertxTestContext) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) Target(org.apache.qpid.proton.amqp.messaging.Target) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Commands(org.eclipse.hono.client.command.Commands) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) AsyncResult(io.vertx.core.AsyncResult) CommandConstants(org.eclipse.hono.util.CommandConstants) Direction(org.eclipse.hono.service.metric.MetricsTags.Direction) Vertx(io.vertx.core.Vertx) Mockito.times(org.mockito.Mockito.times) ProtonHelper(io.vertx.proton.ProtonHelper) Released(org.apache.qpid.proton.amqp.messaging.Released) SpanContext(io.opentracing.SpanContext) Mockito.never(org.mockito.Mockito.never) ConnectionAttemptOutcome(org.eclipse.hono.service.metric.MetricsTags.ConnectionAttemptOutcome) ResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ProtonReceiver(io.vertx.proton.ProtonReceiver) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Context(io.vertx.core.Context) ProtonServer(io.vertx.proton.ProtonServer) Timeout(io.vertx.junit5.Timeout) EndpointType(org.eclipse.hono.service.metric.MetricsTags.EndpointType) NotificationType(org.eclipse.hono.notification.NotificationType) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) Binary(org.apache.qpid.proton.amqp.Binary) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) Data(org.apache.qpid.proton.amqp.messaging.Data) MetricsTags(org.eclipse.hono.service.metric.MetricsTags) ProtonQoS(io.vertx.proton.ProtonQoS) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Buffer(io.vertx.core.buffer.Buffer) CommandConsumer(org.eclipse.hono.client.command.CommandConsumer) Mockito.inOrder(org.mockito.Mockito.inOrder) ProtonSender(io.vertx.proton.ProtonSender) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) QoS(org.eclipse.hono.util.QoS) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) ConnectionLimitManager(org.eclipse.hono.adapter.limiting.ConnectionLimitManager) HashMap(java.util.HashMap) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ConnectionEventProducer(org.eclipse.hono.adapter.monitoring.ConnectionEventProducer) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) Constants(org.eclipse.hono.util.Constants) ProtocolAdapterTestSupport(org.eclipse.hono.adapter.test.ProtocolAdapterTestSupport) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) RecordImpl(org.apache.qpid.proton.engine.impl.RecordImpl) Message(org.apache.qpid.proton.message.Message) HttpUtils(org.eclipse.hono.service.http.HttpUtils) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) InOrder(org.mockito.InOrder) Promise(io.vertx.core.Promise) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) CommandResponse(org.eclipse.hono.client.command.CommandResponse) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Source(org.apache.qpid.proton.amqp.transport.Source) Adapter(org.eclipse.hono.util.Adapter) Handler(io.vertx.core.Handler) Record(org.apache.qpid.proton.engine.Record) SpanContext(io.opentracing.SpanContext) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) HashMap(java.util.HashMap) CommandResponse(org.eclipse.hono.client.command.CommandResponse) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) TenantObject(org.eclipse.hono.util.TenantObject) Test(org.junit.jupiter.api.Test)

Example 23 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project hono by eclipse.

the class VertxBasedAmqpProtocolAdapterTest method testMessageLimitExceededForACommandResponseMessage.

/**
 * Verifies that a command response message is rejected due to the limit exceeded.
 *
 * @param ctx The test context to use for running asynchronous tests.
 */
@Test
public void testMessageLimitExceededForACommandResponseMessage(final VertxTestContext ctx) {
    final String replyToAddress = String.format("%s/%s/%s", getCommandResponseEndpoint(), TEST_TENANT_ID, Commands.getDeviceFacingReplyToId("test-reply-id", TEST_DEVICE, MessagingType.amqp));
    final Buffer payload = Buffer.buffer("payload");
    final Message message = getFakeMessage(replyToAddress, payload);
    message.setCorrelationId("correlation-id");
    final Map<String, Object> propertyMap = new HashMap<>();
    propertyMap.put(MessageHelper.APP_PROPERTY_STATUS, 200);
    final ApplicationProperties props = new ApplicationProperties(propertyMap);
    message.setApplicationProperties(props);
    testMessageLimitExceededForADownstreamMessage(ctx, message, s -> {
        assertNoCommandResponseHasBeenSentDownstream();
        verify(metrics).reportCommand(eq(Direction.RESPONSE), eq(TEST_TENANT_ID), any(TenantObject.class), eq(ProcessingOutcome.UNPROCESSABLE), eq(payload.length()), any());
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TenantObject(org.eclipse.hono.util.TenantObject) Message(org.apache.qpid.proton.message.Message) HashMap(java.util.HashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) TenantObject(org.eclipse.hono.util.TenantObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 24 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project azure-iot-sdk-java by Azure.

the class AmqpsTransport method iotHubMessageToProtonMessage.

/**
     * Creates a proton message from the IoTHub message.
     * @param message the IoTHub input message.
     * @return the proton message.
     */
private MessageImpl iotHubMessageToProtonMessage(com.microsoft.azure.sdk.iot.device.Message message) {
    logger.LogInfo("Started converting IoT Hub message into AmpqsMessage, method name is %s ", logger.getMethodName());
    MessageImpl outgoingMessage = (MessageImpl) Proton.message();
    logger.LogInfo("Content of message is %s, method name is %s ", new String(message.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET), logger.getMethodName());
    Properties properties = new Properties();
    if (message.getMessageId() != null) {
        properties.setMessageId(message.getMessageId());
    }
    outgoingMessage.setProperties(properties);
    // Codes_SRS_AMQPSTRANSPORT_15_038: [The function shall add all user properties to the application properties of the Proton message.]
    if (message.getProperties().length > 0) {
        Map<String, String> userProperties = new HashMap<>(message.getProperties().length);
        for (MessageProperty messageProperty : message.getProperties()) {
            if (!MessageProperty.RESERVED_PROPERTY_NAMES.contains(messageProperty.getName())) {
                userProperties.put(messageProperty.getName(), messageProperty.getValue());
            }
        }
        ApplicationProperties applicationProperties = new ApplicationProperties(userProperties);
        outgoingMessage.setApplicationProperties(applicationProperties);
    }
    Binary binary = new Binary(message.getBytes());
    Section section = new Data(binary);
    outgoingMessage.setBody(section);
    logger.LogInfo("Started converting IoT Hub message into AmpqsMessage, method name is %s ", logger.getMethodName());
    return outgoingMessage;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Data(org.apache.qpid.proton.amqp.messaging.Data) Binary(org.apache.qpid.proton.amqp.Binary) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) Section(org.apache.qpid.proton.amqp.messaging.Section)

Example 25 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project activemq-artemis by apache.

the class AMQPMessageTest method testVerySimple.

@Test
public void testVerySimple() {
    MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
    protonMessage.setHeader(new Header());
    Properties properties = new Properties();
    properties.setTo("someNiceLocal");
    protonMessage.setProperties(properties);
    protonMessage.getHeader().setDeliveryCount(new UnsignedInteger(7));
    protonMessage.getHeader().setDurable(Boolean.TRUE);
    protonMessage.setApplicationProperties(new ApplicationProperties(new HashMap()));
    AMQPMessage decoded = encodeAndDecodeMessage(protonMessage);
    assertEquals(7, decoded.getHeader().getDeliveryCount().intValue());
    assertEquals(true, decoded.getHeader().getDurable());
    assertEquals("someNiceLocal", decoded.getAddress());
}
Also used : Header(org.apache.qpid.proton.amqp.messaging.Header) HashMap(java.util.HashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) UnsignedInteger(org.apache.qpid.proton.amqp.UnsignedInteger) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) AMQPMessage(org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage) Test(org.junit.Test)

Aggregations

ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)41 HashMap (java.util.HashMap)25 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)14 Properties (org.apache.qpid.proton.amqp.messaging.Properties)12 AMQPMessage (org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage)11 Message (org.apache.qpid.proton.message.Message)11 Binary (org.apache.qpid.proton.amqp.Binary)10 MessageAnnotations (org.apache.qpid.proton.amqp.messaging.MessageAnnotations)10 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)9 Test (org.junit.Test)9 ICoreMessage (org.apache.activemq.artemis.api.core.ICoreMessage)8 Data (org.apache.qpid.proton.amqp.messaging.Data)8 Symbol (org.apache.qpid.proton.amqp.Symbol)7 Section (org.apache.qpid.proton.amqp.messaging.Section)7 Map (java.util.Map)6 Header (org.apache.qpid.proton.amqp.messaging.Header)6 Test (org.junit.jupiter.api.Test)5 Buffer (io.vertx.core.buffer.Buffer)3 JsonObject (io.vertx.core.json.JsonObject)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3