use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method getTwinThrowsVerificationFailure.
/*
**Tests_SRS_DEVICETWIN_25_010: [** The function shall verify the response status and throw proper Exception **]**
*/
@Test(expected = IotHubException.class)
public void getTwinThrowsVerificationFailure() throws Exception {
//arrange
final String connectionString = "testString";
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
new NonStrictExpectations() {
{
mockedDevice.getDeviceId();
result = "SomeDevID";
IotHubExceptionManager.httpResponseVerification(mockedHttpResponse);
result = new IotHubException();
}
};
//act
testTwin.getTwin(mockedDevice);
}
use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.
the class DeviceMethodTest method invoke_throwOnHttpRequester_failed.
/* Tests_SRS_DEVICEMETHOD_21_009: [The invoke shall send the created request and get the response using the HttpRequester.] */
/* Tests_SRS_DEVICEMETHOD_21_010: [The invoke shall create a new HttpRequest with http method as `POST`.] */
@Test(expected = IotHubException.class)
public void invoke_throwOnHttpRequester_failed(@Mocked final MethodParser methodParser, @Mocked final IotHubConnectionStringBuilder mockedConnectionStringBuilder) throws Exception {
//arrange
DeviceMethod testMethod = DeviceMethod.createFromConnectionString(STANDARD_CONNECTIONSTRING);
new NonStrictExpectations() {
{
methodParser.toJson();
result = STANDARD_JSON;
iotHubConnectionString.getUrlMethod(STANDARD_DEVICEID);
result = STANDARD_URL;
}
};
new MockUp<DeviceOperations>() {
@Mock
HttpResponse request(IotHubConnectionString iotHubConnectionString, URL url, HttpMethod method, byte[] payload, String requestId, long timeoutInMs) throws IOException, IotHubException, IllegalArgumentException {
throw new IotHubException();
}
};
//act
testMethod.invoke(STANDARD_DEVICEID, STANDARD_METHODNAME, STANDARD_TIMEOUT_SECONDS, STANDARD_TIMEOUT_SECONDS, STANDARD_PAYLOAD_MAP);
}
use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.
the class DeviceManagerSample method GetDevice.
private static void GetDevice() throws Exception {
RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
Device returnDevice = null;
try {
returnDevice = registryManager.getDevice(SampleUtils.deviceId);
System.out.println("Device: " + returnDevice.getDeviceId());
System.out.println("Device primary key: " + returnDevice.getPrimaryKey());
System.out.println("Device secondary key: " + returnDevice.getSecondaryKey());
System.out.println("Device eTag: " + returnDevice.geteTag());
} catch (IotHubException iote) {
iote.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.
the class DeviceMethodSample method main.
/**
* @param args
* @throws IOException
* @throws URISyntaxException
*/
public static void main(String[] args) throws Exception {
System.out.println("Starting sample...");
DeviceMethod methodClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);
try {
// Manage complete Method
System.out.println("Getting device Method");
MethodResult result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, payload);
if (result == null) {
throw new IOException("Method invoke returns null");
}
System.out.println("Status=" + result.getStatus());
System.out.println("Payload=" + result.getPayload());
} catch (IotHubException e) {
System.out.println(e.getMessage());
}
System.out.println("Shutting down sample...");
}
use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.
the class IotHubExceptionManagerTest method httpResponseVerification_300.
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_012: [The function shall return without exception if the response status equal or less than 300]
@Test
public void httpResponseVerification_300() throws IotHubException {
// Arrange
final int status = 300;
final byte[] body = { 1 };
final Map<String, List<String>> headerFields = new HashMap<>();
final byte[] errorReason = { 123, 125 };
HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
// Act
IotHubExceptionManager.httpResponseVerification(response);
IotHubException iotHubException = new IotHubException();
IotHubExceptionManager iotHubExceptionManager = new IotHubExceptionManager();
// Assert
assertNotEquals(null, iotHubException);
assertNotEquals(null, iotHubExceptionManager);
}
Aggregations