Search in sources :

Example 91 with IotHubConnectionString

use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registrationsUnwindForClientOfDifferentHostName.

@Test
public void registrationsUnwindForClientOfDifferentHostName() throws Exception {
    Device nonMultiplexedDevice = Tools.getTestDevice(iotHubConnectionString, testInstance.protocol, AuthenticationType.SAS, false).getDevice();
    String deviceConnectionString = registryManager.getDeviceConnectionString(nonMultiplexedDevice);
    // intentionally change the hostname of the device to simulate registering a device with a different hostname
    // to a multiplexing client. It shouldn't matter that the hostname itself isn't tied to an actual IoT Hub since
    // no network requests should be made before this hostname validation.
    String actualHostName = IotHubConnectionString.createConnectionString(iotHubConnectionString).getHostName();
    deviceConnectionString = deviceConnectionString.replace(actualHostName, "some-fake-host-name.azure-devices.net");
    DeviceClient deviceClientWithDifferentHostName = new DeviceClient(deviceConnectionString, testInstance.protocol);
    registrationsUnwindForUnsupportedOperationExceptions(deviceClientWithDifferentHostName);
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 92 with IotHubConnectionString

use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registrationsUnwindForDifferentProtocolClient.

@Test
public void registrationsUnwindForDifferentProtocolClient() throws Exception {
    // Protocol for the new client is AMQPS if the test parameters are for AMQPS_WS, and vice versa. MultiplexingClient
    // should throw an exception since this new client's protocol doesn't match, even though both AMQPS and AMQPS_WS are valid
    // protocols
    IotHubClientProtocol protocol = testInstance.protocol == IotHubClientProtocol.AMQPS ? IotHubClientProtocol.AMQPS_WS : IotHubClientProtocol.AMQPS;
    Device newDevice = Tools.getTestDevice(iotHubConnectionString, protocol, AuthenticationType.SAS, false).getDevice();
    String deviceConnectionString = registryManager.getDeviceConnectionString(newDevice);
    DeviceClient differentProtocolDeviceClient = new DeviceClient(deviceConnectionString, protocol);
    registrationsUnwindForUnsupportedOperationExceptions(differentProtocolDeviceClient);
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubClientProtocol(com.microsoft.azure.sdk.iot.device.IotHubClientProtocol) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 93 with IotHubConnectionString

use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method invokeMethodSucceed.

@Test
@StandardTierHubOnlyTest
public void invokeMethodSucceed() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    testInstance.multiplexingClient.open();
    DeviceMethod deviceMethodServiceClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        // Subscribe to methods on the multiplexed client
        String expectedMethodName = UUID.randomUUID().toString();
        DeviceMethodCallback deviceMethodCallback = new DeviceMethodCallback(expectedMethodName);
        subscribeToDeviceMethod(testInstance.deviceClientArray.get(i), deviceMethodCallback);
        testDeviceMethod(deviceMethodServiceClient, testInstance.deviceIdentityArray.get(i).getDeviceId(), expectedMethodName, deviceMethodCallback);
    }
    testInstance.multiplexingClient.close();
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 94 with IotHubConnectionString

use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.

the class Tools method addModules.

private static void addModules(Iterable<Module> modules, String connectionString) throws IOException, IotHubException {
    if (modules == null) {
        throw new IllegalArgumentException("modules cannot be null");
    }
    IotHubConnectionString iotHubConnectionString = IotHubConnectionString.createConnectionString(connectionString);
    URL url = getBulkDeviceAddUrl(iotHubConnectionString);
    List<ExportImportDeviceParser> parsers = new ArrayList<>();
    for (Module module : modules) {
        ExportImportDeviceParser exportImportDevice = new ExportImportDeviceParser();
        exportImportDevice.setId(module.getDeviceId());
        exportImportDevice.setModuleId(module.getId());
        AuthenticationParser authenticationParser = new AuthenticationParser();
        if (module.getAuthenticationType() == AuthenticationType.SAS) {
            authenticationParser.setType(AuthenticationTypeParser.SAS);
            authenticationParser.setSymmetricKey(new SymmetricKeyParser(module.getSymmetricKey().getPrimaryKey(), module.getSymmetricKey().getSecondaryKey()));
        } else {
            authenticationParser.setType(AuthenticationTypeParser.SELF_SIGNED);
            authenticationParser.setThumbprint(new X509ThumbprintParser(module.getPrimaryThumbprint(), module.getSecondaryThumbprint()));
        }
        exportImportDevice.setAuthentication(authenticationParser);
        exportImportDevice.setImportMode(IMPORT_MODE_CREATE);
        parsers.add(exportImportDevice);
    }
    ExportImportDevicesParser body = new ExportImportDevicesParser();
    body.setExportImportDevices(parsers);
    bulkRegistryOperation(body.toJson(), url, connectionString);
}
Also used : AuthenticationParser(com.microsoft.azure.sdk.iot.deps.serializer.AuthenticationParser) SymmetricKeyParser(com.microsoft.azure.sdk.iot.deps.serializer.SymmetricKeyParser) ExportImportDeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser) ArrayList(java.util.ArrayList) X509ThumbprintParser(com.microsoft.azure.sdk.iot.deps.serializer.X509ThumbprintParser) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Module(com.microsoft.azure.sdk.iot.service.Module) URL(java.net.URL)

Example 95 with IotHubConnectionString

use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.

the class Tools method getX509TestModule.

private static TestModuleIdentity getX509TestModule(String iotHubConnectionString, IotHubClientProtocol protocol, boolean needCleanTwin) throws URISyntaxException, IOException, IotHubException, InterruptedException, ModuleClientException, GeneralSecurityException {
    // remove an identity from the queue.
    synchronized (testX509ModuleQueueLock) {
        TestModuleIdentity testModuleIdentity;
        if (!needCleanTwin && testX509ModuleWithTwinQueue.size() > 0) {
            log.debug("Acquiring test module from testX509ModuleWithTwinQueue");
            testModuleIdentity = testX509ModuleWithTwinQueue.remove();
        } else {
            if (testX509ModuleQueue.size() < 1) {
                // No cached modules to return, so create a new set of modules to cache, and return one of the newly created modules
                log.debug("Proactively adding another {} modules to the SAS test module queue", PROACTIVE_TEST_DEVICE_REGISRATION_COUNT);
                List<Device> devices = new ArrayList<>();
                List<Module> modulesToAdd = new ArrayList<>();
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    TestDeviceIdentity testDeviceIdentity = getTestDevice(iotHubConnectionString, protocol, AuthenticationType.SELF_SIGNED, needCleanTwin);
                    devices.add(testDeviceIdentity.device);
                    Module module = Module.createModule(testDeviceIdentity.device.getDeviceId(), "test-module-" + UUID.randomUUID(), AuthenticationType.SELF_SIGNED);
                    String x509Thumbprint = IntegrationTest.x509CertificateGenerator.getX509Thumbprint();
                    module.setThumbprintFinal(x509Thumbprint, x509Thumbprint);
                    modulesToAdd.add(module);
                }
                addModules(modulesToAdd, iotHubConnectionString);
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    testX509ModuleQueue.add(new TestModuleIdentity(null, devices.get(i), modulesToAdd.get(i)));
                }
            }
            log.debug("Acquiring test module from testX509ModuleQueue");
            testModuleIdentity = testX509ModuleQueue.remove();
        }
        SSLContext sslContext = SSLContextBuilder.buildSSLContext(IntegrationTest.x509CertificateGenerator.getPublicCertificate(), IntegrationTest.x509CertificateGenerator.getPrivateKey());
        ModuleClient moduleClient = new ModuleClient(DeviceConnectionString.get(iotHubConnectionString, testModuleIdentity.device, testModuleIdentity.module), protocol, sslContext);
        testModuleIdentity.setModuleClient(moduleClient);
        return testModuleIdentity;
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) SSLContext(javax.net.ssl.SSLContext) Module(com.microsoft.azure.sdk.iot.service.Module) ModuleClient(com.microsoft.azure.sdk.iot.device.ModuleClient)

Aggregations

IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)196 Test (org.junit.Test)171 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)30 Expectations (mockit.Expectations)21 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)20 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)17 AzureSasCredential (com.azure.core.credential.AzureSasCredential)16 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)13 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)13 Device (com.microsoft.azure.sdk.iot.service.Device)12 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)12 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)12 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)11 URL (java.net.URL)11 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)11 NonStrictExpectations (mockit.NonStrictExpectations)10 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)9 ArrayList (java.util.ArrayList)9 Verifications (mockit.Verifications)9 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)8