public async Task CreatePackageCommentAsync_UnsuccessfulRequest_ShouldThrowFactroApiException() { // Arrange var projectId = Guid.NewGuid().ToString(); var packageId = Guid.NewGuid().ToString(); var createPackageCommentRequest = new CreatePackageCommentRequest(Guid.NewGuid().ToString()); var expectedResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, RequestMessage = new HttpRequestMessage { RequestUri = new Uri("http://www.mock-web-address.com"), }, }; var packageApi = this.fixture.GetPackageApi(expectedResponse); // Act Func <Task> act = async() => await packageApi.CreatePackageCommentAsync(projectId, packageId, createPackageCommentRequest); // Assert await act.Should().ThrowAsync <FactroApiException>(); }
public async Task CreatePackageCommentAsync_ValidRequest_ShouldReturnCreatedPackageComment() { // Arrange var existingProject = new GetProjectPayload { Id = Guid.NewGuid().ToString(), }; var existingPackage = new GetPackagePayload { Id = Guid.NewGuid().ToString(), }; var text = Guid.NewGuid().ToString(); var createPackageCommentRequest = new CreatePackageCommentRequest(text); var expectedPackageComment = new CreatePackageCommentResponse { Text = text, }; var expectedResponseContent = new StringContent(JsonConvert.SerializeObject(expectedPackageComment, this.fixture.JsonSerializerSettings)); var expectedResponse = new HttpResponseMessage { Content = expectedResponseContent, }; var packageApi = this.fixture.GetPackageApi(expectedResponse); var createPackageCommentResponse = default(CreatePackageCommentResponse); // Act Func <Task> act = async() => createPackageCommentResponse = await packageApi.CreatePackageCommentAsync(existingProject.Id, existingPackage.Id, createPackageCommentRequest); // Assert await act.Should().NotThrowAsync(); createPackageCommentResponse.Should().BeEquivalentTo(expectedPackageComment); }
public async Task CreatePackageCommentAsync_InvalidPackageId_ShouldThrowArgumentNullException(string packageId) { // Arrange var existingProject = new GetProjectPayload { Id = Guid.NewGuid().ToString(), }; var text = Guid.NewGuid().ToString(); var createPackageCommentRequest = new CreatePackageCommentRequest(text); var packageApi = this.fixture.GetPackageApi(); // Act Func <Task> act = async() => await packageApi.CreatePackageCommentAsync(existingProject.Id, packageId, createPackageCommentRequest); // Assert await act.Should().ThrowAsync <ArgumentNullException>(); }
public async Task <CreatePackageCommentResponse> CreatePackageCommentAsync(string projectId, string packageId, CreatePackageCommentRequest createPackageCommentRequest) { if (string.IsNullOrWhiteSpace(projectId)) { throw new ArgumentNullException(nameof(projectId), $"{nameof(projectId)} can not be null, empty or whitespace."); } if (string.IsNullOrWhiteSpace(packageId)) { throw new ArgumentNullException(nameof(packageId), $"{nameof(packageId)} can not be null, empty or whitespace."); } if (createPackageCommentRequest == null) { throw new ArgumentNullException(nameof(createPackageCommentRequest), $"{nameof(createPackageCommentRequest)} can not be null."); } if (createPackageCommentRequest.Text == null) { throw new ArgumentNullException(nameof(createPackageCommentRequest), $"{nameof(createPackageCommentRequest.Text)} can not be null."); } var requestRoute = PackageApiEndpoints.Comment.Create(projectId, packageId); var requestString = JsonConvert.SerializeObject(createPackageCommentRequest, this.jsonSerializerSettings); var requestContent = ApiHelpers.GetStringContent(requestString); var response = await this.httpClient.PostAsync(requestRoute, requestContent); if (!response.IsSuccessStatusCode) { throw new FactroApiException( $"Could not create comment in package with id '{packageId}' of project with id '{projectId}'.", response.RequestMessage.RequestUri.ToString(), response.StatusCode, response.Content == null ? null : await response.Content.ReadAsStringAsync()); } var responseContentString = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <CreatePackageCommentResponse>( responseContentString, this.jsonSerializerSettings); return(result); }