Search in sources :

Example 11 with NonStrictExpectations

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

the class MessageTest method setPropertyRejectsIllegalName.

// Tests_SRS_MESSAGE_11_030: [If name contains a character not specified in RFC 2047, the function shall throw an IllegalArgumentException.]
@Test(expected = IllegalArgumentException.class)
public void setPropertyRejectsIllegalName(@Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final String invalidName = "  ";
    final String value = "test-value";
    new NonStrictExpectations() {

        {
            new MessageProperty(invalidName, value);
            result = new IllegalArgumentException();
        }
    };
    Message msg = new Message(body);
    msg.setProperty(invalidName, value);
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 12 with NonStrictExpectations

use of mockit.NonStrictExpectations 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 13 with NonStrictExpectations

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

the class TemplateTest method testAddToSetThrowsIfEmptyInputSet.

// Tests_SRS_TEMPLATE_99_011: [The method shall throw IllegalArgumentException if the collection to be added was either empty or null .]
@Test(expected = IllegalArgumentException.class)
public void testAddToSetThrowsIfEmptyInputSet(@Mocked final Set<String> mockedSet) {
    //arrange
    final String testString = "testString";
    /* Set any expectations on mocked object */
    new NonStrictExpectations() {

        {
            mockedSet.size();
            result = 0;
        }
    };
    /* Put the class in expected state before testing it */
    Template testObject = Deencapsulation.newInstance(Template.class, testString);
    Deencapsulation.invoke(testObject, "open");
    //act
    testObject.addToSet(mockedSet);
//assert
/* Throws exception */
}
Also used : NonStrictExpectations(mockit.NonStrictExpectations) Template(com.microsoft.azure.sdk.iot.device.Template) Test(org.junit.Test)

Example 14 with NonStrictExpectations

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

the class IotHubSasTokenTest method signatureSetCorrectly.

// Tests_SRS_IOTHUBSASTOKEN_11_005: [The signature shall be correctly computed and set.]
// Tests_SRS_IOTHUBSASTOKEN_11_006: [The function shall return the string representation of the SAS token.]
@Test
public void signatureSetCorrectly() throws URISyntaxException {
    final long expiryTime = 100;
    final String signature = "sample-sig";
    final IotHubConnectionString iotHubConnectionString = Deencapsulation.newInstance(IotHubConnectionString.class, new Class[] { String.class, String.class, String.class, String.class }, "iothub.sample-iothub-hostname.net", "sample-device-ID", "sample-device-key", null);
    new NonStrictExpectations() {

        {
            mockSig.toString();
            result = signature;
        }
    };
    IotHubSasToken token = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), expiryTime);
    String tokenStr = token.toString();
    // extract the value assigned to sig.
    int signatureKeyIdx = tokenStr.indexOf("sig=");
    int signatureStartIdx = signatureKeyIdx + 4;
    int signatureEndIdx = tokenStr.indexOf("&", signatureStartIdx);
    if (signatureEndIdx == -1) {
        signatureEndIdx = tokenStr.length();
    }
    String testSignature = tokenStr.substring(signatureStartIdx, signatureEndIdx);
    final String expectedSignature = signature;
    assertThat(testSignature, is(expectedSignature));
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 15 with NonStrictExpectations

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

the class IotHubSasTokenTest method sasTokenHasCorrectFormat.

// Tests_SRS_IOTHUBSASTOKEN_11_001: [The SAS token shall have the format "SharedAccessSignature sig=<signature>&se=<expiryTime>&sr=<resourceURI>". The params can be in any order.]
@Test
public void sasTokenHasCorrectFormat() throws URISyntaxException {
    final long expiryTime = 100;
    final String signature = "sample-sig";
    final IotHubConnectionString iotHubConnectionString = Deencapsulation.newInstance(IotHubConnectionString.class, new Class[] { String.class, String.class, String.class, String.class }, "iothub.sample-iothub-hostname.net", "sample-device-ID", "sample-device-key", null);
    new NonStrictExpectations() {

        {
            mockSig.toString();
            result = signature;
        }
    };
    IotHubSasToken token = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), expiryTime);
    String tokenStr = token.toString();
    // assert that sig, se and sr exist in the token in any order.
    assertThat(tokenStr.indexOf("SharedAccessSignature "), is(not(-1)));
    assertThat(tokenStr.indexOf("sig="), is(not(-1)));
    assertThat(tokenStr.indexOf("se="), is(not(-1)));
    assertThat(tokenStr.indexOf("sr="), is(not(-1)));
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) 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