public async void Update_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IDeviceService, IDeviceRepository>(); var model = new ApiDeviceServerRequestModel(); var validatorMock = new Mock <IApiDeviceServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiDeviceServerRequestModel>())).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 Device())); var service = new DeviceService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALDeviceMapperMock, mock.DALMapperMockFactory.DALDeviceActionMapperMock); UpdateResponse <ApiDeviceServerResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiDeviceServerRequestModel>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <DeviceUpdatedNotification>(), 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; IDeviceService service = testServer.Host.Services.GetService(typeof(IDeviceService)) as IDeviceService; var model = new ApiDeviceServerRequestModel(); model.SetProperties(DateTime.Parse("1/1/1988 12:00:00 AM"), true, "B", Guid.Parse("3842cac4-b9a0-8223-0dcc-509a6f75849b")); CreateResponse <ApiDeviceServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.DeviceDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiDeviceServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiDeviceServerRequestModel model) { CreateResponse <ApiDeviceServerResponseModel> result = await this.DeviceService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Devices/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void MapServerResponseToRequest() { var mapper = new ApiDeviceServerModelMapper(); var model = new ApiDeviceServerResponseModel(); model.SetProperties(1, DateTime.Parse("1/1/1987 12:00:00 AM"), true, "A", Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da")); ApiDeviceServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.DateOfLastPing.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.IsActive.Should().Be(true); response.Name.Should().Be("A"); response.PublicId.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da")); }
private async Task <ApiDeviceServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiDeviceServerRequestModel> patch) { var record = await this.DeviceService.Get(id); if (record == null) { return(null); } else { ApiDeviceServerRequestModel request = this.DeviceModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void CreatePatch() { var mapper = new ApiDeviceServerModelMapper(); var model = new ApiDeviceServerRequestModel(); model.SetProperties(DateTime.Parse("1/1/1987 12:00:00 AM"), true, "A", Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da")); JsonPatchDocument <ApiDeviceServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiDeviceServerRequestModel(); patch.ApplyTo(response); response.DateOfLastPing.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.IsActive.Should().Be(true); response.Name.Should().Be("A"); response.PublicId.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da")); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IDeviceService, IDeviceRepository>(); var model = new ApiDeviceServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new DeviceService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.DeviceModelValidatorMock.Object, mock.DALMapperMockFactory.DALDeviceMapperMock, 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.DeviceModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <DeviceDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiDeviceServerRequestModel model) { ApiDeviceServerRequestModel request = await this.PatchModel(id, this.DeviceModelMapper.CreatePatch(model)) as ApiDeviceServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiDeviceServerResponseModel> result = await this.DeviceService.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 <ApiDeviceServerRequestModel> patch) { ApiDeviceServerResponseModel record = await this.DeviceService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiDeviceServerRequestModel model = await this.PatchModel(id, patch) as ApiDeviceServerRequestModel; UpdateResponse <ApiDeviceServerResponseModel> result = await this.DeviceService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }