public async Task When_Version_Is_Not_Supported_Should_Return_BadResult(string version)
        {
            var ctx = new DefaultHttpContext();

            ctx.Request.QueryString = new QueryString($"?devEUI=193123&{ApiVersion.QueryStringParamName}={version}");
            var registryManager      = new Mock <RegistryManager>(MockBehavior.Strict);
            var searchDeviceByDevEUI = new SearchDeviceByDevEUI(registryManager.Object);
            var result = await searchDeviceByDevEUI.GetDeviceByDevEUI(ctx.Request, NullLogger.Instance);

            Assert.IsType <BadRequestObjectResult>(result);
        }
        public async Task When_Query_String_Is_Not_Found_Should_Return_BadResult()
        {
            var ctx = new DefaultHttpContext();

            ctx.Request.QueryString = new QueryString($"?{ApiVersion.QueryStringParamName}={ApiVersion.Version_2019_02_12_Preview.Version}");
            var registryManager      = new Mock <RegistryManager>(MockBehavior.Strict);
            var searchDeviceByDevEUI = new SearchDeviceByDevEUI(registryManager.Object);
            var result = await searchDeviceByDevEUI.GetDeviceByDevEUI(ctx.Request, NullLogger.Instance);

            Assert.IsType <BadRequestObjectResult>(result);
        }
        public async Task When_Version_Is_Missing_Should_Return_BadResult()
        {
            var ctx = new DefaultHttpContext();

            ctx.Request.QueryString = new QueryString($"?devEUI=193123");
            var registryManager = new Mock <RegistryManager>(MockBehavior.Strict);
            SearchDeviceByDevEUI searchDeviceByDevEUI = new SearchDeviceByDevEUI(registryManager.Object);
            var result = await searchDeviceByDevEUI.GetDeviceByDevEUI(ctx.Request, NullLogger.Instance, new ExecutionContext());

            Assert.IsType <BadRequestObjectResult>(result);
        }
        public async Task When_Device_Is_Found_Should_Returns_Device_Information()
        {
            // arrange
            var devEui     = new DevEui(13213123212131);
            var primaryKey = Convert.ToBase64String(Encoding.UTF8.GetBytes(PrimaryKey));

            var(registryManager, request) = SetupIotHubQuery(devEui.ToString(), PrimaryKey);
            var searchDeviceByDevEUI = new SearchDeviceByDevEUI(registryManager.Object);

            // act
            var result = await searchDeviceByDevEUI.GetDeviceByDevEUI(request, NullLogger.Instance);

            // assert
            var okObjectResult = Assert.IsType <OkObjectResult>(result);

            Assert.Equal(JsonConvert.SerializeObject(new { DevEUI = devEui.ToString(), PrimaryKey = primaryKey }), JsonConvert.SerializeObject(okObjectResult.Value));
            registryManager.VerifyAll();
        }
        public async Task Complies_With_Contract()
        {
            // arrange
            var devEui     = SearchByDevEuiContract.Eui;
            var primaryKey = SearchByDevEuiContract.PrimaryKey;

            var(registryManager, request) = SetupIotHubQuery(devEui, primaryKey);
            var searchDeviceByDevEUI = new SearchDeviceByDevEUI(registryManager.Object);

            // act
            var result = await searchDeviceByDevEUI.GetDeviceByDevEUI(request, NullLogger.Instance);

            // assert
            var okObjectResult = Assert.IsType <OkObjectResult>(result);

            Assert.Equal(SearchByDevEuiContract.Response, JsonConvert.SerializeObject(okObjectResult.Value));
            registryManager.VerifyAll();
        }
        public async Task When_Device_Is_Not_Found_Should_Returns_NotFound(string format)
        {
            var devEUI = new DevEui(13213123212131);
            var ctx    = new DefaultHttpContext();

            ctx.Request.QueryString = new QueryString($"?devEUI={devEUI.ToString(format, null)}&{ApiVersion.QueryStringParamName}={ApiVersion.LatestVersion}");

            var registryManager = new Mock <RegistryManager>(MockBehavior.Strict);

            registryManager.Setup(x => x.GetDeviceAsync(devEUI.ToString()))
            .ReturnsAsync((Device)null);

            var searchDeviceByDevEUI = new SearchDeviceByDevEUI(registryManager.Object);

            var result = await searchDeviceByDevEUI.GetDeviceByDevEUI(ctx.Request, NullLogger.Instance);

            Assert.IsType <NotFoundResult>(result);

            registryManager.VerifyAll();
        }
        public async Task When_Device_Is_Found_Should_Returns_Device_Information()
        {
            const string devEUI     = "13213123212131";
            var          primaryKey = Convert.ToBase64String(Encoding.UTF8.GetBytes(PrimaryKey));
            var          ctx        = new DefaultHttpContext();

            ctx.Request.QueryString = new QueryString($"?devEUI={devEUI}&{ApiVersion.QueryStringParamName}={ApiVersion.LatestVersion}");

            var registryManager = new Mock <RegistryManager>(MockBehavior.Strict);
            var deviceInfo      = new Device(devEUI)
            {
                Authentication = new AuthenticationMechanism()
                {
                    SymmetricKey = new SymmetricKey()
                    {
                        PrimaryKey = primaryKey
                    }
                },
            };

            registryManager.Setup(x => x.GetDeviceAsync(devEUI))
            .ReturnsAsync(deviceInfo);

            SearchDeviceByDevEUI searchDeviceByDevEUI = new SearchDeviceByDevEUI(registryManager.Object);

            var result = await searchDeviceByDevEUI.GetDeviceByDevEUI(ctx.Request, NullLogger.Instance, new ExecutionContext());

            Assert.IsType <OkObjectResult>(result);
            Assert.IsType <List <IoTHubDeviceInfo> >(((OkObjectResult)result).Value);
            var devices = (List <IoTHubDeviceInfo>)((OkObjectResult)result).Value;

            Assert.Single(devices);
            Assert.Equal(devEUI, devices[0].DevEUI);
            Assert.Equal(primaryKey, devices[0].PrimaryKey);

            registryManager.VerifyAll();
        }