|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager.sphere; |
| 5 | + |
| 6 | +import com.azure.core.credential.TokenCredential; |
| 7 | +import com.azure.core.http.policy.HttpLogDetailLevel; |
| 8 | +import com.azure.core.http.policy.HttpLogOptions; |
| 9 | +import com.azure.core.management.AzureEnvironment; |
| 10 | +import com.azure.core.management.Region; |
| 11 | +import com.azure.core.management.profile.AzureProfile; |
| 12 | +import com.azure.core.test.TestBase; |
| 13 | +import com.azure.core.test.annotation.LiveOnly; |
| 14 | +import com.azure.core.util.Configuration; |
| 15 | +import com.azure.core.util.CoreUtils; |
| 16 | +import com.azure.identity.DefaultAzureCredentialBuilder; |
| 17 | +import com.azure.resourcemanager.resources.ResourceManager; |
| 18 | +import com.azure.resourcemanager.sphere.models.Catalog; |
| 19 | +import org.junit.jupiter.api.Assertions; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.util.Random; |
| 23 | + |
| 24 | +public class AzureSphereManagerTests extends TestBase { |
| 25 | + private static final Random RANDOM = new Random(); |
| 26 | + private static final Region REGION_USEAST = Region.US_EAST; |
| 27 | + private static final Region REGION_GLOBAL = Region.create("global", "Global"); |
| 28 | + private String resourceGroupName = "rg" + randomPadding(); |
| 29 | + private AzureSphereManager azureSphereManager = null; |
| 30 | + private ResourceManager resourceManager; |
| 31 | + private boolean testEnv; |
| 32 | + |
| 33 | + @Override |
| 34 | + public void beforeTest() { |
| 35 | + final TokenCredential credential = new DefaultAzureCredentialBuilder().build(); |
| 36 | + final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); |
| 37 | + |
| 38 | + azureSphereManager = AzureSphereManager |
| 39 | + .configure() |
| 40 | + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) |
| 41 | + .authenticate(credential, profile); |
| 42 | + |
| 43 | + resourceManager = ResourceManager |
| 44 | + .configure() |
| 45 | + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) |
| 46 | + .authenticate(credential, profile) |
| 47 | + .withDefaultSubscription(); |
| 48 | + |
| 49 | + // use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI |
| 50 | + String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME"); |
| 51 | + testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup); |
| 52 | + if (testEnv) { |
| 53 | + resourceGroupName = testResourceGroup; |
| 54 | + } else { |
| 55 | + resourceManager.resourceGroups() |
| 56 | + .define(resourceGroupName) |
| 57 | + .withRegion(REGION_USEAST) |
| 58 | + .create(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void afterTest() { |
| 64 | + if (!testEnv) { |
| 65 | + resourceManager.resourceGroups().beginDeleteByName(resourceGroupName); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + @LiveOnly |
| 71 | + public void testCreateCatalog() { |
| 72 | + Catalog catalog = null; |
| 73 | + try { |
| 74 | + String catalogName = "catalog" + randomPadding(); |
| 75 | + // @embedStart |
| 76 | + catalog = azureSphereManager.catalogs() |
| 77 | + .define(catalogName) |
| 78 | + .withRegion(REGION_GLOBAL) |
| 79 | + .withExistingResourceGroup(resourceGroupName) |
| 80 | + .create(); |
| 81 | + // @embedEnd |
| 82 | + catalog.refresh(); |
| 83 | + Assertions.assertEquals(catalogName, catalog.name()); |
| 84 | + Assertions.assertEquals(catalogName, azureSphereManager.catalogs().getById(catalog.id()).name()); |
| 85 | + Assertions.assertTrue(azureSphereManager.catalogs().listByResourceGroup(resourceGroupName).stream().findAny().isPresent()); |
| 86 | + } finally { |
| 87 | + if (catalog != null) { |
| 88 | + azureSphereManager.catalogs().deleteById(catalog.id()); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static String randomPadding() { |
| 94 | + return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000)); |
| 95 | + } |
| 96 | +} |
0 commit comments