forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some basic tests for the IoT hub control plane SDK (Azure#22951)
- Loading branch information
1 parent
4206f64
commit 8addd7b
Showing
4 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...zure-resourcemanager-iothub/src/main/test/com/azure/resourcemanager/iothub/Constants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.resourcemanager.iothub; | ||
|
||
import com.azure.core.management.Region; | ||
import com.azure.resourcemanager.iothub.models.IotHubSku; | ||
import com.azure.resourcemanager.iothub.models.IotHubSkuInfo; | ||
|
||
public class Constants | ||
{ | ||
static final String DEFAULT_INSTANCE_NAME = "JavaIotHubControlPlaneSDKTest"; | ||
static final Region DEFAULT_REGION = Region.US_WEST_CENTRAL; | ||
|
||
public static class DefaultSku | ||
{ | ||
static final String NAME = "S1"; | ||
static final Long CAPACITY = 1L; | ||
static IotHubSkuInfo INSTANCE = new IotHubSkuInfo() | ||
.withCapacity(Constants.DefaultSku.CAPACITY) | ||
.withName(IotHubSku.fromString(Constants.DefaultSku.NAME)); | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
...-iothub/src/main/test/com/azure/resourcemanager/iothub/IotHubResourceManagementTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.resourcemanager.iothub; | ||
|
||
import com.azure.core.test.annotation.DoNotRecord; | ||
import com.azure.core.util.Context; | ||
import com.azure.resourcemanager.iothub.fluent.models.IotHubDescriptionInner; | ||
import com.azure.resourcemanager.iothub.models.ErrorDetailsException; | ||
import com.azure.resourcemanager.iothub.models.IotHubDescription; | ||
import com.azure.resourcemanager.iothub.models.IotHubSkuInfo; | ||
import com.azure.resourcemanager.resources.ResourceManager; | ||
import com.azure.resourcemanager.resources.models.ResourceGroup; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.azure.resourcemanager.iothub.Constants.DEFAULT_INSTANCE_NAME; | ||
import static com.azure.resourcemanager.iothub.Constants.DEFAULT_REGION; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class IotHubResourceManagementTests extends IotHubTestBase { | ||
@Test | ||
@DoNotRecord(skipInPlayback = true) | ||
public void ServiceCRUD() { | ||
ResourceManager resourceManager = createResourceManager(); | ||
IotHubManager iotHubManager = createIotHubManager(); | ||
ResourceGroup resourceGroup = createResourceGroup(resourceManager); | ||
|
||
String serviceName = DEFAULT_INSTANCE_NAME + "-" + createRandomSuffix(); | ||
|
||
try { | ||
// Define the IoT Hub | ||
IotHubDescription createServiceDescription = iotHubManager | ||
.iotHubResources() | ||
.define(serviceName) | ||
.withRegion(DEFAULT_REGION) | ||
.withExistingResourceGroup(resourceGroup.name()) | ||
.withSku(Constants.DefaultSku.INSTANCE) | ||
.create(); | ||
|
||
// Deploy the IoT Hub | ||
IotHubDescriptionInner updatedIotHubServiceDescriptionInner = | ||
iotHubManager | ||
.serviceClient() | ||
.getIotHubResources() | ||
.createOrUpdate(resourceGroup.name(), serviceName, createServiceDescription.innerModel()); | ||
|
||
// Verify that the deployed IoT Hub has the expected SKU and name | ||
assertNotNull(updatedIotHubServiceDescriptionInner); | ||
assertEquals(Constants.DefaultSku.NAME, updatedIotHubServiceDescriptionInner.sku().name().toString()); | ||
assertEquals(serviceName, updatedIotHubServiceDescriptionInner.name()); | ||
|
||
// Try getting the newly created resource | ||
IotHubDescription getResponse = iotHubManager | ||
.iotHubResources() | ||
.getByResourceGroup(resourceGroup.name(), serviceName); | ||
|
||
assertNotNull(getResponse); | ||
assertNotNull(getResponse.etag()); | ||
assertEquals(Constants.DefaultSku.INSTANCE.name().toString(), getResponse.sku().name().toString()); | ||
assertEquals(Constants.DefaultSku.INSTANCE.capacity().longValue(), getResponse.sku().capacity().longValue()); | ||
assertEquals(DEFAULT_REGION.toString(), getResponse.location()); | ||
|
||
// Delete the service | ||
iotHubManager.iotHubResources().delete(resourceGroup.name(), serviceName, Context.NONE); | ||
} finally { | ||
// No matter if the test fails or not, delete the resource group that contains these test resources | ||
resourceManager.resourceGroups().beginDeleteByName(resourceGroup.name()); | ||
} | ||
} | ||
|
||
@Test | ||
@DoNotRecord(skipInPlayback = true) | ||
public void updateSKU() { | ||
ResourceManager resourceManager = createResourceManager(); | ||
IotHubManager iotHubManager = createIotHubManager(); | ||
ResourceGroup resourceGroup = createResourceGroup(resourceManager); | ||
|
||
try { | ||
// Deploy an IoT Hub | ||
IotHubDescriptionInner iotHubDescription = | ||
createIotHub(iotHubManager, resourceGroup); | ||
|
||
// locally increase the SKU capacity by 1 | ||
long expectedSkuCapacity = iotHubDescription.sku().capacity() + 1; | ||
IotHubSkuInfo newSku = | ||
iotHubDescription | ||
.sku() | ||
.withCapacity(expectedSkuCapacity); | ||
|
||
// update the service representation to use the new SKU | ||
iotHubDescription = iotHubManager | ||
.serviceClient() | ||
.getIotHubResources() | ||
.createOrUpdate( | ||
resourceGroup.name(), | ||
iotHubDescription.name(), | ||
iotHubDescription.withSku(newSku)); | ||
|
||
// verify that the returned IoT Hub description has the new capacity | ||
assertEquals(expectedSkuCapacity, iotHubDescription.sku().capacity()); | ||
} finally { | ||
// No matter if the test fails or not, delete the resource group that contains these test resources | ||
resourceManager.resourceGroups().beginDeleteByName(resourceGroup.name()); | ||
} | ||
} | ||
|
||
@Test | ||
@DoNotRecord(skipInPlayback = true) | ||
public void CreateFailure() { | ||
ResourceManager resourceManager = createResourceManager(); | ||
IotHubManager iotHubManager = createIotHubManager(); | ||
ResourceGroup resourceGroup = createResourceGroup(resourceManager); | ||
|
||
try { | ||
iotHubManager | ||
.iotHubResources() | ||
.define("some invalid service name *&^-#2?") | ||
.withRegion(DEFAULT_REGION) | ||
.withExistingResourceGroup(resourceGroup.name()) | ||
.withSku(Constants.DefaultSku.INSTANCE) | ||
.create(); | ||
|
||
fail("Creating an IoT hub with an invalid name should have thrown an exception"); | ||
} catch (ErrorDetailsException ex) { | ||
// expected throw | ||
} finally { | ||
// No matter if the test fails or not, delete the resource group that contains these test resources | ||
resourceManager.resourceGroups().beginDeleteByName(resourceGroup.name()); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...resourcemanager-iothub/src/main/test/com/azure/resourcemanager/iothub/IotHubTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.resourcemanager.iothub; | ||
|
||
import com.azure.core.management.AzureEnvironment; | ||
import com.azure.core.management.profile.AzureProfile; | ||
import com.azure.core.test.TestBase; | ||
import com.azure.identity.DefaultAzureCredentialBuilder; | ||
import com.azure.resourcemanager.iothub.fluent.models.IotHubDescriptionInner; | ||
import com.azure.resourcemanager.iothub.models.IotHubDescription; | ||
import com.azure.resourcemanager.resources.ResourceManager; | ||
import com.azure.resourcemanager.resources.models.ResourceGroup; | ||
|
||
import java.util.UUID; | ||
|
||
import static com.azure.resourcemanager.iothub.Constants.DEFAULT_INSTANCE_NAME; | ||
import static com.azure.resourcemanager.iothub.Constants.DEFAULT_REGION; | ||
|
||
public class IotHubTestBase extends TestBase { | ||
public ResourceManager createResourceManager() { | ||
return ResourceManager | ||
.authenticate(new DefaultAzureCredentialBuilder().build(), new AzureProfile(AzureEnvironment.AZURE)) | ||
.withDefaultSubscription(); | ||
} | ||
|
||
public ResourceGroup createResourceGroup(ResourceManager resourceManager) { | ||
String resourceGroupName = DEFAULT_INSTANCE_NAME + "-" + createRandomSuffix(); | ||
return resourceManager.resourceGroups() | ||
.define(resourceGroupName) | ||
.withRegion(DEFAULT_REGION) | ||
.create(); | ||
} | ||
|
||
public IotHubManager createIotHubManager() { | ||
return IotHubManager | ||
.authenticate(new DefaultAzureCredentialBuilder().build(), new AzureProfile(AzureEnvironment.AZURE)); | ||
} | ||
|
||
public IotHubDescriptionInner createIotHub(IotHubManager iotHubManager, ResourceGroup resourceGroup) { | ||
String serviceName = DEFAULT_INSTANCE_NAME + "-" + createRandomSuffix(); | ||
|
||
IotHubDescription iotHubDescription = iotHubManager | ||
.iotHubResources() | ||
.define(serviceName) | ||
.withRegion(DEFAULT_REGION) | ||
.withExistingResourceGroup(resourceGroup.name()) | ||
.withSku(Constants.DefaultSku.INSTANCE) | ||
.create(); | ||
|
||
IotHubDescriptionInner inner = iotHubManager | ||
.serviceClient() | ||
.getIotHubResources() | ||
.createOrUpdate(resourceGroup.name(), serviceName, iotHubDescription.innerModel()); | ||
|
||
return inner; | ||
} | ||
|
||
public String createRandomSuffix() { | ||
// need to shorten the UUID since max service name is 50 characters | ||
return UUID.randomUUID().toString().substring(0, 18); | ||
} | ||
} |