Skip to content

Commit

Permalink
Add some basic tests for the IoT hub control plane SDK (Azure#22951)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtay-microsoft authored Aug 24, 2021
1 parent 4206f64 commit 8addd7b
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 0 deletions.
24 changes: 24 additions & 0 deletions sdk/iothub/azure-resourcemanager-iothub/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@
<artifactId>azure-core-management</artifactId>
<version>1.4.0</version> <!-- {x-version-update;com.azure:azure-core-management;dependency} -->
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.3.5</version> <!-- {x-version-update;com.azure:azure-identity;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager-resources</artifactId>
<version>2.7.0</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-resources;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-test</artifactId>
<version>1.7.0</version> <!-- {x-version-update;com.azure:azure-core-test;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version> <!-- {x-version-update;org.slf4j:slf4j-simple;external_dependency} -->
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
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));
}
}
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());
}
}
}
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);
}
}

0 comments on commit 8addd7b

Please sign in to comment.