public async void Update_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IEventStatusService, IEventStatusRepository>(); var model = new ApiEventStatusServerRequestModel(); var validatorMock = new Mock <IApiEventStatusServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiEventStatusServerRequestModel>())).Returns(Task.FromResult(new ValidationResult(new List <ValidationFailure>() { new ValidationFailure("text", "test") }))); mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult(new EventStatus())); var service = new EventStatusService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALEventStatusMapperMock, mock.DALMapperMockFactory.DALEventMapperMock); UpdateResponse <ApiEventStatusServerResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiEventStatusServerRequestModel>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <EventStatusUpdatedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async void TestDelete() { var builder = new WebHostBuilder() .UseEnvironment("Production") .UseStartup <TestStartup>(); TestServer testServer = new TestServer(builder); var client = new ApiClient(testServer.CreateClient()); client.SetBearerToken(JWTTestHelper.GenerateBearerToken()); ApplicationDbContext context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext; IEventStatusService service = testServer.Host.Services.GetService(typeof(IEventStatusService)) as IEventStatusService; var model = new ApiEventStatusServerRequestModel(); model.SetProperties("B"); CreateResponse <ApiEventStatusServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.EventStatusDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiEventStatusServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public void MapServerResponseToRequest() { var mapper = new ApiEventStatusServerModelMapper(); var model = new ApiEventStatusServerResponseModel(); model.SetProperties(1, "A"); ApiEventStatusServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Name.Should().Be("A"); }
public virtual async Task <IActionResult> Create([FromBody] ApiEventStatusServerRequestModel model) { CreateResponse <ApiEventStatusServerResponseModel> result = await this.EventStatusService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/EventStatus/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void CreatePatch() { var mapper = new ApiEventStatusServerModelMapper(); var model = new ApiEventStatusServerRequestModel(); model.SetProperties("A"); JsonPatchDocument <ApiEventStatusServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiEventStatusServerRequestModel(); patch.ApplyTo(response); response.Name.Should().Be("A"); }
private async Task <ApiEventStatusServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiEventStatusServerRequestModel> patch) { var record = await this.EventStatusService.Get(id); if (record == null) { return(null); } else { ApiEventStatusServerRequestModel request = this.EventStatusModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IEventStatusService, IEventStatusRepository>(); var model = new ApiEventStatusServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new EventStatusService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.EventStatusModelValidatorMock.Object, mock.DALMapperMockFactory.DALEventStatusMapperMock, mock.DALMapperMockFactory.DALEventMapperMock); ActionResponse response = await service.Delete(default(int)); response.Should().NotBeNull(); response.Success.Should().BeTrue(); mock.RepositoryMock.Verify(x => x.Delete(It.IsAny <int>())); mock.ModelValidatorMockFactory.EventStatusModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <EventStatusDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiEventStatusServerRequestModel model) { ApiEventStatusServerRequestModel request = await this.PatchModel(id, this.EventStatusModelMapper.CreatePatch(model)) as ApiEventStatusServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiEventStatusServerResponseModel> result = await this.EventStatusService.Update(id, request); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiEventStatusServerRequestModel> patch) { ApiEventStatusServerResponseModel record = await this.EventStatusService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiEventStatusServerRequestModel model = await this.PatchModel(id, patch) as ApiEventStatusServerRequestModel; UpdateResponse <ApiEventStatusServerResponseModel> result = await this.EventStatusService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }