public async Task When_Payload_Is_Empty_ExternalDecoder_Should_Be_Called_With_Empty_Payload()
        {
            var devEui       = new DevEui(0x12);
            var fport        = FramePorts.App8;
            var decodedValue = "{\"from\":\"http\"}";

            using var httpMessageHandler = new HttpMessageHandlerMock();
            httpMessageHandler.SetupHandler((r) =>
            {
                var queryDictionary = System.Web.HttpUtility.ParseQueryString(r.RequestUri.Query);
                Assert.Equal("0000000000000012", queryDictionary.Get("devEUI"));
                Assert.Equal(((byte)fport).ToString(CultureInfo.InvariantCulture), queryDictionary.Get("fport"));
                Assert.Empty(queryDictionary.Get("payload"));

                return(new HttpResponseMessage(System.Net.HttpStatusCode.OK)
                {
                    Content = new StringContent(decodedValue, Encoding.UTF8, "application/json"),
                });
            });

            using var httpClient = new HttpClient(httpMessageHandler);
            var target = new LoRaPayloadDecoder(httpClient);
            var result = await target.DecodeMessageAsync(devEui, Array.Empty <byte>(), fport, "http://test/decoder");

            var json = JsonConvert.SerializeObject(result.GetDecodedPayload());

            Assert.Equal(decodedValue, json);
        }
        public async Task When_Payload_Is_Null_ExternalDecoder_Should_Be_Called_With_Empty_Payload()
        {
            var  devEUI             = "12";
            byte fport              = 8;
            var  decodedValue       = "{\"from\":\"http\"}";
            var  httpMessageHandler = new HttpMessageHandlerMock();

            httpMessageHandler.SetupHandler((r) =>
            {
                var queryDictionary = System.Web.HttpUtility.ParseQueryString(r.RequestUri.Query);
                Assert.Equal(devEUI, queryDictionary.Get("devEUI"));
                Assert.Equal(fport.ToString(), queryDictionary.Get("fport"));
                Assert.Empty(queryDictionary.Get("payload"));

                return(new HttpResponseMessage(System.Net.HttpStatusCode.OK)
                {
                    Content = new StringContent(decodedValue, Encoding.UTF8, "application/json"),
                });
            });

            var target = new LoRaPayloadDecoder(new HttpClient(httpMessageHandler));
            var result = await target.DecodeMessageAsync(devEUI, null, fport, "http://test/decoder");

            var json = JsonConvert.SerializeObject(result.GetDecodedPayload());

            Assert.Equal(decodedValue, json);
        }
        public async Task When_Decoder_Is_Undefined_Return_In_Value(string decoder, string payloadString, FramePort fport)
        {
            var payload = Encoding.UTF8.GetBytes(payloadString);

            var target = new LoRaPayloadDecoder(NullLogger <LoRaPayloadDecoder> .Instance);
            var actual = await target.DecodeMessageAsync(new DevEui(0x12), payload, fport, decoder);

            Assert.NotNull(actual.Error);
        }
        public async Task When_Decoder_Is_Undefined_Return_In_Value(string decoder, string payloadString, byte fport)
        {
            var payload = Encoding.UTF8.GetBytes(payloadString);

            var target = new LoRaPayloadDecoder();
            var actual = await target.DecodeMessageAsync("12", payload, fport, decoder);

            Assert.NotNull(actual.Error);
        }
        public async Task When_Decoder_Is_DecoderValueSensor_Return_In_Value(string decoder, string payloadString, byte fport)
        {
            var payload = Encoding.UTF8.GetBytes(payloadString);

            var target = new LoRaPayloadDecoder();
            var actual = await target.DecodeMessageAsync(payload, fport, decoder);

            Assert.Equal(payloadString, actual["value"]);
        }
        public async Task When_Payload_Is_Null_DecoderHexSensor_Should_Return_Empty()
        {
            var target = new LoRaPayloadDecoder(NullLogger <LoRaPayloadDecoder> .Instance);

            var result = await target.DecodeMessageAsync(new DevEui(0x12), null, FramePorts.App1, "DecoderHexSensor");

            var json = JsonConvert.SerializeObject(result.GetDecodedPayload());

            Assert.Equal("{\"value\":\"\"}", json);
        }
        public async Task When_Value_From_String_Is_Passed_Should_Try_To_Validate_As_Number(string value, string expectedJson)
        {
            var target = new LoRaPayloadDecoder(NullLogger <LoRaPayloadDecoder> .Instance);

            var result = await target.DecodeMessageAsync(new DevEui(0x12), Encoding.UTF8.GetBytes(value), FramePorts.App1, "DecoderValueSensor");

            var json = JsonConvert.SerializeObject(result.GetDecodedPayload());

            Assert.Equal(expectedJson, json);
        }
        public async Task When_Payload_Is_Empty_DecoderValueSensor_Should_Return_Empty()
        {
            var target = new LoRaPayloadDecoder();

            var result = await target.DecodeMessageAsync("12", new byte[0], 1, "DecoderValueSensor");

            var json = JsonConvert.SerializeObject(result.GetDecodedPayload());

            Assert.Equal("{\"value\":\"\"}", json);
        }
        public async Task When_Value_From_String_Is_Passed_Should_Try_To_Validate_As_Number(string value, string expectedJson)
        {
            var target = new LoRaPayloadDecoder();

            var result = await target.DecodeMessageAsync("12", Encoding.UTF8.GetBytes(value), 1, "DecoderValueSensor");

            var json = JsonConvert.SerializeObject(result.GetDecodedPayload());

            Assert.Equal(expectedJson, json);
        }
        public async Task When_Decoder_Is_Undefined_Return_In_Value(string decoder, string payloadString, byte fport)
        {
            var payload = Encoding.UTF8.GetBytes(payloadString);

            var target = new LoRaPayloadDecoder();
            var actual = await target.DecodeMessageAsync(payload, fport, decoder);

            Assert.NotNull(actual["error"]);
            Assert.Equal(Convert.ToBase64String(payload), actual["rawpayload"]);
        }
        public async Task When_Decoder_Is_DecoderHexSensor_Return_In_Value(string decoder, string payloadString, object expectedValue, FramePort fport)
        {
            var payload = Encoding.UTF8.GetBytes(payloadString);

            var target = new LoRaPayloadDecoder(NullLogger <LoRaPayloadDecoder> .Instance);
            var actual = await target.DecodeMessageAsync(new DevEui(0x12), payload, fport, decoder);

            Assert.IsType <DecodedPayloadValue>(actual.Value);
            var decodedPayloadValue = (DecodedPayloadValue)actual.Value;

            Assert.Equal(expectedValue, decodedPayloadValue.Value);
        }
        public async Task When_Decoder_Is_DecoderValueSensor_Return_In_Value(string decoder, string payloadString, object expectedValue, byte fport)
        {
            var payload = Encoding.UTF8.GetBytes(payloadString);

            var target = new LoRaPayloadDecoder();
            var actual = await target.DecodeMessageAsync("12", payload, fport, decoder);

            Assert.IsType <DecodedPayloadValue>(actual.Value);
            var decodedPayloadValue = (DecodedPayloadValue)actual.Value;

            Assert.Equal(expectedValue, decodedPayloadValue.Value);
        }