Search in sources :

Example 1 with TwinCollection

use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.

the class JobResultTest method constructorParseJson.

/* Tests_SRS_JOBRESULT_21_002: [The constructor shall parse the body using the JobsResponseParser.] */
@Test
public void constructorParseJson() throws IOException {
    // arrange
    final String json = "validJson";
    TwinCollection tags = new TwinCollection();
    tags.putFinal("tag1", "val1");
    TwinState twinState = new TwinState(tags, null, null);
    twinState.setDeviceId(DEVICE_ID);
    twinState.setETag(ETAG);
    JobsResponseParserExpectations(json, twinState, null, new Date(), null, "scheduleUpdateTwin");
    // act
    JobResult jobResult = Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, json.getBytes(StandardCharsets.UTF_8));
    // assert
    new Verifications() {

        {
            JobsResponseParser.createFromJson(json);
            times = 1;
        }
    };
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) Date(java.util.Date) Test(org.junit.Test)

Example 2 with TwinCollection

use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.

the class JobResultTest method constructorStoreJsonContentNoTags.

/* Tests_SRS_JOBRESULT_21_004: [The constructor shall locally store all results information in the provided body.] */
@Test
public void constructorStoreJsonContentNoTags() throws IOException {
    // arrange
    final String json = "validJson";
    final Date now = new Date();
    TwinCollection desired = new TwinCollection();
    desired.putFinal("prop1", "val1");
    TwinState twinState = new TwinState(null, desired, null);
    twinState.setDeviceId(DEVICE_ID);
    twinState.setETag(ETAG);
    JobsResponseParserExpectations(json, twinState, null, now, null, "scheduleUpdateTwin");
    // act
    JobResult jobResult = Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, json.getBytes(StandardCharsets.UTF_8));
    // assert
    assertNotNull(Deencapsulation.getField(jobResult, "updateTwin"));
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) Date(java.util.Date) Test(org.junit.Test)

Example 3 with TwinCollection

use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.

the class JobClient method getParserFromDevice.

private TwinState getParserFromDevice(DeviceTwinDevice device) {
    TwinCollection tags = null;
    TwinCollection desired = null;
    TwinCollection reported = null;
    if (device.getTags() != null) {
        tags = setToMap(device.getTags());
    }
    if (device.getDesiredProperties() != null) {
        desired = setToMap(device.getDesiredProperties());
    }
    if (device.getReportedProperties() != null) {
        reported = setToMap(device.getReportedProperties());
    }
    TwinState twinState = new TwinState(tags, desired, reported);
    if (device.getDeviceId() != null) {
        twinState.setDeviceId(device.getDeviceId());
    }
    if (device.getETag() == null) {
        twinState.setETag("*");
    } else {
        twinState.setETag(device.getETag());
    }
    return twinState;
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState)

Example 4 with TwinCollection

use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection 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(@Mocked DeviceTwinDevice mockedDevice) throws Exception {
    // arrange
    final String connectionString = "testString";
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    TwinCollection testMap = new TwinCollection();
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            Deencapsulation.invoke(mockedDevice, "getDesiredMap");
            result = testMap;
            Deencapsulation.invoke(mockedDevice, "getTagsMap");
            result = testMap;
        }
    };
    // act
    testTwin.updateTwin(mockedDevice);
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) 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 5 with TwinCollection

use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.

the class DeviceTwinTest method getTwinOperationSucceeds.

/*
    **Tests_SRS_DEVICETWIN_25_006: [** The function shall create a new SAS token **]**
    **Tests_SRS_DEVICETWIN_25_007: [** The function shall create a new HttpRequest with http method as Get **]**
    **Tests_SRS_DEVICETWIN_25_008: [** 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_009: [** The function shall send the created request and get the response **]**
     **Tests_SRS_DEVICETWIN_25_011: [** The function shall deserialize the payload by calling updateTwin Api on the twin object **]**
     **Tests_SRS_DEVICETWIN_25_012: [** The function shall set eTag, tags, desired property map, reported property map on the user device **]**
     */
@Test
public void getTwinOperationSucceeds(@Mocked DeviceTwinDevice mockedDevice, @Mocked final URL mockUrl) throws Exception {
    // arrange
    final String connectionString = "testString";
    constructorExpectations(connectionString);
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    TwinCollection testMap = new TwinCollection();
    String expectedConnectionState = TwinConnectionState.CONNECTED.toString();
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            TwinState.createFromTwinJson((String) any);
            result = mockedTwinState;
            mockedTwinState.getCapabilities();
            result = mockCapabilities;
            mockedTwinState.getConfigurations();
            result = mockConfigurations;
            mockedTwinState.getConnectionState();
            result = expectedConnectionState;
        }
    };
    // act
    Deencapsulation.invoke(testTwin, "getTwinOperation", new Class[] { URL.class, DeviceTwinDevice.class }, mockUrl, mockedDevice);
    // assert
    new Verifications() {

        {
            mockedHttpRequest.setReadTimeoutMillis(anyInt);
            times = 1;
            mockedHttpRequest.setHeaderField(anyString, anyString);
            times = 6;
            mockedHttpRequest.send();
            times = 1;
            TwinState.createFromTwinJson((String) any);
            times = 1;
            mockedTwinState.getETag();
            times = 1;
            Deencapsulation.invoke(mockedDevice, "setETag", anyString);
            times = 1;
            mockedTwinState.getTags();
            times = 1;
            Deencapsulation.invoke(mockedDevice, "setTags", testMap);
            times = 1;
            mockedTwinState.getDesiredProperty();
            times = 1;
            Deencapsulation.invoke(mockedDevice, "setDesiredProperties", testMap);
            times = 1;
            mockedTwinState.getReportedProperty();
            times = 1;
            Deencapsulation.invoke(mockedDevice, "setReportedProperties", testMap);
            times = 1;
            Deencapsulation.invoke(mockedDevice, "setCapabilities", mockCapabilities);
            times = 1;
            Deencapsulation.invoke(mockedDevice, "setConfigurations", mockConfigurations);
            times = 1;
            Deencapsulation.invoke(mockedDevice, "setConnectionState", expectedConnectionState);
            times = 1;
        }
    };
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) 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

TwinCollection (com.microsoft.azure.sdk.iot.deps.twin.TwinCollection)23 Test (org.junit.Test)20 TwinState (com.microsoft.azure.sdk.iot.deps.twin.TwinState)12 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)9 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)9 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)9 Date (java.util.Date)9 NonStrictExpectations (mockit.NonStrictExpectations)9 Verifications (mockit.Verifications)5 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)3 Expectations (mockit.Expectations)3 StrictExpectations (mockit.StrictExpectations)3 MethodParser (com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)2 Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)2 SqlQuery (com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery)2 SimpleDateFormat (java.text.SimpleDateFormat)2 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)1 Pair (com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair)1 TwinPropertiesCallback (com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertiesCallback)1 ModuleClient (com.microsoft.azure.sdk.iot.device.ModuleClient)1