Search in sources :

Example 1 with ContinuousIntegrationTest

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

the class JobClientTests method mixScheduleInFutureSucceed.

@Test(timeout = TEST_TIMEOUT_MILLISECONDS)
@ContinuousIntegrationTest
@Ignore
public void mixScheduleInFutureSucceed() 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 + "']";
    // 10 seconds in the future.
    final Date future = new Date(new Date().getTime() + 10000L);
    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 index = i;
        final int jobTemperature = (newTemperature++);
        executor.submit(() -> {
            String jobId = JOB_ID_NAME + UUID.randomUUID();
            jobIdsPending.add(jobId);
            try {
                if (index % 2 == 0) {
                    jobClient.scheduleDeviceMethod(jobId, queryCondition, DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, PAYLOAD_STRING, future, MAX_EXECUTION_TIME_IN_SECONDS);
                } else {
                    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 = queryDeviceJobResult(jobId, ((index % 2 == 0) ? JobType.scheduleDeviceMethod : JobType.scheduleUpdateTwin), JobStatus.completed);
                jobResults.put(jobId, jobResult);
            } catch (IotHubException | IOException | InterruptedException e) {
                jobExceptions.put(jobId, e);
            }
            jobIdsPending.remove(jobId);
        });
    }
    cleanupJobs(executor, jobIdsPending);
    // wait until identity receive the twin change
    ConcurrentMap<String, ConcurrentLinkedQueue<Object>> changes = deviceTestManger.getTwinChanges();
    int timeout = 0;
    while (changes.size() == 0) {
        if ((timeout += MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_MILLISECONDS) >= TEST_TIMEOUT_MILLISECONDS) {
            fail("Device didn't receive the twin change");
        }
        Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_MILLISECONDS);
        changes = deviceTestManger.getTwinChanges();
    }
    // 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());
    ConcurrentLinkedQueue<Object> temperatures = changes.get(STANDARD_PROPERTY_HOMETEMP);
    assertNotNull("There is no " + STANDARD_PROPERTY_HOMETEMP + " in the device changes", temperatures);
    for (Map.Entry<String, JobResult> job : jobResults.entrySet()) {
        JobResult jobResult = job.getValue();
        String jobId = jobResult.getJobId();
        assertNotNull(jobResult);
        if (jobResult.getJobType() == JobType.scheduleDeviceMethod) {
            MethodResult methodResult = jobResult.getOutcomeResult();
            assertNotNull("Device method didn't return any outcome", methodResult);
            assertEquals(200L, (long) methodResult.getStatus());
            assertEquals(DeviceEmulator.METHOD_LOOPBACK + ":" + PAYLOAD_STRING, methodResult.getPayload());
        } else {
            String temperature = twinExpectedTemperature.get(jobId) + ".0";
            assertTrue("Device do not change " + STANDARD_PROPERTY_HOMETEMP + " to " + temperature, temperatures.contains(temperature));
        }
    }
    // asserts for the client side.
    assertEquals(0, deviceTestManger.getStatusError());
}
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) 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) 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) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) 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 2 with ContinuousIntegrationTest

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

the class RegistryManagerTests method getDeviceStatisticsTest.

// TODO what is this testing?
@Test
@ContinuousIntegrationTest
public void getDeviceStatisticsTest() throws Exception {
    RegistryManager registryManager = RegistryManager.createFromConnectionString(iotHubConnectionString, RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    Tools.getStatisticsWithRetry(registryManager);
}
Also used : RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) 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) 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)

Example 3 with ContinuousIntegrationTest

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

the class ServiceClientTests method serviceClientValidatesRemoteCertificateWhenSendingTelemetry.

// The IoT Hub instance we use for this test is currently offline, so this test cannot be run
@Ignore
@Test
@ContinuousIntegrationTest
public void serviceClientValidatesRemoteCertificateWhenSendingTelemetry() throws IOException {
    boolean expectedExceptionWasCaught = false;
    ServiceClient serviceClient = ServiceClient.createFromConnectionString(invalidCertificateServerConnectionString, testInstance.protocol);
    try {
        serviceClient.open();
        // don't need a real device Id since the request is sent to a fake service
        serviceClient.send("some deviceId", new Message("some message"));
    } catch (IOException e) {
        expectedExceptionWasCaught = true;
    } catch (Exception e) {
        fail(buildExceptionMessage("Expected IOException, but received: " + Tools.getStackTraceFromThrowable(e), hostName));
    }
    assertTrue(buildExceptionMessage("Expected an exception due to service presenting invalid certificate", hostName), expectedExceptionWasCaught);
}
Also used : Message(com.microsoft.azure.sdk.iot.service.Message) CorrelationDetailsLoggingAssert.buildExceptionMessage(tests.integration.com.microsoft.azure.sdk.iot.helpers.CorrelationDetailsLoggingAssert.buildExceptionMessage) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) IOException(java.io.IOException) IOException(java.io.IOException) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) 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) 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)

Example 4 with ContinuousIntegrationTest

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

the class SendMessagesTests method tokenExpiredAfterOpenButBeforeSendHttp.

@Test
@ContinuousIntegrationTest
public void tokenExpiredAfterOpenButBeforeSendHttp() throws Exception {
    final long SECONDS_FOR_SAS_TOKEN_TO_LIVE = 3;
    final long MILLISECONDS_TO_WAIT_FOR_TOKEN_TO_EXPIRE = 5000;
    if (testInstance.protocol != HTTPS || testInstance.authenticationType != SAS || testInstance.useHttpProxy) {
        // as long as token did not expire before open. X509 doesn't apply either
        return;
    }
    this.testInstance.setup();
    String soonToBeExpiredSASToken = generateSasTokenForIotDevice(hostName, testInstance.identity.getDeviceId(), ((TestDeviceIdentity) testInstance.identity).getDevice().getPrimaryKey(), SECONDS_FOR_SAS_TOKEN_TO_LIVE);
    DeviceClient client = new DeviceClient(soonToBeExpiredSASToken, testInstance.protocol);
    client.open();
    // Force the SAS token to expire before sending messages
    Thread.sleep(MILLISECONDS_TO_WAIT_FOR_TOKEN_TO_EXPIRE);
    IotHubServicesCommon.sendMessagesExpectingSASTokenExpiration(client, testInstance.protocol.toString(), 1, RETRY_MILLISECONDS, SEND_TIMEOUT_MILLISECONDS, testInstance.authenticationType);
    client.closeNow();
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) TestDeviceIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) Test(org.junit.Test) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)

Example 5 with ContinuousIntegrationTest

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

the class DeviceTwinWithVersionTests method testUpdateReportedPropertiesWithLowerVersionFailed.

@Test
@StandardTierHubOnlyTest
@ContinuousIntegrationTest
public void testUpdateReportedPropertiesWithLowerVersionFailed() throws IOException, InterruptedException, URISyntaxException, IotHubException {
    // arrange
    createDevice(testInstance.protocol);
    testInstance.deviceTwinWithVersionTestDevice.expectedProperties = new HashSet<>(PROPERTIES);
    // Create the first version of the reported properties.
    testInstance.deviceTwinWithVersionTestDevice.deviceTwinStatus = STATUS.UNKNOWN;
    testInstance.deviceTwinWithVersionTestDevice.deviceClient.sendReportedProperties(PROPERTIES);
    Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB);
    testInstance.deviceTwinWithVersionTestDevice.deviceClient.getDeviceTwin();
    long startTime = System.currentTimeMillis();
    while (!testInstance.deviceTwinWithVersionTestDevice.expectedProperties.isEmpty()) {
        if (System.currentTimeMillis() - startTime > EXPECTED_PROPERTIES_MAX_WAIT_MILLISECONDS) {
            fail(buildExceptionMessage("Timed out waiting for expected property change", testInstance.deviceTwinWithVersionTestDevice.deviceClient));
        }
        Thread.sleep(BREATHE_TIME);
        if (testInstance.deviceTwinWithVersionTestDevice.deviceTwinStatus == STATUS.IOTHUB_FAILURE) {
            throw new IOException("IoTHub send Http error code");
        }
        if (testInstance.deviceTwinWithVersionTestDevice.deviceTwinStatus == STATUS.BAD_ANSWER) {
            throw new IOException(testInstance.deviceTwinWithVersionTestDevice.exception);
        }
    }
    assertEquals(buildExceptionMessage("Expected 2, but reported properties version was " + testInstance.deviceTwinWithVersionTestDevice.reportedPropertyVersion, testInstance.deviceTwinWithVersionTestDevice.deviceClient), 2, (int) testInstance.deviceTwinWithVersionTestDevice.reportedPropertyVersion);
    // New values for the reported properties
    final Set<Property> newValues = new HashSet<Property>() {

        {
            add(new Property(PROPERTY_KEY_1, "newValue1"));
            add(new Property(PROPERTY_KEY_2, "newValue2"));
        }
    };
    testInstance.deviceTwinWithVersionTestDevice.expectedProperties = new HashSet<>(newValues);
    testInstance.deviceTwinWithVersionTestDevice.deviceTwinStatus = STATUS.UNKNOWN;
    testInstance.deviceTwinWithVersionTestDevice.reportedPropertyVersion = null;
    testInstance.deviceTwinWithVersionTestDevice.receivedProperties = new HashSet<>();
    // act
    testInstance.deviceTwinWithVersionTestDevice.deviceClient.sendReportedProperties(newValues, 1);
    // assert
    // test device client
    Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB);
    while ((testInstance.deviceTwinWithVersionTestDevice.deviceTwinStatus != STATUS.BAD_ANSWER) && (testInstance.deviceTwinWithVersionTestDevice.deviceTwinStatus != STATUS.IOTHUB_FAILURE)) {
        Thread.sleep(BREATHE_TIME);
    }
    testInstance.deviceTwinWithVersionTestDevice.expectedProperties = new HashSet<>(PROPERTIES);
    testInstance.deviceTwinWithVersionTestDevice.deviceTwinStatus = STATUS.UNKNOWN;
    testInstance.deviceTwinWithVersionTestDevice.reportedPropertyVersion = null;
    testInstance.deviceTwinWithVersionTestDevice.receivedProperties = new HashSet<>();
    testInstance.deviceTwinWithVersionTestDevice.deviceClient.getDeviceTwin();
    startTime = System.currentTimeMillis();
    while (!testInstance.deviceTwinWithVersionTestDevice.expectedProperties.isEmpty()) {
        if (System.currentTimeMillis() - startTime > EXPECTED_PROPERTIES_MAX_WAIT_MILLISECONDS) {
            fail(buildExceptionMessage("Timed out waiting for expected property change", testInstance.deviceTwinWithVersionTestDevice.deviceClient));
        }
        Thread.sleep(BREATHE_TIME);
        if (testInstance.deviceTwinWithVersionTestDevice.deviceTwinStatus == STATUS.BAD_ANSWER) {
            throw new IOException(testInstance.deviceTwinWithVersionTestDevice.exception);
        }
    }
    assertEquals(buildExceptionMessage("Expected 2, but reported properties version was " + testInstance.deviceTwinWithVersionTestDevice.reportedPropertyVersion, testInstance.deviceTwinWithVersionTestDevice.deviceClient), 2, (int) testInstance.deviceTwinWithVersionTestDevice.reportedPropertyVersion);
    // test service client
    DeviceTwinDevice deviceOnServiceClient = new DeviceTwinDevice(testInstance.deviceTwinWithVersionTestDevice.deviceId);
    testInstance.twinServiceClient.getTwin(deviceOnServiceClient);
    assertEquals(buildExceptionMessage("Expected reported properties version 2 but was " + deviceOnServiceClient.getReportedPropertiesVersion(), testInstance.deviceTwinWithVersionTestDevice.deviceClient), 2, (int) deviceOnServiceClient.getReportedPropertiesVersion());
    Set<Pair> reported = deviceOnServiceClient.getReportedProperties();
    assertSetEquals(PROPERTIES, reported);
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) IOException(java.io.IOException) Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) Pair(com.microsoft.azure.sdk.iot.service.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) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) 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

ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)22 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)22 Test (org.junit.Test)20 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)15 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)14 IOException (java.io.IOException)9 Ignore (org.junit.Ignore)6 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)5 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)5 IotHubUnathorizedException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException)5 ExecutorService (java.util.concurrent.ExecutorService)4 DeviceTestManager (tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager)4 Device (com.microsoft.azure.sdk.iot.service.Device)3 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)3 MethodResult (com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)3 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)3 ArrayList (java.util.ArrayList)3 Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)2 Message (com.microsoft.azure.sdk.iot.device.Message)2 Module (com.microsoft.azure.sdk.iot.service.Module)2