Exemplo n.º 1
0
        public void ParseValuteNullTest()
        {
            //Arrange
            CurrencyClient currencyClient = new CurrencyClient();
            //Act
            Action act = () => currencyClient.ParseValute(null);
            //Assert
            Exception exception = Assert.Throws <ArgumentNullException>(act);

            Assert.Equal("Value cannot be null. (Parameter 'xml')", exception.Message);
        }
Exemplo n.º 2
0
        public async Task Execute(Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            DateTime       dateTime;
            CurrencyClient currencyClient = new CurrencyClient();
            var            chatId         = message.Chat.Id;
            var            messageId      = message.MessageId;
            var            match          = new Regex(@"\d{2}.\d{2}.\d{4}").Matches(message.Text).FirstOrDefault();

            if (match != null && DateTime.TryParse(match.Value, out dateTime))
            {
                var xml        = currencyClient.GetXML(dateTime.Date.ToString());
                var valuteName = new Regex(@"\b[a-zA-Z]{3}\b").Matches(message.Text).FirstOrDefault();
                if (valuteName == null)
                {
                    valuteName = new Regex(@"\b[a-zA-Z]{1}\d{5}\b").Matches(message.Text).FirstOrDefault();
                    if (valuteName == null)
                    {
                        await _client.SendTextMessageAsync(chatId, "Can't parse valute name", replyToMessageId : messageId);

                        return;
                    }
                }
                currencyClient.ValCurs = currencyClient.ParseValute(xml);
                var rate = "";
                foreach (var item in currencyClient.ValCurs.Valutes)
                {
                    if (item.ID == valuteName.Value.ToUpper() || item.CharCode == valuteName.Value.ToUpper())
                    {
                        rate = item.Value.ToString();
                        break;
                    }
                }
                if (rate == "")
                {
                    await _client.SendTextMessageAsync(chatId, "Valute name or code isn't correct", replyToMessageId : messageId);
                }
                else
                {
                    await _client.SendTextMessageAsync(chatId, rate, replyToMessageId : messageId);
                }
            }
            else
            {
                await _client.SendTextMessageAsync(chatId, "Can't parse date", replyToMessageId : messageId);
            }
        }
Exemplo n.º 3
0
        public void ParseValuteTest()
        {
            //Arrange
            CurrencyClient currencyClient      = new CurrencyClient();
            string         expectedName        = "Foreign Currency Market";
            string         expectedDate        = "31.03.2021";
            var            expectedValutesType = typeof(List <Valute>);
            //Act
            var result = currencyClient.ParseValute(currencyClient.GetXML("31.03.2021"));

            //Assert
            Assert.Equal(expectedDate, result.Date);
            Assert.Equal(expectedName, result.Name);
            Assert.IsType(expectedValutesType, result.Valutes);
        }