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);
}
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;
}
};
}
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 */
}
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));
}
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)));
}
Aggregations