public async Task UpdateWorkItemAsync_OnNullWorkItemDetailsOrWorkItem_Throws()
        {
            // Arrange
            GitHubRepoIssueReaderWriter target = CreateTarget(new CtorArgs {
                Search = new GitHubSearchInBodyMock(searchResults: null).Search
            });

            // Act/Assert
            await Assert.ThrowsAsync <ArgumentNullException>(() => target.UpdateWorkItemAsync(workItemDetails: null));

            await Assert.ThrowsAsync <ArgumentNullException>(() => target.UpdateWorkItemAsync(new WorkItemDetails {
                WorkItem = null
            }));
        }
        public async Task UpdateWorkItemAsync_IfMultipleWorkItemsExist_Throws()
        {
            // Arrange
            var searchMock = new GitHubSearchInBodyMock(searchResults: new[] { CreateSampleIssue(), CreateSampleIssue() });

            // Act
            GitHubRepoIssueReaderWriter target = CreateTarget(new CtorArgs {
                Search = searchMock.Search
            });

            // Assert
            await Assert.ThrowsAsync <WorkItemIdentificationException>(() => target.UpdateWorkItemAsync(CreateSampleWorkItemDetails()));
        }
        public async Task UpdateWorkItemAsync_IfWorkItemExists_Succeeds(bool hasAttachments)
        {
            // Arrange
            Issue           issue           = CreateSampleIssue(label: "AnyLabel");
            WorkItemDetails workItemDetails = CreateSampleWorkItemDetails(hasAttachments: hasAttachments);

            var      issuesMock = new GitHubIssueMock();
            var      searchMock = new GitHubSearchInBodyMock(searchResults: new[] { issue });
            CtorArgs ctorArgs   = new CtorArgs {
                Search = searchMock.Search, Issues = issuesMock.Issues
            };

            GitHubRepoIssueReaderWriter target = CreateTarget(ctorArgs);

            // Act
            await target.UpdateWorkItemAsync(workItemDetails);

            // Assert: Search
            Assert.Contains(workItemDetails.WorkItem.Id.ToString(), searchMock.SearchIssuesRequest.Term);
            Assert.Equal(IssueTypeQualifier.Issue, searchMock.SearchIssuesRequest.Type);
            Assert.Single(searchMock.SearchIssuesRequest.Repos);
            Assert.Equal(ctorArgs.FullRepoName, searchMock.SearchIssuesRequest.Repos[0]);
            Assert.Contains(searchMock.SearchIssuesRequest.In, q => q == IssueInQualifier.Body);
            searchMock.VerifySearchCallCount(callCount: 1);

            // Assert: Update -- Owner + Repo + Issue number
            Assert.Equal(ctorArgs.RepoOwner, issuesMock.UpdateIssueArgs.Owner);
            Assert.Equal(ctorArgs.Repo, issuesMock.UpdateIssueArgs.Name);
            Assert.Equal(issue.Number, issuesMock.UpdateIssueArgs.Number);

            // Assert: Update -- Title + Body
            Assert.Equal(workItemDetails.WorkItem.Summary, issuesMock.UpdateIssueArgs.IssueUpdate.Title);
            Assert.Contains(TextUtilities.GetFormattedWorkItemBody(workItemDetails.WorkItem, workItemDetails.FileAttachments), issuesMock.UpdateIssueArgs.IssueUpdate.Body);

            // Assert: Update -- Labels
            Assert.Single(issuesMock.UpdateIssueArgs.IssueUpdate.Labels);
            Assert.Contains(issuesMock.UpdateIssueArgs.IssueUpdate.Labels, label => label == GitHubLabels.CodePlexMigrated);

            // Assert: Update -- Attachments
            if (hasAttachments)
            {
                Assert.Contains(Resources.Attachments, issuesMock.UpdateIssueArgs.IssueUpdate.Body);
            }
            else
            {
                Assert.DoesNotContain(Resources.Attachments, issuesMock.UpdateIssueArgs.IssueUpdate.Body);
            }

            issuesMock.VerifyIssuesCallCount(methodName: nameof(IIssuesClient.Update), callCount: 2);
        }
        public async Task UpdateWorkItemAsync_IfWorkItemHasComments_DropsAndRecreatesComments()
        {
            // Arrange
            int   issueCommentCount = 10;
            Issue issue             = CreateSampleIssue(label: "AnyLabel");

            IssueComment[]  issueComments   = Enumerable.Range(0, issueCommentCount).Select(i => CreateSampleComment(issue.Number)).ToArray();
            WorkItemDetails workItemDetails = CreateSampleWorkItemDetails(hasComments: true);

            var issuesMock = new GitHubIssueMock();

            issuesMock.SetCommentsForIssue(issue.Number, issueComments);

            var      searchMock = new GitHubSearchInBodyMock(searchResults: new[] { issue });
            CtorArgs ctorArgs   = new CtorArgs {
                Search = searchMock.Search, Issues = issuesMock.Issues
            };

            GitHubRepoIssueReaderWriter target = CreateTarget(ctorArgs);

            // Act
            await target.UpdateWorkItemAsync(workItemDetails);

            // Assert: Search
            searchMock.VerifySearchCallCount(callCount: 1);

            // Assert: Delete comments
            Assert.True(issuesMock.DeleteCommentArgs.All(args => args.Owner == ctorArgs.RepoOwner));
            Assert.True(issuesMock.DeleteCommentArgs.All(args => args.Name == ctorArgs.Repo));
            Assert.Equal(issueComments.Select(c => c.Id), issuesMock.DeleteCommentArgs.Select(args => args.Id));
            issuesMock.VerifyCommentCallCount(nameof(IIssueCommentsClient.Delete), callCount: issueCommentCount);

            // Assert: Create comments
            Assert.True(issuesMock.CreateCommentArgs.All(args => args.Owner == ctorArgs.RepoOwner));
            Assert.True(issuesMock.CreateCommentArgs.All(args => args.Name == ctorArgs.Repo));
            Assert.True(issuesMock.CreateCommentArgs.All(args => args.Number == issue.Number));

            string[] createdComments = issuesMock.CreateCommentArgs.Select(c => c.NewComment).ToArray();
            foreach (WorkItemComment comment in workItemDetails.Comments)
            {
                Assert.Contains(createdComments, c => c.Contains(comment.Message));
            }

            issuesMock.VerifyCommentCallCount(nameof(IIssueCommentsClient.Create), callCount: workItemDetails.Comments.Count());
        }