use of com.microsoft.rest.RestException in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientTests method digitalTwinClientTokenRenewalWithAzureSasCredential.
@Test
@StandardTierHubOnlyTest
public void digitalTwinClientTokenRenewalWithAzureSasCredential() {
if (protocol != MQTT) {
// This test is for the service client, so no need to rerun it for all the different device protocols
return;
}
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(IOTHUB_CONNECTION_STRING);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
digitalTwinClient = new DigitalTwinClient(iotHubConnectionStringObj.getHostName(), sasCredential);
// get a digital twin with a valid SAS token in the AzureSasCredential instance
// don't care about the return value, just checking that the request isn't unauthorized
digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
// deliberately expire the SAS token to provoke a 401 to ensure that the digital twin client is using the shared
// access signature that is set here.
sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
try {
digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
fail("Expected get digital twin call to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
} catch (RestException e) {
if (e.response().code() == 401) {
log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
} else {
throw e;
}
}
// Renew the expired shared access signature
serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
sasCredential.update(serviceSasToken.toString());
// get a digital twin using the renewed shared access signature
// don't care about the return value, just checking that the request isn't unauthorized
digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
}
use of com.microsoft.rest.RestException in project azure-iot-sdk-java by Azure.
the class Thermostat method InvokeMethodOnRootLevel.
private static void InvokeMethodOnRootLevel() throws IOException, InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
String commandName = "getMaxMinReport";
String commandInput = ZonedDateTime.now(ZoneOffset.UTC).minusMinutes(5).format(DateTimeFormatter.ISO_DATE_TIME);
// Invoke a method on root level.
asyncClient.invokeCommand(digitalTwinid, commandName, commandInput).subscribe(response -> {
System.out.println("Invoked Command " + commandName + " response: " + prettyString(response.getPayload()));
latch.countDown();
}, error -> {
RestException ex = (RestException) error;
if (ex.response().code() == 404) {
System.out.println("Invoked Command " + commandName + " failed: " + error);
} else {
System.out.println("Ensure the device sample is running for this sample to succeed - https://github.com/Azure/azure-iot-sdk-java/tree/main/device/iot-device-samples/pnp-device-sample/thermostat-device-sample.");
}
latch.countDown();
});
latch.await(MAX_WAIT_TIME_ASYNC_OPERATIONS_IN_SECONDS, TimeUnit.SECONDS);
}
use of com.microsoft.rest.RestException in project azure-tools-for-java by Microsoft.
the class ContainerRegistryMvpModelTest method testListContainerRegistries.
@Test
public void testListContainerRegistries() throws IOException {
List<Subscription> subscriptions = new ArrayList<Subscription>();
Subscription sub1 = mock(Subscription.class);
when(sub1.getId()).thenReturn("1");
Subscription sub2 = mock(Subscription.class);
when(sub2.getId()).thenReturn("2");
Subscription sub3 = mock(Subscription.class);
when(sub3.getId()).thenReturn("3");
when(mvpModel.getSelectedSubscriptions()).thenReturn(subscriptions);
ContainerRegistryMvpModel mockModel = spy(containerRegistryMvpModel);
when(authMethodManagerMock.getAzureClient(anyString())).thenReturn(azureMock);
when(registriesMock.list()).thenReturn(new PagedList<Registry>() {
@Override
public Page<Registry> nextPage(String nextPageLink) throws RestException, IOException {
return null;
}
});
mockModel.listContainerRegistries(false);
verify(mockModel, times(0)).listRegistryBySubscriptionId(anyString(), eq(false));
subscriptions.add(sub1);
subscriptions.add(sub2);
subscriptions.add(sub3);
mockModel.listContainerRegistries(false);
verify(mockModel, times(3)).listRegistryBySubscriptionId(anyString(), eq(false));
reset(mockModel);
mockModel.listContainerRegistries(true);
verify(mockModel, times(3)).listRegistryBySubscriptionId(anyString(), eq(true));
}
Aggregations