Search in sources :

Example 1 with Verifications

use of mockit.Verifications in project jersey by jersey.

the class FormDataMultiPartReaderWriterTest method mimeTempFileRemovedAfterAbortedUpload.

/**
     * Mocked JERSEY-2794 reproducer. Real test is under integration tests.
     */
@Test
public void mimeTempFileRemovedAfterAbortedUpload(@Mocked final MIMEMessage message) throws Exception {
    new Expectations() {

        {
            message.getAttachments();
            result = new MIMEParsingException();
        }
    };
    final URL url = new URL(getBaseUri().toString() + "MediaTypeWithBoundaryResource");
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("PUT");
    connection.setRequestProperty("Accept", "text/plain");
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=XXXX_YYYY");
    connection.setDoOutput(true);
    connection.connect();
    final OutputStream outputStream = connection.getOutputStream();
    outputStream.write("--XXXX_YYYY".getBytes());
    outputStream.write('\n');
    outputStream.write("Content-Type: text/plain".getBytes());
    outputStream.write('\n');
    outputStream.write("Content-Disposition: form-data; name=\"big-part\"".getBytes());
    outputStream.write('\n');
    outputStream.write('\n');
    // Send big chunk of data.
    for (int i = 0; i < 16 * 4096; i++) {
        outputStream.write('E');
        if (i % 1024 == 0) {
            outputStream.flush();
        }
    }
    // Do NOT send end of the MultiPart message to simulate the issue.
    // Get Response ...
    final int response = connection.getResponseCode();
    // ... Disconnect.
    connection.disconnect();
    assertThat("Bad Request expected", response, is(400));
    // Make sure that the Mimepull message and it's parts have been closed and temporary files deleted.
    new Verifications() {

        {
            message.close();
            times = 1;
        }
    };
}
Also used : Expectations(mockit.Expectations) HttpURLConnection(java.net.HttpURLConnection) MIMEParsingException(org.jvnet.mimepull.MIMEParsingException) OutputStream(java.io.OutputStream) Verifications(mockit.Verifications) URL(java.net.URL) Test(org.junit.Test)

Example 2 with Verifications

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

the class TemplateTest method testCloseWhenCalledTwiceReturnsSuccessfully.

// Tests_SRS_TEMPLATE_99_007: [If close is already called then this method shall do nothing and return.]
@Test
public void testCloseWhenCalledTwiceReturnsSuccessfully(@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);
    /*Use Dencapsulation to control the expected value of private fields */
    Deencapsulation.setField(testObject, "unionSet", mockedSet);
    //act
    /* Use Deencapsulation or reflection to access objects that are not scoped for test */
    Deencapsulation.invoke(testObject, "close");
    Deencapsulation.invoke(testObject, "close");
    //assert
    /* Verify any call flow and number of times the call is expected on mocked objects close is invoked */
    new Verifications() {

        {
            mockedSet.clear();
            times = 1;
        }
    };
}
Also used : Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Template(com.microsoft.azure.sdk.iot.device.Template) Test(org.junit.Test)

Example 3 with Verifications

use of mockit.Verifications 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 4 with Verifications

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

the class MqttDeviceMethodTest method stopSucceedsDoesNotCallUnSubscribeIfNotStarted.

@Test
public void stopSucceedsDoesNotCallUnSubscribeIfNotStarted() throws IOException {
    //arrange
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    //act
    testMethod.stop();
    //assert
    new Verifications() {

        {
            Deencapsulation.invoke(mockedMqtt, "unsubscribe", anyString);
            maxTimes = 0;
        }
    };
}
Also used : MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Verifications(mockit.Verifications) Test(org.junit.Test)

Example 5 with Verifications

use of mockit.Verifications 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)

Aggregations

Verifications (mockit.Verifications)329 Test (org.junit.Test)326 NonStrictExpectations (mockit.NonStrictExpectations)163 Expectations (mockit.Expectations)52 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)34 Tuple (org.apache.storm.tuple.Tuple)28 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)24 AmqpResponseVerification (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpResponseVerification)22 HttpConnection (com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection)21 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)21 ArrayList (java.util.ArrayList)21 List (java.util.List)21 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)20 FileUploadNotificationReceiver (com.microsoft.azure.sdk.iot.service.FileUploadNotificationReceiver)18 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)17 HashMap (java.util.HashMap)16 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)14 IOException (java.io.IOException)14 Values (org.apache.storm.tuple.Values)14 SaslListenerImpl (com.microsoft.azure.sdk.iot.deps.transport.amqp.SaslListenerImpl)13