Skip to content
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

test(PublishTelemetry): Add E2E test for Publish Telemetry API #12479

Merged
merged 7 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Net;
using System.Threading.Tasks;
using FluentAssertions;
using Azure.DigitalTwins.Core.Models;
using System;
using FluentAssertions;
using NUnit.Framework;
using Azure.Core.TestFramework;

namespace Azure.DigitalTwins.Core.Tests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// 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);

// Create the models needed for the digital twin.
await CreateModelsAndTwins(client, wifiModelId, roomWithWifiModelId, wifiComponentName, roomWithWifiTwinId);
barustum marked this conversation as resolved.
Show resolved Hide resolved

// Act - Test publishing telemetry to a digital twin.
Response publishTelemetryResponse = await client.PublishTelemetryAsync(roomWithWifiTwinId, "{\"Telemetry1\": 5}");

// Assert
publishTelemetryResponse.Status.Should().Be((int)HttpStatusCode.NoContent);
barustum marked this conversation as resolved.
Show resolved Hide resolved

// Act - Test publishing telemetry to a component in a digital twin.
var telemetryPayload = new Dictionary<string, int>
{
{ "ComponentTelemetry1", 9}
};
Response publishComponentTelemetryResponse = await client.PublishComponentTelemetryAsync(roomWithWifiTwinId, wifiComponentName, JsonSerializer.Serialize(telemetryPayload));

// 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);
}

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;
}
}
}