public void IssueWithEmptyPriorityHandledWithoutException() { var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonIssueWithNullPriority) }; var responseHandler = new Mock <DelegatingHandler>(); responseHandler .Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .Returns(Task.FromResult(response)); var connection = new Connection("http://jira", "any", "any") { Client = new HttpClient(responseHandler.Object) }; var svc = new HttpJiraService("http://jira", Empty, Empty) { Connection = connection }; var jqlRule = new JqlRule { Jql = "any", HowToNotify = new Notify { Subject = "subject", MetaAddressers = new[] { "assignee" }, MetaCarbonCopy = new string[] { } } }; var jqlSupplier = new JqlSupplier(svc, new[] { jqlRule }, new Mock <ILogger>().Object); var messenger = new Mock <IMessenger>(); var pipe = new ReactionPipe <Issue> { PackageSupplier = jqlSupplier, PackageConverter = new IssuePackageConverter("http://jira"), Messenger = messenger.Object, HttpHandler = new Mock <IHttpHandler>().Object }; pipe.Run(); responseHandler .Protected() .Verify <Task <HttpResponseMessage> >("SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>( r => r.RequestUri == new Uri("http://jira/rest/api/2/search?jql=any&maxResults=50")), ItExpr.IsAny <CancellationToken>()); messenger.Verify(m => m.SendAll(It.Is <IEnumerable <Message> >( msgs => ExpectedNotificationWithEmptyPriority.Equals(msgs.Single().Body))), Times.Once()); }
public void CheckMoreThanOneCallRestAreCalled() { // Setup var rule = new JqlRule { HowToUpdate = new[] { new Update { UrlPattern = "http://example.com" }, new Update { UrlPattern = "http://example.com" } } }; var issuesSupplier = new Mock <IJiraService>(); issuesSupplier .Setup(s => s.GetIssuesForJql(It.IsAny <string>())) .Returns(new[] { new Issue() }) ; var jqlSupplier = new JqlSupplier(issuesSupplier.Object, new[] { rule }, new Mock <ILogger>().Object); var httpHandler = new Mock <IHttpHandler>(); var pipe = new ReactionPipe <Issue> { PackageSupplier = jqlSupplier, PackageConverter = new IssuePackageConverter("http://jira"), HttpHandler = httpHandler.Object }; // Experiment pipe.Run(); // Check result httpHandler.Verify(h => h.HandleAll(It.Is <IEnumerable <HttpRequest> >(r => r.Count() == 2))); }
public void EnsureRequestToUpdateFormedCorrectlyAndRan() { var responseWhenGetIssues = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonTypicalIssue) }; var responseWhenDoPost = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("") }; var responseHandler = new Mock <DelegatingHandler>(); responseHandler .Protected() .SetupSequence <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .Returns(Task.FromResult(responseWhenGetIssues)) .Returns(Task.FromResult(responseWhenDoPost)) ; var connection = new Connection("http://jira", "any", "any") { Client = new HttpClient(responseHandler.Object) }; var svc = new HttpJiraService("http://jira", Empty, Empty) { Connection = connection }; var jqlRule = new JqlRule { Jql = "any", HowToUpdate = new[] { new Update { Verb = "POST", UrlPattern = "{{@jiraRoot}}/rest/api/2/issue/{{@issueKey}}/transitions", BodyPattern = @"{ ""update"": { ""comment"": [ { ""add"": { ""body"": ""Issue {{@issueKey}} Closed automatically because of activity absence"" } } ] }, ""transition"": { ""id"": ""61"" } }" } } }; var jqlSupplier = new JqlSupplier(svc, new[] { jqlRule }, new Mock <ILogger>().Object); var messenger = new Mock <IMessenger>(); var pipe = new ReactionPipe <Issue> { PackageSupplier = jqlSupplier, PackageConverter = new IssuePackageConverter("http://jira"), Messenger = messenger.Object, HttpHandler = svc }; pipe.Run(); responseHandler .Protected() .Verify <Task <HttpResponseMessage> >("SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>( r => r.RequestUri == new Uri("http://jira/rest/api/2/issue/BENDER-961/transitions") && r.Method == HttpMethod.Post && r.Content !.ReadAsStringAsync().Result.Contains("Issue BENDER-961 Closed automatically because of activity absence") ), ItExpr.IsAny <CancellationToken>()); }
public void CheckAssigneeAndReporterReplacement() { // Setup var rule = @"<configuration> <jqlRule group=""test""> <jql>any</jql> <callRest verb=""PUT"" urlPattern=""https://jira.example.com/swap-assignee-and-reporter-where/?assignee={{@assignee.name}}&reporter={{@reporter.name}}""> <body><![CDATA[ { ""update"": { ""assignee"": [{""set"": {""name"": ""{{@reporter.name}}""}}], ""reporter"": [{""set"": {""name"": ""{{@assignee.name}}""}}] } } ]]> </body> </callRest> </jqlRule> </configuration>"; var rulesConfig = new XmlRulesConfig(XDocument.Parse(rule), new Mock <ILogger>().Object); var responseWhenGetIssues = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonTypicalIssue) }; var responseWhenDoPut = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("") }; var responseHandler = new Mock <DelegatingHandler>(); responseHandler .Protected() .SetupSequence <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .Returns(Task.FromResult(responseWhenGetIssues)) .Returns(Task.FromResult(responseWhenDoPut)) ; var connection = new Connection("http://jira", Empty, Empty) { Client = new HttpClient(responseHandler.Object) }; var jiraService = new HttpJiraService("http://jira", Empty, Empty) { Connection = connection }; var packageSupplier = new JqlSupplier(jiraService, rulesConfig.GetJqlRules("test"), new Mock <ILogger>().Object); var pipe = new ReactionPipe <Issue>() { PackageSupplier = packageSupplier, PackageConverter = new IssuePackageConverter(Empty), HttpHandler = jiraService }; // Experiment pipe.Run(); // Check results responseHandler .Protected() .Verify <Task <HttpResponseMessage> >("SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>( r => r.RequestUri == new Uri("https://jira.example.com/swap-assignee-and-reporter-where/?assignee=alice&reporter=bob") && r.Method == HttpMethod.Put && r.Content !.ReadAsStringAsync().Result.Contains(@"""assignee"": [{""set"": {""name"": ""bob""}}]") && r.Content !.ReadAsStringAsync().Result.Contains(@"""reporter"": [{""set"": {""name"": ""alice""}}]") ), ItExpr.IsAny <CancellationToken>()); }