Search in sources :

Example 66 with NonStrictExpectations

use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.

the class MqttIotHubConnectionTest method sendEventSendsDeviceTwinMessage.

@Test
public void sendEventSendsDeviceTwinMessage(@Mocked final DeviceTwinMessage mockDeviceTwinMsg) throws IOException {
    baseExpectations();
    openExpectations();
    final byte[] msgBody = { 0x61, 0x62, 0x63 };
    new NonStrictExpectations() {

        {
            mockDeviceTwinMsg.getBytes();
            result = msgBody;
            mockDeviceTwinMsg.getMessageType();
            result = MessageType.DeviceTwin;
        }
    };
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.open();
    IotHubStatusCode result = connection.sendEvent(mockDeviceTwinMsg);
    assertEquals(IotHubStatusCode.OK_EMPTY, result);
    new Verifications() {

        {
            mockDeviceMethods.send((DeviceMethodMessage) any);
            times = 0;
            mockDeviceMessaging.send(mockDeviceTwinMsg);
            times = 0;
            mockDeviceTwin.start();
            times = 1;
            mockDeviceTwin.send(mockDeviceTwinMsg);
            times = 1;
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 67 with NonStrictExpectations

use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.

the class MqttIotHubConnectionTest method openThrowsIOExceptionIfConnectionFailsInMethod.

@Test(expected = IOException.class)
public void openThrowsIOExceptionIfConnectionFailsInMethod() throws IOException {
    baseExpectations();
    new NonStrictExpectations() {

        {
            new IotHubSasToken(mockConfig, anyLong);
            result = mockToken;
            new MqttMessaging(sslPrefix + iotHubHostName + sslPortSuffix, deviceId, anyString, anyString, mockIotHubSSLContext);
            result = mockDeviceMessaging;
            new MqttDeviceMethod();
            result = new IOException(anyString);
        }
    };
    try {
        MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
        connection.open();
    } catch (Exception e) {
        new Verifications() {

            {
                mockDeviceMessaging.stop();
                times = 1;
            }
        };
        throw e;
    }
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IOException(java.io.IOException) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) IOException(java.io.IOException) Test(org.junit.Test)

Example 68 with NonStrictExpectations

use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.

the class MqttIotHubConnectionTest method openThrowsIOExceptionIfConnectionFails.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_005: [If an MQTT connection is unable to be established for any reason,
// the function shall throw an IOException.]
@Test(expected = IOException.class)
public void openThrowsIOExceptionIfConnectionFails() throws IOException {
    baseExpectations();
    new NonStrictExpectations() {

        {
            new IotHubSasToken(mockConfig, anyLong);
            result = mockToken;
            new MqttMessaging(sslPrefix + iotHubHostName + sslPortSuffix, deviceId, anyString, anyString, mockIotHubSSLContext);
            result = new IOException(anyString);
        }
    };
    try {
        MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
        connection.open();
    } catch (Exception e) {
        new Verifications() {

            {
            }
        };
        throw e;
    }
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IOException(java.io.IOException) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) IOException(java.io.IOException) Test(org.junit.Test)

Example 69 with NonStrictExpectations

use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.

the class MqttIotHubConnectionTest method sendEventReturnsBadFormatIfMessageHasNullBody.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_010: [If the message is null or empty,
// the function shall return status code BAD_FORMAT.]
@Test
public void sendEventReturnsBadFormatIfMessageHasNullBody(@Mocked final Message mockMsg) throws IOException {
    baseExpectations();
    openExpectations();
    final byte[] msgBody = null;
    new NonStrictExpectations() {

        {
            mockMsg.getBytes();
            result = msgBody;
        }
    };
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.open();
    IotHubStatusCode result = connection.sendEvent(null);
    assertEquals(IotHubStatusCode.BAD_FORMAT, result);
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 70 with NonStrictExpectations

use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.

the class DeviceTest method constructor_create_symmetrickey.

// Tests_SRS_SERVICE_SDK_JAVA_DEVICE_12_005: [If the input symmetric key is empty, the constructor shall create
// a new SymmetricKey instance using AES encryption and store it into a member variable]
@Test
public void constructor_create_symmetrickey() throws NoSuchAlgorithmException {
    // Arrange
    String encryptionMethod = "AES";
    String deviceId = "xxx-device";
    new NonStrictExpectations() {

        {
            SymmetricKey symmetricKey = new SymmetricKey();
            KeyGenerator.getInstance(encryptionMethod);
        }
    };
    // Act
    Device device = Deencapsulation.newInstance(Device.class, deviceId, DeviceStatus.class, SymmetricKey.class);
    // Assert
    assertNotEquals(device.getSymmetricKey(), null);
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) SymmetricKey(com.microsoft.azure.sdk.iot.service.auth.SymmetricKey) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Aggregations

NonStrictExpectations (mockit.NonStrictExpectations)476 Test (org.junit.Test)470 Verifications (mockit.Verifications)154 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)77 IOException (java.io.IOException)64 HttpConnection (com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection)51 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)51 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)47 MessageProperty (com.microsoft.azure.sdk.iot.device.MessageProperty)32 HttpsSingleMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage)31 HttpConnection (com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection)26 HttpMethod (com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod)26 HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)26 HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)26 HashMap (java.util.HashMap)26 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)25 IotHubConnectionString (com.microsoft.azure.sdk.iot.device.IotHubConnectionString)17 Date (java.util.Date)16 HashSet (java.util.HashSet)16 Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)15