Beispiel #1
0
        public void Can_use_Dsl_to_retrieve_a_work_item()
        {
            using (ShimsContext.Create())
            {
                // arrange
                // redirect the console
                var consoleOut = Helpers.Logging.RedirectConsoleOut();

                var wi = new ShimWorkItem()
                {
                    TitleGet = () => "The wi title", IdGet = () => 99
                };
                var emailProvider = new Moq.Mock <IEmailProvider>();
                var tfsProvider   = new Moq.Mock <ITfsProvider>();
                tfsProvider.Setup(t => t.GetWorkItem(99)).Returns(wi);
                var engine = new TFSEventsProcessor.Dsl.DslProcessor();

                // act
                engine.RunScript(@"dsl\tfs\loadwi.py", tfsProvider.Object, emailProvider.Object);

                // assert
                Assert.AreEqual(
                    "Work item '99' has the title 'The wi title'" + Environment.NewLine,
                    consoleOut.ToString());
            }
        }
 internal static IPendingCheckin CreatePendingCheckin(ShimWorkItem item, string rootPath = "$/Project/Folder")
 {
     return(CreatePendingCheckin(new List <ShimWorkItem>()
     {
         item
     }, rootPath));
 }
Beispiel #3
0
        public void Can_use_Dsl_to_update_a_work_item()
        {
            using (ShimsContext.Create())
            {
                // arrange
                // redirect the console
                var consoleOut = Helpers.Logging.RedirectConsoleOut();

                var wi = new ShimWorkItem()
                {
                    TitleGet = () => "Title", IdGet = () => 99
                };
                var emailProvider = new Moq.Mock <IEmailProvider>();
                var tfsProvider   = new Moq.Mock <ITfsProvider>();
                tfsProvider.Setup(t => t.CreateWorkItem(
                                      "tp",
                                      "Bug",
                                      new Dictionary <string, object>()
                {
                    { "Title", "The Title" },
                    { "Estimate", 2 }
                })).Returns(wi);
                var engine = new TFSEventsProcessor.Dsl.DslProcessor();

                // act
                engine.RunScript(@"dsl\tfs\updatewi.py", tfsProvider.Object, emailProvider.Object);

                // assert
                tfsProvider.Verify(t => t.UpdateWorkItem(It.IsAny <WorkItem>()));
            }
        }
		internal static ShimWorkItem CreateCodeReviewResponse(int id, string state, string reviewResult)
		{
			var responseFields = new List<Field>()
				{
					new ShimField()
					{ 
						NameGet = () => CodeReviewPolicy.ClosedStatus,
						ValueGet = () => reviewResult
					}
				};
			var fakeResponseFields = new ShimFieldCollection()
			{
				ItemGetString = (s) => responseFields.SingleOrDefault(f => f.Name == s)
			};

			var responseWorkItem = new ShimWorkItem()
			{
				TypeGet = () => new ShimWorkItemType()
				{
					NameGet = () => "Code Review Response"
				},
				IdGet = () => id,
				StateGet = () => state,
				FieldsGet = () => fakeResponseFields,
			};
			return responseWorkItem;
		}
Beispiel #5
0
        public void Can_use_Dsl_to_find_if_no_parent_work_item()
        {
            using (ShimsContext.Create())
            {
                // arrange
                // redirect the console
                var consoleOut = Helpers.Logging.RedirectConsoleOut();

                var wi = new ShimWorkItem()
                {
                    TitleGet = () => "The wi title", IdGet = () => 99
                };
                var emailProvider = new Moq.Mock <IEmailProvider>();
                var tfsProvider   = new Moq.Mock <ITfsProvider>();
                tfsProvider.Setup(t => t.GetWorkItem(99)).Returns(wi);
                // don't need to assign a value for the parent call as will return null by default
                var engine = new TFSEventsProcessor.Dsl.DslProcessor();

                // act
                engine.RunScript(@"dsl\tfs\loadparentwi.py", tfsProvider.Object, emailProvider.Object);

                // assert
                Assert.AreEqual(
                    "Work item '99' has no parent" + Environment.NewLine,
                    consoleOut.ToString());
            }
        }
        internal static ShimWorkItem CreateCodeReviewResponse(int id, string state, string reviewResult)
        {
            var responseFields = new List <Field>()
            {
                new ShimField()
                {
                    NameGet  = () => CodeReviewPolicy.ClosedStatus,
                    ValueGet = () => reviewResult
                }
            };
            var fakeResponseFields = new ShimFieldCollection()
            {
                ItemGetString = (s) => responseFields.SingleOrDefault(f => f.Name == s)
            };

            var responseWorkItem = new ShimWorkItem()
            {
                TypeGet = () => new ShimWorkItemType()
                {
                    NameGet = () => "Code Review Response"
                },
                IdGet     = () => id,
                StateGet  = () => state,
                FieldsGet = () => fakeResponseFields,
            };

            return(responseWorkItem);
        }
        public void Can_use_Dsl_to_send_templated_email()
        {
            using (ShimsContext.Create())
            {
                // arrange
                var wi = new ShimWorkItem()
                {
                    TitleGet = () => "The wi title", IdGet = () => 99
                };
                var emailProvider = new Moq.Mock <IEmailProvider>();
                var tfsProvider   = new Moq.Mock <ITfsProvider>();
                tfsProvider.Setup(t => t.GetWorkItem(99)).Returns(wi);
                var engine = new TFSEventsProcessor.Dsl.DslProcessor();

                var args = new Dictionary <string, object> {
                    { "Arguments", new[] { "WorkItemEvent", "99" } },
                };

                // act
                engine.RunScript(
                    @".\dsl",
                    @"dsl\tfs",
                    "sendtemplatedemail.py",
                    args,
                    tfsProvider.Object,
                    emailProvider.Object,
                    Helpers.TestData.DummyWorkItemChangedAlertXml());

                // assert
                emailProvider.Verify(
                    e =>
                    e.SendEmailAlert(Moq.It.IsAny <IFieldLookupProvider>(), @"dsl\tfs\EmailTemplate.htm", true, true));
            }
        }
        private WorkItem GetFakeWorkItem()
        {
            var wi = new ShimWorkItem()
            {
                IdGet = () => 2
            };

            return(wi);
        }
        public void Cannot_use_Dsl_to_update_parent_work_item_if_children_not_complete()
        {
            using (ShimsContext.Create())
            {
                // arrange
                var memLogger = Helpers.Logging.CreateMemoryTargetLogger(LogLevel.Debug);

                var emailProvider = new Moq.Mock <IEmailProvider>(MockBehavior.Strict);
                var wi100         = new ShimWorkItem()
                {
                    TitleGet = () => "The child title",
                    IdGet    = () => 100,
                    StateGet = () => "Done"
                };
                var wi101 = new ShimWorkItem()
                {
                    TitleGet = () => "The child title",
                    IdGet    = () => 101,
                    StateGet = () => "Committed"
                };
                var parent99 = new ShimWorkItem()
                {
                    TitleGet = () => "The parent title",
                    IdGet    = () => 99,
                    StateGet = () => "Committed"
                };


                var tfsProvider = new Moq.Mock <ITfsProvider>(MockBehavior.Strict);
                tfsProvider.Setup(t => t.GetWorkItem(It.IsAny <int>())).Returns(wi100);
                tfsProvider.Setup(t => t.GetParentWorkItem(It.IsAny <WorkItem>())).Returns(parent99);
                tfsProvider.Setup(t => t.GetChildWorkItems(It.IsAny <WorkItem>()))
                .Returns(new WorkItem[] { wi100, wi101 });

                var engine = new TFSEventsProcessor.Dsl.DslProcessor();

                var args = new Dictionary <string, object> {
                    { "Arguments", new[] { "WorkItemEvent", "100" } }
                };

                // act
                engine.RunScript(
                    @"dsl\tfs\changeparentworkitemstate.py",
                    args,
                    tfsProvider.Object,
                    emailProvider.Object);

                // assert
                foreach (var line in memLogger.Logs)
                {
                    Console.WriteLine(line);
                }

                // we rely on the MockBehavior.Strict to throw an error if any methods are called we have not expected
                // i.e. a call to the emailer or update of the work item
            }
        }
        internal static ShimWorkItem CreateCodeReviewRequest(int id, string state, string closedStatus, List <WorkItem> responses)
        {
            var requestFields = new List <Field>()
            {
                new ShimField()
                {
                    NameGet  = () => CodeReviewPolicy.ClosedStatus,
                    ValueGet = () => closedStatus
                }
            };
            var fakeRequestFields = new ShimFieldCollection()
            {
                ItemGetString = (s) => requestFields.SingleOrDefault(f => f.Name == s)
            };

            var links = new List <WorkItemLink>();

            foreach (var response in responses)
            {
                links.Add(new ShimWorkItemLink()
                {
                    LinkTypeEndGet = () => new ShimWorkItemLinkTypeEnd()
                    {
                        NameGet = () => "Child",
                    },
                    TargetIdGet = () => response.Id
                });
            }
            var fakeLinks = new ShimWorkItemLinkCollection();

            fakeLinks.Bind(links);

            var fakeProject = new ShimProject()
            {
                StoreGet = () => new ShimWorkItemStore()
                {
                    GetWorkItemInt32 = (i) => responses.SingleOrDefault(r => r.Id == i)
                }
            };

            var reviewWorkItem = new ShimWorkItem()
            {
                TypeGet = () => new ShimWorkItemType()
                {
                    NameGet = () => "Code Review Request"
                },
                StateGet         = () => state,
                FieldsGet        = () => fakeRequestFields,
                WorkItemLinksGet = () => fakeLinks,
                ProjectGet       = () => fakeProject
            };

            return(reviewWorkItem);
        }
        public void WorkItemNativePropertiesShouldBeAccessedAsSuch()
        {
            var workItem = new ShimWorkItem();

            workItem.TitleGet = () => "Title";
            workItem.DescriptionGet = () => "Description";

            dynamic workItemWrapper = new WorkItemModel(workItem);

            Assert.AreEqual("Title", workItemWrapper.Title);
            Assert.AreEqual("Description", workItemWrapper.Description);
        }
		public void Test_SyncIsCalled()
		{
			using (var context = ShimsContext.Create())
			{
				bool syncCalled = false;
				var workItem = new ShimWorkItem()
					{
						SyncToLatest = () => { syncCalled = true; }
					};
				var args = new Dictionary<string, object>()	{
						{ "WorkItem", (WorkItem)workItem }
					};
				WorkflowInvoker.Invoke(new RefreshWorkItem(), args);

				Assert.IsTrue(syncCalled);
			}
		}
        private Dictionary <string, object> GetWorkflowArgs(bool containsIntegratedIn, bool isValid)
        {
            var workItem = new ShimWorkItem()
            {
                TypeGet = () => new ShimWorkItemType()
                {
                    NameGet             = () => "Task",
                    FieldDefinitionsGet = () => new ShimFieldDefinitionCollection()
                    {
                        ContainsString = (s) => containsIntegratedIn
                    }
                },
                PartialOpen         = () => { },
                ItemSetStringObject = (s, o) => { if (!containsIntegratedIn)
                                                  {
                                                      throw new ApplicationException("Should not be calling set IntegratedIn");
                                                  }
                },
                HistorySetString = (s) => { if (!containsIntegratedIn)
                                            {
                                                throw new ApplicationException("Should not be calling set History");
                                            }
                },
                IsValid  = () => isValid,
                Validate = () => isValid ? new ArrayList() : new ArrayList(new List <Field>()
                {
                    new ShimField()
                    {
                        NameGet = () => "Field1", ValueGet = () => "Err1"
                    },
                    new ShimField()
                    {
                        NameGet = () => "Field2", ValueGet = () => "Err2"
                    }
                })
            };
            var args = new Dictionary <string, object>()
            {
                { "WorkItem", (WorkItem)workItem },
                { "BuildNumber", "123" },
                { "WorkItemTypeFieldCache", new Dictionary <string, bool>() }
            };

            return(args);
        }
Beispiel #14
0
        public void Test_SyncIsCalled()
        {
            using (var context = ShimsContext.Create())
            {
                bool syncCalled = false;
                var  workItem   = new ShimWorkItem()
                {
                    SyncToLatest = () => { syncCalled = true; }
                };
                var args = new Dictionary <string, object>()
                {
                    { "WorkItem", (WorkItem)workItem }
                };
                WorkflowInvoker.Invoke(new RefreshWorkItem(), args);

                Assert.IsTrue(syncCalled);
            }
        }
Beispiel #15
0
        public void Can_use_Dsl_to_create_a_work_item()
        {
            using (ShimsContext.Create())
            {
                // arrange
                // redirect the console
                var consoleOut = Helpers.Logging.RedirectConsoleOut();

                var wi = new ShimWorkItem()
                {
                    TitleGet = () => "Title", IdGet = () => 99
                };
                var emailProvider = new Moq.Mock <IEmailProvider>();
                var tfsProvider   = new Moq.Mock <ITfsProvider>();
                tfsProvider.Setup(t => t.CreateWorkItem(
                                      "tp",
                                      "Bug",
                                      new Dictionary <string, object>()
                {
                    { "Title", "The Title" },
                    { "Estimate", 2 }
                })).Returns(wi);
                var engine = new TFSEventsProcessor.Dsl.DslProcessor();

                // act
                engine.RunScript(@"dsl\tfs\createwi.py", tfsProvider.Object, emailProvider.Object);

                // assert
                tfsProvider.Verify(t => t.CreateWorkItem("tp",
                                                         "Bug",
                                                         new Dictionary <string, object>()
                {
                    { "Title", "The Title" },
                    { "Estimate", 2 }
                }));

                Assert.AreEqual(
                    "Work item '99' has been created with the title 'Title'" + Environment.NewLine,
                    consoleOut.ToString());
            }
        }
        public void WorkItemTemplatePropertiesShouldBeAccessedAsNativeProperties()
        {
            var workItem = new ShimWorkItem();

            var fieldCollectionFake = new ShimFieldCollection();

            fieldCollectionFake.Bind(new Field[]
            {
                new ShimField()
                {
                    NameGet = () => "CustomField",
                    ValueGet = () => "CustomValue"
                }
            });

            workItem.FieldsGet = () => fieldCollectionFake;

            dynamic workItemWrapper = new WorkItemModel(workItem);

            Assert.AreEqual("CustomValue", workItemWrapper.CustomField);
        }
        /// <summary>
        /// Configures a query shim to simulate Run on TFS Query objects
        /// </summary>
        private void SetupQueryShim(FakeTfsContext tfsContext)
        {
            ShimQuery.ConstructorWorkItemStoreString = (q, ws, wi) =>
            {
                ShimQuery query = new ShimQuery();

                query.QueryStringGet = () => wi;

                q = query;
            };

            var workItem = new ShimWorkItem();
            workItem.IdGet = () => 5;
            workItem.TitleGet = () => "Linked Work Item";

            var workItemLinkInfo = new WorkItemLinkInfo();
            workItemLinkInfo.TargetId = 5;

            tfsContext.AddWorkItem(workItem);

            ShimWorkItemCollection collection = SetupWorkItemCollection(workItem);

            ShimQuery.AllInstances.IsLinkQueryGet = (q) => false;
            ShimQuery.AllInstances.RunQuery = (q) => collection;
            ShimQuery.AllInstances.RunLinkQuery = (q) => new List<WorkItemLinkInfo>() { workItemLinkInfo }.ToArray();
        }
		private WorkItem GetFakeWorkItem()
		{
			var wi = new ShimWorkItem()
				{
					IdGet = () => 2
				};
			return wi;
		}
		internal static ShimWorkItem CreateCodeReviewRequest(int id, string state, string closedStatus, List<WorkItem> responses)
		{
			var requestFields = new List<Field>()
				{
					new ShimField()
					{ 
						NameGet = () => CodeReviewPolicy.ClosedStatus,
						ValueGet = () => closedStatus
					}
				};
			var fakeRequestFields = new ShimFieldCollection()
			{
				ItemGetString = (s) => requestFields.SingleOrDefault(f => f.Name == s)
			};

			var links = new List<WorkItemLink>();
			foreach(var response in responses)
			{
				links.Add(new ShimWorkItemLink()
					{
						LinkTypeEndGet = () => new ShimWorkItemLinkTypeEnd()
						{
							NameGet = () => "Child",
						},
						TargetIdGet = () => response.Id
					});
			}
			var fakeLinks = new ShimWorkItemLinkCollection();
			fakeLinks.Bind(links);

			var fakeProject = new ShimProject()
			{
				StoreGet = () => new ShimWorkItemStore()
				{
					GetWorkItemInt32 = (i) => responses.SingleOrDefault(r => r.Id == i)
				}
			};

			var reviewWorkItem = new ShimWorkItem()
			{
				TypeGet = () => new ShimWorkItemType()
				{
					NameGet = () => "Code Review Request"
				},
				StateGet = () => state,
				FieldsGet = () => fakeRequestFields,
				WorkItemLinksGet = () => fakeLinks,
				ProjectGet = () => fakeProject
			};

			return reviewWorkItem;
		}
		internal static IPendingCheckin CreatePendingCheckin(ShimWorkItem item)
		{
			return CreatePendingCheckin(new List<ShimWorkItem>() { item });
		}
		private Dictionary<string, object> GetWorkflowArgs(bool containsIntegratedIn, bool isValid)
		{
			var workItem = new ShimWorkItem()
			{
				TypeGet = () => new ShimWorkItemType()
				{
					NameGet = () => "Task",
					FieldDefinitionsGet = () => new ShimFieldDefinitionCollection()
					{
						ContainsString = (s) => containsIntegratedIn
					}
				},
				PartialOpen = () => { },
				ItemSetStringObject = (s, o) => { if (!containsIntegratedIn) throw new ApplicationException("Should not be calling set IntegratedIn"); },
				HistorySetString = (s) => { if (!containsIntegratedIn) throw new ApplicationException("Should not be calling set History"); },
				IsValid = () => isValid,
				Validate = () => isValid ? new ArrayList() : new ArrayList(new List<Field>()
					{
						new ShimField() { NameGet = () => "Field1", ValueGet = () => "Err1" },
						new ShimField() { NameGet = () => "Field2", ValueGet = () => "Err2" } 
					})
			};
			var args = new Dictionary<string, object>()	{
						{ "WorkItem", (WorkItem)workItem },
						{ "BuildNumber", "123" },
						{ "WorkItemTypeFieldCache", new Dictionary<string, bool>() }
					};
			return args;
		}
        public void Can_use_Dsl_to_update_parent_work_item_when_all_children_done()
        {
            using (ShimsContext.Create())
            {
                // arrange
                var memLogger = Helpers.Logging.CreateMemoryTargetLogger(LogLevel.Debug);

                var emailProvider = new Moq.Mock <IEmailProvider>();
                var wi100         = new ShimWorkItem()
                {
                    TitleGet = () => "The child title",
                    IdGet    = () => 100,
                    StateGet = () => "Done"
                };
                var wi101 = new ShimWorkItem()
                {
                    TitleGet = () => "The child title",
                    IdGet    = () => 101,
                    StateGet = () => "Done"
                };
                var parent99 = new ShimWorkItem()
                {
                    TitleGet = () => "The parent title",
                    IdGet    = () => 99,
                    StateGet = () => "Committed"
                };

                var tfsProvider = new Moq.Mock <ITfsProvider>();
                tfsProvider.Setup(t => t.GetWorkItem(It.IsAny <int>())).Returns(wi100);
                tfsProvider.Setup(t => t.GetParentWorkItem(It.IsAny <WorkItem>())).Returns(parent99);
                tfsProvider.Setup(t => t.GetChildWorkItems(It.IsAny <WorkItem>()))
                .Returns(new WorkItem[] { wi100, wi101 });

                var engine = new TFSEventsProcessor.Dsl.DslProcessor();

                var args = new Dictionary <string, object> {
                    { "Arguments", new[] { "WorkItemEvent", "100" } }
                };

                // act
                engine.RunScript(
                    @"dsl\tfs\changeparentworkitemstate.py",
                    args,
                    tfsProvider.Object,
                    emailProvider.Object);

                // assert
                foreach (var line in memLogger.Logs)
                {
                    Console.WriteLine(line);
                }

                tfsProvider.Verify(t => t.UpdateWorkItem(parent99));
                emailProvider.Verify(
                    e =>
                    e.SendEmailAlert(
                        "richard@typhoontfs",
                        "Work item '99' has been updated",
                        "Work item '99' has been set as 'Done' as all its child work items are done"));
            }
        }