|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager.deviceprovisioningservices; |
| 5 | + |
| 6 | +import com.azure.core.test.annotation.DoNotRecord; |
| 7 | +import com.azure.core.util.Context; |
| 8 | +import com.azure.resourcemanager.deviceprovisioningservices.fluent.models.ProvisioningServiceDescriptionInner; |
| 9 | +import com.azure.resourcemanager.deviceprovisioningservices.models.ErrorDetailsException; |
| 10 | +import com.azure.resourcemanager.deviceprovisioningservices.models.IotDpsPropertiesDescription; |
| 11 | +import com.azure.resourcemanager.deviceprovisioningservices.models.IotDpsSkuInfo; |
| 12 | +import com.azure.resourcemanager.deviceprovisioningservices.models.NameAvailabilityInfo; |
| 13 | +import com.azure.resourcemanager.deviceprovisioningservices.models.OperationInputs; |
| 14 | +import com.azure.resourcemanager.deviceprovisioningservices.models.ProvisioningServiceDescription; |
| 15 | +import com.azure.resourcemanager.resources.ResourceManager; |
| 16 | +import com.azure.resourcemanager.resources.models.ResourceGroup; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.*; |
| 20 | + |
| 21 | +public class DeviceProvisioningResourceManagementTests extends DeviceProvisioningTestBase { |
| 22 | + @Test |
| 23 | + @DoNotRecord(skipInPlayback = true) |
| 24 | + public void serviceCRUD() { |
| 25 | + ResourceManager resourceManager = createResourceManager(); |
| 26 | + IotDpsManager iotDpsManager = createIotDpsManager(); |
| 27 | + ResourceGroup resourceGroup = createResourceGroup(resourceManager); |
| 28 | + |
| 29 | + String serviceName = DEFAULT_INSTANCE_NAME + "-" + createRandomSuffix(); |
| 30 | + try { |
| 31 | + try { |
| 32 | + iotDpsManager.iotDpsResources() |
| 33 | + .checkProvisioningServiceNameAvailability(new OperationInputs().withName(serviceName)); |
| 34 | + } catch (ErrorDetailsException ex) { |
| 35 | + // error code signifies that the resource name is not available, need to delete it before creating a |
| 36 | + // new one. |
| 37 | + if (ex.getValue().getHttpStatusCode().equals("404307")) { |
| 38 | + // Delete the service if it already exists |
| 39 | + iotDpsManager.iotDpsResources().delete(resourceGroup.name(), serviceName, Context.NONE); |
| 40 | + |
| 41 | + // After deleting the existing service, check that the name is now available to use |
| 42 | + NameAvailabilityInfo availabilityInfo = iotDpsManager.iotDpsResources() |
| 43 | + .checkProvisioningServiceNameAvailability(new OperationInputs().withName(serviceName)); |
| 44 | + |
| 45 | + assertTrue( |
| 46 | + availabilityInfo.nameAvailable(), |
| 47 | + "Service name was unavailable even after deleting the existing service with the name"); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + ProvisioningServiceDescription createServiceDescription = iotDpsManager |
| 52 | + .iotDpsResources() |
| 53 | + .define(serviceName) |
| 54 | + .withRegion(DEFAULT_REGION) |
| 55 | + .withExistingResourceGroup(resourceGroup.name()) |
| 56 | + .withProperties(new IotDpsPropertiesDescription()) |
| 57 | + .withSku(Constants.DefaultSku.INSTANCE) |
| 58 | + .create(); |
| 59 | + |
| 60 | + ProvisioningServiceDescriptionInner updatedProvisioningServiceDescriptionInner = |
| 61 | + iotDpsManager |
| 62 | + .serviceClient() |
| 63 | + .getIotDpsResources() |
| 64 | + .createOrUpdate(resourceGroup.name(), serviceName, createServiceDescription.innerModel()); |
| 65 | + |
| 66 | + assertNotNull(updatedProvisioningServiceDescriptionInner); |
| 67 | + assertEquals(Constants.DefaultSku.NAME, updatedProvisioningServiceDescriptionInner.sku().name().toString()); |
| 68 | + assertEquals(serviceName, updatedProvisioningServiceDescriptionInner.name()); |
| 69 | + |
| 70 | + // Try getting the newly created resource |
| 71 | + ProvisioningServiceDescription getResponse = iotDpsManager |
| 72 | + .iotDpsResources() |
| 73 | + .getByResourceGroup(resourceGroup.name(), serviceName); |
| 74 | + |
| 75 | + assertNotNull(getResponse); |
| 76 | + assertNotNull(getResponse.etag()); |
| 77 | + assertEquals(Constants.DefaultSku.INSTANCE.name().toString(), getResponse.sku().name().toString()); |
| 78 | + assertEquals(Constants.DefaultSku.INSTANCE.capacity().longValue(), getResponse.sku().capacity().longValue()); |
| 79 | + assertEquals(DEFAULT_REGION.toString(), getResponse.location()); |
| 80 | + |
| 81 | + // Delete the service |
| 82 | + iotDpsManager.iotDpsResources().delete(resourceGroup.name(), serviceName, Context.NONE); |
| 83 | + } finally { |
| 84 | + // No matter if the test fails or not, delete the resource group that contains these test resources |
| 85 | + deleteResourceGroup(resourceManager, resourceGroup); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + @DoNotRecord(skipInPlayback = true) |
| 91 | + public void updateSKU() { |
| 92 | + ResourceManager resourceManager = createResourceManager(); |
| 93 | + IotDpsManager iotDpsManager = createIotDpsManager(); |
| 94 | + ResourceGroup resourceGroup = createResourceGroup(resourceManager); |
| 95 | + |
| 96 | + try { |
| 97 | + // create the provisioning service |
| 98 | + ProvisioningServiceDescriptionInner provisioningServiceDescription = |
| 99 | + createProvisioningService(iotDpsManager, resourceGroup); |
| 100 | + |
| 101 | + // locally increase the SKU capacity by 1 |
| 102 | + long expectedSkuCapacity = provisioningServiceDescription.sku().capacity() + 1; |
| 103 | + IotDpsSkuInfo newSku = |
| 104 | + provisioningServiceDescription |
| 105 | + .sku() |
| 106 | + .withCapacity(expectedSkuCapacity); |
| 107 | + |
| 108 | + // update the service representation to use the new SKU |
| 109 | + provisioningServiceDescription = iotDpsManager |
| 110 | + .serviceClient() |
| 111 | + .getIotDpsResources() |
| 112 | + .createOrUpdate( |
| 113 | + resourceGroup.name(), |
| 114 | + provisioningServiceDescription.name(), |
| 115 | + provisioningServiceDescription.withSku(newSku)); |
| 116 | + |
| 117 | + assertEquals(expectedSkuCapacity, provisioningServiceDescription.sku().capacity()); |
| 118 | + } finally { |
| 119 | + // No matter if the test fails or not, delete the resource group that contains these test resources |
| 120 | + deleteResourceGroup(resourceManager, resourceGroup); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + @DoNotRecord(skipInPlayback = true) |
| 126 | + public void createFailure() { |
| 127 | + ResourceManager resourceManager = createResourceManager(); |
| 128 | + IotDpsManager iotDpsManager = createIotDpsManager(); |
| 129 | + ResourceGroup resourceGroup = createResourceGroup(resourceManager); |
| 130 | + |
| 131 | + try { |
| 132 | + iotDpsManager |
| 133 | + .iotDpsResources() |
| 134 | + .define("some invalid service name *&^-#2?") |
| 135 | + .withRegion(DEFAULT_REGION) |
| 136 | + .withExistingResourceGroup(resourceGroup.name()) |
| 137 | + .withProperties(new IotDpsPropertiesDescription()) |
| 138 | + .withSku(Constants.DefaultSku.INSTANCE) |
| 139 | + .create(); |
| 140 | + |
| 141 | + fail("Creating a device provisioning service with an invalid name should have thrown an exception"); |
| 142 | + } catch (ErrorDetailsException ex) { |
| 143 | + // expected throw |
| 144 | + } finally { |
| 145 | + // No matter if the test fails or not, delete the resource group that contains these test resources |
| 146 | + deleteResourceGroup(resourceManager, resourceGroup); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments