use of com.microsoft.azure.sdk.iot.service.Module in project azure-iot-sdk-java by Azure.
the class Tools method addModuleWithRetry.
public static Module addModuleWithRetry(RegistryManager registryManager, Module module) throws IotHubException, IOException, InterruptedException {
long startTime = System.currentTimeMillis();
Module ret = null;
while (System.currentTimeMillis() - startTime < RETRY_TIMEOUT_ON_NETWORK_FAILURE_MILLISECONDS) {
try {
log.debug("Attempting to add module {} to registry", module.getId());
ret = registryManager.addModule(module);
log.debug("Successfully added module {} to registry", module.getId());
break;
} catch (UnknownHostException | SocketException | SocketTimeoutException e) {
log.warn("Failed to add module " + module.getId());
e.printStackTrace();
Thread.sleep(WAIT_FOR_RETRY);
if (System.currentTimeMillis() - startTime >= RETRY_TIMEOUT_ON_NETWORK_FAILURE_MILLISECONDS) {
throw e;
}
}
}
return ret;
}
use of com.microsoft.azure.sdk.iot.service.Module 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;
}
}
use of com.microsoft.azure.sdk.iot.service.Module in project azure-iot-sdk-java by Azure.
the class ModuleTest method createModule_success.
// Tests_SRS_SERVICE_SDK_JAVA_MODULE_28_005: [The function shall create a new instance of Module using the given
// moduleId for the device with deviceId and return it]
@Test
public void createModule_success() {
// Arrange
String deviceId = "device-xxx";
String moduleId = "module-xxx";
// Act
Module module = Module.createModule(deviceId, moduleId, AuthenticationType.SAS);
// Assert
assertNotNull(module);
assertEquals(deviceId, module.getDeviceId());
assertEquals(moduleId, module.getId());
assertNotNull(module.getSymmetricKey());
}
use of com.microsoft.azure.sdk.iot.service.Module in project azure-iot-sdk-java by Azure.
the class ModuleTest method module_get_all_properties.
// Tests_SRS_SERVICE_SDK_JAVA_MODULE_28_001: [The Module class shall have the following properties: id, deviceId,
// generationId, Etag, ConnectionState, ConnectionStateUpdatedTime, LastActivityTime, cloudToDeviceMessageCount,
// authentication, managedBy.]
// Assert
@Test
public void module_get_all_properties() {
// Arrange
String deviceId = "xxx-device";
String moduleId = "xxx-module";
SymmetricKey expectedSymmetricKey = new SymmetricKey();
String expectedPrimaryThumbprint = "0000000000000000000000000000000000000000";
String expectedSecondaryThumbprint = "1111111111111111111111111111111111111111";
// Act
Module module = Module.createFromId(deviceId, moduleId, null);
module.setSymmetricKey(expectedSymmetricKey);
assertEquals(expectedSymmetricKey, module.getSymmetricKey());
module.setThumbprintFinal(expectedPrimaryThumbprint, expectedSecondaryThumbprint);
assertEquals(expectedPrimaryThumbprint, module.getPrimaryThumbprint());
assertEquals(expectedSecondaryThumbprint, module.getSecondaryThumbprint());
module.getId();
module.getManagedBy();
module.getPrimaryThumbprint();
module.getSecondaryThumbprint();
module.getDeviceId();
module.getGenerationId();
module.getPrimaryKey();
module.getSecondaryKey();
module.geteTag();
module.getConnectionState();
module.getConnectionStateUpdatedTime();
module.getLastActivityTime();
module.getCloudToDeviceMessageCount();
}
use of com.microsoft.azure.sdk.iot.service.Module in project azure-iot-sdk-java by Azure.
the class ModuleTest method createFromId_success.
// Tests_SRS_SERVICE_SDK_JAVA_MODULE_28_003: [The function shall create a new instance
// of Module using the given moduleId for device with deviceId and return it]
// Assert
@Test
public void createFromId_success() {
String deviceName = "device-xxx";
String moduleName = "module-xxx";
// Act
Module module = Module.createFromId(deviceName, moduleName, null);
// Assert
assertNotNull(module);
assertEquals(deviceName, module.getDeviceId());
assertEquals(moduleName, module.getId());
assertNotNull(module.getSymmetricKey());
}
Aggregations