Search in sources :

Example 16 with NonStrictExpectations

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

the class IotHubMessageUriTest method getHostnameIsCorrect.

// Tests_SRS_IOTHUBMESSAGEURI_11_003: [The function shall return the hostname given in the constructor.] 
@Test
public void getHostnameIsCorrect() throws URISyntaxException {
    final String iotHubHostname = "test.iothub";
    final String deviceId = "test-deviceid";
    final String hostname = "test-hostname";
    new NonStrictExpectations() {

        {
            mockIotHubUri.getHostname();
            result = hostname;
        }
    };
    IotHubMessageUri messageUri = new IotHubMessageUri(iotHubHostname, deviceId);
    String testHostname = messageUri.getHostname();
    final String expectedHostname = hostname;
    assertThat(testHostname, is(expectedHostname));
}
Also used : IotHubMessageUri(com.microsoft.azure.sdk.iot.device.net.IotHubMessageUri) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 17 with NonStrictExpectations

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

the class TemplateTest method testOpenCreatesNewSetOnlyOnce.

// Tests_SRS_TEMPLATE_99_005: [If open is already called then this method shall do nothing and return.]
@Test
public void testOpenCreatesNewSetOnlyOnce(@Mocked final Set<String> mockedSet) {
    //arrange
    final String testString = "testString";
    /* Set any expectations on mocked object */
    new NonStrictExpectations() {

        {
        }
    };
    /* Use Deencapsulation or reflection to access objects that are not scoped for test */
    Template testObject = Deencapsulation.newInstance(Template.class, testString);
    //act
    /* Use Deencapsulation or reflection to access objects that are not scoped for test */
    Deencapsulation.invoke(testObject, "open");
    Deencapsulation.invoke(testObject, "open");
    //assert
    /* Use Deencapsulation or reflection to access objects that are not scoped for test */
    final Set actualSet = Deencapsulation.getField(testObject, "unionSet");
    assertNotNull(actualSet);
    assertEquals(actualSet, mockedSet);
    /* Verify any call flow on mocked objects that is expected on act */
    new Verifications() {

        {
            new HashSet<>();
        }
    };
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Template(com.microsoft.azure.sdk.iot.device.Template) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with NonStrictExpectations

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

the class HttpsSingleMessageTest method toMessageCopiesBody.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_007: [The function shall return an IoT Hub message with a copy of the message body as its body.]
@Test
public void toMessageCopiesBody(@Mocked final HttpsResponse mockResponse, @Mocked final MessageProperty mockProperty, @Mocked final Message mockMsg) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final Map<String, String> headerFields = new HashMap<>();
    final String propertyName = "iothub-app-test-property-name";
    final String propertyValue = "test-property-value";
    headerFields.put(propertyName, propertyValue);
    new NonStrictExpectations() {

        {
            mockResponse.getBody();
            result = body;
            mockResponse.getHeaderFields();
            result = headerFields;
            MessageProperty.isValidAppProperty(propertyName, propertyValue);
            result = true;
            new MessageProperty(propertyName, propertyValue);
            result = mockProperty;
            mockProperty.getName();
            result = propertyName;
            mockProperty.getValue();
            result = propertyValue;
            new Message(body);
            result = mockMsg;
        }
    };
    HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockResponse);
    httpsMsg.toMessage();
    final byte[] expectedBody = body;
    new Verifications() {

        {
            new Message(expectedBody);
        }
    };
}
Also used : HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Message(com.microsoft.azure.sdk.iot.device.Message) HashMap(java.util.HashMap) 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 19 with NonStrictExpectations

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

the class HttpsSingleMessageTest method parseHttpsMessageFromResponseDoesNotBase64EncodeBody.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_005: [The parsed HttpsSingleMessage shall not be Base64-encoded.]
@Test
public void parseHttpsMessageFromResponseDoesNotBase64EncodeBody(@Mocked final HttpsResponse mockResponse, @Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final Map<String, String> headerFields = new HashMap<>();
    final String propertyName = "test-property-name";
    final String propertyValue = "test-property-value";
    headerFields.put(propertyName, propertyValue);
    new NonStrictExpectations() {

        {
            mockResponse.getBody();
            result = body;
            mockResponse.getHeaderFields();
            result = headerFields;
        }
    };
    HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockResponse);
    boolean testBase64Encoded = httpsMsg.isBase64Encoded();
    boolean expectedBase64Encoded = false;
    assertThat(testBase64Encoded, is(expectedBase64Encoded));
}
Also used : HashMap(java.util.HashMap) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 20 with NonStrictExpectations

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

the class HttpsSingleMessageTest method parseHttpsMessageFromResponseCopiesBody.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_004: [The parsed HttpsSingleMessage shall have a copy of the original response body as its body.]
@Test
public void parseHttpsMessageFromResponseCopiesBody(@Mocked final HttpsResponse mockResponse, @Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final Map<String, String> headerFields = new HashMap<>();
    final String propertyName = "test-property-name";
    final String propertyValue = "test-property-value";
    headerFields.put(propertyName, propertyValue);
    new NonStrictExpectations() {

        {
            mockResponse.getBody();
            result = body;
            mockResponse.getHeaderFields();
            result = headerFields;
        }
    };
    HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockResponse);
    byte[] testBody = httpsMsg.getBody();
    byte[] expectedBody = body;
    assertThat(testBody, is(expectedBody));
    expectedBody[0] = 0x34;
    assertThat(testBody, is(not(expectedBody)));
}
Also used : HashMap(java.util.HashMap) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) 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