Пример #1
0
        public void Accept_ShouldCreateAudit()
        {
            // Arrrange
            var dummyAudit = new ServiceModel.Audit();

            var mockLogger = Mock.Of <ILoggerAdapter <AuditServiceFireForget> >(MockBehavior.Strict);

            Mock.Get(mockLogger)
            .Setup(l => l.LogError(string.Empty))
            .Verifiable();
            var mockFireForgetEventHandler = Mock.Of <IFireForgetEventHandler>(MockBehavior.Strict);

            Mock.Get(mockFireForgetEventHandler)
            .Setup(m => m.Execute(It.IsAny <Func <IAuditService, Task> >()))
            .Verifiable();

            // Act
            var auditService = new AuditServiceFireForget(mockFireForgetEventHandler, mockLogger);

            auditService.Accept(dummyAudit);

            //Assert
            Mock.Get(mockFireForgetEventHandler)
            .Verify(m => m.Execute(It.IsAny <Func <IAuditService, Task> >()), Times.Once);

            Mock.Get(mockLogger).Verify(m => m.LogError(string.Empty), Times.Never);
        }
        /// <summary>
        /// Create async helper.
        /// </summary>
        /// <param name="expectedStatusCode">Status code to mock test.</param>
        private async Task EndpointHelper(ServiceModels.Audit audit, int expectedStatusCode)
        {
            // Arrange
            // Act
            var result = await CreatePostAsync("/api/audit", GetStringContent(audit));

            // Assert
            result.Should().BeOfType <HttpResponseMessage>().Which.StatusCode.Should().Be(expectedStatusCode);
        }
Пример #3
0
 /// <summary>
 /// Set service Audit model.
 /// </summary>
 private void SetServiceAuditHelper(string textMessage)
 {
     _serviceAudit = new ServiceModel.Audit()
     {
         Action   = 0,
         Message  = textMessage,
         Severity = 1,
         Ukprn    = 12345,
         User     = "******"
     };
 }
Пример #4
0
        public void CreateAsync_WhenArgumentNull_ThrowsArgumentNullException()
        {
            // Arrange
            ServiceModel.Audit smAudit = null;
            IAuditService      sut     = GetAuditServiceHelper();

            // Act
            Func <Task> func = () => sut.CreateAsync(smAudit);

            // Assert
            func.Should().Throw <ArgumentNullException>();
        }
        /// <summary>
        /// Create new Audit repository model and pass on to the repository.
        /// </summary>
        /// <param name="request">Audit entity.</param>
        /// <returns>Returns void.</returns>
        public async Task CreateAsync(ServiceModel.Audit request)
        {
            It.IsNull(request)
            .AsGuard <ArgumentNullException>();

            var dmAudit = _mapper.Map <ServiceModel.Audit, DataModel.Audit>(request);

            dmAudit.CreatedAt = DateTime.UtcNow;
            dmAudit.User      = dmAudit.User ?? "AuditUser";
            await _unitOfWork.AuditRepository.AddAsync(dmAudit);

            await _unitOfWork.CommitAsync();
        }
Пример #6
0
 /// <summary>
 /// Accept a fire and forget audit message.
 /// </summary>
 /// <param name="message">Audit.</param>
 public void Accept(ServiceModel.Audit message)
 {
     _fireForgetEventHandler.Execute(async a =>
     {
         try
         {
             await a.CreateAsync(message);
         }
         catch (Exception ex)
         {
             _logger.LogError(ex, $"Unexpected error during fire and forget audit creation of audit message : {message.Message}.");
         }
     });
 }
        public async Task Execute_ShouldCreateAuditAsync()
        {
            // Arrrange
            var dummyAudit          = new ServiceModel.Audit();
            var mockAuditService    = Mock.Of <IAuditService>(MockBehavior.Strict);
            var serviceProvider     = Mock.Of <IServiceProvider>(MockBehavior.Strict);
            var serviceScope        = Mock.Of <IServiceScope>(MockBehavior.Strict);
            var serviceScopeFactory = Mock.Of <IServiceScopeFactory>(MockBehavior.Strict);

            Mock.Get(mockAuditService)
            .Setup(x => x.CreateAsync(dummyAudit))
            .Returns(Task.CompletedTask);

            Mock.Get(serviceScope).SetupGet(x => x.ServiceProvider).Returns(serviceProvider);

            Mock.Get(serviceScopeFactory)
            .Setup(x => x.CreateScope())
            .Returns(serviceScope);

            Mock.Get(serviceProvider)
            .Setup(x => x.GetService(typeof(IAuditService)))
            .Returns(mockAuditService);

            Mock.Get(serviceProvider)
            .Setup(x => x.GetService(typeof(IServiceScopeFactory)))
            .Returns(serviceScopeFactory);

            // Act
            var fireForgetEventHandler = new FireForgetEventHandler(serviceScopeFactory);

            fireForgetEventHandler.Execute(async auditService => await auditService.CreateAsync(dummyAudit));

            // We execute run in a separate thread then the current one so we need to delay to assert.
            await Task.Delay(100);

            //Assert
            Mock.Get(serviceScopeFactory).Verify(x => x.CreateScope(), Times.Once);
            Mock.Get(serviceScope).VerifyGet(x => x.ServiceProvider, Times.Once);
            Mock.Get(serviceProvider).Verify(x => x.GetService(typeof(IAuditService)), Times.Once);
            Mock.Get(mockAuditService).Verify(x => x.CreateAsync(dummyAudit), Times.Once);
        }
Пример #8
0
        public async Task CreateAsync_ShouldCreateAudit()
        {
            // Arrrange
            _auditRepositoryMock.Setup(m => m.AddAsync(_dmAudit)).Returns(Task.CompletedTask);
            _unitOfWorkMock.Setup(m => m.AuditRepository).Returns(_auditRepositoryMock.Object);
            _unitOfWorkMock.Setup(m => m.CommitAsync()).Returns(Task.CompletedTask);
            var smAudit = new ServiceModel.Audit()
            {
                Action = 5, Message = "Test", Severity = 1, Ukprn = 12345
            };
            IAuditService sut = GetAuditServiceHelper();

            // Act
            await sut.CreateAsync(smAudit);

            //Assert
            _auditRepositoryMock.Verify();
            _auditRepositoryMock.Verify(x => x.AddAsync(_dmAudit), Times.Once);
            _unitOfWorkMock.Verify(x => x.CommitAsync(), Times.Once);
            Mock.VerifyAll(_mockMapper);
        }
Пример #9
0
        public async Task <IActionResult> CreateAsync(ServiceModels.Audit request)
        {
            await _auditService.CreateAsync(request);

            return(StatusCode(StatusCodes.Status201Created));
        }
Пример #10
0
 public IActionResult Accept(ServiceModels.Audit request)
 {
     _auditServiceFireForget.Accept(request);
     return(StatusCode(StatusCodes.Status202Accepted));
 }