use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class ServiceAuthenticationWithSharedAccessPolicyTokenTest method populate_good_case.
// Tests_SRS_SERVICE_SDK_JAVA_SERVICEAUTHENTICATIONWITHSHAREDACCESSTOKEN_12_003: [The function shall save the policyName and token to the target object]
// Tests_SRS_SERVICE_SDK_JAVA_SERVICEAUTHENTICATIONWITHSHAREDACCESSTOKEN_12_004: [The function shall set the access key to null]
@Test
public void populate_good_case() throws Exception {
// Arrange
String regex = "[a-zA-Z0-9_\\-\\.]+$";
String iotHubName = "b.c.d";
String hostName = "HOSTNAME." + iotHubName;
String sharedAccessKeyName = "ACCESSKEYNAME";
String policyName = "SharedAccessKey";
String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
String connectionString = "HostName=" + hostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
IotHubConnectionString iotHubConnectionString = IotHubConnectionStringBuilder.createConnectionString(connectionString);
String newPolicyName = "XXX";
String newPolicyToken = "YYY";
ServiceAuthenticationWithSharedAccessPolicyToken auth = new ServiceAuthenticationWithSharedAccessPolicyToken(newPolicyName, newPolicyToken);
// Act
auth.populate(iotHubConnectionString);
// Assert
assertEquals(newPolicyName, iotHubConnectionString.getSharedAccessKeyName());
assertEquals(newPolicyToken, iotHubConnectionString.getSharedAccessSignature());
assertNull(iotHubConnectionString.getSharedAccessKey());
// Act
Deencapsulation.invoke(auth, "setPolicyName", policyName);
Deencapsulation.invoke(auth, "setToken", sharedAccessKey);
// Assert
assertEquals(policyName, auth.getPolicyName());
assertEquals(sharedAccessKey, auth.getToken());
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class TokenCredentialTests method deviceLifecycleWithTokenCredential.
@Test
public void deviceLifecycleWithTokenCredential() throws Exception {
// -Create-//
RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
String deviceId = "some-device-" + UUID.randomUUID();
Device deviceAdded = Device.createFromId(deviceId, DeviceStatus.Enabled, null);
registryManager.addDevice(deviceAdded);
// -Read-//
Device deviceRetrieved = registryManager.getDevice(deviceId);
// -Update-//
Device deviceUpdated = registryManager.getDevice(deviceId);
deviceUpdated.setStatus(DeviceStatus.Disabled);
deviceUpdated = registryManager.updateDevice(deviceUpdated);
// -Delete-//
registryManager.removeDevice(deviceId);
// Assert
assertEquals(deviceId, deviceAdded.getDeviceId());
assertEquals(deviceId, deviceRetrieved.getDeviceId());
assertEquals(DeviceStatus.Disabled, deviceUpdated.getStatus());
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class JobClientTest method getJobCreateURL.
/* Tests_SRS_JOBCLIENT_21_025: [The getJob shall create a URL for Jobs using the iotHubConnectionString.] */
@Test
public void getJobCreateURL() throws IOException, IotHubException {
// arrange
final String connectionString = "testString";
final String jobId = "validJobId";
JobClient testJobClient = null;
new NonStrictExpectations() {
{
IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
result = mockedIotHubConnectionString;
IotHubConnectionString.getUrlJobs(anyString, jobId);
result = mockedURL;
DeviceOperations.request(anyString, mockedURL, HttpMethod.GET, new byte[] {}, (String) any, anyInt, anyInt, (Proxy) any);
result = mockedHttpResponse;
Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, (byte[]) any);
result = mockedJobResult;
}
};
try {
testJobClient = JobClient.createFromConnectionString(connectionString);
} catch (IllegalArgumentException e) {
assertTrue("Test did not run because createFromConnectionString failed to create new instance of the JobClient", true);
}
// act
testJobClient.getJob(jobId);
// assert
new Verifications() {
{
IotHubConnectionString.getUrlJobs(anyString, jobId);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class JobClientTest method queryDeviceJobSucceeds.
// Tests_SRS_JOBCLIENT_25_039: [The queryDeviceJob shall create a query object for the type DEVICE_JOB.]
// Tests_SRS_JOBCLIENT_25_040: [The queryDeviceJob shall send a query request on the query object using Query URL, HTTP POST method and wait for the response by calling sendQueryRequest.]
@Test
public void queryDeviceJobSucceeds(@Mocked Query mockedQuery) throws IotHubException, IOException {
// arrange
final String connectionString = "testString";
JobClient testJobClient = JobClient.createFromConnectionString(connectionString);
new Expectations() {
{
Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.DEVICE_JOB);
result = mockedQuery;
}
};
// act
testJobClient.queryDeviceJob(VALID_SQL_QUERY);
// assert
new Verifications() {
{
mockedQuery.sendQueryRequest((TokenCredentialCache) any, (AzureSasCredential) any, (IotHubConnectionString) any, (URL) any, HttpMethod.POST, anyInt, anyInt, (Proxy) any);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class JobClientTest method nextRetrievesCorrectly.
// Tests_SRS_JOBCLIENT_25_049: [getNextJob shall return next Job Result if the exist, and throw NoSuchElementException other wise.]
// Tests_SRS_JOBCLIENT_25_051: [getNextJob method shall parse the next job element from the query response provide the response as JobResult object.]
@Test
public void nextRetrievesCorrectly(@Mocked Query mockedQuery) throws IotHubException, IOException {
// arrange
final String connectionString = "testString";
JobClient testJobClient = JobClient.createFromConnectionString(connectionString);
final String expectedString = "testJsonAsNext";
new NonStrictExpectations() {
{
Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.DEVICE_JOB);
result = mockedQuery;
Deencapsulation.invoke(mockedQuery, "hasNext");
result = true;
Deencapsulation.invoke(mockedQuery, "next");
result = expectedString;
Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, (byte[]) any);
result = mockedJobResult;
}
};
Query testQuery = testJobClient.queryDeviceJob(VALID_SQL_QUERY);
// act
testJobClient.getNextJob(testQuery);
// assert
new Verifications() {
{
mockedQuery.sendQueryRequest((TokenCredentialCache) any, (AzureSasCredential) any, (IotHubConnectionString) any, (URL) any, HttpMethod.POST, anyInt, anyInt, (Proxy) any);
times = 1;
Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, expectedString.getBytes(StandardCharsets.UTF_8));
times = 1;
}
};
}
Aggregations