Search in sources :

Example 21 with DeviceTwin

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin in project azure-iot-sdk-java by Azure.

the class DeviceTwinTest method replaceDesiredPropertiesThrowsIfDeviceIsNull.

/*
    **Tests_SRS_DEVICETWIN_25_030: [** The function shall build the URL for this operation by calling getUrlTwinDesired **]**
    **Tests_SRS_DEVICETWIN_25_031: [** The function shall serialize the desired properties map by calling resetDesiredProperty Api on the twin object for the device provided by the user**]**
    **Tests_SRS_DEVICETWIN_25_032: [** The function shall create a new SAS token **]**

    **Tests_SRS_DEVICETWIN_25_033: [** The function shall create a new HttpRequest with http method as PUT **]**

    **Tests_SRS_DEVICETWIN_25_034: [** The function shall set the following HTTP headers specified in the IotHub DeviceTwin doc.
                                                1. Key as authorization with value as sastoken
                                                2. Key as request id with a new string value for every request
                                                3. Key as User-Agent with value specified by the clientIdentifier and its version
                                                4. Key as Accept with value as application/json
                                                5. Key as Content-Type and value as application/json
                                                6. Key as charset and value as utf-8
                                                7. Key as If-Match and value as '*'  **]**

    **Tests_SRS_DEVICETWIN_25_035: [** The function shall send the created request and get the response **]**

    **Tests_SRS_DEVICETWIN_25_036: [** The function shall verify the response status and throw proper Exception **]**

     */
/*
    @Test
    public void replaceDesiredPropertiesSucceeds() throws Exception
    {
        //arrange
        final String connectionString = "testString";
        DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
        new NonStrictExpectations()
        {
            {
                mockedDevice.getDeviceId();
                result = "SomeDevID";
                mockedTwinObject.resetDesiredProperty((Map<String, Object>)any);
                result = "SomeJsonString";
            }
        };

        //act
        testTwin.replaceDesiredProperties(mockedDevice);

        //assert
        new Verifications()
        {
            {
                mockedConnectionString.getUrlTwinDesired(anyString);
                times = 1;
                mockedTwinObject.resetDesiredProperty((Map<String, Object>)any);
                times = 1;
                mockedHttpRequest.setReadTimeoutMillis(anyInt);
                times = 1;
                mockedHttpRequest.setHeaderField(anyString, anyString);
                times = 7;
                mockedHttpRequest.send();
                times = 1;
            }
        };
    }
*/
/*
    **Tests_SRS_DEVICETWIN_25_029: [** The function shall throw IllegalArgumentException if the input device is null or if deviceId is null or empty **]**
     */
@Test(expected = IllegalArgumentException.class)
public void replaceDesiredPropertiesThrowsIfDeviceIsNull() throws Exception {
    //arrange
    final String connectionString = "testString";
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    //act
    testTwin.replaceDesiredProperties(null);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 22 with DeviceTwin

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin in project azure-iot-sdk-java by Azure.

the class DeviceTwinTest method updateTwinThrowsIfBothDesiredAndTagsIsEmpty.

/*
    **Tests_SRS_DEVICETWIN_25_045: [** The function shall throw IllegalArgumentException if the both desired and tags maps are either empty or null **]**
     */
@Test(expected = IllegalArgumentException.class)
public void updateTwinThrowsIfBothDesiredAndTagsIsEmpty() throws Exception {
    //arrange
    final String connectionString = "testString";
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    Map<String, Object> testMap = new HashMap<>();
    //testMap.put("TestKey", "TestValue");
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            Deencapsulation.invoke(mockedDevice, "getDesiredMap");
            result = testMap;
            Deencapsulation.invoke(mockedDevice, "getTagsMap");
            result = testMap;
        }
    };
    //act
    testTwin.updateTwin(mockedDevice);
}
Also used : HashMap(java.util.HashMap) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 23 with DeviceTwin

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin in project azure-iot-sdk-java by Azure.

the class DeviceTwinTest method getTwinThrowsOnEmptyDeviceID.

@Test(expected = IllegalArgumentException.class)
public void getTwinThrowsOnEmptyDeviceID() throws Exception {
    //arrange
    final String connectionString = "testString";
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "";
        }
    };
    //act
    testTwin.getTwin(mockedDevice);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 24 with DeviceTwin

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin in project azure-iot-sdk-java by Azure.

the class DeviceTwinTest method getTwinThrowsVerificationFailure.

/*
    **Tests_SRS_DEVICETWIN_25_010: [** The function shall verify the response status and throw proper Exception **]**
     */
@Test(expected = IotHubException.class)
public void getTwinThrowsVerificationFailure() throws Exception {
    //arrange
    final String connectionString = "testString";
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            IotHubExceptionManager.httpResponseVerification(mockedHttpResponse);
            result = new IotHubException();
        }
    };
    //act
    testTwin.getTwin(mockedDevice);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 25 with DeviceTwin

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin in project azure-iot-sdk-java by Azure.

the class DeviceTwinTest method updateTwinSucceeds.

/*
    **Tests_SRS_DEVICETWIN_25_030: [** The function shall build the URL for this operation by calling getUrlTwinDesired **]**
    **Tests_SRS_DEVICETWIN_25_031: [** The function shall serialize the desired properties map by calling resetDesiredProperty Api on the twin object for the device provided by the user**]**
    **Tests_SRS_DEVICETWIN_25_016: [** The function shall create a new SAS token **]**

    **Tests_SRS_DEVICETWIN_25_017: [** The function shall create a new HttpRequest with http method as Patch **]**

    **Tests_SRS_DEVICETWIN_25_018: [** The function shall set the following HTTP headers specified in the IotHub DeviceTwin doc.
                                                1. Key as authorization with value as sastoken
                                                2. Key as request id with a new string value for every request
                                                3. Key as User-Agent with value specified by the clientIdentifier and its version
                                                4. Key as Accept with value as application/json
                                                5. Key as Content-Type and value as application/json
                                                6. Key as charset and value as utf-8
                                                7. Key as If-Match and value as '*'  **]**

    **Tests_SRS_DEVICETWIN_25_019: [** The function shall send the created request and get the response **]**

    **Tests_SRS_DEVICETWIN_25_020: [** The function shall verify the response status and throw proper Exception **]**

    *Tests_SRS_DEVICETWIN_25_036: [** The function shall verify the response status and throw proper Exception **]**

     */
@Test
public void updateTwinSucceeds() throws Exception {
    //arrange
    final String connectionString = "testString";
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    Map<String, Object> testMap = new HashMap<>();
    testMap.put("TestKey", "TestValue");
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            Deencapsulation.invoke(mockedDevice, "getDesiredMap");
            result = testMap;
            Deencapsulation.invoke(mockedDevice, "getTagsMap");
            result = testMap;
            mockedTwinParser.updateTwin((Map<String, Object>) any, null, (Map<String, Object>) any);
            result = "SomeJsonString";
        }
    };
    //act
    testTwin.updateTwin(mockedDevice);
    //assert
    new Verifications() {

        {
            mockedConnectionString.getUrlTwin(anyString);
            times = 1;
            mockedTwinParser.updateTwin((Map<String, Object>) any, null, (Map<String, Object>) any);
            times = 1;
            mockedHttpRequest.setReadTimeoutMillis(anyInt);
            times = 1;
            mockedHttpRequest.setHeaderField(anyString, anyString);
            times = 5;
            mockedHttpRequest.send();
            times = 1;
        }
    };
}
Also used : HashMap(java.util.HashMap) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Aggregations

DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)30 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)29 Test (org.junit.Test)29 NonStrictExpectations (mockit.NonStrictExpectations)22 Verifications (mockit.Verifications)10 HashMap (java.util.HashMap)8 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)3 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)1 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1