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; IEventService service = testServer.Host.Services.GetService(typeof(IEventService)) as IEventService; var model = new ApiEventServerRequestModel(); model.SetProperties("B", "B", 1, DateTime.Parse("1/1/1988 12:00:00 AM"), "B", DateTime.Parse("1/1/1988 12:00:00 AM"), "B", "B", DateTime.Parse("1/1/1988 12:00:00 AM"), "B"); CreateResponse <ApiEventServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.EventDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiEventServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IEventService, IEventRepository>(); var model = new ApiEventServerRequestModel(); var validatorMock = new Mock <IApiEventServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateDeleteAsync(It.IsAny <int>())).Returns(Task.FromResult(new FluentValidation.Results.ValidationResult(new List <ValidationFailure>() { new ValidationFailure("text", "test") }))); var service = new EventService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALEventMapperMock, mock.DALMapperMockFactory.DALEventStudentMapperMock, mock.DALMapperMockFactory.DALEventTeacherMapperMock); ActionResponse response = await service.Delete(default(int)); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <EventDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Create([FromBody] ApiEventServerRequestModel model) { CreateResponse <ApiEventServerResponseModel> result = await this.EventService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Events/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
private async Task <ApiEventServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiEventServerRequestModel> patch) { var record = await this.EventService.Get(id); if (record == null) { return(null); } else { ApiEventServerRequestModel request = this.EventModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void MapServerResponseToRequest() { var mapper = new ApiEventServerModelMapper(); var model = new ApiEventServerResponseModel(); model.SetProperties(1, DateTime.Parse("1/1/1987 12:00:00 AM"), DateTime.Parse("1/1/1987 12:00:00 AM"), 1m, 1, DateTime.Parse("1/1/1987 12:00:00 AM"), DateTime.Parse("1/1/1987 12:00:00 AM"), "A", "A"); ApiEventServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.ActualEndDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.ActualStartDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.BillAmount.Should().Be(1m); response.EventStatusId.Should().Be(1); response.ScheduledEndDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.ScheduledStartDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.StudentNotes.Should().Be("A"); response.TeacherNotes.Should().Be("A"); }
public void CreatePatch() { var mapper = new ApiEventServerModelMapper(); var model = new ApiEventServerRequestModel(); model.SetProperties(DateTime.Parse("1/1/1987 12:00:00 AM"), DateTime.Parse("1/1/1987 12:00:00 AM"), 1m, 1, DateTime.Parse("1/1/1987 12:00:00 AM"), DateTime.Parse("1/1/1987 12:00:00 AM"), "A", "A"); JsonPatchDocument <ApiEventServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiEventServerRequestModel(); patch.ApplyTo(response); response.ActualEndDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.ActualStartDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.BillAmount.Should().Be(1m); response.EventStatusId.Should().Be(1); response.ScheduledEndDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.ScheduledStartDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.StudentNotes.Should().Be("A"); response.TeacherNotes.Should().Be("A"); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IEventService, IEventRepository>(); var model = new ApiEventServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new EventService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.EventModelValidatorMock.Object, 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.EventModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <EventDeletedNotification>(), It.IsAny <CancellationToken>())); }
public void MapServerResponseToRequest() { var mapper = new ApiEventServerModelMapper(); var model = new ApiEventServerResponseModel(); model.SetProperties(1, "A", "A", 1, DateTime.Parse("1/1/1987 12:00:00 AM"), "A", DateTime.Parse("1/1/1987 12:00:00 AM"), "A", "A", DateTime.Parse("1/1/1987 12:00:00 AM"), "A"); ApiEventServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Address1.Should().Be("A"); response.Address2.Should().Be("A"); response.CityId.Should().Be(1); response.Date.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.Description.Should().Be("A"); response.EndDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.Facebook.Should().Be("A"); response.Name.Should().Be("A"); response.StartDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.Website.Should().Be("A"); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiEventServerRequestModel model) { ApiEventServerRequestModel request = await this.PatchModel(id, this.EventModelMapper.CreatePatch(model)) as ApiEventServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiEventServerResponseModel> result = await this.EventService.Update(id, request); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }
public void CreatePatch() { var mapper = new ApiEventServerModelMapper(); var model = new ApiEventServerRequestModel(); model.SetProperties("A", "A", 1, DateTime.Parse("1/1/1987 12:00:00 AM"), "A", DateTime.Parse("1/1/1987 12:00:00 AM"), "A", "A", DateTime.Parse("1/1/1987 12:00:00 AM"), "A"); JsonPatchDocument <ApiEventServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiEventServerRequestModel(); patch.ApplyTo(response); response.Address1.Should().Be("A"); response.Address2.Should().Be("A"); response.CityId.Should().Be(1); response.Date.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.Description.Should().Be("A"); response.EndDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.Facebook.Should().Be("A"); response.Name.Should().Be("A"); response.StartDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.Website.Should().Be("A"); }
public async void Create_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IEventService, IEventRepository>(); var model = new ApiEventServerRequestModel(); mock.RepositoryMock.Setup(x => x.Create(It.IsAny <Event>())).Returns(Task.FromResult(new Event())); var service = new EventService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.EventModelValidatorMock.Object, mock.DALMapperMockFactory.DALEventMapperMock, mock.DALMapperMockFactory.DALEventStudentMapperMock, mock.DALMapperMockFactory.DALEventTeacherMapperMock); CreateResponse <ApiEventServerResponseModel> response = await service.Create(model); response.Should().NotBeNull(); response.Success.Should().BeTrue(); mock.ModelValidatorMockFactory.EventModelValidatorMock.Verify(x => x.ValidateCreateAsync(It.IsAny <ApiEventServerRequestModel>())); mock.RepositoryMock.Verify(x => x.Create(It.IsAny <Event>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <EventCreatedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiEventServerRequestModel> patch) { ApiEventServerResponseModel record = await this.EventService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiEventServerRequestModel model = await this.PatchModel(id, patch) as ApiEventServerRequestModel; UpdateResponse <ApiEventServerResponseModel> result = await this.EventService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }