use of com.microsoft.azure.sdk.iot.device.DeviceClientConfig in project azure-iot-sdk-java by Azure.
the class DeviceClientConfigTest method getAndSetDeviceMethodAndTwinMessageCallbackAndContextsMatch.
@Test
public void getAndSetDeviceMethodAndTwinMessageCallbackAndContextsMatch(@Mocked final MessageCallback mockCallback) throws URISyntaxException {
final String iotHubHostname = "test.iothubhostname";
final String deviceId = "test-deviceid";
final String deviceKey = "test-devicekey";
final String sharedAccessToken = null;
final IotHubConnectionString iotHubConnectionString = Deencapsulation.newInstance(IotHubConnectionString.class, new Class[] { String.class, String.class, String.class, String.class }, iotHubHostname, deviceId, deviceKey, sharedAccessToken);
DeviceClientConfig config = new DeviceClientConfig(iotHubConnectionString);
Object dMContext = new Object();
config.setDeviceMethodMessageCallback(mockCallback, dMContext);
Object testContextDM = config.getDeviceMethodMessageContext();
Object dTcontext = new Object();
config.setDeviceTwinMessageCallback(mockCallback, dTcontext);
Object testContextDT = config.getDeviceTwinMessageContext();
final Object expectedDTContext = dTcontext;
assertThat(testContextDT, is(expectedDTContext));
assertEquals(config.getDeviceTwinMessageCallback(), mockCallback);
final Object expectedDMContext = dMContext;
assertThat(testContextDM, is(expectedDMContext));
assertEquals(config.getDeviceMethodMessageCallback(), mockCallback);
}
use of com.microsoft.azure.sdk.iot.device.DeviceClientConfig in project azure-iot-sdk-java by Azure.
the class DeviceClientConfigTest method getAndSetMessageCallbackContextsMatch.
// Tests_SRS_DEVICECLIENTCONFIG_11_006: [The function shall set the message callback, with its associated context.]
// Tests_SRS_DEVICECLIENTCONFIG_11_011: [The function shall return the current message context.]
@Test
public void getAndSetMessageCallbackContextsMatch(@Mocked final MessageCallback mockCallback) throws URISyntaxException {
final String iotHubHostname = "test.iothubhostname";
final String deviceId = "test-deviceid";
final String deviceKey = "test-devicekey";
final String sharedAccessToken = null;
final IotHubConnectionString iotHubConnectionString = Deencapsulation.newInstance(IotHubConnectionString.class, new Class[] { String.class, String.class, String.class, String.class }, iotHubHostname, deviceId, deviceKey, sharedAccessToken);
DeviceClientConfig config = new DeviceClientConfig(iotHubConnectionString);
Object context = new Object();
config.setMessageCallback(mockCallback, context);
Object testContext = config.getMessageContext();
final Object expectedContext = context;
assertThat(testContext, is(expectedContext));
}
use of com.microsoft.azure.sdk.iot.device.DeviceClientConfig in project azure-iot-sdk-java by Azure.
the class AmqpsIotHubConnectionTest method constructorCopiesAllData.
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_002: [The constructor shall save the configuration into private member variables.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_003: [The constructor shall initialize the sender and receiver
// endpoint private member variables using the send/receiveEndpointFormat constants and device id.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_004: [The constructor shall initialize a new Handshaker
// (Proton) object to handle communication handshake.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_005: [The constructor shall initialize a new FlowController
// (Proton) object to handle communication flow.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_006: [The constructor shall set its state to CLOSED.]
@Test
public void constructorCopiesAllData() throws IOException {
baseExpectations();
AmqpsIotHubConnection connection = new AmqpsIotHubConnection(mockConfig, false);
DeviceClientConfig actualConfig = Deencapsulation.getField(connection, "config");
String actualHostName = Deencapsulation.getField(connection, "hostName");
String actualUserName = Deencapsulation.getField(connection, "userName");
String actualSendEndpoint = Deencapsulation.getField(connection, "sendEndpoint");
String actualReceiveEndpoint = Deencapsulation.getField(connection, "receiveEndpoint");
assertEquals(mockConfig, actualConfig);
assertEquals(hostName + ":" + amqpPort, actualHostName);
assertEquals(deviceId + "@sas." + hubName, actualUserName);
String expectedSendEndpoint = "/devices/test-deviceId/messages/events";
assertEquals(expectedSendEndpoint, actualSendEndpoint);
String expectedReceiveEndpoint = "/devices/test-deviceId/messages/devicebound";
assertEquals(expectedReceiveEndpoint, actualReceiveEndpoint);
new Verifications() {
{
new Handshaker();
times = 1;
new FlowController();
times = 1;
}
};
State actualState = Deencapsulation.getField(connection, "state");
assertEquals(State.CLOSED, actualState);
}
use of com.microsoft.azure.sdk.iot.device.DeviceClientConfig in project azure-iot-sdk-java by Azure.
the class IotHubSasTokenTest method expiryTimeSetCorrectly.
// Tests_SRS_IOTHUBSASTOKEN_11_002: [The expiry time shall be the given expiry time, where it is a UNIX timestamp and indicates the time after which the token becomes invalid.]
@Test
public void expiryTimeSetCorrectly() throws URISyntaxException {
final long expiryTime = 100;
final String signature = "sample-sig";
final IotHubConnectionString iotHubConnectionString = Deencapsulation.newInstance(IotHubConnectionString.class, new Class[] { String.class, String.class, String.class, String.class }, "iothub.sample-iothub-hostname.net", "sample-device-ID", "sample-device-key", null);
new NonStrictExpectations() {
{
mockSig.toString();
result = signature;
}
};
IotHubSasToken token = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), expiryTime);
String tokenStr = token.toString();
// extract the value assigned to se.
int expiryTimeKeyIdx = tokenStr.indexOf("se=");
int expiryTimeStartIdx = expiryTimeKeyIdx + 3;
int expiryTimeEndIdx = tokenStr.indexOf("&", expiryTimeStartIdx);
if (expiryTimeEndIdx == -1) {
expiryTimeEndIdx = tokenStr.length();
}
String testExpiryTimeStr = tokenStr.substring(expiryTimeStartIdx, expiryTimeEndIdx);
String expectedExpiryTimeStr = Long.toString(expiryTime);
assertThat(testExpiryTimeStr, is(expectedExpiryTimeStr));
}
use of com.microsoft.azure.sdk.iot.device.DeviceClientConfig in project azure-iot-sdk-java by Azure.
the class IotHubSasTokenTest method doesNotSetSASTokenWithoutSr.
// Tests_SRS_IOTHUBSASTOKEN_25_008: [**The required format for the SAS Token shall be verified and IllegalArgumentException is thrown if unmatched.**]**
//Tests_SRS_IOTHUBSASTOKEN_11_001: [**The SAS token shall have the format `SharedAccessSignature sig=<signature >&se=<expiryTime>&sr=<resourceURI>`. The params can be in any order.**]**
@Test(expected = IllegalArgumentException.class)
public void doesNotSetSASTokenWithoutSr() throws URISyntaxException {
String sastoken = "SharedAccessSignature sig=S3%2flPidfBF48B7%2fOFAxMOYH8rpOneq68nu61D%2fBP6fo%3d&se=1469813873";
;
final IotHubConnectionString iotHubConnectionString = Deencapsulation.newInstance(IotHubConnectionString.class, new Class[] { String.class, String.class, String.class, String.class }, "iothub.sample-iothub-hostname.net", "sample-device-ID", null, sastoken);
IotHubSasToken token = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), 0);
String tokenStr = token.toString();
assertTrue(tokenStr != sastoken);
}
Aggregations