Skip to content

Commit 9ebf84c

Browse files
mgmt, dps, move test to correct location (Azure#26722)
* mgmt, dps, move test to correct location * fix checkstyle * prepare for enabling live test
1 parent 6a944e2 commit 9ebf84c

File tree

8 files changed

+691
-0
lines changed

8 files changed

+691
-0
lines changed

sdk/deviceprovisioningservices/azure-resourcemanager-deviceprovisioningservices/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
<artifactId>azure-core-management</artifactId>
5252
<version>1.5.1</version> <!-- {x-version-update;com.azure:azure-core-management;dependency} -->
5353
</dependency>
54+
<dependency>
55+
<groupId>org.junit.jupiter</groupId>
56+
<artifactId>junit-jupiter-engine</artifactId>
57+
<version>5.8.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-engine;external_dependency} -->
58+
<scope>test</scope>
59+
</dependency>
5460
<dependency>
5561
<groupId>com.azure</groupId>
5662
<artifactId>azure-identity</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.resourcemanager.deviceprovisioningservices.fluent.models.ProvisioningServiceDescriptionInner;
8+
import com.azure.resourcemanager.deviceprovisioningservices.models.AllocationPolicy;
9+
import com.azure.resourcemanager.deviceprovisioningservices.models.IotDpsPropertiesDescription;
10+
import com.azure.resourcemanager.resources.ResourceManager;
11+
import com.azure.resourcemanager.resources.models.ResourceGroup;
12+
import org.junit.jupiter.api.Test;
13+
14+
import static org.junit.jupiter.api.Assertions.assertTrue;
15+
16+
public class AllocationPolicyTests extends DeviceProvisioningTestBase {
17+
@Test
18+
@DoNotRecord(skipInPlayback = true)
19+
public void get() {
20+
ResourceManager resourceManager = createResourceManager();
21+
IotDpsManager iotDpsManager = createIotDpsManager();
22+
ResourceGroup resourceGroup = createResourceGroup(resourceManager);
23+
24+
try {
25+
ProvisioningServiceDescriptionInner provisioningServiceDescription =
26+
createProvisioningService(iotDpsManager, resourceGroup);
27+
28+
AllocationPolicy allocationPolicy =
29+
provisioningServiceDescription
30+
.properties()
31+
.allocationPolicy();
32+
33+
assertTrue(Constants.ALLOCATION_POLICIES.contains(allocationPolicy));
34+
} finally {
35+
// No matter if the test fails or not, delete the resource group that contains these test resources
36+
deleteResourceGroup(resourceManager, resourceGroup);
37+
}
38+
}
39+
40+
@Test
41+
@DoNotRecord(skipInPlayback = true)
42+
public void update() {
43+
ResourceManager resourceManager = createResourceManager();
44+
IotDpsManager iotDpsManager = createIotDpsManager();
45+
ResourceGroup resourceGroup = createResourceGroup(resourceManager);
46+
47+
try {
48+
ProvisioningServiceDescriptionInner provisioningServiceDescription =
49+
createProvisioningService(iotDpsManager, resourceGroup);
50+
51+
// pick a new allocation policy that is different from the current allocation policy
52+
AllocationPolicy newAllocationPolicy = AllocationPolicy.GEO_LATENCY;
53+
if (provisioningServiceDescription.properties().allocationPolicy() == AllocationPolicy.GEO_LATENCY)
54+
{
55+
newAllocationPolicy = AllocationPolicy.HASHED;
56+
}
57+
58+
// update the service's allocation policy to the new policy
59+
IotDpsPropertiesDescription propertiesDescription =
60+
provisioningServiceDescription
61+
.properties()
62+
.withAllocationPolicy(newAllocationPolicy);
63+
64+
provisioningServiceDescription.withProperties(propertiesDescription);
65+
66+
iotDpsManager
67+
.serviceClient()
68+
.getIotDpsResources()
69+
.createOrUpdate(
70+
resourceGroup.name(),
71+
provisioningServiceDescription.name(),
72+
provisioningServiceDescription);
73+
74+
} finally {
75+
// No matter if the test fails or not, delete the resource group that contains these test resources
76+
deleteResourceGroup(resourceManager, resourceGroup);
77+
}
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.resourcemanager.deviceprovisioningservices.fluent.models.CertificateListDescriptionInner;
8+
import com.azure.resourcemanager.deviceprovisioningservices.fluent.models.CertificateResponseInner;
9+
import com.azure.resourcemanager.deviceprovisioningservices.fluent.models.ProvisioningServiceDescriptionInner;
10+
import com.azure.resourcemanager.deviceprovisioningservices.fluent.models.VerificationCodeResponseInner;
11+
import com.azure.resourcemanager.deviceprovisioningservices.models.CertificateBodyDescription;
12+
import com.azure.resourcemanager.resources.ResourceManager;
13+
import com.azure.resourcemanager.resources.models.ResourceGroup;
14+
import org.junit.jupiter.api.Test;
15+
16+
import static org.junit.jupiter.api.Assertions.*;
17+
18+
public class CertificatesTests extends DeviceProvisioningTestBase {
19+
@Test
20+
@DoNotRecord(skipInPlayback = true)
21+
public void certificateCRUD() {
22+
ResourceManager resourceManager = createResourceManager();
23+
IotDpsManager iotDpsManager = createIotDpsManager();
24+
ResourceGroup resourceGroup = createResourceGroup(resourceManager);
25+
26+
try {
27+
ProvisioningServiceDescriptionInner provisioningServiceDescription =
28+
createProvisioningService(iotDpsManager, resourceGroup);
29+
30+
CertificateBodyDescription certificateBodyDescription =
31+
new CertificateBodyDescription().withCertificate(Constants.Certificate.CONTENT);
32+
33+
// create a new certificate
34+
iotDpsManager
35+
.serviceClient()
36+
.getDpsCertificates()
37+
.createOrUpdate(
38+
resourceGroup.name(),
39+
provisioningServiceDescription.name(),
40+
Constants.Certificate.NAME,
41+
certificateBodyDescription);
42+
43+
CertificateListDescriptionInner certificateListDescription =
44+
iotDpsManager
45+
.serviceClient()
46+
.getDpsCertificates()
47+
.list(
48+
resourceGroup.name(),
49+
provisioningServiceDescription.name());
50+
51+
assertEquals(1, certificateListDescription.value().size());
52+
53+
CertificateResponseInner certificate = certificateListDescription.value().get(0);
54+
assertFalse(certificate.properties().isVerified());
55+
assertEquals(Constants.Certificate.SUBJECT, certificate.properties().subject());
56+
assertEquals(Constants.Certificate.THUMBPRINT, certificate.properties().thumbprint());
57+
58+
// verify that you can generate certificate verification codes
59+
VerificationCodeResponseInner verificationCodeResponse =
60+
iotDpsManager
61+
.serviceClient()
62+
.getDpsCertificates()
63+
.generateVerificationCode(
64+
certificate.name(),
65+
certificate.etag(),
66+
resourceGroup.name(),
67+
provisioningServiceDescription.name());
68+
69+
assertNotNull(verificationCodeResponse.properties().verificationCode());
70+
71+
// delete the certificate
72+
iotDpsManager
73+
.serviceClient()
74+
.getDpsCertificates()
75+
.delete(
76+
resourceGroup.name(),
77+
verificationCodeResponse.etag(),
78+
provisioningServiceDescription.name(),
79+
certificate.name());
80+
81+
// verify that the certificate isn't listed anymore
82+
certificateListDescription =
83+
iotDpsManager
84+
.serviceClient()
85+
.getDpsCertificates()
86+
.list(
87+
resourceGroup.name(),
88+
provisioningServiceDescription.name());
89+
90+
assertEquals(0, certificateListDescription.value().size());
91+
92+
} finally {
93+
// No matter if the test fails or not, delete the resource group that contains these test resources
94+
deleteResourceGroup(resourceManager, resourceGroup);
95+
}
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.resourcemanager.deviceprovisioningservices.models.AllocationPolicy;
7+
import com.azure.resourcemanager.deviceprovisioningservices.models.IotDpsSku;
8+
import com.azure.resourcemanager.deviceprovisioningservices.models.IotDpsSkuInfo;
9+
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
public class Constants {
14+
static final String DEFAULT_LOCATION = "WestUS2";
15+
static final String OWNER_ACCESS_KEY_NAME = "provisioningserviceowner";
16+
static final String IOTHUB_OWNER_ACCESS_KEY_NAME = "iothubowner";
17+
18+
public static class DefaultSku {
19+
static final String NAME = "S1";
20+
static final Long CAPACITY = 1L;
21+
static final IotDpsSkuInfo INSTANCE = new IotDpsSkuInfo()
22+
.withCapacity(Constants.DefaultSku.CAPACITY)
23+
.withName(IotDpsSku.fromString(Constants.DefaultSku.NAME));
24+
}
25+
26+
public static class Certificate {
27+
static final String CONTENT =
28+
"MIIBvjCCAWOgAwIBAgIQG6PoBFT6GLJGNKn/EaxltTAKBggqhkjOPQQDAjAcMRow"
29+
+ "GAYDVQQDDBFBenVyZSBJb1QgUm9vdCBDQTAeFw0xNzExMDMyMDUyNDZaFw0xNzEy"
30+
+ "MDMyMTAyNDdaMBwxGjAYBgNVBAMMEUF6dXJlIElvVCBSb290IENBMFkwEwYHKoZI"
31+
+ "zj0CAQYIKoZIzj0DAQcDQgAE+CgpnW3K+KRNIi/U6Zqe/Al9m8PExHX2KgakmGTf"
32+
+ "E04nNBwnSoygWb0ekqpT+Lm+OP56LMMe9ynVNryDEr9OSKOBhjCBgzAOBgNVHQ8B"
33+
+ "Af8EBAMCAgQwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMBMB8GA1UdEQQY"
34+
+ "MBaCFENOPUF6dXJlIElvVCBSb290IENBMBIGA1UdEwEB/wQIMAYBAf8CAQwwHQYD"
35+
+ "VR0OBBYEFDjiklfHQzw1G0A33BcmRQTjAivTMAoGCCqGSM49BAMCA0kAMEYCIQCt"
36+
+ "jJ4bAvoYuDhwr92Kk+OkvpPF+qBFiRfrA/EC4YGtzQIhAO79WPtbUnBQ5fsQnW2a"
37+
+ "UAT4yJGWL+7l4/qfmqblb96n";
38+
39+
static final String NAME = "DPStestCert";
40+
static final String SUBJECT = "Azure IoT Root CA";
41+
static final String THUMBPRINT = "9F0983E8F2DB2DB3582997FEF331247D872DEE32";
42+
}
43+
44+
static final List<AllocationPolicy> ALLOCATION_POLICIES = Arrays.asList(AllocationPolicy.GEO_LATENCY, AllocationPolicy.HASHED, AllocationPolicy.STATIC);
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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

Comments
 (0)