Search in sources :

Example 6 with DeviceTestManager

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager in project azure-iot-sdk-java by Azure.

the class DeviceMethodCommon method invokeMethodSucceed.

protected void invokeMethodSucceed() throws Exception {
    // Arrange
    DeviceTestManager deviceTestManger = this.testInstance.deviceTestManager;
    // Act
    MethodResult result;
    if (testInstance.identity instanceof TestModuleIdentity) {
        result = testInstance.methodServiceClient.invoke(testInstance.identity.getDeviceId(), ((TestModuleIdentity) testInstance.identity).getModule().getId(), DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, PAYLOAD_STRING);
    } else {
        result = testInstance.methodServiceClient.invoke(testInstance.identity.getDeviceId(), DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, PAYLOAD_STRING);
    }
    deviceTestManger.waitIotHub(1, 10);
    // Assert
    assertNotNull(result);
    assertEquals((long) DeviceEmulator.METHOD_SUCCESS, (long) result.getStatus());
    assertEquals(DeviceEmulator.METHOD_LOOPBACK + ":" + PAYLOAD_STRING, result.getPayload());
    Assert.assertEquals(0, deviceTestManger.getStatusError());
}
Also used : DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) TestModuleIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestModuleIdentity)

Example 7 with DeviceTestManager

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager in project azure-iot-sdk-java by Azure.

the class JobClientTests method scheduleUpdateTwinSucceed.

@Test(timeout = TEST_TIMEOUT_MILLISECONDS)
@Ignore
public void scheduleUpdateTwinSucceed() throws InterruptedException {
    // Arrange
    ExecutorService executor = Executors.newFixedThreadPool(MAX_NUMBER_JOBS);
    DeviceTestManager deviceTestManger = devices.get(0);
    final String deviceId = testDevice.getDeviceId();
    final String queryCondition = "DeviceId IN ['" + deviceId + "']";
    final ConcurrentMap<String, Exception> jobExceptions = new ConcurrentHashMap<>();
    final ConcurrentMap<String, JobResult> jobResults = new ConcurrentHashMap<>();
    final ConcurrentMap<String, Integer> twinExpectedTemperature = new ConcurrentHashMap<>();
    final ArrayList<String> jobIdsPending = new ArrayList<>();
    // Act
    for (int i = 0; i < MAX_NUMBER_JOBS; i++) {
        final int jobTemperature = (newTemperature++);
        executor.submit(() -> {
            String jobId = JOB_ID_NAME + UUID.randomUUID();
            jobIdsPending.add(jobId);
            try {
                DeviceTwinDevice deviceTwinDevice = new DeviceTwinDevice(deviceId);
                Set<Pair> testDesProp = new HashSet<>();
                testDesProp.add(new Pair(STANDARD_PROPERTY_HOMETEMP, jobTemperature));
                deviceTwinDevice.setDesiredProperties(testDesProp);
                twinExpectedTemperature.put(jobId, jobTemperature);
                jobClient.scheduleUpdateTwin(jobId, queryCondition, deviceTwinDevice, new Date(), MAX_EXECUTION_TIME_IN_SECONDS);
                JobResult jobResult = jobClient.getJob(jobId);
                while (jobResult.getJobStatus() != JobStatus.completed) {
                    Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_MILLISECONDS);
                    jobResult = jobClient.getJob(jobId);
                }
                jobResult = queryJobResponseResult(jobId, JobType.scheduleUpdateTwin, JobStatus.completed);
                jobResults.put(jobId, jobResult);
            } catch (IotHubException | IOException | InterruptedException e) {
                jobExceptions.put(jobId, e);
            }
            jobIdsPending.remove(jobId);
        });
    }
    cleanupJobs(executor, jobIdsPending);
    // Assert
    // asserts for the client side.
    assertEquals(0, deviceTestManger.getStatusError());
    ConcurrentMap<String, ConcurrentLinkedQueue<Object>> changes = deviceTestManger.getTwinChanges();
    ConcurrentLinkedQueue<Object> receivedTemperatures = changes.get(STANDARD_PROPERTY_HOMETEMP);
    assertNotNull(receivedTemperatures);
    assertEquals(MAX_NUMBER_JOBS, receivedTemperatures.size());
    // asserts for the service side.
    if (jobExceptions.size() != 0) {
        for (Map.Entry<String, Exception> jobException : jobExceptions.entrySet()) {
            log.error("{} threw", jobException.getKey(), jobException.getValue());
        }
        fail("Service throw an exception enqueuing jobs");
    }
    assertEquals("Missing job result", MAX_NUMBER_JOBS, jobResults.size());
    for (Map.Entry<String, JobResult> job : jobResults.entrySet()) {
        String jobId = job.getKey();
        JobResult jobResult = job.getValue();
        assertNotNull(jobResult);
        assertEquals("JobResult reported incorrect jobId", jobId, jobResult.getJobId());
        String expectedTemperature = twinExpectedTemperature.get(jobId) + ".0";
        assertTrue("Device do not change " + STANDARD_PROPERTY_HOMETEMP + " to " + expectedTemperature, receivedTemperatures.contains(expectedTemperature));
    }
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) HashSet(java.util.HashSet) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) Date(java.util.Date) DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Ignore(org.junit.Ignore) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 8 with DeviceTestManager

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager in project azure-iot-sdk-java by Azure.

the class JobClientTests method setUp.

@BeforeClass
public static void setUp() throws IOException, IotHubException, InterruptedException, URISyntaxException {
    iotHubConnectionString = Tools.retrieveEnvironmentVariableValue(TestConstants.IOT_HUB_CONNECTION_STRING_ENV_VAR_NAME);
    isBasicTierHub = Boolean.parseBoolean(Tools.retrieveEnvironmentVariableValue(TestConstants.IS_BASIC_TIER_HUB_ENV_VAR_NAME));
    isPullRequest = Boolean.parseBoolean(Tools.retrieveEnvironmentVariableValue(TestConstants.IS_PULL_REQUEST));
    jobClient = new JobClient(iotHubConnectionString);
    registryManager = new RegistryManager(iotHubConnectionString, RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    String uuid = UUID.randomUUID().toString();
    for (int i = 0; i < MAX_DEVICES; i++) {
        testDevice = Tools.addDeviceWithRetry(registryManager, Device.createFromId(DEVICE_ID_NAME.concat("-" + i + "-" + uuid), DeviceStatus.Enabled, null));
        DeviceTestManager testManager = new DeviceTestManager(new DeviceClient(registryManager.getDeviceConnectionString(testDevice), IotHubClientProtocol.AMQPS));
        testManager.client.open();
        testManager.subscribe(true, true);
        devices.add(testManager);
    }
}
Also used : DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) BeforeClass(org.junit.BeforeClass)

Example 9 with DeviceTestManager

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager in project azure-iot-sdk-java by Azure.

the class JobClientTests method cancelScheduleDeviceMethodSucceed.

@Test(timeout = TEST_TIMEOUT_MILLISECONDS)
@ContinuousIntegrationTest
@Ignore
public void cancelScheduleDeviceMethodSucceed() throws InterruptedException {
    // Arrange
    ExecutorService executor = Executors.newFixedThreadPool(MAX_NUMBER_JOBS);
    DeviceTestManager deviceTestManger = devices.get(0);
    final String deviceId = testDevice.getDeviceId();
    final String queryCondition = "DeviceId IN ['" + deviceId + "']";
    // 3 minutes in the future.
    final Date future = new Date(new Date().getTime() + 180000L);
    final ConcurrentMap<String, Exception> jobExceptions = new ConcurrentHashMap<>();
    final ArrayList<String> jobIdsPending = new ArrayList<>();
    // Act
    for (int i = 0; i < MAX_NUMBER_JOBS; i++) {
        final int index = i;
        executor.submit(() -> {
            String jobId = JOB_ID_NAME + UUID.randomUUID();
            jobIdsPending.add(jobId);
            try {
                jobClient.scheduleDeviceMethod(jobId, queryCondition, DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, PAYLOAD_STRING, (index % 2 == 0) ? future : new Date(), MAX_EXECUTION_TIME_IN_SECONDS);
                JobStatus expectedJobStatus = JobStatus.completed;
                if (index % 2 == 0) {
                    expectedJobStatus = JobStatus.cancelled;
                    // wait 1 seconds and cancel.
                    Thread.sleep(1000);
                    jobClient.cancelJob(jobId);
                }
                JobResult jobResult = jobClient.getJob(jobId);
                while (jobResult.getJobStatus() != expectedJobStatus) {
                    Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_MILLISECONDS);
                    jobResult = jobClient.getJob(jobId);
                }
                log.info("Iothub confirmed {} {} for type {}", jobId, expectedJobStatus, JobType.scheduleDeviceMethod);
            } catch (IotHubException | IOException | InterruptedException e) {
                jobExceptions.put(jobId, e);
            }
            jobIdsPending.remove(jobId);
        });
    }
    cleanupJobs(executor, jobIdsPending);
    // asserts for the service side.
    if (jobExceptions.size() != 0) {
        for (Map.Entry<String, Exception> jobException : jobExceptions.entrySet()) {
            log.error("{} threw", jobException.getKey(), jobException.getValue());
        }
        fail("Service throw an exception enqueuing jobs");
    }
    // asserts for the client side.
    assertEquals(0, deviceTestManger.getStatusError());
}
Also used : JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IOException(java.io.IOException) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) JobStatus(com.microsoft.azure.sdk.iot.service.jobs.JobStatus) DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Ignore(org.junit.Ignore) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)

Example 10 with DeviceTestManager

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager in project azure-iot-sdk-java by Azure.

the class DeviceMethodTests method invokeMethodNullPayloadSucceed.

@Test
@StandardTierHubOnlyTest
@ContinuousIntegrationTest
public void invokeMethodNullPayloadSucceed() throws Exception {
    // Arrange
    super.openDeviceClientAndSubscribeToMethods();
    DeviceTestManager deviceTestManger = this.testInstance.deviceTestManager;
    // Act
    MethodResult result;
    if (testInstance.identity instanceof TestModuleIdentity) {
        result = testInstance.methodServiceClient.invoke(testInstance.identity.getDeviceId(), ((TestModuleIdentity) testInstance.identity).getModuleId(), DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, null);
    } else {
        result = testInstance.methodServiceClient.invoke(testInstance.identity.getDeviceId(), DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, null);
    }
    deviceTestManger.waitIotHub(1, 10);
    // Assert
    assertNotNull(buildExceptionMessage("method result was null", testInstance.deviceTestManager.client), result);
    assertEquals(buildExceptionMessage("Expected SUCCESS but got " + result.getStatus(), testInstance.deviceTestManager.client), (long) DeviceEmulator.METHOD_SUCCESS, (long) result.getStatus());
    assertEquals(buildExceptionMessage("Expected " + DeviceEmulator.METHOD_LOOPBACK + ":null" + " but got " + result.getPayload(), deviceTestManger.client), DeviceEmulator.METHOD_LOOPBACK + ":null", result.getPayload());
    Assert.assertEquals(buildExceptionMessage("Unexpected status errors occurred", testInstance.deviceTestManager.client), 0, deviceTestManger.getStatusError());
}
Also used : DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) TestModuleIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestModuleIdentity) 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) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)

Aggregations

DeviceTestManager (tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager)11 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)6 MethodResult (com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)6 Test (org.junit.Test)6 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)6 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)6 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)5 Date (java.util.Date)5 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)4 IotHubUnathorizedException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException)4 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 ExecutorService (java.util.concurrent.ExecutorService)4 Ignore (org.junit.Ignore)4 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)4 TestModuleIdentity (tests.integration.com.microsoft.azure.sdk.iot.helpers.TestModuleIdentity)3