public async Task AddsRegistryEntry()
        {
            Device device = new Device { DeviceId = "1" };

            await _controller.ProvisionDevice(device);

            _registry.Verify(mock => mock.AddOrUpdate(It.IsAny<DeviceInfo>()));
        }
        public async Task NoDeviceIdReturnsBadRequest()
        {
            Device device = new Device { DeviceId = "1" };

            _controller.ModelState.AddModelError("FakeError", "FakeError");

            IHttpActionResult actionResult = await _controller.ProvisionDevice(device);
            Assert.IsType<InvalidModelStateResult>(actionResult);
        }
        public async Task ReturnsToken()
        {
            Device device = new Device { DeviceId = "1" };
            IHttpActionResult actionResult = await _controller.ProvisionDevice(device);

            var contentResult = actionResult as OkNegotiatedContentResult<DeviceEndpoint>;

            Assert.NotNull(contentResult);
            Assert.NotNull(contentResult.Content);
            Assert.Equal(TOKEN, contentResult.Content.AccessToken);
        }
Пример #4
0
        private static async Task ProvisionDeviceAsync(CancellationToken token)
        {
            string baseUrl = ConfigurationHelper.GetConfigValue<string>("WebApiEndpoint");

            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(baseUrl);

            var device = new Device
            {
                DeviceId = Guid.NewGuid().ToString()
            };

            var result = await client.PostAsJsonAsync("api/provision", device, token);

            if (result.IsSuccessStatusCode)
            {
                var endpoint = await result.Content.ReadAsAsync<DeviceEndpoint>(token);

                Console.WriteLine("Endpoint: {0}", endpoint.Uri);
                Console.WriteLine("AccessToken: {0}", endpoint.AccessToken);

                var connectionString = ServiceBusConnectionStringBuilder.CreateUsingSharedAccessSignature(
                                        new Uri(endpoint.Uri),
                                        endpoint.EventHubName,
                                        device.DeviceId,
                                        endpoint.AccessToken
                                  );

                var sender = EventHubSender.CreateFromConnectionString(connectionString);

                sender.Send(new EventData(Encoding.UTF8.GetBytes("Hello Event Hub")));

                Console.WriteLine("Wrote data to event hub");
            }
            else
            {
                var str = await result.Content.ReadAsStringAsync();
                Console.WriteLine(str);
            }
        }