Search in sources :

Example 16 with Verifications

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);
        }
    };
}
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 17 with Verifications

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();
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 18 with Verifications

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

the class HttpConnectionTest method connectStreamsRequestBody.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_006: [The function shall stream the request body, if present, through the connection.]
@Test
public void connectStreamsRequestBody() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    byte[] body = { 1, 2, 3 };
    final byte[] expectedBody = { 1, 2, 3 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    // Act
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.writeOutput(body);
    body[0] = 5;
    conn.connect();
    // Assert
    new Verifications() {

        {
            mockUrl.openConnection().getOutputStream().write(expectedBody);
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 19 with Verifications

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

the class HttpConnectionTest method constructorSetsRequestMethod.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_003: [The constructor shall set the HTTPS method to the given method.]
@Test
public void constructorSetsRequestMethod() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    // Act
    new HttpConnection(mockUrl, httpsMethod);
    // Assert
    new Verifications() {

        {
            mockUrl.openConnection();
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 20 with Verifications

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

the class HttpConnectionTest method setRequestHeaderSetsRequestHeader.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_010: [The function shall set the given request header field.]
@Test
public void setRequestHeaderSetsRequestHeader() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.POST;
    final String field = "test-field";
    final String value = "test-value";
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    // Act
    conn.setRequestHeader(field, value);
    // Assert
    new Verifications() {

        {
            mockUrl.openConnection().setRequestProperty(field, value);
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Aggregations

Verifications (mockit.Verifications)103 Test (org.junit.Test)103 NonStrictExpectations (mockit.NonStrictExpectations)66 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)14 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)12 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)11 AmqpResponseVerification (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpResponseVerification)11 HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)10 HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)10 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)10 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)10 FileUploadNotificationReceiver (com.microsoft.azure.sdk.iot.service.FileUploadNotificationReceiver)9 HttpConnection (com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection)9 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)9 HashMap (java.util.HashMap)9 Template (com.microsoft.azure.sdk.iot.device.Template)8 CustomLogger (com.microsoft.azure.sdk.iot.device.CustomLogger)6 DeviceMethodMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage)6 IOException (java.io.IOException)6 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)5