Search in sources :

Example 6 with TwinCollection

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

the class DeviceTwinTest method updateTwinDoesNotThrowsIfOnlyTagsHasValue.

@Test
public void updateTwinDoesNotThrowsIfOnlyTagsHasValue(@Mocked DeviceTwinDevice mockedDevice) throws Exception {
    // arrange
    final String connectionString = "testString";
    constructorExpectations(connectionString);
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    TwinCollection testMap = new TwinCollection();
    testMap.putFinal("TestKey", "TestValue");
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            Deencapsulation.invoke(mockedDevice, "getDesiredMap");
            result = null;
            Deencapsulation.invoke(mockedDevice, "getTagsMap");
            result = testMap;
            new TwinState((TwinCollection) any, null, null);
            result = mockedTwinState;
            mockedTwinState.toJsonElement().toString();
            result = "SomeJsonString";
        }
    };
    // act
    testTwin.updateTwin(mockedDevice);
    // assert
    new Verifications() {

        {
            IotHubConnectionString.getUrlTwin(anyString, anyString);
            times = 1;
            mockedHttpRequest.setReadTimeoutMillis(anyInt);
            times = 1;
            mockedHttpRequest.setHeaderField(anyString, anyString);
            times = 6;
            mockedHttpRequest.send();
            times = 1;
        }
    };
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) 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)

Example 7 with TwinCollection

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

the class DeviceTwinTest method nextRetrievesCorrectlyWithoutModelId.

@Test
public void nextRetrievesCorrectlyWithoutModelId() throws IotHubException, IOException {
    // arrange
    final Integer version = 15;
    final String etag = "validEtag";
    final int connectTimeout = 1234;
    final int readTimeout = 5678;
    final String connectionString = "someConnectionString";
    constructorExpectations(connectionString);
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString, DeviceTwinClientOptions.builder().httpConnectTimeout(connectTimeout).httpReadTimeout(readTimeout).build());
    final String expectedString = "testJsonAsNext";
    final String moduleId = "testModuleId";
    TwinCollection tags = new TwinCollection();
    tags.putFinal("tagsKey", "tagsValue");
    TwinCollection rp = new TwinCollection();
    rp.putFinal("rpKey", "rpValue");
    TwinCollection dp = new TwinCollection();
    dp.putFinal("dpKey", "dpValue");
    new Expectations() {

        {
            Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.TWIN);
            result = mockedQuery;
            Deencapsulation.invoke(mockedQuery, "next");
            result = expectedString;
            mockedTwinState.getDeviceId();
            result = "testDeviceID";
            mockedTwinState.getModuleId();
            result = moduleId;
            mockedTwinState.getVersion();
            result = version;
            mockedTwinState.getETag();
            result = etag;
            mockedTwinState.getModelId();
            result = null;
            mockedTwinState.getTags();
            result = tags;
            mockedTwinState.getDesiredProperty();
            result = dp;
            mockedTwinState.getReportedProperty();
            result = rp;
            mockCapabilities.isIotEdge();
            result = Boolean.TRUE;
        }
    };
    Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
    // act
    DeviceTwinDevice result = testTwin.getNextDeviceTwin(testQuery);
    assertNotNull(result.getTags());
    assertNotNull(result.getReportedProperties());
    assertNotNull(result.getDesiredProperties());
    assertEquals(version, result.getVersion());
    assertEquals(etag, result.getETag());
    assertEqualSetAndMap(result.getTags(), (Map) tags);
    assertEqualSetAndMap(result.getDesiredProperties(), (Map) dp);
    assertEqualSetAndMap(result.getReportedProperties(), (Map) rp);
    assertTrue(result.getCapabilities().isIotEdge());
    assertNull(result.getModelId());
    assertEquals(result.getModuleId(), result.getModuleId());
}
Also used : Expectations(mockit.Expectations) StrictExpectations(mockit.StrictExpectations) NonStrictExpectations(mockit.NonStrictExpectations) TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 8 with TwinCollection

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

the class DeviceTwinTest method updateTwinThrowsVerificationThrows.

@Test(expected = IotHubException.class)
public void updateTwinThrowsVerificationThrows(@Mocked DeviceTwinDevice mockedDevice) throws Exception {
    // arrange
    final String connectionString = "testString";
    constructorExpectations(connectionString);
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    TwinCollection testMap = new TwinCollection();
    testMap.putFinal("TestKey", "TestValue");
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            Deencapsulation.invoke(mockedDevice, "getDesiredMap");
            result = testMap;
            Deencapsulation.invoke(mockedDevice, "getTagsMap");
            result = testMap;
            new TwinState((TwinCollection) any, (TwinCollection) any, null);
            result = mockedTwinState;
            mockedTwinState.toJsonElement().toString();
            result = "SomeJsonString";
            IotHubExceptionManager.httpResponseVerification(mockedHttpResponse);
            result = new IotHubException();
        }
    };
    // act
    testTwin.updateTwin(mockedDevice);
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) 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 9 with TwinCollection

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

the class DeviceTwinTest method getTwinSucceeds.

@Test
public void getTwinSucceeds(@Mocked DeviceTwinDevice mockedDevice) throws Exception {
    // arrange
    final String connectionString = "testString";
    constructorExpectations(connectionString);
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    TwinCollection testMap = new TwinCollection();
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            mockedDevice.getModuleId();
            result = "SomeModuleID";
            TwinState.createFromTwinJson((String) any);
            result = mockedTwinState;
            mockedTwinState.getCapabilities();
            result = mockCapabilities;
            mockedTwinState.getConfigurations();
            result = mockConfigurations;
        }
    };
    // act
    testTwin.getTwin(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;
        }
    };
}
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)

Example 10 with TwinCollection

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

the class ExportImportDeviceParserTest method toJsonForTaggedDevice.

@Test
public void toJsonForTaggedDevice() throws NoSuchAlgorithmException {
    // arrange
    ExportImportDeviceParser parser = new ExportImportDeviceParser();
    parser.setAuthentication(new AuthenticationParser());
    parser.getAuthenticationFinal().setType(AuthenticationTypeParser.SAS);
    parser.getAuthenticationFinal().setSymmetricKey(new SymmetricKeyParser("", ""));
    TwinCollection tags = new TwinCollection();
    tags.putFinal("test01", "firstvalue");
    tags.putFinal("test02", "secondvalue");
    parser.setTags(tags);
    // String expectedJson = "{\"authentication\":{\"symmetricKey\":{\"primaryKey\":\"\",\"secondaryKey\":\"\"},\"type\":\"" + SAS_JSON_VALUE + "\"},\"tags\":{\"test01\":\"firstvalue\",\"test02\":\"secondvalue\"}}";
    // act
    String serializedDevice = parser.toJson();
    // assert
    assertTrue(serializedDevice.contains("\"tags\":{"));
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) 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