public async Task Update_UpdatingFailed_AddLogAndReturnServiceErrorResult()
        {
            // Arrange
            var request = new UpdateSampleRequest {
                Id = 1
            };
            var model = new Sample {
                Id = 1, CreatedDate = It.IsAny <DateTime>()
            };
            var exception     = It.IsAny <Exception>();
            var serviceResult = ServiceResult.Error(Messages.UpdatingFailed);

            repository.Setup(x => x.GetAsync(s => s.Id == request.Id)).ReturnsAsync(model);
            sampleFactory.Setup(x => x.CreateUpdateSample(model, request)).Returns(model);
            repository.Setup(x => x.Update(model)).Throws(exception);

            // Act
            var result = await service.Update(request);

            // Assert
            result.IsSuccess.Should().BeFalse();
            result.Message.Should().Be(serviceResult.Message);
            unitOfWork.Verify(x => x.Save(), Times.Never);
            AssertHelpers.VerifyLogger(logger, LogLevel.Error, Times.Once());
        }
Ejemplo n.º 2
0
        public Sample CreateUpdateSample(Sample sample, UpdateSampleRequest request)
        {
            sample.Title        = request.Title;
            sample.ModifiedBy   = "user-2";
            sample.ModifiedDate = DateTime.Now.ToLocalTime();

            return(sample);
        }
Ejemplo n.º 3
0
        public ServiceResult Update(UpdateSampleRequest request)
        {
            var currentSample = repository.FindById(request.Id);
            var sample        = factory.CreateUpdateSample(currentSample, request);
            var result        = repository.ReplaceOne(sample);

            return(!result
                ? ServiceResult.Error(Messages.UpdatingFailed)
                : ServiceResult.Success(Messages.UpdatingSuccess));
        }
        public async Task <IActionResult> Put([FromBody] UpdateSampleRequest request)
        {
            var validationResult = RequestValidator.Validate(request);

            if (!validationResult.IsValid)
            {
                return(BadRequest(Messages.InvalidRequest));
            }

            var result = await sampleService.Update(request);

            return(Ok(result));
        }
Ejemplo n.º 5
0
      public IActionResult Put(UpdateSampleRequest request)
      {
          var validationResult = requestValidator.Validate(request);

          if (!validationResult.IsValid)
          {
              return(BadRequest(Messages.InvalidRequest));
          }

          var result = sampleService.Update(request);

          return(!result.IsSuccess
                 ? Problem(Messages.GeneralError)
                 : Ok(result.Message));
      }
Ejemplo n.º 6
0
        public async Task Put_InvalidRequest_ReturnBadRequest()
        {
            // Arrange
            var request          = new UpdateSampleRequest();
            var validationResult = ValidationResult.Error("invalid request");
            var apiResult        = new BadRequestObjectResult(Messages.InvalidRequest);

            requestValidator.Setup(x => x.Validate(request)).Returns(validationResult);

            // Act
            var result = await controller.Put(request);

            // Assert
            result.Should().BeEquivalentTo(apiResult);
        }
Ejemplo n.º 7
0
        public async Task Put_AddingSucceeds_ReturnSuccessResult()
        {
            // Arrange
            var request          = new UpdateSampleRequest();
            var validationResult = ValidationResult.Success;
            var serviceResult    = ServiceResult.Success();
            var apiResult        = new OkObjectResult(serviceResult);

            requestValidator.Setup(x => x.Validate(request)).Returns(validationResult);
            sampleService.Setup(x => x.Update(request)).ReturnsAsync(serviceResult);

            // Act
            var result = await controller.Put(request);

            // Assert
            result.Should().BeEquivalentTo(apiResult);
        }
Ejemplo n.º 8
0
        public async Task <ServiceResult> Update(UpdateSampleRequest request)
        {
            try
            {
                var currentSample = await repository.GetAsync(x => x.Id == request.Id);

                var sample = sampleFactory.CreateUpdateSample(currentSample, request);

                repository.Update(sample);
                UnitOfWork.Save();

                RefreshCachedSampleToCache(sample);
                await RefreshCachedSampleList();

                return(ServiceResult.Success(Messages.UpdatingSuccess));
            }
            catch (Exception ex)
            {
                Logger.LogError(ex.Message);
                return(ServiceResult.Error(Messages.UpdatingFailed));
            }
        }
        public async Task Update_UpdatingSuccess_UpdateEntityAndReturnServiceSuccessResult()
        {
            // Arrange
            var request = new UpdateSampleRequest {
                Id = 1
            };
            var model = new Sample {
                Id = 1, CreatedDate = It.IsAny <DateTime>()
            };
            const bool isUpdated     = true;
            var        serviceResult = ServiceResult.Error(Messages.UpdatingSuccess);

            repository.Setup(x => x.GetAsync(s => s.Id == request.Id)).ReturnsAsync(model);
            sampleFactory.Setup(x => x.CreateUpdateSample(model, request)).Returns(model);
            repository.Setup(x => x.Update(model)).Returns(isUpdated);

            // Act
            var result = await service.Update(request);

            // Assert
            result.IsSuccess.Should().BeTrue();
            result.Message.Should().Be(serviceResult.Message);
            unitOfWork.Verify(x => x.Save(), Times.Once);
        }
Ejemplo n.º 10
0
        public void CreateUpdateSample_NoCondition_ReturnSample()
        {
            // Arrange
            const string userId = "user-2";
            var          sample = new Sample {
                CreatedDate = It.IsAny <DateTime>()
            };
            var request = new UpdateSampleRequest
            {
                Id     = 1,
                Title  = "Sample Title",
                Status = 1
            };

            // Act
            var result = factory.CreateUpdateSample(sample, request);

            // Assert
            result.Title.Should().Be(request.Title);
            result.StatusId.Should().Be(request.Status);
            result.CreatedDate.Should().BeCloseTo(sample.CreatedDate);
            result.ModifiedBy.Should().Be(userId);
            result.ModifiedDate.Should().BeCloseTo(DateTime.Now, 100, "dates should differ at milliseconds level");
        }