|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Azure.Core; |
| 9 | +using Azure.Core.TestFramework; |
| 10 | +using Azure.ResourceManager.DeviceRegistry.Models; |
| 11 | +using NUnit.Framework; |
| 12 | + |
| 13 | +namespace Azure.ResourceManager.DeviceRegistry.Tests.Scenario |
| 14 | +{ |
| 15 | + public class DeviceRegistryNamespaceDevicesOperationsTest : DeviceRegistryManagementTestBase |
| 16 | + { |
| 17 | + private readonly string _subscriptionId = "8c64812d-6e59-4e65-96b3-14a7cdb1a4e4"; |
| 18 | + private readonly string _namespaceRgName = "adr-sdk-test-rg"; |
| 19 | + private readonly string _namespaceName = "adr-namespace"; |
| 20 | + private readonly string _deviceNamePrefix = "deviceregistry-test-device-sdk"; |
| 21 | + private readonly string _extendedLocationName = "/subscriptions/8c64812d-6e59-4e65-96b3-14a7cdb1a4e4/resourceGroups/adr-sdk-test-rg/providers/Microsoft.ExtendedLocation/customLocations/adr-sdk-test-cluster-cl"; |
| 22 | + |
| 23 | + public DeviceRegistryNamespaceDevicesOperationsTest(bool isAsync) : base(isAsync) |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + [RecordedTest] |
| 28 | + public async Task NamespaceDevicesCrudOperationsTest() |
| 29 | + { |
| 30 | + var deviceName = Recording.GenerateAssetName(_deviceNamePrefix); |
| 31 | + |
| 32 | + var subscription = Client.GetSubscriptionResource(new ResourceIdentifier($"/subscriptions/{_subscriptionId}")); |
| 33 | + var extendedLocation = new DeviceRegistryExtendedLocation() { ExtendedLocationType = "CustomLocation", Name = _extendedLocationName }; |
| 34 | + |
| 35 | + var namespaceRg = await subscription.GetResourceGroupAsync(_namespaceRgName); |
| 36 | + var namespacesCollection = namespaceRg.Value.GetDeviceRegistryNamespaces(); |
| 37 | + var namespaceResource = await namespacesCollection.GetAsync(_namespaceName); |
| 38 | + var devicesCollection = namespaceResource.Value.GetDeviceRegistryNamespaceDevices(); |
| 39 | + |
| 40 | + // Create DeviceRegistry Device |
| 41 | + var deviceData = new DeviceRegistryNamespaceDeviceData(AzureLocation.WestUS2) |
| 42 | + { |
| 43 | + Properties = new() |
| 44 | + { |
| 45 | + Manufacturer = "Contoso", |
| 46 | + Model = "Model 5000", |
| 47 | + OperatingSystem = "Linux", |
| 48 | + OperatingSystemVersion = "18.04", |
| 49 | + Endpoints = new MessagingEndpoints(), |
| 50 | + } |
| 51 | + }; |
| 52 | + deviceData.Properties.Endpoints.Inbound.Add("myendpoint1", new InboundEndpoints() |
| 53 | + { |
| 54 | + Address = "https://myendpoint1.westeurope-1.iothub.azure.net", |
| 55 | + EndpointType = "Microsoft.Devices/IoTHubs" |
| 56 | + }); |
| 57 | + var deviceCreateOrUpdateResponse = await devicesCollection.CreateOrUpdateAsync(WaitUntil.Completed, deviceName, deviceData, CancellationToken.None); |
| 58 | + Assert.IsNotNull(deviceCreateOrUpdateResponse.Value); |
| 59 | + Assert.IsTrue(Guid.TryParse(deviceCreateOrUpdateResponse.Value.Data.Properties.Uuid, out _)); |
| 60 | + Assert.AreEqual(deviceCreateOrUpdateResponse.Value.Data.Properties.ExternalDeviceId, deviceCreateOrUpdateResponse.Value.Data.Properties.Uuid); |
| 61 | + Assert.AreEqual(deviceCreateOrUpdateResponse.Value.Data.Properties.Manufacturer, deviceData.Properties.Manufacturer); |
| 62 | + Assert.AreEqual(deviceCreateOrUpdateResponse.Value.Data.Properties.Model, deviceData.Properties.Model); |
| 63 | + Assert.AreEqual(deviceCreateOrUpdateResponse.Value.Data.Properties.OperatingSystem, deviceData.Properties.OperatingSystem); |
| 64 | + Assert.AreEqual(deviceCreateOrUpdateResponse.Value.Data.Properties.OperatingSystemVersion, deviceData.Properties.OperatingSystemVersion); |
| 65 | + Assert.AreEqual(deviceCreateOrUpdateResponse.Value.Data.Properties.Endpoints.Inbound["myendpoint1"].Address, deviceData.Properties.Endpoints.Inbound["myendpoint1"].Address); |
| 66 | + Assert.AreEqual(deviceCreateOrUpdateResponse.Value.Data.Properties.Endpoints.Inbound["myendpoint1"].EndpointType, deviceData.Properties.Endpoints.Inbound["myendpoint1"].EndpointType); |
| 67 | + Assert.AreEqual(deviceCreateOrUpdateResponse.Value.Data.Properties.Version, 1); |
| 68 | + |
| 69 | + // Read DeviceRegistry Device |
| 70 | + var deviceReadResponse = await devicesCollection.GetAsync(deviceName, CancellationToken.None); |
| 71 | + Assert.IsNotNull(deviceReadResponse.Value); |
| 72 | + Assert.IsTrue(Guid.TryParse(deviceReadResponse.Value.Data.Properties.Uuid, out _)); |
| 73 | + Assert.AreEqual(deviceReadResponse.Value.Data.Properties.ExternalDeviceId, deviceReadResponse.Value.Data.Properties.Uuid); |
| 74 | + Assert.AreEqual(deviceReadResponse.Value.Data.Properties.Manufacturer, deviceData.Properties.Manufacturer); |
| 75 | + Assert.AreEqual(deviceReadResponse.Value.Data.Properties.Model, deviceData.Properties.Model); |
| 76 | + Assert.AreEqual(deviceReadResponse.Value.Data.Properties.OperatingSystem, deviceData.Properties.OperatingSystem); |
| 77 | + Assert.AreEqual(deviceReadResponse.Value.Data.Properties.OperatingSystemVersion, deviceData.Properties.OperatingSystemVersion); |
| 78 | + Assert.AreEqual(deviceReadResponse.Value.Data.Properties.Endpoints.Inbound["myendpoint1"].Address, deviceData.Properties.Endpoints.Inbound["myendpoint1"].Address); |
| 79 | + Assert.AreEqual(deviceReadResponse.Value.Data.Properties.Endpoints.Inbound["myendpoint1"].EndpointType, deviceData.Properties.Endpoints.Inbound["myendpoint1"].EndpointType); |
| 80 | + Assert.AreEqual(deviceReadResponse.Value.Data.Properties.Version, 1); |
| 81 | + |
| 82 | + // List DeviceRegistry Device by Resource Group |
| 83 | + var deviceResourcesListByResourceGroup = new List<DeviceRegistryNamespaceDeviceResource>(); |
| 84 | + var deviceResourceListByResourceGroupAsyncIteratorPage = devicesCollection.GetAllAsync(CancellationToken.None).AsPages(null, 5); |
| 85 | + await foreach (var deviceEntryPage in deviceResourceListByResourceGroupAsyncIteratorPage) |
| 86 | + { |
| 87 | + deviceResourcesListByResourceGroup.AddRange(deviceEntryPage.Values); |
| 88 | + break; // limit to the the first page of results |
| 89 | + } |
| 90 | + Assert.IsNotEmpty(deviceResourcesListByResourceGroup); |
| 91 | + Assert.GreaterOrEqual(deviceResourcesListByResourceGroup.Count, 1); |
| 92 | + |
| 93 | + // Update DeviceRegistry Device |
| 94 | + var device = deviceReadResponse.Value; |
| 95 | + var devicePatchData = new DeviceRegistryNamespaceDevicePatch |
| 96 | + { |
| 97 | + Properties = new() |
| 98 | + { |
| 99 | + OperatingSystemVersion = "20.04", |
| 100 | + } |
| 101 | + }; |
| 102 | + var deviceUpdateResponse = await device.UpdateAsync(WaitUntil.Completed, devicePatchData, CancellationToken.None); |
| 103 | + Assert.IsNotNull(deviceUpdateResponse.Value); |
| 104 | + Assert.IsTrue(Guid.TryParse(deviceUpdateResponse.Value.Data.Properties.Uuid, out _)); |
| 105 | + Assert.AreEqual(deviceUpdateResponse.Value.Data.Properties.ExternalDeviceId, deviceUpdateResponse.Value.Data.Properties.Uuid); |
| 106 | + Assert.AreEqual(deviceUpdateResponse.Value.Data.Properties.Manufacturer, deviceData.Properties.Manufacturer); |
| 107 | + Assert.AreEqual(deviceUpdateResponse.Value.Data.Properties.Model, deviceData.Properties.Model); |
| 108 | + Assert.AreEqual(deviceUpdateResponse.Value.Data.Properties.OperatingSystem, deviceData.Properties.OperatingSystem); |
| 109 | + Assert.AreEqual(deviceUpdateResponse.Value.Data.Properties.OperatingSystemVersion, devicePatchData.Properties.OperatingSystemVersion); |
| 110 | + Assert.AreEqual(deviceUpdateResponse.Value.Data.Properties.Endpoints.Inbound["myendpoint1"].Address, deviceData.Properties.Endpoints.Inbound["myendpoint1"].Address); |
| 111 | + Assert.AreEqual(deviceUpdateResponse.Value.Data.Properties.Endpoints.Inbound["myendpoint1"].EndpointType, deviceData.Properties.Endpoints.Inbound["myendpoint1"].EndpointType); |
| 112 | + Assert.AreEqual(deviceUpdateResponse.Value.Data.Properties.Version, 2); |
| 113 | + |
| 114 | + // Delete DeviceRegistry Device |
| 115 | + try |
| 116 | + { |
| 117 | + await device.DeleteAsync(WaitUntil.Completed, CancellationToken.None); |
| 118 | + } |
| 119 | + catch (RequestFailedException ex) |
| 120 | + { |
| 121 | + // Delete temporary returns 200 since async operation is defined for the resource but not implemented in RP |
| 122 | + if (ex.Status != 200) |
| 123 | + { |
| 124 | + throw; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments