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; ISubscriptionService service = testServer.Host.Services.GetService(typeof(ISubscriptionService)) as ISubscriptionService; var model = new ApiSubscriptionServerRequestModel(); model.SetProperties("B", "B"); CreateResponse <ApiSubscriptionServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.SubscriptionDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiSubscriptionServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public void MapServerResponseToRequest() { var mapper = new ApiSubscriptionServerModelMapper(); var model = new ApiSubscriptionServerResponseModel(); model.SetProperties(1, "A", "A"); ApiSubscriptionServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Name.Should().Be("A"); response.StripePlanName.Should().Be("A"); }
public virtual async Task <IActionResult> Create([FromBody] ApiSubscriptionServerRequestModel model) { CreateResponse <ApiSubscriptionServerResponseModel> result = await this.SubscriptionService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Subscriptions/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void CreatePatch() { var mapper = new ApiSubscriptionServerModelMapper(); var model = new ApiSubscriptionServerRequestModel(); model.SetProperties("A", "A"); JsonPatchDocument <ApiSubscriptionServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiSubscriptionServerRequestModel(); patch.ApplyTo(response); response.Name.Should().Be("A"); response.StripePlanName.Should().Be("A"); }
private async Task <ApiSubscriptionServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiSubscriptionServerRequestModel> patch) { var record = await this.SubscriptionService.Get(id); if (record == null) { return(null); } else { ApiSubscriptionServerRequestModel request = this.SubscriptionModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <ISubscriptionService, ISubscriptionRepository>(); var model = new ApiSubscriptionServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new SubscriptionService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.SubscriptionModelValidatorMock.Object, mock.DALMapperMockFactory.DALSubscriptionMapperMock); 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.SubscriptionModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <SubscriptionDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiSubscriptionServerRequestModel model) { ApiSubscriptionServerRequestModel request = await this.PatchModel(id, this.SubscriptionModelMapper.CreatePatch(model)) as ApiSubscriptionServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiSubscriptionServerResponseModel> result = await this.SubscriptionService.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 <ISubscriptionService, ISubscriptionRepository>(); var model = new ApiSubscriptionServerRequestModel(); var validatorMock = new Mock <IApiSubscriptionServerRequestModelValidator>(); 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 SubscriptionService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALSubscriptionMapperMock); 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 <SubscriptionDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiSubscriptionServerRequestModel> patch) { ApiSubscriptionServerResponseModel record = await this.SubscriptionService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiSubscriptionServerRequestModel model = await this.PatchModel(id, patch) as ApiSubscriptionServerRequestModel; UpdateResponse <ApiSubscriptionServerResponseModel> result = await this.SubscriptionService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }