Search in sources :

Example 21 with StandardTierHubOnlyTest

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

the class DeviceMethodTests method serviceClientTokenRenewalWithAzureSasCredential.

@Test
@StandardTierHubOnlyTest
public void serviceClientTokenRenewalWithAzureSasCredential() throws Exception {
    if (testInstance.protocol != IotHubClientProtocol.AMQPS || testInstance.clientType != ClientType.DEVICE_CLIENT || testInstance.authenticationType != AuthenticationType.SAS) {
        // This test is for the service client, so no need to rerun it for all the different client types or device protocols
        return;
    }
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
    this.testInstance.methodServiceClient = new DeviceMethod(iotHubConnectionStringObj.getHostName(), sasCredential, DeviceMethodClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    super.openDeviceClientAndSubscribeToMethods();
    // add first device just to make sure that the first credential update worked
    super.invokeMethodSucceed();
    // deliberately expire the SAS token to provoke a 401 to ensure that the method client is using the shared
    // access signature that is set here.
    sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
    try {
        super.invokeMethodSucceed();
        fail("Expected invoke method call to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
    } catch (IotHubUnathorizedException e) {
        log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
    }
    // Renew the expired shared access signature
    serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    sasCredential.update(serviceSasToken.toString());
    // final method invocation should succeed since the shared access signature has been renewed
    super.invokeMethodSucceed();
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) 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)

Example 22 with StandardTierHubOnlyTest

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

the class DeviceMethodTests method invokeMethodWithServiceSideProxy.

@Test
@StandardTierHubOnlyTest
public void invokeMethodWithServiceSideProxy() throws Exception {
    if (testInstance.protocol != IotHubClientProtocol.MQTT || testInstance.authenticationType != AuthenticationType.SAS || testInstance.clientType != ClientType.DEVICE_CLIENT) {
        // when the device is using MQTT with SAS auth
        return;
    }
    String testProxyHostname = "127.0.0.1";
    int testProxyPort = 8894;
    HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap().withPort(testProxyPort).start();
    try {
        Proxy serviceSideProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
        ProxyOptions proxyOptions = new ProxyOptions(serviceSideProxy);
        DeviceMethodClientOptions options = DeviceMethodClientOptions.builder().proxyOptions(proxyOptions).httpReadTimeout(HTTP_READ_TIMEOUT).build();
        this.testInstance.methodServiceClient = DeviceMethod.createFromConnectionString(iotHubConnectionString, options);
        super.openDeviceClientAndSubscribeToMethods();
        super.invokeMethodSucceed();
    } finally {
        proxyServer.stop();
    }
}
Also used : DefaultHttpProxyServer(org.littleshoot.proxy.impl.DefaultHttpProxyServer) HttpProxyServer(org.littleshoot.proxy.HttpProxyServer) Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) DeviceMethodClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions) InetSocketAddress(java.net.InetSocketAddress) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) 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)

Example 23 with StandardTierHubOnlyTest

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

the class TransportClientTests method testTwin.

@Test
@StandardTierHubOnlyTest
public void testTwin() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, URISyntaxException {
    testInstance.setup(true);
    testInstance.transportClient = setUpTwin();
    ExecutorService executor = Executors.newFixedThreadPool(MAX_PROPERTIES_TO_TEST);
    // Testing subscribing to desired properties.
    for (int i = 0; i < MAX_DEVICES; i++) {
        testInstance.devicesUnderTest[i].dCDeviceForTwin.getDesiredProp().clear();
        for (int j = 0; j < MAX_PROPERTIES_TO_TEST; j++) {
            PropertyState propertyState = new PropertyState();
            propertyState.callBackTriggered = false;
            propertyState.property = new Property(PROPERTY_KEY + j, PROPERTY_VALUE);
            testInstance.devicesUnderTest[i].dCDeviceForTwin.propertyStateList.add(propertyState);
            testInstance.devicesUnderTest[i].dCDeviceForTwin.setDesiredPropertyCallback(propertyState.property, testInstance.devicesUnderTest[i].dCDeviceForTwin, propertyState);
        }
        // act
        testInstance.devicesUnderTest[i].deviceClient.subscribeToDesiredProperties(testInstance.devicesUnderTest[i].dCDeviceForTwin.getDesiredProp());
        Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_TWIN_OPERATION_MILLISECONDS);
        Set<Pair> desiredProperties = new HashSet<>();
        for (int j = 0; j < MAX_PROPERTIES_TO_TEST; j++) {
            desiredProperties.add(new Pair(PROPERTY_KEY + j, PROPERTY_VALUE_UPDATE + UUID.randomUUID()));
        }
        testInstance.devicesUnderTest[i].sCDeviceForTwin.setDesiredProperties(desiredProperties);
        testInstance.deviceTwinClient.updateTwin(testInstance.devicesUnderTest[i].sCDeviceForTwin);
        Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_TWIN_OPERATION_MILLISECONDS);
        // assert
        Assert.assertEquals(buildExceptionMessage("Device twin status expected to be SUCCESS, but was: " + testInstance.devicesUnderTest[i].deviceTwinStatus, testInstance.devicesUnderTest[i].deviceClient), SUCCESS, testInstance.devicesUnderTest[i].deviceTwinStatus);
        for (PropertyState propertyState : testInstance.devicesUnderTest[i].dCDeviceForTwin.propertyStateList) {
            Assert.assertTrue(buildExceptionMessage("One or more property callbacks were not triggered", testInstance.devicesUnderTest[i].deviceClient), propertyState.callBackTriggered);
            Assert.assertTrue(buildExceptionMessage("Property new value did not start with " + PROPERTY_VALUE_UPDATE, testInstance.devicesUnderTest[i].deviceClient), ((String) propertyState.propertyNewValue).startsWith(PROPERTY_VALUE_UPDATE));
            Assert.assertEquals(buildExceptionMessage("Expected SUCCESS but device twin status was " + testInstance.devicesUnderTest[i].deviceTwinStatus, testInstance.devicesUnderTest[i].deviceClient), SUCCESS, testInstance.devicesUnderTest[i].deviceTwinStatus);
        }
    }
    // Testing updating reported properties
    for (int i = 0; i < MAX_DEVICES; i++) {
        // act
        // send max_prop RP all at once
        testInstance.devicesUnderTest[i].dCDeviceForTwin.createNewReportedProperties(MAX_PROPERTIES_TO_TEST);
        testInstance.devicesUnderTest[i].deviceClient.sendReportedProperties(testInstance.devicesUnderTest[i].dCDeviceForTwin.getReportedProp());
        Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_TWIN_OPERATION_MILLISECONDS);
        // act
        // Update RP
        testInstance.devicesUnderTest[i].dCDeviceForTwin.updateAllExistingReportedProperties();
        testInstance.devicesUnderTest[i].deviceClient.sendReportedProperties(testInstance.devicesUnderTest[i].dCDeviceForTwin.getReportedProp());
        Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_TWIN_OPERATION_MILLISECONDS);
        // assert
        Assert.assertEquals(buildExceptionMessage("Expected status SUCCESS but was " + testInstance.devicesUnderTest[i].deviceTwinStatus, testInstance.devicesUnderTest[i].deviceClient), SUCCESS, testInstance.devicesUnderTest[i].deviceTwinStatus);
        // verify if they are received by SC
        Thread.sleep(MAXIMUM_TIME_FOR_IOTHUB_PROPAGATION_BETWEEN_DEVICE_SERVICE_CLIENTS_MILLISECONDS);
        int actualReportedPropFound = readReportedProperties(testInstance.devicesUnderTest[i], PROPERTY_KEY, PROPERTY_VALUE_UPDATE);
        Assert.assertEquals(buildExceptionMessage("Missing reported properties on the " + (i + 1) + " device out of " + MAX_DEVICE_MULTIPLEX, testInstance.devicesUnderTest[i].deviceClient), MAX_PROPERTIES_TO_TEST.intValue(), actualReportedPropFound);
    }
    // Testing sending reported properties, send max_prop RP one at a time in parallel
    for (int i = 0; i < MAX_PROPERTIES_TO_TEST; i++) {
        final int finalI = i;
        executor.submit(() -> {
            // testSendReportedPropertiesMultiThreaded
            try {
                testInstance.devicesUnderTest[finalI].dCDeviceForTwin.createNewReportedProperties(1);
                testInstance.devicesUnderTest[finalI].deviceClient.sendReportedProperties(testInstance.devicesUnderTest[finalI].dCDeviceForTwin.getReportedProp());
            } catch (IOException e) {
                Assert.fail(buildExceptionMessage(e.getMessage(), testInstance.devicesUnderTest[finalI].deviceClient));
            }
            Assert.assertEquals(buildExceptionMessage("Expected SUCCESS but was " + testInstance.devicesUnderTest[finalI].deviceTwinStatus, testInstance.devicesUnderTest[finalI].deviceClient), SUCCESS, testInstance.devicesUnderTest[finalI].deviceTwinStatus);
            // testUpdateReportedPropertiesMultiThreaded
            try {
                testInstance.devicesUnderTest[finalI].dCDeviceForTwin.updateExistingReportedProperty(finalI);
                testInstance.devicesUnderTest[finalI].deviceClient.sendReportedProperties(testInstance.devicesUnderTest[finalI].dCDeviceForTwin.getReportedProp());
            } catch (IOException e) {
                Assert.fail(buildExceptionMessage(e.getMessage(), testInstance.devicesUnderTest[finalI].deviceClient));
            }
            Assert.assertEquals(buildExceptionMessage("Expected SUCCESS but was " + testInstance.devicesUnderTest[finalI].deviceTwinStatus, testInstance.devicesUnderTest[finalI].deviceClient), SUCCESS, testInstance.devicesUnderTest[finalI].deviceTwinStatus);
        });
    }
    Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_TWIN_OPERATION_MILLISECONDS);
    executor.shutdown();
    if (// 4 minutes
    !executor.awaitTermination(MULTITHREADED_WAIT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS)) {
        executor.shutdownNow();
        fail(buildExceptionMessage("Test threads did not finish before timeout", testInstance.devicesUnderTest[0].deviceClient));
    }
    tearDownTwin(testInstance.transportClient);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) 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)

Example 24 with StandardTierHubOnlyTest

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

the class TransportClientTests method invokeMethodSucceed.

@Test
@StandardTierHubOnlyTest
public void invokeMethodSucceed() throws Exception {
    testInstance.setup(false);
    testInstance.transportClient.open();
    for (int i = 0; i < testInstance.clientArrayList.size(); i++) {
        DeviceMethodStatusCallBack subscribedCallback = new DeviceMethodStatusCallBack();
        ((DeviceClient) testInstance.clientArrayList.get(i)).subscribeToDeviceMethod(new SampleDeviceMethodCallback(), null, subscribedCallback, null);
        long startTime = System.currentTimeMillis();
        while (!subscribedCallback.isSubscribed) {
            Thread.sleep(200);
            if (System.currentTimeMillis() - startTime > METHOD_SUBSCRIBE_TIMEOUT_MILLISECONDS) {
                fail(buildExceptionMessage("Timed out waiting for device to subscribe to methods", testInstance.clientArrayList.get(i)));
            }
        }
        CountDownLatch countDownLatch = new CountDownLatch(1);
        RunnableInvoke runnableInvoke = new RunnableInvoke(methodServiceClient, testInstance.devicesList[i].getDeviceId(), METHOD_NAME, METHOD_PAYLOAD, countDownLatch);
        new Thread(runnableInvoke).start();
        countDownLatch.await(3, TimeUnit.MINUTES);
        MethodResult result = runnableInvoke.getResult();
        Assert.assertNotNull(buildExceptionMessage(runnableInvoke.getException() == null ? "Runnable returns null without exception information" : runnableInvoke.getException().getMessage(), testInstance.clientArrayList.get(i)), result);
        Assert.assertEquals(buildExceptionMessage("result was not success, but was: " + result.getStatus(), testInstance.clientArrayList.get(i)), (long) METHOD_SUCCESS, (long) result.getStatus());
        Assert.assertEquals(buildExceptionMessage("Received unexpected payload", testInstance.clientArrayList.get(i)), runnableInvoke.getExpectedPayload(), METHOD_NAME + ":" + result.getPayload().toString());
    }
    testInstance.transportClient.closeNow();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) 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)

Example 25 with StandardTierHubOnlyTest

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

the class TransportClientTests method invokeMethodInvokeParallelSucceed.

@Test
@StandardTierHubOnlyTest
@ContinuousIntegrationTest
public void invokeMethodInvokeParallelSucceed() throws Exception {
    testInstance.setup(false);
    testInstance.transportClient.open();
    for (int i = 0; i < testInstance.clientArrayList.size(); i++) {
        DeviceMethodStatusCallBack subscribedCallback = new DeviceMethodStatusCallBack();
        ((DeviceClient) testInstance.clientArrayList.get(i)).subscribeToDeviceMethod(new SampleDeviceMethodCallback(), null, subscribedCallback, null);
        long startTime = System.currentTimeMillis();
        while (!subscribedCallback.isSubscribed) {
            Thread.sleep(200);
            if (System.currentTimeMillis() - startTime > METHOD_SUBSCRIBE_TIMEOUT_MILLISECONDS) {
                fail(buildExceptionMessage("Timed out waiting for device to subscribe to methods", testInstance.clientArrayList.get(i)));
            }
        }
    }
    List<RunnableInvoke> runs = new LinkedList<>();
    CountDownLatch countDownLatch = new CountDownLatch(MAX_DEVICE_MULTIPLEX);
    for (int i = 0; i < testInstance.clientArrayList.size(); i++) {
        RunnableInvoke runnableInvoke = new RunnableInvoke(methodServiceClient, testInstance.devicesList[i].getDeviceId(), METHOD_NAME, METHOD_PAYLOAD, countDownLatch);
        new Thread(runnableInvoke).start();
        runs.add(runnableInvoke);
    }
    countDownLatch.await(3, TimeUnit.MINUTES);
    Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_PER_CALL_MILLISECONDS * MAX_DEVICE_MULTIPLEX);
    for (RunnableInvoke runnableInvoke : runs) {
        MethodResult result = runnableInvoke.getResult();
        Assert.assertNotNull(CorrelationDetailsLoggingAssert.buildExceptionMessage(runnableInvoke.getException() == null ? "Runnable returns null without exception information" : runnableInvoke.getException().getMessage(), testInstance.clientArrayList), result);
        Assert.assertEquals(CorrelationDetailsLoggingAssert.buildExceptionMessage("result was not success, but was: " + result.getStatus(), testInstance.clientArrayList), (long) METHOD_SUCCESS, (long) result.getStatus());
        Assert.assertEquals(CorrelationDetailsLoggingAssert.buildExceptionMessage("Received unexpected payload", testInstance.clientArrayList), runnableInvoke.getExpectedPayload(), METHOD_NAME + ":" + result.getPayload().toString());
    }
    testInstance.transportClient.closeNow();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) 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) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)

Aggregations

StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)45 Test (org.junit.Test)40 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)32 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)31 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)28 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)19 DigitalTwinTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest)12 AzureSasCredential (com.azure.core.credential.AzureSasCredential)10 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)10 DigitalTwinGetHeaders (com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders)9 BasicDigitalTwin (com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin)9 Device (com.microsoft.azure.sdk.iot.service.Device)8 IOException (java.io.IOException)8 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)7 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)7 Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)6 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)6 DigitalTwinClient (com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient)6 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)5 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)5