Search in sources :

Example 26 with Message

use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.

the class MqttDeviceMethodTest method receiveReturnsNullMessageIfTopicWasNotPost.

/*
    Tests_SRS_MqttDeviceMethod_25_027: [**This method shall parse topic to look for Post topic ($iothub/methods/POST/) and throw unsupportedoperation exception other wise.**]**
     */
@Test(expected = UnsupportedOperationException.class)
public void receiveReturnsNullMessageIfTopicWasNotPost() throws IOException {
    //arrange
    String topic = "$iothub/methods/Not_POST/testMethod/?$rid=10";
    byte[] actualPayload = "TestPayload".getBytes();
    ConcurrentSkipListMap<String, byte[]> testAllReceivedMessages = new ConcurrentSkipListMap<>();
    testAllReceivedMessages.put(topic, actualPayload);
    Deencapsulation.setField(mockedMqtt, "allReceivedMessages", testAllReceivedMessages);
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    testMethod.start();
    //act
    Message testMessage = testMethod.receive();
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Message(com.microsoft.azure.sdk.iot.device.Message) DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Test(org.junit.Test)

Example 27 with Message

use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.

the class MqttIotHubConnection method receiveMessage.

/**
     * Receives a message, if one exists.
     *
     * @return the message received, or null if none exists.
     *
     * @throws IllegalStateException if the connection state is currently closed.
     * @throws IOException if receiving on any of messaging clients fail.
     */
public Message receiveMessage() throws IllegalStateException, IOException {
    // the function shall throw an IllegalStateException.]
    if (this.state == State.CLOSED) {
        throw new IllegalStateException("The MQTT connection is currently closed. Call open() before attempting " + "to receive a message.");
    }
    Message message = null;
    // Codes_SRS_MQTTIOTHUBCONNECTION_15_014: [The function shall attempt to consume a message
    // from various messaging clients.]
    /*
        **Codes_SRS_MQTTIOTHUBCONNECTION_25_016: [**If any of the messaging clients fail to receive, the function shall throw an IOException.**]**
         */
    message = this.deviceMethod.receive();
    if (message == null) {
        message = deviceTwin.receive();
    }
    if (message == null) {
        message = deviceMessaging.receive();
    }
    return message;
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) DeviceTwinMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage) DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage)

Example 28 with Message

use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.

the class HttpsSingleMessage method toMessage.

/**
     * Returns the Iot Hub message represented by the HTTPS message.
     *
     * @return the IoT Hub message represented by the HTTPS message.
     */
public Message toMessage() {
    // Codes_SRS_HTTPSSINGLEMESSAGE_11_007: [The function shall return an IoT Hub message with a copy of the message body as its body.]
    Message msg = new Message(this.getBody());
    // Codes_SRS_HTTPSSINGLEMESSAGE_11_008: [The function shall return an IoT Hub message with application-defined properties that have the prefix 'iothub-app' removed.]
    for (MessageProperty property : this.properties) {
        String propertyName = httpsAppPropertyToAppProperty(property.getName());
        msg.setProperty(propertyName, property.getValue());
    }
    return msg;
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty)

Example 29 with Message

use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.

the class IotHubOutboundPacketTest method getMessageReturnsMessage.

// Tests_SRS_IOTHUBOUTBOUNDPACKET_11_001: [The constructor shall save the message, callback, and callback context.]
// Tests_SRS_IOTHUBOUTBOUNDPACKET_11_002: [The function shall return the message given in the constructor.]
@Test
public void getMessageReturnsMessage() {
    final Map<String, Object> context = new HashMap<>();
    IotHubOutboundPacket packet = new IotHubOutboundPacket(mockMsg, mockCallback, context);
    Message testMsg = packet.getMessage();
    final Message expectedMsg = mockMsg;
    assertThat(testMsg, is(expectedMsg));
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) HashMap(java.util.HashMap) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) Test(org.junit.Test)

Example 30 with Message

use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.

the class MqttTest method receiveThrowsIOExceptionWhenParsePayloadReturnsNull.

/*
    **Tests_SRS_Mqtt_25_025: [**If the call to parsePayload returns null when topic is non-null then this method will throw IOException**]**
     */
@Test(expected = IOException.class)
public void receiveThrowsIOExceptionWhenParsePayloadReturnsNull() throws IOException, MqttException {
    //arrange
    final byte[] payload = { 0x61, 0x62, 0x63 };
    baseConstructorExpectations(true);
    new MockUp<MqttMessaging>() {

        @Mock
        String parseTopic() throws IOException {
            return mockParseTopic;
        }

        @Mock
        byte[] parsePayload(String topic) throws IOException {
            return null;
        }
    };
    final Mqtt mockMqtt = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    //act
    try {
        Message receivedMessage = mockMqtt.receive();
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Message(com.microsoft.azure.sdk.iot.device.Message) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Aggregations

Message (com.microsoft.azure.sdk.iot.device.Message)31 Test (org.junit.Test)29 DeviceMethodMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage)7 MessageProperty (com.microsoft.azure.sdk.iot.device.MessageProperty)7 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)6 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)6 NonStrictExpectations (mockit.NonStrictExpectations)6 Mqtt (com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt)4 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)4 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)3 HttpsSingleMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage)3 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)3 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 DeviceConnectionString (tests.integration.com.microsoft.azure.sdk.iot.device.DeviceConnectionString)3 EventCallback (tests.integration.com.microsoft.azure.sdk.iot.device.EventCallback)3 Success (tests.integration.com.microsoft.azure.sdk.iot.device.Success)3 Verifications (mockit.Verifications)2 DeviceTwinMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage)1