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; IDeviceActionService service = testServer.Host.Services.GetService(typeof(IDeviceActionService)) as IDeviceActionService; var model = new ApiDeviceActionServerRequestModel(); model.SetProperties("B", 1, "B"); CreateResponse <ApiDeviceActionServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.DeviceActionDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiDeviceActionServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiDeviceActionServerRequestModel model) { CreateResponse <ApiDeviceActionServerResponseModel> result = await this.DeviceActionService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/DeviceActions/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void MapServerResponseToRequest() { var mapper = new ApiDeviceActionServerModelMapper(); var model = new ApiDeviceActionServerResponseModel(); model.SetProperties(1, "A", 1, "A"); ApiDeviceActionServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Action.Should().Be("A"); response.DeviceId.Should().Be(1); response.Name.Should().Be("A"); }
public void CreatePatch() { var mapper = new ApiDeviceActionServerModelMapper(); var model = new ApiDeviceActionServerRequestModel(); model.SetProperties("A", 1, "A"); JsonPatchDocument <ApiDeviceActionServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiDeviceActionServerRequestModel(); patch.ApplyTo(response); response.Action.Should().Be("A"); response.DeviceId.Should().Be(1); response.Name.Should().Be("A"); }
private async Task <ApiDeviceActionServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiDeviceActionServerRequestModel> patch) { var record = await this.DeviceActionService.Get(id); if (record == null) { return(null); } else { ApiDeviceActionServerRequestModel request = this.DeviceActionModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IDeviceActionService, IDeviceActionRepository>(); var model = new ApiDeviceActionServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new DeviceActionService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.DeviceActionModelValidatorMock.Object, mock.DALMapperMockFactory.DALDeviceActionMapperMock); 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.DeviceActionModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <DeviceActionDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiDeviceActionServerRequestModel model) { ApiDeviceActionServerRequestModel request = await this.PatchModel(id, this.DeviceActionModelMapper.CreatePatch(model)) as ApiDeviceActionServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiDeviceActionServerResponseModel> result = await this.DeviceActionService.Update(id, request); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }
public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IDeviceActionService, IDeviceActionRepository>(); var model = new ApiDeviceActionServerRequestModel(); var validatorMock = new Mock <IApiDeviceActionServerRequestModelValidator>(); 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 DeviceActionService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALDeviceActionMapperMock); 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 <DeviceActionDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiDeviceActionServerRequestModel> patch) { ApiDeviceActionServerResponseModel record = await this.DeviceActionService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiDeviceActionServerRequestModel model = await this.PatchModel(id, patch) as ApiDeviceActionServerRequestModel; UpdateResponse <ApiDeviceActionServerResponseModel> result = await this.DeviceActionService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }