Search in sources :

Example 21 with NonStrictExpectations

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

the class HttpsSingleMessageTest method getBodyAsStringsReturnsUtf8Body.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_010: [The function shall return the message body as a string encoded using charset UTF-8.]
@Test
public void getBodyAsStringsReturnsUtf8Body(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final MessageProperty[] properties = { mockProperty };
    final String propertyName = "test-property-name";
    final String propertyValue = "test-property-value";
    new NonStrictExpectations() {

        {
            mockMsg.getBytes();
            result = body;
            mockMsg.getProperties();
            result = properties;
            mockProperty.getName();
            result = propertyName;
            mockProperty.getValue();
            result = propertyValue;
        }
    };
    HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
    String testBody = httpsMsg.getBodyAsString();
    String expectedBody = "abc";
    assertThat(testBody, is(expectedBody));
}
Also used : MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 22 with NonStrictExpectations

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

the class HttpsSingleMessageTest method getPropertiesReturnsCopyOfProperties.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_013: [The function shall return a copy of the message properties.]
@Test
public void getPropertiesReturnsCopyOfProperties(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final MessageProperty[] properties = { mockProperty };
    final String propertyName = "test-property-name";
    final String httpsPropertyName = "iothub-app-test-property-name";
    final String propertyValue = "test-property-value";
    new NonStrictExpectations() {

        {
            mockMsg.getBytes();
            result = body;
            mockMsg.getProperties();
            result = properties;
            mockProperty.getName();
            result = propertyName;
            result = httpsPropertyName;
            mockProperty.getValue();
            result = propertyValue;
        }
    };
    HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
    MessageProperty[] testProperties = httpsMsg.getProperties();
    final MessageProperty[] expectedProperties = properties;
    assertThat(testProperties.length, is(expectedProperties.length));
    final String expectedPropertyName = httpsPropertyName;
    final String expectedPropertyValue = propertyValue;
    new Verifications() {

        {
            new MessageProperty(expectedPropertyName, expectedPropertyValue);
            times = 2;
        }
    };
}
Also used : MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 23 with NonStrictExpectations

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

the class HttpsSingleMessageTest method getBodyReturnsCopyOfBody.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_009: [The function shall return a copy of the message body.]
@Test
public void getBodyReturnsCopyOfBody(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final MessageProperty[] properties = { mockProperty };
    final String propertyName = "test-property-name";
    final String propertyValue = "test-property-value";
    new NonStrictExpectations() {

        {
            mockMsg.getBytes();
            result = body;
            mockMsg.getProperties();
            result = properties;
            mockProperty.getName();
            result = propertyName;
            mockProperty.getValue();
            result = propertyValue;
        }
    };
    HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
    byte[] testBody = httpsMsg.getBody();
    byte[] expectedBody = body;
    assertThat(testBody, is(expectedBody));
    testBody[0] = 0x34;
    testBody = httpsMsg.getBody();
    assertThat(testBody, is(expectedBody));
}
Also used : MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 24 with NonStrictExpectations

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

the class HttpsSingleMessageTest method parseHttpsMessageFromMessageCopiesBody.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_001: [The parsed HttpsSingleMessage shall have a copy of the original message body as its body.]
@Test
public void parseHttpsMessageFromMessageCopiesBody(@Mocked final Message mockMsg, @Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final MessageProperty[] properties = { mockProperty };
    final String propertyName = "test-property-name";
    final String propertyValue = "test-property-value";
    new NonStrictExpectations() {

        {
            mockMsg.getBytes();
            result = body;
            mockMsg.getProperties();
            result = properties;
            mockProperty.getName();
            result = propertyName;
            mockProperty.getValue();
            result = propertyValue;
        }
    };
    HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockMsg);
    byte[] testBody = httpsMsg.getBody();
    byte[] expectedBody = body;
    assertThat(testBody, is(expectedBody));
    expectedBody[0] = 0x34;
    assertThat(testBody, is(not(expectedBody)));
}
Also used : MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 25 with NonStrictExpectations

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

the class DeviceTwinTest method getTwinSucceeds.

/*
    **Tests_SRS_DEVICETWIN_25_005: [** The function shall build the URL for this operation by calling getUrlTwin **]**
    **Tests_SRS_DEVICETWIN_25_006: [** The function shall create a new SAS token **]**
    **Tests_SRS_DEVICETWIN_25_007: [** The function shall create a new HttpRequest with http method as Get **]**
    **Tests_SRS_DEVICETWIN_25_008: [** The function shall set the following HTTP headers specified in the IotHub DeviceTwin doc.
                                                1. Key as authorization with value as sastoken
                                                2. Key as request id with a new string value for every request
                                                3. Key as User-Agent with value specified by the clientIdentifier and its version
                                                4. Key as Accept with value as application/json
                                                5. Key as Content-Type and value as application/json
                                                6. Key as charset and value as utf-8
                                                7. Key as If-Match and value as '*'  **]**
     **Tests_SRS_DEVICETWIN_25_009: [** The function shall send the created request and get the response **]**
     **Tests_SRS_DEVICETWIN_25_011: [** The function shall deserialize the payload by calling updateTwin Api on the twin object **]**
     **Tests_SRS_DEVICETWIN_25_012: [** The function shall set tags, desired property map, reported property map on the user device **]**
     */
@Test
public void getTwinSucceeds() throws Exception {
    //arrange
    final String connectionString = "testString";
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    Map<String, Object> testMap = new HashMap<>();
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            Deencapsulation.invoke(mockedDevice, "getTwinParser");
            result = mockedTwinParser;
        }
    };
    //act
    testTwin.getTwin(mockedDevice);
    //assert
    new Verifications() {

        {
            mockedConnectionString.getUrlTwin(anyString);
            times = 1;
            mockedHttpRequest.setReadTimeoutMillis(anyInt);
            times = 1;
            mockedHttpRequest.setHeaderField(anyString, anyString);
            times = 5;
            mockedHttpRequest.send();
            times = 1;
            mockedTwinParser.updateTwin(anyString);
            times = 1;
            Deencapsulation.invoke(mockedDevice, "getTwinParser");
            times = 4;
            mockedTwinParser.getTagsMap();
            times = 1;
            Deencapsulation.invoke(mockedDevice, "setTags", testMap);
            times = 1;
            Deencapsulation.invoke(mockedDevice, "setDesiredProperties", testMap);
            times = 1;
            Deencapsulation.invoke(mockedDevice, "setReportedProperties", testMap);
            times = 1;
        }
    };
}
Also used : HashMap(java.util.HashMap) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Aggregations

NonStrictExpectations (mockit.NonStrictExpectations)492 Test (org.junit.Test)472 Verifications (mockit.Verifications)154 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)77 IOException (java.io.IOException)64 HttpConnection (com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection)51 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)51 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)47 MessageProperty (com.microsoft.azure.sdk.iot.device.MessageProperty)32 HttpsSingleMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage)31 HttpConnection (com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection)26 HttpMethod (com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod)26 HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)26 HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)26 HashMap (java.util.HashMap)26 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)25 IotHubConnectionString (com.microsoft.azure.sdk.iot.device.IotHubConnectionString)17 Date (java.util.Date)16 HashSet (java.util.HashSet)16 Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)15