use of com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt in project azure-iot-sdk-java by Azure.
the class MqttTest method receiveThrowsExceptionWhenConfigurationIsNotSet.
@Test(expected = InvalidParameterException.class)
public void receiveThrowsExceptionWhenConfigurationIsNotSet() throws IOException, MqttException {
//arrange
final byte[] payload = { 0x61, 0x62, 0x63 };
baseConstructorExpectations(false);
new MockUp<MqttMessaging>() {
@Mock
String parseTopic() throws IOException {
return mockParseTopic;
}
@Mock
byte[] parsePayload(String topic) throws IOException {
return null;
}
};
final Mqtt mockMqtt = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
Deencapsulation.setField(mockMqtt, "info", null);
//act
try {
Message receivedMessage = mockMqtt.receive();
} finally {
testCleanUp(mockMqtt);
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt in project azure-iot-sdk-java by Azure.
the class MqttTest method constructorWithParametersIfCalledMultipleTimesDoesntReinitialize.
/*
** Tests_SRS_Mqtt_25_004: [**If an instance of the inner class MqttConnectionInfo is already created than it shall return doing nothing.**]**
*/
@Test
public void constructorWithParametersIfCalledMultipleTimesDoesntReinitialize() throws IOException, MqttException {
//arrange
baseConstructorExpectations(true);
baseConstructorExpectations(true);
//act
Mqtt mockMqtt1 = instantiateMqtt(true);
//assert
Object actualInfoInstance1 = Deencapsulation.getField(mockMqtt1, "info");
ConcurrentSkipListMap<String, byte[]> actualMap1 = Deencapsulation.getField(mockMqtt1, "allReceivedMessages");
Mqtt mockMqtt2 = instantiateMqtt(false);
Object actualInfoInstance2 = Deencapsulation.getField(mockMqtt2, "info");
ConcurrentSkipListMap<String, byte[]> actualMap2 = Deencapsulation.getField(mockMqtt2, "allReceivedMessages");
Object actualLock1 = Deencapsulation.getField(mockMqtt1, "MQTT_LOCK");
Object actualLock2 = Deencapsulation.getField(mockMqtt2, "MQTT_LOCK");
assertEquals(actualInfoInstance1, actualInfoInstance2);
assertEquals(actualMap1, actualMap2);
assertEquals(actualLock1, actualLock2);
baseConstructorVerifications(true);
baseConstructorVerifications(false);
mockMqtt1.restartBaseMqtt();
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt in project azure-iot-sdk-java by Azure.
the class MqttTest method subscribeThrowsExceptionWhenTopicIsNull.
/*
**Tests_SRS_Mqtt_25_016: [**If the subscribeTopic is null or empty, the function shall throw an InvalidParameter Exception.**]**
*/
@Test(expected = InvalidParameterException.class)
public void subscribeThrowsExceptionWhenTopicIsNull() throws IOException, MqttException {
//arrange
Mqtt mockMqtt = null;
try {
baseConstructorExpectations(true);
mockMqtt = instantiateMqtt(true);
Deencapsulation.invoke(mockMqtt, "connect");
//act
Deencapsulation.invoke(mockMqtt, "subscribe", String.class);
} finally {
testCleanUp(mockMqtt);
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt in project azure-iot-sdk-java by Azure.
the class MqttTest method disconnectSucceeds.
/*
**Tests_SRS_Mqtt_25_009: [**The function shall close the MQTT connection.**]**
*/
@Test
public void disconnectSucceeds() throws IOException, MqttException {
//arrange
Mqtt mockMqtt = null;
try {
baseConstructorExpectations(true);
baseConnectExpectation();
baseDisconnectExpectations();
mockMqtt = instantiateMqtt(true);
Deencapsulation.invoke(mockMqtt, "connect");
//act
Deencapsulation.invoke(mockMqtt, "disconnect");
//assert
new Verifications() {
{
mockMqttAsyncClient.isConnected();
times = 2;
mockMqttAsyncClient.disconnect();
times = 1;
mockMqttToken.waitForCompletion();
times = 1;
}
};
Object actualInfoInstance = Deencapsulation.getField(mockMqtt, "info");
MqttAsyncClient actualMqttAsyncClient = Deencapsulation.getField(actualInfoInstance, "mqttAsyncClient");
assertNull(actualMqttAsyncClient);
} finally {
testCleanUp(mockMqtt);
}
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt in project azure-iot-sdk-java by Azure.
the class MqttTest method publishFailsWhenNotConnected.
/*
**Tests_SRS_Mqtt_25_012: [**If the MQTT connection is closed, the function shall throw an IOException.**]**
*/
@Test(expected = IOException.class)
public void publishFailsWhenNotConnected() throws IOException, MqttException {
//arrange
Mqtt mockMqtt = null;
try {
baseConstructorExpectations(true);
final byte[] payload = { 0x61, 0x62, 0x63 };
new NonStrictExpectations() {
{
mockMqttAsyncClient.isConnected();
result = false;
}
};
mockMqtt = instantiateMqtt(true);
//act
Deencapsulation.invoke(mockMqtt, "publish", mockParseTopic, payload);
} finally {
testCleanUp(mockMqtt);
}
}
Aggregations