use of mockit.Verifications in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method sendEventSendsDeviceTwinMessage.
@Test
public void sendEventSendsDeviceTwinMessage(@Mocked final DeviceTwinMessage mockDeviceTwinMsg) throws IOException {
baseExpectations();
openExpectations();
final byte[] msgBody = { 0x61, 0x62, 0x63 };
new NonStrictExpectations() {
{
mockDeviceTwinMsg.getBytes();
result = msgBody;
mockDeviceTwinMsg.getMessageType();
result = MessageType.DeviceTwin;
}
};
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.open();
IotHubStatusCode result = connection.sendEvent(mockDeviceTwinMsg);
assertEquals(IotHubStatusCode.OK_EMPTY, result);
new Verifications() {
{
mockDeviceMethods.send((DeviceMethodMessage) any);
times = 0;
mockDeviceMessaging.send(mockDeviceTwinMsg);
times = 0;
mockDeviceTwin.start();
times = 1;
mockDeviceTwin.send(mockDeviceTwinMsg);
times = 1;
}
};
}
use of mockit.Verifications in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method openThrowsIOExceptionIfConnectionFailsInMethod.
@Test(expected = IOException.class)
public void openThrowsIOExceptionIfConnectionFailsInMethod() throws IOException {
baseExpectations();
new NonStrictExpectations() {
{
new IotHubSasToken(mockConfig, anyLong);
result = mockToken;
new MqttMessaging(sslPrefix + iotHubHostName + sslPortSuffix, deviceId, anyString, anyString, mockIotHubSSLContext);
result = mockDeviceMessaging;
new MqttDeviceMethod();
result = new IOException(anyString);
}
};
try {
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.open();
} catch (Exception e) {
new Verifications() {
{
mockDeviceMessaging.stop();
times = 1;
}
};
throw e;
}
}
use of mockit.Verifications in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method openThrowsIOExceptionIfConnectionFails.
// Tests_SRS_MQTTIOTHUBCONNECTION_15_005: [If an MQTT connection is unable to be established for any reason,
// the function shall throw an IOException.]
@Test(expected = IOException.class)
public void openThrowsIOExceptionIfConnectionFails() throws IOException {
baseExpectations();
new NonStrictExpectations() {
{
new IotHubSasToken(mockConfig, anyLong);
result = mockToken;
new MqttMessaging(sslPrefix + iotHubHostName + sslPortSuffix, deviceId, anyString, anyString, mockIotHubSSLContext);
result = new IOException(anyString);
}
};
try {
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.open();
} catch (Exception e) {
new Verifications() {
{
}
};
throw e;
}
}
use of mockit.Verifications in project azure-iot-sdk-java by Azure.
the class AmqpSendHandlerTest method createProtonMessage_creates_Message_and_sets_Properties.
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_004: [The function shall create a new Message (Proton) object]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_005: [The function shall set the “to” property on the Message object using the created device path]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_006: [The function shall create a Binary (Proton) object from the content string]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_007: [The function shall create a data Section (Proton) object from the Binary]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_008: [The function shall set the Message body to the created data section]
@Test
public void createProtonMessage_creates_Message_and_sets_Properties() throws UnsupportedEncodingException {
// Arrange
String hostName = "aaa";
String userName = "bbb";
String sasToken = "ccc";
String deviceId = "deviceId";
String content = "abcdefghijklmnopqrst";
String toProperty = "/devices/deviceId/messages/devicebound";
IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
AmqpSendHandler amqpSendHandler = new AmqpSendHandler(hostName, userName, sasToken, iotHubServiceClientProtocol);
com.microsoft.azure.sdk.iot.service.Message iotMessage = new com.microsoft.azure.sdk.iot.service.Message(content);
Map<String, String> userDefinedProperties = new HashMap<>(5);
userDefinedProperties.put("key1", "value1");
userDefinedProperties.put("key2", "value2");
userDefinedProperties.put("key3", "value3");
userDefinedProperties.put("key4", "value4");
userDefinedProperties.put("key5", "value5");
iotMessage.setProperties(userDefinedProperties);
// Assert
new Expectations() {
{
message = Proton.message();
new Properties();
result = properties;
properties.setTo(toProperty);
message.setProperties(properties);
binary = new Binary(content.getBytes());
section = new Data(binary);
message.setApplicationProperties((ApplicationProperties) any);
message.setBody(section);
}
};
// Act
amqpSendHandler.createProtonMessage(deviceId, iotMessage);
new Verifications() {
{
properties.setTo(toProperty);
properties.setMessageId(any);
properties.setAbsoluteExpiryTime((Date) any);
properties.setCorrelationId(any);
}
};
}
use of mockit.Verifications in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method constructorOpensConnection.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_001: [The constructor shall open a connection to the given URL.]
@Test
public void constructorOpensConnection() throws IOException {
// Arrange
final HttpMethod httpMethod = HttpMethod.PUT;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
}
};
// Act
new HttpConnection(mockUrl, httpMethod);
// Assert
new Verifications() {
{
mockUrl.openConnection();
}
};
}
Aggregations