Example #1
0
        public void Handle_doesnt_compare_data_if_input_not_found()
        {
            // Arrange
            var newInputEvent = new NewInputIntegrationEvent();

            var eventBus = new Mock <IRabbitMQEventBus>();
            var logic    = new Mock <IDiffLogic>();

            var repository = new Mock <IInputRepository>();

            repository
            .Setup(s => s.FindAsync(It.IsAny <string>()))
            .ReturnsAsync(() => null);

            var handler = GetHandler(eventBus: eventBus, repository: repository);

            // Act
            handler.Handle(newInputEvent);

            // Assert
            logic.Verify(
                df => df.CompareData(It.IsAny <InputData>(), It.IsAny <InputData>()),
                Times.Never
                );

            eventBus.Verify(
                eb => eb.Publish(It.IsAny <NewResultIntegrationEvent>()),
                Times.Never
                );
        }
Example #2
0
        public void Handle_logs_exception_before_throwing_it()
        {
            // Arrange
            var newEvent     = new NewInputIntegrationEvent();
            var anyException = new Exception($"Failed to process event {newEvent.Id}");

            var failingRepo = new Mock <IInputRepository>();

            failingRepo
            .Setup(s => s.FindAsync(It.IsAny <string>()))
            .Throws(anyException);

            var logger = new Mock <ILogger <NewInputIntegrationEventHandler> >();

            var handler = GetHandler(repository: failingRepo, logger: logger);

            // Act
            Assert.ThrowsAsync <Exception>(() => handler.Handle(newEvent));

            // Assert
            logger.Verify(l =>
                          l.Log(
                              LogLevel.Error,
                              0,
                              It.Is <FormattedLogValues>(v => v.ToString() == anyException.Message),
                              It.Is <Exception>(e => e == anyException),
                              It.IsAny <Func <object, Exception, string> >())
                          );
        }
Example #3
0
        public void Handle_publishes_new_result_event()
        {
            // Arrange
            var newInputEvent = new NewInputIntegrationEvent();
            var anyResult     = new DiffResult(ResultType.Different, null);

            var eventBus = new Mock <IRabbitMQEventBus>();

            var logic = new Mock <IDiffLogic>();

            logic.Setup(s => s.CompareData(It.IsAny <InputData>(), It.IsAny <InputData>()))
            .Returns(anyResult);

            var repository = new Mock <IInputRepository>();

            repository
            .Setup(s => s.FindAsync(It.IsAny <string>()))
            .ReturnsAsync(() => Mock.Of <InputData>());

            var handler = GetHandler(
                eventBus: eventBus,
                repository: repository,
                logic: logic.Object
                );

            // Act
            handler.Handle(newInputEvent);

            // Assert
            eventBus.Verify(
                eb => eb.Publish(It.IsAny <NewResultIntegrationEvent>()),
                Times.Once
                );
        }
Example #4
0
        public void Handle_should_return_expected_differences_for_different_inputs(
            InputData input,
            InputData opposingInput,
            DiffResult expectedResult
            )
        {
            // Arrange
            var resultEvent   = new NewResultIntegrationEvent(input, string.Empty, expectedResult);
            var newInputEvent = new NewInputIntegrationEvent()
            {
                Position = input.Position
            };

            var eventBus = new Mock <IRabbitMQEventBus>();

            var repository = new Mock <IInputRepository>();

            repository
            .Setup(s => s.FindAsync(It.IsAny <string>()))
            .ReturnsAsync(() => input);

            repository
            .Setup(s => s.GetLastInputBeforeAsync(It.IsAny <Guid>(), It.IsAny <InputPosition>(), It.IsAny <DateTime>()))
            .ReturnsAsync(() => opposingInput);

            var handler = GetHandler(
                eventBus: eventBus,
                repository: repository,
                logic: new DiffLogic()
                );

            // Act
            handler.Handle(newInputEvent);

            // Assert
            eventBus.Verify(
                eb => eb.Publish(
                    It.Is <NewResultIntegrationEvent>(e =>
                                                      e.Result == ResultType.Different &&
                                                      e.Differences.Count == expectedResult.Differences.Count &&
                                                      e.Differences.All(_ => expectedResult.Differences.Contains(_))
                                                      )
                    )
                );
        }
Example #5
0
        public void Handle_consider_input_larger_if_no_opposing_input_found(InputPosition position, ResultType expectedResultType)
        {
            // Arrange
            var input          = new InputData(position, "test");
            var expectedResult = new DiffResult(expectedResultType, null);
            var resultEvent    = new NewResultIntegrationEvent(input, string.Empty, expectedResult);
            var newInputEvent  = new NewInputIntegrationEvent()
            {
                Position = input.Position
            };

            var eventBus = new Mock <IRabbitMQEventBus>();

            var logic = new Mock <IDiffLogic>();

            logic.Setup(s => s.CompareData(It.IsAny <InputData>(), It.IsAny <InputData>()))
            .Returns(expectedResult);

            var repository = new Mock <IInputRepository>();

            repository
            .Setup(s => s.FindAsync(It.IsAny <string>()))
            .ReturnsAsync(() => input);

            repository
            .Setup(s => s.GetLastInputBeforeAsync(It.IsAny <Guid>(), It.IsAny <InputPosition>(), It.IsAny <DateTime>()))
            .ReturnsAsync(() => null);

            var handler = GetHandler(
                eventBus: eventBus,
                repository: repository,
                logic: logic.Object
                );

            // Act
            handler.Handle(newInputEvent);

            // Assert
            eventBus.Verify(
                eb => eb.Publish(
                    It.Is <NewResultIntegrationEvent>(e => e.Result == expectedResult.Result)
                    )
                );
        }
Example #6
0
        private async Task <IActionResult> HandlePostInputRequest(Guid diffId, NewInputViewModel input, InputPosition position)
        {
            if (!ModelState.IsValid)
            {
                _logger.LogWarning($"Post{Enum.GetName(typeof(InputPosition), position)}({diffId}) failed validation");

                return(BadRequest(ModelState));
            }

            _logger.LogInformation($"Find({diffId}): retrieving item on repository");

            var diff = await _diffRepository.FindAsync(_ => _.UUID == diffId);

            if (diff == null)
            {
                _logger.LogWarning($"Find({diffId}): item not found on repository");

                return(NotFound(new ResourceNotFoundForIdResultMessage <Diff>(diffId)));
            }

            var newInput = new InputData(diffId, position, input.Data);

            _logger.LogInformation($"Save({diffId}, Diff obj): saving item on repository");

            await _inputRepository.AddOneAsync(newInput);

            _logger.LogInformation($"Save({diffId}, Diff obj): saving item on repository");

            var eventMessage = new NewInputIntegrationEvent(newInput);

            _logger.LogInformation($"Input registered, sending integration event ({eventMessage.Id})");

            _eventBus.Publish(eventMessage);

            _logger.LogInformation($"Integration event ({eventMessage.Id}) sent!");

            var viewModel = _mapper.Map <InputViewModel>(newInput);

            return(Created(newInput.Id, viewModel));
        }