Search in sources :

Example 16 with IotHubStatusCode

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

the class IotHubStatusCodeTest method getIotHubStatusCodeMapsUnauthorizedCorrectly.

// Tests_SRS_IOTHUBSTATUSCODE_11_001: [The function shall convert the given HTTPS status code to the corresponding IoT Hub status code.]
@Test
public void getIotHubStatusCodeMapsUnauthorizedCorrectly() {
    final int httpsStatus = 401;
    IotHubStatusCode testStatus = IotHubStatusCode.getIotHubStatusCode(httpsStatus);
    final IotHubStatusCode expectedStatus = IotHubStatusCode.UNAUTHORIZED;
    assertThat(testStatus, is(expectedStatus));
}
Also used : IotHubStatusCode(com.microsoft.azure.sdk.iot.device.IotHubStatusCode) Test(org.junit.Test)

Example 17 with IotHubStatusCode

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

the class IotHubStatusCodeTest method getIotHubStatusCodeMapsTooManyDevicesCorrectly.

// Tests_SRS_IOTHUBSTATUSCODE_11_001: [The function shall convert the given HTTPS status code to the corresponding IoT Hub status code.]
@Test
public void getIotHubStatusCodeMapsTooManyDevicesCorrectly() {
    final int httpsStatus = 403;
    IotHubStatusCode testStatus = IotHubStatusCode.getIotHubStatusCode(httpsStatus);
    final IotHubStatusCode expectedStatus = IotHubStatusCode.TOO_MANY_DEVICES;
    assertThat(testStatus, is(expectedStatus));
}
Also used : IotHubStatusCode(com.microsoft.azure.sdk.iot.device.IotHubStatusCode) Test(org.junit.Test)

Example 18 with IotHubStatusCode

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

the class IotHubStatusCodeTest method getIotHubStatusCodeMapsOkEmptyCorrectly.

// Tests_SRS_IOTHUBSTATUSCODE_11_001: [The function shall convert the given HTTPS status code to the corresponding IoT Hub status code.]
@Test
public void getIotHubStatusCodeMapsOkEmptyCorrectly() {
    final int httpsStatus = 204;
    IotHubStatusCode testStatus = IotHubStatusCode.getIotHubStatusCode(httpsStatus);
    final IotHubStatusCode expectedStatus = IotHubStatusCode.OK_EMPTY;
    assertThat(testStatus, is(expectedStatus));
}
Also used : IotHubStatusCode(com.microsoft.azure.sdk.iot.device.IotHubStatusCode) Test(org.junit.Test)

Example 19 with IotHubStatusCode

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

the class MqttIotHubConnection method sendEvent.

/**
     * Sends an event message.
     *
     * @param message the event message.
     *
     * @return the status code from sending the event message.
     *
     * @throws IllegalStateException if the MqttIotHubConnection is not open
     */
public IotHubStatusCode sendEvent(Message message) throws IllegalStateException {
    synchronized (MQTT_CONNECTION_LOCK) {
        // the function shall return status code BAD_FORMAT.]
        if (message == null || message.getBytes() == null || ((message.getMessageType() != MessageType.DeviceTwin && message.getMessageType() != MessageType.DeviceMethods) && message.getBytes().length == 0)) {
            return IotHubStatusCode.BAD_FORMAT;
        }
        // the function shall throw an IllegalStateException.]
        if (this.state == State.CLOSED) {
            throw new IllegalStateException("Cannot send event using a closed MQTT connection");
        }
        // Codes_SRS_MQTTIOTHUBCONNECTION_15_008: [The function shall send an event message
        // to the IoT Hub given in the configuration.]
        // Codes_SRS_MQTTIOTHUBCONNECTION_15_011: [If the message was successfully received by the service,
        // the function shall return status code OK_EMPTY.]
        IotHubStatusCode result = IotHubStatusCode.OK_EMPTY;
        try {
            // Codes_SRS_MQTTIOTHUBCONNECTION_15_009: [The function shall send the message payload.]
            if (message.getMessageType() == MessageType.DeviceMethods) {
                this.deviceMethod.start();
                this.deviceMethod.send((DeviceMethodMessage) message);
            } else if (message.getMessageType() == MessageType.DeviceTwin) {
                this.deviceTwin.start();
                this.deviceTwin.send((DeviceTwinMessage) message);
            } else {
                this.deviceMessaging.send(message);
            }
        }// received by the service, the function shall return status code ERROR.]
         catch (Exception e) {
            result = IotHubStatusCode.ERROR;
        }
        return result;
    }
}
Also used : IotHubStatusCode(com.microsoft.azure.sdk.iot.device.IotHubStatusCode) DeviceTwinMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage) IOException(java.io.IOException)

Example 20 with IotHubStatusCode

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

the class IotHubCallbackPacketTest method getContextReturnsContext.

// Tests_SRS_IOTHUBCALLBACKPACKET_11_001: [The constructor shall save the status, eventCallback, and callback context.]
// Tests_SRS_IOTHUBCALLBACKPACKET_11_004: [The function shall return the callback context given in the constructor.]
@Test
public void getContextReturnsContext() {
    final IotHubStatusCode status = IotHubStatusCode.OK;
    final Map<String, Object> context = new HashMap<>();
    final String key = "test-key";
    final String value = "test-value";
    context.put(key, value);
    IotHubCallbackPacket packet = new IotHubCallbackPacket(status, mockEventCallback, context);
    Map<String, Object> testContext = (Map<String, Object>) packet.getContext();
    Set<Map.Entry<String, Object>> testEntrySet = testContext.entrySet();
    final Set<Map.Entry<String, Object>> expectedEntrySet = context.entrySet();
    assertThat(testEntrySet, everyItem(isIn(expectedEntrySet)));
}
Also used : IotHubStatusCode(com.microsoft.azure.sdk.iot.device.IotHubStatusCode) HashMap(java.util.HashMap) IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

IotHubStatusCode (com.microsoft.azure.sdk.iot.device.IotHubStatusCode)20 Test (org.junit.Test)19 ResponseMessage (com.microsoft.azure.sdk.iot.device.ResponseMessage)5 IotHubCallbackPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)4 HashMap (java.util.HashMap)4 DeviceTwinMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage)1 IotHubEventCallback (com.microsoft.azure.sdk.iot.device.IotHubEventCallback)1 IotHubResponseCallback (com.microsoft.azure.sdk.iot.device.IotHubResponseCallback)1 IOException (java.io.IOException)1 Map (java.util.Map)1