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