Search in sources :

Example 6 with NonStrictExpectations

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

the class IotHubSasTokenTest method expiryTimeSetCorrectly.

// Tests_SRS_IOTHUBSASTOKEN_11_002: [The expiry time shall be the given expiry time, where it is a UNIX timestamp and indicates the time after which the token becomes invalid.]
@Test
public void expiryTimeSetCorrectly() throws URISyntaxException {
    final long expiryTime = 100;
    final String signature = "sample-sig";
    final IotHubConnectionString iotHubConnectionString = Deencapsulation.newInstance(IotHubConnectionString.class, new Class[] { String.class, String.class, String.class, String.class }, "iothub.sample-iothub-hostname.net", "sample-device-ID", "sample-device-key", null);
    new NonStrictExpectations() {

        {
            mockSig.toString();
            result = signature;
        }
    };
    IotHubSasToken token = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), expiryTime);
    String tokenStr = token.toString();
    // extract the value assigned to se.
    int expiryTimeKeyIdx = tokenStr.indexOf("se=");
    int expiryTimeStartIdx = expiryTimeKeyIdx + 3;
    int expiryTimeEndIdx = tokenStr.indexOf("&", expiryTimeStartIdx);
    if (expiryTimeEndIdx == -1) {
        expiryTimeEndIdx = tokenStr.length();
    }
    String testExpiryTimeStr = tokenStr.substring(expiryTimeStartIdx, expiryTimeEndIdx);
    String expectedExpiryTimeStr = Long.toString(expiryTime);
    assertThat(testExpiryTimeStr, is(expectedExpiryTimeStr));
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 7 with NonStrictExpectations

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

the class IotHubAbandonUriTest method toStringIsCorrect.

// Tests_SRS_IOTHUBABANDONURI_11_002: [The string representation of the IoT Hub event URI shall be constructed with the format "[iotHubHostname]/devices/[deviceId]/messages/devicebound/[eTag]/abandon?api-version=2016-02-03".]
@Test
public void toStringIsCorrect() throws URISyntaxException {
    final String iotHubHostname = "test.iothub";
    final String deviceId = "test-deviceid";
    final String eTag = "test-etag";
    final String uriStr = "test-uri-str";
    new NonStrictExpectations() {

        {
            mockIotHubUri.toString();
            result = uriStr;
        }
    };
    IotHubAbandonUri abandonUri = new IotHubAbandonUri(iotHubHostname, deviceId, eTag);
    String testUriStr = abandonUri.toString();
    final String expectedUriStr = uriStr;
    assertThat(testUriStr, is(expectedUriStr));
}
Also used : IotHubAbandonUri(com.microsoft.azure.sdk.iot.device.net.IotHubAbandonUri) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 8 with NonStrictExpectations

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

the class MessageTest method setPropertyRejectsIllegalValue.

// Tests_SRS_MESSAGE_11_031: [If value name contains a character not specified in RFC 2047, the function shall throw an IllegalArgumentException.]
@Test(expected = IllegalArgumentException.class)
public void setPropertyRejectsIllegalValue(@Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final String name = "test-name";
    final String invalidValue = "test-value@";
    new NonStrictExpectations() {

        {
            new MessageProperty(name, invalidValue);
            result = new IllegalArgumentException();
        }
    };
    Message msg = new Message(body);
    msg.setProperty(name, invalidValue);
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 9 with NonStrictExpectations

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

the class MessageTest method getPropertiesReturnsCopyOfProperties.

// Tests_SRS_MESSAGE_11_033: [The function shall return a copy of the message properties.]
@Test
public void getPropertiesReturnsCopyOfProperties(@Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final String name = "test-name";
    final String value = "test-value";
    final String httpsName = "test-https-name";
    new NonStrictExpectations() {

        {
            new MessageProperty(name, value);
            result = mockProperty;
            mockProperty.hasSameName(name);
            result = true;
            mockProperty.getValue();
            result = value;
            mockProperty.getName();
            result = httpsName;
        }
    };
    Message msg = new Message(body);
    msg.setProperty(name, value);
    MessageProperty[] testProperties = msg.getProperties();
    int expectedNumProperties = 1;
    assertThat(testProperties.length, is(expectedNumProperties));
    assertThat(testProperties[0], is(not(mockProperty)));
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 10 with NonStrictExpectations

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

the class MessageTest method setPropertyAndGetPropertyMatch.

// Tests_SRS_MESSAGE_11_026: [The function shall set the message property to the given value.]
// Tests_SRS_MESSAGE_11_032: [The function shall return the value associated with the message property name, where the name can be either the HTTPS or AMQPS property name.]
@Test
public void setPropertyAndGetPropertyMatch(@Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final String name = "test-name";
    final String value1 = "test-value1";
    final String value2 = "test-value2";
    new NonStrictExpectations() {

        {
            new MessageProperty(name, value1);
            result = mockProperty;
            mockProperty.hasSameName(name);
            result = true;
            mockProperty.getValue();
            result = value1;
        }
    };
    Message msg = new Message(body);
    msg.setProperty(name, value1);
    String testValue = msg.getProperty(name);
    String expectedValue = value1;
    assertThat(testValue, is(expectedValue));
    new NonStrictExpectations() {

        {
            new MessageProperty(name, value2);
            result = mockProperty;
            mockProperty.hasSameName(name);
            result = true;
            mockProperty.getValue();
            result = value2;
        }
    };
    msg.setProperty(name, value2);
    testValue = msg.getProperty(name);
    expectedValue = value2;
    assertThat(testValue, is(expectedValue));
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Aggregations

NonStrictExpectations (mockit.NonStrictExpectations)492 Test (org.junit.Test)472 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