Search in sources :

Example 1 with RestException

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);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) RestException(com.microsoft.rest.RestException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 2 with RestException

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);
}
Also used : RestException(com.microsoft.rest.RestException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with RestException

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));
}
Also used : ArrayList(java.util.ArrayList) RestException(com.microsoft.rest.RestException) Page(com.microsoft.azure.Page) Registry(com.microsoft.azure.management.containerregistry.Registry) Mockito.anyString(org.mockito.Mockito.anyString) IOException(java.io.IOException) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

RestException (com.microsoft.rest.RestException)3 Test (org.junit.Test)2 AzureSasCredential (com.azure.core.credential.AzureSasCredential)1 Page (com.microsoft.azure.Page)1 Registry (com.microsoft.azure.management.containerregistry.Registry)1 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)1 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)1 DigitalTwinClient (com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient)1 Subscription (com.microsoft.azure.toolkit.lib.common.model.Subscription)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Mockito.anyString (org.mockito.Mockito.anyString)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)1 DigitalTwinTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest)1 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)1