Ejemplo n.º 1
0
        public async Task EventRoutes_Lifecycle()
        {
            // arrange
            DigitalTwinsClient client = GetClient();

            // Ensure unique eventRouteId
            string eventRouteId = $"someEventRouteId-{GetRandom()}";

            string filter     = "$eventType = 'DigitalTwinTelemetryMessages' or $eventType = 'DigitalTwinLifecycleNotification'";
            var    eventRoute = new DigitalTwinsEventRoute(EndpointName, filter);

            // Test CreateEventRoute
            Response createEventRouteResponse = await client.CreateOrReplaceEventRouteAsync(eventRouteId, eventRoute).ConfigureAwait(false);

            createEventRouteResponse.Status.Should().Be((int)HttpStatusCode.NoContent);

            // Wait certain amount of time to test if the issue is with propagation.
            // TODO: azabbasi: remove this logic once experiment is over.
            await WaitIfLiveAsync(_creationDelay);

            // Test GetEventRoute
            DigitalTwinsEventRoute getEventRouteResult = await client.GetEventRouteAsync(eventRouteId);

            eventRoute.EndpointName.Should().Be(getEventRouteResult.EndpointName);
            eventRoute.Filter.Should().Be(getEventRouteResult.Filter);
            eventRouteId.Should().Be(getEventRouteResult.Id);

            // Test GetEventRoutes
            AsyncPageable <DigitalTwinsEventRoute> eventRouteList = client.GetEventRoutesAsync();
            bool eventRouteFoundInList = false;

            await foreach (DigitalTwinsEventRoute eventRouteListEntry in eventRouteList)
            {
                if (StringComparer.Ordinal.Equals(eventRouteListEntry.Id, eventRouteId))
                {
                    eventRouteFoundInList = true;
                    eventRoute.EndpointName.Should().Be(getEventRouteResult.EndpointName);
                    eventRoute.Filter.Should().Be(getEventRouteResult.Filter);
                    break;
                }
            }

            eventRouteFoundInList.Should().BeTrue("Newly created event route should have been present when listing all event routes");

            // Test DeleteEventRoute
            Response deleteEventRouteResponse = await client.DeleteEventRouteAsync(eventRouteId).ConfigureAwait(false);

            deleteEventRouteResponse.Status.Should().Be((int)HttpStatusCode.NoContent);

            // Verify event route was deleted by trying to get it again

            // act
            Func <Task> act = async() => await client.GetEventRouteAsync(eventRouteId).ConfigureAwait(false);

            // assert
            act.Should().Throw <RequestFailedException>()
            .And.Status.Should().Be((int)HttpStatusCode.NotFound);
        }
        public static async Task CreateEventRouteBasic(DigitalTwinsClient client)
        {
            // ------------------ CREATE EVENT ROUTE (Basic) ---------------------
            // <CreateEventRoute>
            string eventFilter = "$eventType = 'DigitalTwinTelemetryMessages' or $eventType = 'DigitalTwinLifecycleNotification'";
            var    er          = new DigitalTwinsEventRoute("endpointName", eventFilter);
            await client.CreateOrReplaceEventRouteAsync("routeId", er);

            // </CreateEventRoute>
        }
        private async Task <DigitalTwinsEventRoute> CreateEventRoute(DigitalTwinsClient client, string eventRouteId)
        {
            string filter     = "type = 'Microsoft.DigitalTwins.Twin.Create' OR type = 'microsoft.iot.telemetry'";
            var    eventRoute = new DigitalTwinsEventRoute(EndpointName, filter);

            // Create an event route.
            Response createEventRouteResponse = await client.CreateEventRouteAsync(eventRouteId, eventRoute).ConfigureAwait(false);

            createEventRouteResponse.Status.Should().Be((int)HttpStatusCode.NoContent);

            return(eventRoute);
        }
Ejemplo n.º 4
0
        public void EventRoutes_MalformedEventRouteFilter_ThrowsBadRequestException()
        {
            // arrange

            DigitalTwinsClient client = GetClient();

            // Ensure unique eventRouteId and endpointName
            string eventRouteId = $"someEventRouteId-{GetRandom()}";
            string filter       = "this is not a valid filter string";
            var    eventRoute   = new DigitalTwinsEventRoute(EndpointName, filter);

            // Test CreateEventRoute
            Func <Task> act = async() => await client.CreateOrReplaceEventRouteAsync(eventRouteId, eventRoute).ConfigureAwait(false);

            act.Should().Throw <RequestFailedException>()
            .And.Status.Should().Be((int)HttpStatusCode.BadRequest);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates event route for digital Twin
        /// </summary>
        public async Task CreateEventRoute()
        {
            PrintHeader("CREATE EVENT ROUTE");
            try
            {
                #region Snippet:DigitalTwinsSampleCreateEventRoute

                string eventFilter = "$eventType = 'DigitalTwinTelemetryMessages' or $eventType = 'DigitalTwinLifecycleNotification'";
                var    eventRoute  = new DigitalTwinsEventRoute(eventhubEndpointName, eventFilter);

                await client.CreateOrReplaceEventRouteAsync(_eventRouteId, eventRoute);

                Console.WriteLine($"Created event route '{_eventRouteId}'.");

                #endregion Snippet:DigitalTwinsSampleCreateEventRoute
            }
            catch (Exception ex)
            {
                FatalError($"CreateEventRoute: Failed to create event route due to: {ex.Message}");
            }
        }
        public async Task PublishTelemetry_Lifecycle()
        {
            // Setup

            // Create a DigitalTwinsClient instance.
            DigitalTwinsClient client = GetClient();

            string wifiComponentName = "wifiAccessPoint";
            string wifiModelId       = await GetUniqueModelIdAsync(client, TestAssetDefaults.WifiModelIdPrefix).ConfigureAwait(false);

            string roomWithWifiModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.RoomWithWifiModelIdPrefix).ConfigureAwait(false);

            string roomWithWifiTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomWithWifiTwinIdPrefix).ConfigureAwait(false);

            string eventRouteId = $"someEventRouteId-{GetRandom()}";

            try
            {
                // Create an event route for the digital twins client.
                DigitalTwinsEventRoute 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 PublishTelemetryOptions()
                {
                    TimeStamp = default
                };
                Response publishTelemetryResponse = await client.PublishTelemetryAsync(roomWithWifiTwinId, Recording.Random.NewGuid().ToString(), "{\"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 PublishComponentTelemetryOptions()
                {
                    TimeStamp = default
                };
                var telemetryPayload = new Dictionary <string, int>
                {
                    { "ComponentTelemetry1", 9 }
                };
                Response publishComponentTelemetryResponse = await client
                                                             .PublishComponentTelemetryAsync(roomWithWifiTwinId, wifiComponentName, Recording.Random.NewGuid().ToString(), 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}");
                }
            }
        }
        // ------------------ CREATE, LIST, AND DELETE EVENT ROUTE (Full sample) ---------------------
        // <FullEventRouteSample>
        public static async Task CreateEventRouteAsync(DigitalTwinsClient client, string routeId, DigitalTwinsEventRoute er)
        {
            try
            {
                Console.WriteLine("Create a route: testRoute1");

                // Make a filter that passes everything
                er.Filter = "true";
                await client.CreateOrReplaceEventRouteAsync(routeId, er);

                Console.WriteLine("Create route succeeded.");

                // List routes
                AsyncPageable <DigitalTwinsEventRoute> results = client.GetEventRoutesAsync();
                await foreach (DigitalTwinsEventRoute route in results)
                {
                    Console.WriteLine($"Route {route.Id} to endpoint {route.EndpointName} with filter {route.Filter} ");
                }

                // Delete route created earlier
                Console.WriteLine($"Deleting route {routeId}:");
                await client.DeleteEventRouteAsync(routeId);
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine($"*** Error in event route processing ({e.ErrorCode}):\n${e.Message}");
            }
        }