diff --git a/sdk/databasewatcher/azure-resourcemanager-databasewatcher/pom.xml b/sdk/databasewatcher/azure-resourcemanager-databasewatcher/pom.xml index 5b46abc9625e..f4060273b432 100644 --- a/sdk/databasewatcher/azure-resourcemanager-databasewatcher/pom.xml +++ b/sdk/databasewatcher/azure-resourcemanager-databasewatcher/pom.xml @@ -70,5 +70,11 @@ 1.15.3 test + + com.azure.resourcemanager + azure-resourcemanager-resources + 2.48.0 + test + diff --git a/sdk/databasewatcher/azure-resourcemanager-databasewatcher/src/test/java/com/azure/resourcemanager/databasewatcher/DatabaseWatcherManagerTests.java b/sdk/databasewatcher/azure-resourcemanager-databasewatcher/src/test/java/com/azure/resourcemanager/databasewatcher/DatabaseWatcherManagerTests.java new file mode 100644 index 000000000000..fd4199b36dda --- /dev/null +++ b/sdk/databasewatcher/azure-resourcemanager-databasewatcher/src/test/java/com/azure/resourcemanager/databasewatcher/DatabaseWatcherManagerTests.java @@ -0,0 +1,95 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.resourcemanager.databasewatcher; + +import com.azure.core.credential.TokenCredential; +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.management.AzureEnvironment; +import com.azure.core.management.Region; +import com.azure.core.management.profile.AzureProfile; +import com.azure.core.test.TestProxyTestBase; +import com.azure.core.test.annotation.LiveOnly; +import com.azure.core.util.Configuration; +import com.azure.core.util.CoreUtils; +import com.azure.identity.AzurePowerShellCredentialBuilder; +import com.azure.resourcemanager.databasewatcher.models.ManagedServiceIdentityType; +import com.azure.resourcemanager.databasewatcher.models.ManagedServiceIdentityV4; +import com.azure.resourcemanager.databasewatcher.models.Watcher; +import com.azure.resourcemanager.resources.ResourceManager; +import com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Random; + +public class DatabaseWatcherManagerTests extends TestProxyTestBase { + private static final Random RANDOM = new Random(); + private static final Region REGION = Region.US_EAST2; + private static final String RANDOM_PADDING = randomPadding(); + private static String resourceGroupName = "rg" + RANDOM_PADDING; + private DatabaseWatcherManager databaseWatcherManager; + private ResourceManager resourceManager; + private boolean testEnv; + + @Override + public void beforeTest() { + final TokenCredential credential = new AzurePowerShellCredentialBuilder().build(); + final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); + + resourceManager = ResourceManager.configure() + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) + .authenticate(credential, profile) + .withDefaultSubscription(); + + databaseWatcherManager = DatabaseWatcherManager.configure() + .withPolicy(new ProviderRegistrationPolicy(resourceManager)) + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)) + .authenticate(credential, profile); + + // use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI + String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME"); + testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup); + if (testEnv) { + resourceGroupName = testResourceGroup; + } else { + resourceManager.resourceGroups().define(resourceGroupName).withRegion(REGION).create(); + } + } + + @Override + protected void afterTest() { + if (!testEnv) { + resourceManager.resourceGroups().beginDeleteByName(resourceGroupName); + } + } + + @Test + @LiveOnly + public void testCreateDatabaseWatch() { + Watcher watcher = null; + try { + String watchName = "dw" + RANDOM_PADDING; + // @embedmeStart + watcher = databaseWatcherManager.watchers() + .define(watchName) + .withRegion(REGION) + .withExistingResourceGroup(resourceGroupName) + .withIdentity(new ManagedServiceIdentityV4().withType(ManagedServiceIdentityType.SYSTEM_ASSIGNED)) + .create(); + // @embedmeEnd + Assertions.assertEquals(watcher.name(), watchName); + Assertions.assertEquals(watcher.name(), databaseWatcherManager.watchers().getById(watcher.id()).name()); + Assertions.assertTrue(databaseWatcherManager.watchers().list().stream().findAny().isPresent()); + } finally { + if (watcher != null) { + databaseWatcherManager.watchers().deleteById(watcher.id()); + } + } + } + + private static String randomPadding() { + return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000)); + } +} diff --git a/sdk/databasewatcher/test-resources.bicep b/sdk/databasewatcher/test-resources.bicep new file mode 100644 index 000000000000..3ff2bac58bec --- /dev/null +++ b/sdk/databasewatcher/test-resources.bicep @@ -0,0 +1,22 @@ +@description('The tenant id to which the application and resources belong.') +param tenantId string = '72f988bf-86f1-41af-91ab-2d7cd011db47' + +@description('The client id of the service principal used to run tests.') +param testApplicationId string + +@description('This is the object id of the service principal used to run tests.') +param testApplicationOid string + +var contributorRoleId = '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c' + +resource contributorRoleId_name 'Microsoft.Authorization/roleAssignments@2022-04-01' = { + name: guid('contributorRoleId${resourceGroup().name}') + properties: { + roleDefinitionId: contributorRoleId + principalId: testApplicationOid + } +} + +output AZURE_TENANT_ID string = tenantId +output AZURE_SUBSCRIPTION_ID string = subscription().subscriptionId +output AZURE_RESOURCE_GROUP_NAME string = resourceGroup().name diff --git a/sdk/databasewatcher/tests.mgmt.yml b/sdk/databasewatcher/tests.mgmt.yml new file mode 100644 index 000000000000..0a6504957b19 --- /dev/null +++ b/sdk/databasewatcher/tests.mgmt.yml @@ -0,0 +1,15 @@ +trigger: none + +pr: none + +extends: + template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml + parameters: + ServiceDirectory: databasewatcher + Artifacts: + - name: azure-resourcemanager-databasewatcher + groupId: com.azure.resourcemanager + safeName: azureresourcemanagerdatabasewatcher + # Only run tests on Windows to save cost. + MatrixFilters: + - pool=.*(win).*