public async Task Handle_Success_ShouldReturnUpdateMarketResponse_SuccessTrue() { //Arrange var selectionCount = new Faker().Random.Int(1, 5); var updateMarketCommand = FakerHelpers.FakeUpdateMarketCommand(selectionCount); var updateMarketEntity = FakerHelpers.FakeMarketUpdateEntity(updateMarketCommand.CorrelationId, selectionCount); var filter = Builders <MarketUpdateEntity> .Filter.Eq(nameof(updateMarketEntity.Id), updateMarketEntity.Id); var replaceOptions = new ReplaceOptions { IsUpsert = true }; _mapperMock.Setup(x => x.Map <MarketUpdateEntity>(updateMarketCommand)).Returns(updateMarketEntity); _repositoryMock.Setup(x => x.GetCollectionContext() .ReplaceOneAsync(filter, updateMarketEntity, replaceOptions, CancellationToken.None)); // Act var response = await _sut.Handle(updateMarketCommand, CancellationToken.None); // Assert Assert.True(response.Success); _repositoryMock.Verify(x => x.GetCollectionContext(), Times.Once); }
public async Task ProduceAsync_Success_ShouldNotThrow_ProducesOneMessageOnly() { //Arrange var selectionCount = new Faker().Random.Int(1, 5); var updateMarketCommand = FakerHelpers.FakeUpdateMarketCommand(selectionCount); var key = updateMarketCommand.MarketId; var kafkaMessage = new Message <int, UpdateMarketCommand> { Headers = new Headers { new Header("CorrelationId", updateMarketCommand.CorrelationId.ToByteArray()) }, Key = key, Timestamp = new Timestamp(DateTimeOffset.UtcNow), Value = updateMarketCommand }; var deliveryResult = new DeliveryResult <int, UpdateMarketCommand>(); _producerMock.Setup(x => x.ProduceAsync("the-topic", kafkaMessage, CancellationToken.None)) .ReturnsAsync(deliveryResult); // Act await _sut.ProduceAsync(key, updateMarketCommand); // Assert _loggerMock.Verify(x => x.Log( LogLevel.Information, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>())); _producerMock.Verify( x => x.ProduceAsync(It.IsAny <string>(), It.IsAny <Message <int, UpdateMarketCommand> >(), CancellationToken.None), Times.Once); }
public async Task Process_Success_ShouldReturnResponseIsSuccessFalse_ShouldNotProduceEvent() { //Arrange var selectionCount = new Faker().Random.Int(1, 5); var updateMarketCommand = FakerHelpers.FakeUpdateMarketCommand(selectionCount); var updateMarketResponse = new UpdateMarketResponse(false); // Act await _sut.Process(updateMarketCommand, updateMarketResponse, CancellationToken.None); // Assert Assert.False(updateMarketResponse.Success); _producerServiceMock.Verify(x => x.ProduceAsync(It.IsAny <int>(), It.IsAny <UpdateMarketSuccessEvent>()), Times.Never); }
public async Task ProduceAsync_Failed_ShouldThrow_ProducesNoMessage() { //Arrange var selectionCount = new Faker().Random.Int(1, 5); var updateMarketCommand = FakerHelpers.FakeUpdateMarketCommand(selectionCount); var key = updateMarketCommand.MarketId; // Act await Assert.ThrowsAsync <NullReferenceException>(() => _sut.ProduceAsync(key, null)); // Assert _loggerMock.Verify(x => x.Log( LogLevel.Error, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>())); _producerMock.Verify( x => x.ProduceAsync(It.IsAny <string>(), It.IsAny <Message <int, UpdateMarketCommand> >(), CancellationToken.None), Times.Never); }
public async Task Process_Success_ShouldReturnResponseIsSuccessTrue_ShouldProduceEvent() { //Arrange var selectionCount = new Faker().Random.Int(1, 5); var updateMarketCommand = FakerHelpers.FakeUpdateMarketCommand(selectionCount); var updateMarketResponse = new UpdateMarketResponse(true); var updateMarketSuccessEvent = FakerHelpers.FakeUpdateMarketSuccessEvent(updateMarketCommand.CorrelationId, selectionCount); _mapperMock.Setup(x => x.Map <UpdateMarketSuccessEvent>(updateMarketCommand)).Returns(updateMarketSuccessEvent); _producerServiceMock.Setup(x => x.ProduceAsync(updateMarketSuccessEvent.MarketId, updateMarketSuccessEvent)) .Returns(Task.CompletedTask); // Act await _sut.Process(updateMarketCommand, updateMarketResponse, CancellationToken.None); // Assert Assert.True(updateMarketResponse.Success); Assert.True(updateMarketCommand.CorrelationId == updateMarketSuccessEvent.CorrelationId); Assert.True(updateMarketCommand.Selections.Count() == updateMarketSuccessEvent.Selections.Count()); Assert.True(updateMarketSuccessEvent.Processed); _producerServiceMock.Verify(x => x.ProduceAsync(It.IsAny <int>(), It.IsAny <UpdateMarketSuccessEvent>()), Times.Once); }