Search in sources :

Example 21 with TwinCollection

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

the class JobResultTest method toStringReturnClassContent.

/* Tests_SRS_JOBRESULT_21_020: [The toString shall return a String with a pretty print json that represents this class.] */
@Test
public void toStringReturnClassContent() throws IOException {
    // arrange
    final String json = "validJson";
    final Date now = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT_JSON);
    String nowString = dateFormat.format(now);
    final String expectedPrettyPrint = "{\n" + "  \"jobId\": \"validJobId\",\n" + "  \"queryCondition\": \"DeviceId IN ['validDevice']\",\n" + "  \"createdTime\": \"" + nowString + "\",\n" + "  \"startTime\": \"" + nowString + "\",\n" + "  \"lastUpdatedDateTime\": \"" + nowString + "\",\n" + "  \"endTime\": \"" + nowString + "\",\n" + "  \"maxExecutionTimeInSeconds\": 100,\n" + "  \"jobType\": \"scheduleUpdateTwin\",\n" + "  \"jobStatus\": \"enqueued\",\n" + "  \"updateTwin\": {\n" + "    \"deviceId\": \"validDeviceId\",\n" + "    \"eTag\": \"validETag\",\n" + "    \"tag\": {\n" + "      \"tag1\": \"val1\"\n" + "    },\n" + "    \"desiredProperties\": {},\n" + "    \"parentScopes\": []\n" + "  },\n" + "  \"failureReason\": \"This is a valid failure reason\",\n" + "  \"statusMessage\": \"This is a valid status message\",\n" + "  \"jobStatistics\": {\n" + "    \"deviceCount\": 0,\n" + "    \"failedCount\": 0,\n" + "    \"succeededCount\": 0,\n" + "    \"runningCount\": 0,\n" + "    \"pendingCount\": 0\n" + "  },\n" + "  \"deviceId\": \"validDeviceId\",\n" + "  \"parentJobId\": \"validParentJobId\"\n" + "}";
    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, now, null, "scheduleUpdateTwin");
    JobResult jobResult = Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, json.getBytes(StandardCharsets.UTF_8));
    // act
    String prettyPrint = jobResult.toString();
    // assert
    assertThat(prettyPrint, is(expectedPrettyPrint));
}
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) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 22 with TwinCollection

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

the class JobResultTest method constructorStoreJsonContent.

/* Tests_SRS_JOBRESULT_21_004: [The constructor shall locally store all results information in the provided body.] */
@Test
public void constructorStoreJsonContent() throws IOException {
    // arrange
    final String json = "validJson";
    final Date now = new Date();
    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, now, null, "scheduleUpdateTwin");
    // act
    JobResult jobResult = Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, json.getBytes(StandardCharsets.UTF_8));
    // assert
    assertEquals(JOB_ID, Deencapsulation.getField(jobResult, "jobId"));
    assertEquals(QUERY_CONDITION, Deencapsulation.getField(jobResult, "queryCondition"));
    assertEquals(now, Deencapsulation.getField(jobResult, "createdTime"));
    assertEquals(now, Deencapsulation.getField(jobResult, "startTime"));
    assertEquals(now, Deencapsulation.getField(jobResult, "endTime"));
    assertEquals(MAX_EXECUTION_TIME_IN_SECONDS, (long) Deencapsulation.getField(jobResult, "maxExecutionTimeInSeconds"));
    assertEquals(JobType.scheduleUpdateTwin, Deencapsulation.getField(jobResult, "jobType"));
    assertEquals(JobStatus.enqueued, Deencapsulation.getField(jobResult, "jobStatus"));
    assertNull(Deencapsulation.getField(jobResult, "cloudToDeviceMethod"));
    assertNotNull(Deencapsulation.getField(jobResult, "updateTwin"));
    assertEquals(FAILURE_REASON, Deencapsulation.getField(jobResult, "failureReason"));
    assertEquals(STATUS_MESSAGE, Deencapsulation.getField(jobResult, "statusMessage"));
    assertNotNull(Deencapsulation.getField(jobResult, "jobStatistics"));
    assertEquals(DEVICE_ID, Deencapsulation.getField(jobResult, "deviceId"));
    assertEquals(PARENT_JOB_ID, Deencapsulation.getField(jobResult, "parentJobId"));
}
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 23 with TwinCollection

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

the class DesiredPropertiesTests method testSubscribeToDesiredPropertiesBatch.

// This test is for the startDeviceTwin/startTwin API that takes the TwinPropertiesCallback rather than the TwinPropertyCallback
// This callback should receive the full twin update in one callback, rather than one callback per updated
// desired property
@Test
@StandardTierHubOnlyTest
public void testSubscribeToDesiredPropertiesBatch() throws Exception {
    super.setUpNewDeviceAndModule(false);
    String expectedKey1 = UUID.randomUUID().toString();
    String expectedValue1 = UUID.randomUUID().toString();
    String expectedKey2 = UUID.randomUUID().toString();
    String expectedValue2 = UUID.randomUUID().toString();
    TwinCollection expectedDesiredProperties = new TwinCollection();
    expectedDesiredProperties.putFinal(expectedKey1, expectedValue1);
    expectedDesiredProperties.putFinal(expectedKey2, expectedValue2);
    TwinPropertiesCallback twinPropertiesCallback = new TwinPropertiesCallbackImpl(expectedDesiredProperties);
    Success desiredPropertiesCallbackState = new Success();
    testInstance.testIdentity.getClient().open();
    if (testInstance.testIdentity.getClient() instanceof DeviceClient) {
        ((DeviceClient) testInstance.testIdentity.getClient()).startDeviceTwin(new DeviceTwinStatusCallBack(), testInstance.deviceUnderTest, twinPropertiesCallback, desiredPropertiesCallbackState);
    } else {
        ((ModuleClient) testInstance.testIdentity.getClient()).startTwin(new DeviceTwinStatusCallBack(), testInstance.deviceUnderTest, twinPropertiesCallback, desiredPropertiesCallbackState);
    }
    long startTime = System.currentTimeMillis();
    while (testInstance.deviceUnderTest.deviceTwinStatus != IotHubStatusCode.OK) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > START_TWIN_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for twin to start");
        }
    }
    DeviceTwinDevice serviceClientTwin;
    if (testInstance.clientType == ClientType.DEVICE_CLIENT) {
        serviceClientTwin = new DeviceTwinDevice(testInstance.testIdentity.getClient().getConfig().getDeviceId());
    } else {
        serviceClientTwin = new DeviceTwinDevice(testInstance.testIdentity.getClient().getConfig().getDeviceId(), testInstance.testIdentity.getClient().getConfig().getModuleId());
    }
    Set<com.microsoft.azure.sdk.iot.service.devicetwin.Pair> desiredProperties = new HashSet<>();
    desiredProperties.add(new com.microsoft.azure.sdk.iot.service.devicetwin.Pair(expectedKey1, expectedValue1));
    desiredProperties.add(new com.microsoft.azure.sdk.iot.service.devicetwin.Pair(expectedKey2, expectedValue2));
    serviceClientTwin.setDesiredProperties(desiredProperties);
    testInstance.twinServiceClient.updateTwin(serviceClientTwin);
    startTime = System.currentTimeMillis();
    while (!desiredPropertiesCallbackState.wasCallbackFired()) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > DESIRED_PROPERTIES_PROPAGATION_TIME_MILLISECONDS) {
            fail("Timed out waiting for desired properties callback to execute");
        }
    }
    assertTrue("Desired properties callback executed, but with unexpected properties", desiredPropertiesCallbackState.getResult());
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) ModuleClient(com.microsoft.azure.sdk.iot.device.ModuleClient) TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) TwinPropertiesCallback(com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertiesCallback) Pair(com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) 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