public async void Update_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IMachineService, IMachineRepository>(); var model = new ApiMachineServerRequestModel(); var validatorMock = new Mock <IApiMachineServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiMachineServerRequestModel>())).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 Machine())); var service = new MachineService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALMachineMapperMock, mock.DALMapperMockFactory.DALLinkMapperMock); UpdateResponse <ApiMachineServerResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiMachineServerRequestModel>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <MachineUpdatedNotification>(), 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; IMachineService service = testServer.Host.Services.GetService(typeof(IMachineService)) as IMachineService; var model = new ApiMachineServerRequestModel(); model.SetProperties("B", "B", "B", Guid.Parse("3842cac4-b9a0-8223-0dcc-509a6f75849b"), "B"); CreateResponse <ApiMachineServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.MachineDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiMachineServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiMachineServerRequestModel model) { CreateResponse <ApiMachineServerResponseModel> result = await this.MachineService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Machines/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void MapServerResponseToRequest() { var mapper = new ApiMachineServerModelMapper(); var model = new ApiMachineServerResponseModel(); model.SetProperties(1, "A", "A", "A", Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"), "A"); ApiMachineServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Description.Should().Be("A"); response.JwtKey.Should().Be("A"); response.LastIpAddress.Should().Be("A"); response.MachineGuid.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da")); response.Name.Should().Be("A"); }
private async Task <ApiMachineServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiMachineServerRequestModel> patch) { var record = await this.MachineService.Get(id); if (record == null) { return(null); } else { ApiMachineServerRequestModel request = this.MachineModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void CreatePatch() { var mapper = new ApiMachineServerModelMapper(); var model = new ApiMachineServerRequestModel(); model.SetProperties("A", "A", "A", Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"), "A"); JsonPatchDocument <ApiMachineServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiMachineServerRequestModel(); patch.ApplyTo(response); response.Description.Should().Be("A"); response.JwtKey.Should().Be("A"); response.LastIpAddress.Should().Be("A"); response.MachineGuid.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da")); response.Name.Should().Be("A"); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IMachineService, IMachineRepository>(); var model = new ApiMachineServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new MachineService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.MachineModelValidatorMock.Object, mock.DALMapperMockFactory.DALMachineMapperMock, mock.DALMapperMockFactory.DALLinkMapperMock); 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.MachineModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <MachineDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiMachineServerRequestModel model) { ApiMachineServerRequestModel request = await this.PatchModel(id, this.MachineModelMapper.CreatePatch(model)) as ApiMachineServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiMachineServerResponseModel> result = await this.MachineService.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 <ApiMachineServerRequestModel> patch) { ApiMachineServerResponseModel record = await this.MachineService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiMachineServerRequestModel model = await this.PatchModel(id, patch) as ApiMachineServerRequestModel; UpdateResponse <ApiMachineServerResponseModel> result = await this.MachineService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }