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());
}
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));
}
}
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);
}
}
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());
}
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());
}
Aggregations