public async void Create_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IOfficerService, IOfficerRepository>(); var model = new ApiOfficerServerRequestModel(); mock.RepositoryMock.Setup(x => x.Create(It.IsAny <Officer>())).Returns(Task.FromResult(new Officer())); var service = new OfficerService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.OfficerModelValidatorMock.Object, mock.DALMapperMockFactory.DALOfficerMapperMock, mock.DALMapperMockFactory.DALNoteMapperMock, mock.DALMapperMockFactory.DALOfficerCapabilitiesMapperMock, mock.DALMapperMockFactory.DALUnitOfficerMapperMock, mock.DALMapperMockFactory.DALVehicleOfficerMapperMock); CreateResponse <ApiOfficerServerResponseModel> response = await service.Create(model); response.Should().NotBeNull(); response.Success.Should().BeTrue(); mock.ModelValidatorMockFactory.OfficerModelValidatorMock.Verify(x => x.ValidateCreateAsync(It.IsAny <ApiOfficerServerRequestModel>())); mock.RepositoryMock.Verify(x => x.Create(It.IsAny <Officer>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <OfficerCreatedNotification>(), It.IsAny <CancellationToken>())); }
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; IOfficerService service = testServer.Host.Services.GetService(typeof(IOfficerService)) as IOfficerService; var model = new ApiOfficerServerRequestModel(); model.SetProperties("B", "B", "B", "B", "B"); CreateResponse <ApiOfficerServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.OfficerDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiOfficerServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IOfficerService, IOfficerRepository>(); var model = new ApiOfficerServerRequestModel(); var validatorMock = new Mock <IApiOfficerServerRequestModelValidator>(); 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 OfficerService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALOfficerMapperMock, mock.DALMapperMockFactory.DALNoteMapperMock, mock.DALMapperMockFactory.DALOfficerCapabilitiesMapperMock, mock.DALMapperMockFactory.DALUnitOfficerMapperMock, mock.DALMapperMockFactory.DALVehicleOfficerMapperMock); 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 <OfficerDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Create([FromBody] ApiOfficerServerRequestModel model) { CreateResponse <ApiOfficerServerResponseModel> result = await this.OfficerService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Officers/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void MapServerResponseToRequest() { var mapper = new ApiOfficerServerModelMapper(); var model = new ApiOfficerServerResponseModel(); model.SetProperties(1, "A", "A", "A", "A", "A"); ApiOfficerServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Badge.Should().Be("A"); response.Email.Should().Be("A"); response.FirstName.Should().Be("A"); response.LastName.Should().Be("A"); response.Password.Should().Be("A"); }
private async Task <ApiOfficerServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiOfficerServerRequestModel> patch) { var record = await this.OfficerService.Get(id); if (record == null) { return(null); } else { ApiOfficerServerRequestModel request = this.OfficerModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void CreatePatch() { var mapper = new ApiOfficerServerModelMapper(); var model = new ApiOfficerServerRequestModel(); model.SetProperties("A", "A", "A", "A", "A"); JsonPatchDocument <ApiOfficerServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiOfficerServerRequestModel(); patch.ApplyTo(response); response.Badge.Should().Be("A"); response.Email.Should().Be("A"); response.FirstName.Should().Be("A"); response.LastName.Should().Be("A"); response.Password.Should().Be("A"); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiOfficerServerRequestModel model) { ApiOfficerServerRequestModel request = await this.PatchModel(id, this.OfficerModelMapper.CreatePatch(model)) as ApiOfficerServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiOfficerServerResponseModel> result = await this.OfficerService.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 <ApiOfficerServerRequestModel> patch) { ApiOfficerServerResponseModel record = await this.OfficerService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiOfficerServerRequestModel model = await this.PatchModel(id, patch) as ApiOfficerServerRequestModel; UpdateResponse <ApiOfficerServerResponseModel> result = await this.OfficerService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }