Search in sources :

Example 46 with Expectations

use of mockit.Expectations in project azure-iot-sdk-java by Azure.

the class AmqpSendHandlerTest method onConnectionBound_call_flow_and_init_ok_amqp_ws.

// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_009: [The event handler shall set the SASL PLAIN authentication on the Transport using the given user name and sas token]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_010: [The event handler shall set ANONYMUS_PEER authentication mode on the domain of the Transport]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_024: [The event handler shall initialize WebSocket if the protocol is AMQP_WS]
@Test
public void onConnectionBound_call_flow_and_init_ok_amqp_ws() {
    // Arrange
    String hostName = "aaa";
    String userName = "bbb";
    String sasToken = "ccc";
    String hostAddr = hostName + ":443";
    IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS_WS;
    AmqpSendHandler amqpSendHandler = new AmqpSendHandler(hostName, userName, sasToken, iotHubServiceClientProtocol);
    // Assert
    new Expectations() {

        {
            event.getConnection();
            result = connection;
            connection.getTransport();
            result = transportInternal;
            new WebSocketImpl();
            result = webSocket;
            webSocket.configure(anyString, anyString, 0, anyString, null, null);
            transportInternal.addTransportLayer(webSocket);
            sasl.plain(anyString, anyString);
            Proton.sslDomain();
            result = sslDomain;
            sslDomain.init(SslDomain.Mode.CLIENT);
            sslDomain.setPeerAuthentication(SslDomain.VerifyMode.ANONYMOUS_PEER);
            transportInternal.ssl(sslDomain);
        }
    };
    // Act
    amqpSendHandler.onConnectionBound(event);
}
Also used : Expectations(mockit.Expectations) AmqpSendHandler(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler) WebSocketImpl(com.microsoft.azure.sdk.iot.deps.ws.impl.WebSocketImpl) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Test(org.junit.Test)

Example 47 with Expectations

use of mockit.Expectations in project azure-iot-sdk-java by Azure.

the class AmqpSendHandlerTest method onLinkFlowBufferOverflow_call_flow_ok.

// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_017: [The event handler shall get the Sender (Proton) object from the link]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_018: [The event handler shall encode the message and copy to the byte buffer]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_019: [The event handler shall set the delivery tag on the Sender (Proton) object]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_020: [The event handler shall send the encoded bytes]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_021: [The event handler shall close the Sender, Session and Connection]
@Test
public void onLinkFlowBufferOverflow_call_flow_ok() throws UnsupportedEncodingException {
    // Arrange
    String hostName = "aaa";
    String userName = "bbb";
    String sasToken = "ccc";
    String hostAddr = hostName + ":5671";
    String deviceId = "deviceId";
    String content = "abcdefghijklmnopqrst";
    com.microsoft.azure.sdk.iot.service.Message iotMessage = new com.microsoft.azure.sdk.iot.service.Message(content);
    String endpoint = "/messages/devicebound";
    exceptionCount = 0;
    IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
    createProtonObjects();
    AmqpSendHandler amqpSendHandler = new AmqpSendHandler(hostName, userName, sasToken, iotHubServiceClientProtocol);
    Queue<Message> testMessagesToBeSent = new LinkedBlockingQueue<>();
    testMessagesToBeSent.add(messageWithException);
    Deencapsulation.setField(amqpSendHandler, "messagesToBeSent", testMessagesToBeSent);
    // Assert
    new Expectations() {

        {
            link = event.getLink();
            link.getCredit();
        }
    };
    // Act
    amqpSendHandler.onLinkFlow(event);
}
Also used : Expectations(mockit.Expectations) Message(org.apache.qpid.proton.message.Message) AmqpSendHandler(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Test(org.junit.Test)

Example 48 with Expectations

use of mockit.Expectations 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);
        }
    };
}
Also used : Expectations(mockit.Expectations) AmqpSendHandler(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler) Message(org.apache.qpid.proton.message.Message) Properties(org.apache.qpid.proton.amqp.messaging.Properties) Verifications(mockit.Verifications) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Binary(org.apache.qpid.proton.amqp.Binary) Test(org.junit.Test)

Example 49 with Expectations

use of mockit.Expectations in project azure-iot-sdk-java by Azure.

the class AmqpSendHandlerTest method sendComplete_flow_OK.

/*
    Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_25_029: [** The event handler shall check the status queue to get the response for the sent message **]**

    Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_25_030: [** The event handler shall remove the response from the queue **]**

    Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_25_031: [** The event handler shall get the exception from the response and throw is it is not null **]**
     */
@Test
public void sendComplete_flow_OK(@Mocked final AmqpResponseVerification mockedVerification) throws IotHubException, IOException {
    // Arrange
    String hostName = "aaa";
    String userName = "bbb";
    String sasToken = "ccc";
    IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
    AmqpSendHandler amqpSendHandler = new AmqpSendHandler(hostName, userName, sasToken, iotHubServiceClientProtocol);
    Queue<AmqpResponseVerification> testsendStatusQueue = new LinkedBlockingQueue<>();
    testsendStatusQueue.add(mockedVerification);
    Deencapsulation.setField(amqpSendHandler, "sendStatusQueue", testsendStatusQueue);
    // Assert
    new Expectations() {

        {
            mockedVerification.getException();
            result = null;
        }
    };
    // Act
    amqpSendHandler.sendComplete();
}
Also used : Expectations(mockit.Expectations) AmqpSendHandler(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) AmqpResponseVerification(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpResponseVerification) Test(org.junit.Test)

Example 50 with Expectations

use of mockit.Expectations in project azure-iot-sdk-java by Azure.

the class AmqpSendHandlerTest method onLinkFlow_call_flow_ok.

// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_017: [The event handler shall get the Sender (Proton) object from the link]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_018: [The event handler shall encode the message and copy to the byte buffer]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_019: [The event handler shall set the delivery tag on the Sender (Proton) object]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_020: [The event handler shall send the encoded bytes]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_021: [The event handler shall close the Sender, Session and Connection]
@Test
public void onLinkFlow_call_flow_ok() throws UnsupportedEncodingException {
    // Arrange
    String hostName = "aaa";
    String userName = "bbb";
    String sasToken = "ccc";
    String hostAddr = hostName + ":5671";
    String deviceId = "deviceId";
    String content = "abcdefghijklmnopqrst";
    com.microsoft.azure.sdk.iot.service.Message iotMessage = new com.microsoft.azure.sdk.iot.service.Message(content);
    String endpoint = "/messages/devicebound";
    IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
    createProtonObjects();
    AmqpSendHandler amqpSendHandler = new AmqpSendHandler(hostName, userName, sasToken, iotHubServiceClientProtocol);
    amqpSendHandler.createProtonMessage(deviceId, iotMessage);
    // Assert
    new Expectations() {

        {
            link = event.getLink();
            link.getCredit();
            byte[] buffer = new byte[1024];
            message.encode(buffer, 0, 1024);
        }
    };
    // Act
    amqpSendHandler.onLinkFlow(event);
}
Also used : Expectations(mockit.Expectations) Message(org.apache.qpid.proton.message.Message) AmqpSendHandler(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Test(org.junit.Test)

Aggregations

Expectations (mockit.Expectations)114 Test (org.junit.Test)98 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)60 FeedbackReceiver (com.microsoft.azure.sdk.iot.service.FeedbackReceiver)19 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)19 Test (org.testng.annotations.Test)14 AmqpSendHandler (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler)12 ByteBuffer (java.nio.ByteBuffer)12 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)10 AmqpFeedbackReceivedHandler (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpFeedbackReceivedHandler)6 AmqpReceive (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpReceive)5 URIEndpointObject (io.servicecomb.foundation.common.net.URIEndpointObject)5 FeedbackBatch (com.microsoft.azure.sdk.iot.service.FeedbackBatch)4 Message (com.microsoft.azure.sdk.iot.service.Message)4 AmqpSend (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSend)4 HeartbeatResponse (io.servicecomb.serviceregistry.api.response.HeartbeatResponse)4 WebSocketImpl (com.microsoft.azure.sdk.iot.deps.ws.impl.WebSocketImpl)3 Tools (com.microsoft.azure.sdk.iot.service.Tools)3 AmqpFileUploadNotificationReceive (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpFileUploadNotificationReceive)3 Endpoint (io.servicecomb.core.Endpoint)3