-
Notifications
You must be signed in to change notification settings - Fork 5.1k
test(PublishTelemetry): Add E2E test for Publish Telemetry API #12479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a286bd1
fix(adt): fix BasicDigitalTwin and use in an example
7c71e59
Merge commit 'refs/pull/12428/head' of https://github.com/Azure/azure…
0b2fd1e
test(PublishTelemetry): Add E2E test for PublishTelemetry API
e9c707e
Add ConfigureAwait for await calls
29aacf7
Add recorded sessions
76e0621
Sanitize a couple of headers for the Publish Telemetry E2E tests
b470456
mock the message id and timestamp instead of sanitizing them
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 2 additions & 3 deletions
5
sdk/digitaltwins/Azure.DigitalTwins.Core/tests/EventRouteTests.cs
This file contains hidden or 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
145 changes: 145 additions & 0 deletions
145
sdk/digitaltwins/Azure.DigitalTwins.Core/tests/PublishTelemetryTests.cs
This file contains hidden or 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,145 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using Azure.DigitalTwins.Core.Models; | ||
| using FluentAssertions; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Azure.DigitalTwins.Core.Tests | ||
| { | ||
| /// <summary> | ||
| /// Tests for DigitalTwinsClient APIs that handle publishing telemetry messages to Azure Digital Twins. | ||
| /// </summary> | ||
| public class PublishTelemetryTests : E2eTestBase | ||
| { | ||
| public PublishTelemetryTests(bool isAsync) | ||
| : base(isAsync) | ||
| { | ||
| } | ||
|
|
||
| // Infrastructure setup script uses this hardcoded value when linking the test eventhub to the test digital twins instance. | ||
| private const string EndpointName = "someEventHubEndpoint"; | ||
|
|
||
| [Test] | ||
| public async Task PublishTelemetry_Lifecycle() | ||
| { | ||
| // Setup | ||
|
|
||
| // Create a DigitalTwinsClient instance. | ||
| DigitalTwinsClient client = GetClient(); | ||
|
|
||
| string wifiComponentName = "wifiAccessPoint"; | ||
| string wifiModelId = await GetUniqueModelIdAsync(client, TestAssetSettings.WifiModelIdPrefix).ConfigureAwait(false); | ||
| string roomWithWifiModelId = await GetUniqueModelIdAsync(client, TestAssetSettings.RoomWithWifiModelIdPrefix).ConfigureAwait(false); | ||
| string roomWithWifiTwinId = await GetUniqueTwinIdAsync(client, TestAssetSettings.RoomWithWifiTwinIdPrefix).ConfigureAwait(false); | ||
| string eventRouteId = $"someEventRouteId-{GetRandom()}"; | ||
|
|
||
| try | ||
| { | ||
| // Create an event route for the digital twins client. | ||
| EventRoute eventRoute = await CreateEventRoute(client, eventRouteId).ConfigureAwait(false); | ||
|
|
||
| // Create the models needed for the digital twin. | ||
| await CreateModelsAndTwins(client, wifiModelId, roomWithWifiModelId, wifiComponentName, roomWithWifiTwinId).ConfigureAwait(false); | ||
|
|
||
| // Act - Test publishing telemetry to a digital twin. | ||
| var telemetryOptions = new TelemetryOptions() | ||
| { | ||
| MessageId = Recording.Random.NewGuid().ToString(), | ||
| TimeStamp = default | ||
| }; | ||
| Response publishTelemetryResponse = await client.PublishTelemetryAsync(roomWithWifiTwinId, "{\"Telemetry1\": 5}", telemetryOptions).ConfigureAwait(false); | ||
|
|
||
| // Assert | ||
| publishTelemetryResponse.Status.Should().Be((int)HttpStatusCode.NoContent); | ||
|
|
||
| // Act - Test publishing telemetry to a component in a digital twin. | ||
| var componentTelemetryOptions = new TelemetryOptions() | ||
| { | ||
| MessageId = Recording.Random.NewGuid().ToString(), | ||
| TimeStamp = default | ||
| }; | ||
| var telemetryPayload = new Dictionary<string, int> | ||
| { | ||
| { "ComponentTelemetry1", 9} | ||
| }; | ||
| Response publishComponentTelemetryResponse = await client | ||
| .PublishComponentTelemetryAsync(roomWithWifiTwinId, wifiComponentName, JsonSerializer.Serialize(telemetryPayload), componentTelemetryOptions) | ||
| .ConfigureAwait(false); | ||
|
|
||
| // Assert | ||
| publishComponentTelemetryResponse.Status.Should().Be((int)HttpStatusCode.NoContent); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Assert.Fail($"Failure in executing a step in the test case: {ex.Message}."); | ||
| } | ||
| finally | ||
| { | ||
| // clean up | ||
| try | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(eventRouteId)) | ||
| { | ||
| await client.DeleteEventRouteAsync(eventRouteId).ConfigureAwait(false); | ||
| } | ||
| if (!string.IsNullOrWhiteSpace(roomWithWifiTwinId)) | ||
| { | ||
| await client.DeleteDigitalTwinAsync(roomWithWifiTwinId).ConfigureAwait(false); | ||
| } | ||
| if (!string.IsNullOrWhiteSpace(roomWithWifiModelId)) | ||
| { | ||
| await client.DeleteModelAsync(roomWithWifiModelId).ConfigureAwait(false); | ||
| } | ||
| if (!string.IsNullOrWhiteSpace(wifiModelId)) | ||
| { | ||
| await client.DeleteModelAsync(wifiModelId).ConfigureAwait(false); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Assert.Fail($"Test clean up failed: {ex.Message}"); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private async Task CreateModelsAndTwins(DigitalTwinsClient client, string wifiModelId, string roomWithWifiModelId, string wifiComponentName, string roomWithWifiTwinId) | ||
| { | ||
| // Generate the payload needed to create the wifi component model. | ||
| string wifiModel = TestAssetsHelper.GetWifiModelPayload(wifiModelId); | ||
|
|
||
| // Generate the payload needed to create the room with wifi model. | ||
| string roomWithWifiModel = TestAssetsHelper.GetRoomWithWifiModelPayload(roomWithWifiModelId, wifiModelId, wifiComponentName); | ||
|
|
||
| // Create the room and wifi models. | ||
| await client.CreateModelsAsync(new List<string> { roomWithWifiModel, wifiModel }).ConfigureAwait(false); | ||
|
|
||
| // Generate the payload needed to create the room with wifi twin. | ||
| string roomWithWifiTwin = TestAssetsHelper.GetRoomWithWifiTwinPayload(roomWithWifiModelId, wifiModelId, wifiComponentName); | ||
|
|
||
| // Create the room with wifi component digital twin. | ||
| await client.CreateDigitalTwinAsync(roomWithWifiTwinId, roomWithWifiTwin).ConfigureAwait(false); | ||
| } | ||
|
|
||
| private async Task<EventRoute> CreateEventRoute(DigitalTwinsClient client, string eventRouteId) | ||
| { | ||
| string filter = "type = 'Microsoft.DigitalTwins.Twin.Create' OR type = 'microsoft.iot.telemetry'"; | ||
| var eventRoute = new EventRoute(EndpointName) | ||
| { | ||
| Filter = filter | ||
| }; | ||
|
|
||
| // Create an event route. | ||
| Response createEventRouteResponse = await client.CreateEventRouteAsync(eventRouteId, eventRoute).ConfigureAwait(false); | ||
| createEventRouteResponse.Status.Should().Be((int)HttpStatusCode.NoContent); | ||
|
|
||
| return eventRoute; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.