コード例 #1
0
        public async Task Given_A_Valid_Message_If_Banlist_Exists_Should_Invoke_Add_Method_Once()
        {
            // Arrange
            const int expected            = 1;
            var       banlistDataConsumer = new BanlistDataConsumer
            {
                Message = "{\"ArticleId\":642752,\"Title\":\"April 2000 Lists\",\"BanlistType\":\"Ocg\",\"StartDate\":\"2000-04-01T00:00:00\",\"Sections\":[{\"Title\":\"April 2000 Lists\",\"Content\":[]},{\"Title\":\"Full Lists\",\"Content\":[]},{\"Title\":\"Limited\",\"Content\":[\"Change of Heart\",\"Dark Hole\",\"Exodia the Forbidden One\",\"Last Will\",\"Left Arm of the Forbidden One\",\"Left Leg of the Forbidden One\",\"Mirror Force\",\"Pot of Greed\",\"Raigeki\",\"Right Arm of the Forbidden One\",\"Right Leg of the Forbidden One\"]},{\"Title\":\"Semi-Limited\",\"Content\":[\"Graceful Charity\",\"Harpie's Feather Duster\",\"Monster Reborn\"]}]}"
            };

            _banlistService.BanlistExist(Arg.Any <int>()).Returns(false);
            _banlistService.Add(Arg.Any <YugiohBanlist>()).Returns(new Banlist());

            // Act
            await _sut.Handle(banlistDataConsumer, CancellationToken.None);

            // Assert
            await _banlistService.Received(expected).Add(Arg.Any <YugiohBanlist>());
        }
コード例 #2
0
        public async Task <BanlistDataConsumerResult> Handle(BanlistDataConsumer request, CancellationToken cancellationToken)
        {
            var banlistDataConsumerResult = new BanlistDataConsumerResult();

            try
            {
                var yugiohBanlist = JsonConvert.DeserializeObject <YugiohBanlist>(request.Message);

                if (yugiohBanlist != null && yugiohBanlist.Sections.Any())
                {
                    banlistDataConsumerResult.YugiohBanlist = yugiohBanlist;

                    _logger.LogInformation(
                        $"{yugiohBanlist.BanlistType.ToString()}, {yugiohBanlist.Title}, {yugiohBanlist.StartDate}");

                    var banlistExists = await _banlistService.BanlistExist(yugiohBanlist.ArticleId);

                    var result = banlistExists
                        ? await _banlistService.Update(yugiohBanlist)
                        : await _banlistService.Add(yugiohBanlist);

                    banlistDataConsumerResult.BanlistId = result.Id;
                }
                else
                {
                    _logger.LogInformation("Banlist not processed, {@Title}, {@StartDate}", yugiohBanlist.Title, yugiohBanlist.StartDate);
                }
            }
            catch (ArgumentNullException ex)
            {
                _logger.LogError("Argument {@Param} was null. Message: {@Message}: ", ex.ParamName, request.Message);
            }
            catch (NullReferenceException ex)
            {
                _logger.LogError("Null reference exception {@Message}. Exception: {@Exception}: ", request.Message, ex);
            }
            catch (Exception ex)
            {
                _logger.LogError("Unexpected error occured {@Exception}: ", ex);
            }

            return(banlistDataConsumerResult);
        }