public void Setup()
		{
            ProjectStatusOnServer server = new ProjectStatusOnServer(new ProjectStatus("myProject", IntegrationStatus.Success, DateTime.Now),
                new DefaultServerSpecifier("myServer"));
            ProjectStatusListAndExceptions statusList = new ProjectStatusListAndExceptions(
                new ProjectStatusOnServer[] {
                    server
                }, new CruiseServerException[] {
                });

			farmServiceMock = new DynamicMock(typeof(IFarmService));
            farmServiceMock.SetupResult("GetProjectStatusListAndCaptureExceptions", statusList, typeof(IServerSpecifier), typeof(string));
			viewGeneratorMock = new DynamicMock(typeof(IVelocityViewGenerator));
			linkFactoryMock = new DynamicMock(typeof(ILinkFactory));
            ServerLocation serverConfig = new ServerLocation();
            serverConfig.ServerName = "myServer";
            configuration.Servers = new ServerLocation[] {
                serverConfig
            };
            var urlBuilderMock = new DynamicMock(typeof(ICruiseUrlBuilder));
            urlBuilderMock.SetupResult("BuildProjectUrl", string.Empty, typeof(string), typeof(IProjectSpecifier));

			plugin = new ProjectReportProjectPlugin((IFarmService) farmServiceMock.MockInstance,
				(IVelocityViewGenerator) viewGeneratorMock.MockInstance,
				(ILinkFactory) linkFactoryMock.MockInstance,
                configuration,
                (ICruiseUrlBuilder)urlBuilderMock.MockInstance);

			cruiseRequestMock = new DynamicMock(typeof(ICruiseRequest));
			cruiseRequest = (ICruiseRequest ) cruiseRequestMock.MockInstance;

		}
		private HtmlFragmentResponse GenerateView(ProjectStatusListAndExceptions projectStatusListAndExceptions,
            string actionName, ICruiseRequest request, IServerSpecifier serverSpecifier)
		{
            this.translations = Translations.RetrieveCurrent();
            cruiseUrlBuilder = request.UrlBuilder;
            urlBuilder = request.UrlBuilder.InnerBuilder;
			Hashtable velocityContext = new Hashtable();
            velocityContext["forceBuildMessage"] = ForceBuildIfNecessary(request.Request);
            velocityContext["parametersCall"] = new ServerLink(cruiseUrlBuilder, new DefaultServerSpecifier("null"), string.Empty, ProjectParametersAction.ActionName).Url;

			velocityContext["wholeFarm"] = serverSpecifier == null ?  true : false;

			string category = request.Request.GetText("Category");
			velocityContext["showCategoryColumn"] = string.IsNullOrEmpty(category) ? true : false;

			ProjectGridSortColumn sortColumn = GetSortColumn(request.Request);
			bool sortReverse = SortAscending(request.Request);

			velocityContext["projectNameSortLink"] = GenerateSortLink(serverSpecifier, actionName, ProjectGridSortColumn.Name, sortColumn, sortReverse);
			velocityContext["buildStatusSortLink"] = GenerateSortLink(serverSpecifier, actionName, ProjectGridSortColumn.BuildStatus, sortColumn, sortReverse);
			velocityContext["lastBuildDateSortLink"] = GenerateSortLink(serverSpecifier, actionName, ProjectGridSortColumn.LastBuildDate, sortColumn, sortReverse);
			velocityContext["serverNameSortLink"] = GenerateSortLink(serverSpecifier, actionName, ProjectGridSortColumn.ServerName, sortColumn, sortReverse);
			velocityContext["projectCategorySortLink"] = GenerateSortLink(serverSpecifier, actionName, ProjectGridSortColumn.Category, sortColumn, sortReverse);

            ProjectGridRow[] projectGridRows = projectGrid.GenerateProjectGridRows(projectStatusListAndExceptions.StatusAndServerList, actionName, sortColumn, sortReverse, category, cruiseUrlBuilder, this.translations);

            velocityContext["projectGrid"] = projectGridRows;
			velocityContext["exceptions"] = projectStatusListAndExceptions.Exceptions;

            Array categoryList = this.GenerateCategoryList(projectGridRows);
            velocityContext["categoryList"] = categoryList;
            velocityContext["barAtTop"] = (this.SuccessIndicatorBarLocation == IndicatorBarLocation.Top) ||
                (this.SuccessIndicatorBarLocation == IndicatorBarLocation.TopAndBottom);
            velocityContext["barAtBottom"] = (this.SuccessIndicatorBarLocation == IndicatorBarLocation.Bottom) ||
                (this.SuccessIndicatorBarLocation == IndicatorBarLocation.TopAndBottom);

			return viewGenerator.GenerateView(@"ProjectGrid.vm", velocityContext);
		}
        public void ExecuteGeneratesReport()
        {
            var projectName = "daProject";
            var farmService = this.mocks.StrictMock<IFarmService>();
            var cruiseRequest = this.mocks.StrictMock<ICruiseRequest>();
            var sessionRetriever = this.mocks.StrictMock<ISessionRetriever>();
            var server = this.mocks.StrictMock<IServerSpecifier>();
            var project = new ProjectStatus(projectName, IntegrationStatus.Success, new DateTime(2010, 1, 2, 3, 4, 5));
            project.ServerName = "TESTMACHINE";
            var status = new ProjectStatusOnServer(project, server);
            var snapshot = new ProjectStatusListAndExceptions(
                new ProjectStatusOnServer[] { status },
                new CruiseServerException[0]);
            SetupResult.For(cruiseRequest.ProjectName).Return(projectName);
            SetupResult.For(cruiseRequest.ServerSpecifier).Return(server);
            SetupResult.For(cruiseRequest.RetrieveSessionToken(sessionRetriever)).Return(null);
            SetupResult.For(farmService.GetProjectStatusListAndCaptureExceptions(server, null))
                .Return(snapshot);

            this.mocks.ReplayAll();
            var report = new ProjectXmlReport(farmService, sessionRetriever);
            var response = report.Execute(cruiseRequest);

            this.mocks.VerifyAll();
            Assert.IsInstanceOf<XmlFragmentResponse>(response);
            var actual = response as XmlFragmentResponse;
            var expected = "<CruiseControl>" +
                "<Projects>" +
                "<Project name=\"daProject\" category=\"\" activity=\"Sleeping\" " +
                    "status=\"Running\" lastBuildStatus=\"Success\" lastBuildLabel=\"\" " +
                    "lastBuildTime=\"" + XmlConvert.ToString(project.LastBuildDate, XmlDateTimeSerializationMode.Local) + 
                    "\" nextBuildTime=\"" + XmlConvert.ToString(project.NextBuildTime, XmlDateTimeSerializationMode.Local) + "\" " +
                    "webUrl=\"\" buildStage=\"\" serverName=\"TESTMACHINE\" />" +
                "</Projects>" +
                "<Queues />" +
                "</CruiseControl>";
            Assert.AreEqual(expected, actual.ResponseFragment);
        }
		public void ShouldGenerateFarmViewIfNoServerSpecified()
		{
			// Setup
			cruiseRequestWrapperMock.ExpectAndReturn("ServerName", "");
			pluginLinkCalculatorMock.ExpectAndReturn("GetFarmPluginLinks", links);
			farmServiceMock.ExpectAndReturn("GetServerSpecifiers", serverSpecifiers);            
			linkListFactoryMock.ExpectAndReturn("CreateServerLinkList", serverLinks, serverSpecifiers, "ViewServerReport");

            ProjectStatus ps = new ProjectStatus("", "", null, 0, 0, null, DateTime.Now, null, null, DateTime.Now, null, "", 0, new List<ParameterBase>());
            ProjectStatusOnServer[] psosa = new ProjectStatusOnServer[] { new ProjectStatusOnServer(ps, serverSpecifiers[0]) };
            ProjectStatusListAndExceptions pslae = new ProjectStatusListAndExceptions(psosa, new CruiseServerException[0]);
            farmServiceMock.ExpectAndReturn("GetProjectStatusListAndCaptureExceptions", pslae, serverSpecifiers[0], (string)null);

			velocityContext["links"] = links;
			velocityContext["serverlinks"] = serverLinks;

            velocityContext["showCategories"] = false;
            velocityContext["categorylinks"] = null;
            CruiseControl.WebDashboard.Dashboard.DefaultLinkFactory x = new DefaultLinkFactory(new DefaultUrlBuilder(),null,null);

            IAbsoluteLink farmLink = x.CreateFarmLink("Dashboard", FarmReportFarmPlugin.ACTION_NAME);
            linkFactoryMock.ExpectAndReturn("CreateFarmLink", farmLink, "Dashboard", FarmReportFarmPlugin.ACTION_NAME);
            velocityContext["farmLink"] = farmLink;

            System.Diagnostics.Debug.WriteLine("starting");

			velocityViewGeneratorMock.ExpectAndReturn("GenerateView", velocityResponse, @"FarmSideBar.vm", new HashtableConstraint(velocityContext));

			// Execute
            HtmlFragmentResponse returnedResponse = sideBarViewBuilder.Execute(cruiseRequestWrapperMock.MockInstance as ICruiseRequest);

			// Verify
			Assert.AreEqual(velocityResponse, returnedResponse);
			VerifyAll();
		}
		public void ShouldGenerateServerViewIfServerButNoProjectSpecified()
		{
			// Setup
			IServerSpecifier serverSpecifier = new DefaultServerSpecifier("myServer");
			cruiseRequestWrapperMock.ExpectAndReturn("ServerName", "myServer");
			cruiseRequestWrapperMock.ExpectAndReturn("ProjectName", "");
			cruiseRequestWrapperMock.ExpectAndReturn("ServerSpecifier", serverSpecifier);

			pluginLinkCalculatorMock.ExpectAndReturn("GetServerPluginLinks", links, serverSpecifier);

            ProjectStatus ps = new ProjectStatus("", "myCategory", null, 0, 0, null, DateTime.Now, null, null, DateTime.Now, null, "", 0, new List<ParameterBase>());
			ProjectStatusOnServer[] psosa = new ProjectStatusOnServer[] { new ProjectStatusOnServer(ps, serverSpecifier) };
			ProjectStatusListAndExceptions pslae = new ProjectStatusListAndExceptions(psosa, new CruiseServerException[0]);
			farmServiceMock.ExpectAndReturn("GetProjectStatusListAndCaptureExceptions", pslae, serverSpecifier, (string)null);

			IAbsoluteLink link = new GeneralAbsoluteLink("link");
			IAbsoluteLink[] categoryLinks = new GeneralAbsoluteLink[] { new GeneralAbsoluteLink("myCategory", "?Category=myCategory") };
			linkFactoryMock.ExpectAndReturn("CreateServerLink", link, serverSpecifier, ServerReportServerPlugin.ACTION_NAME);
			linkFactoryMock.ExpectAndReturn("CreateServerLink", link, serverSpecifier, ServerReportServerPlugin.ACTION_NAME);

			velocityContext["links"] = links;
			velocityContext["serverlink"] = link;
			velocityContext["showCategories"] = true;
			velocityContext["categorylinks"] = categoryLinks;

			velocityViewGeneratorMock.ExpectAndReturn("GenerateView", velocityResponse, @"ServerSideBar.vm", new HashtableConstraint(velocityContext));

			// Execute
            HtmlFragmentResponse returnedResponse = sideBarViewBuilder.Execute(cruiseRequestWrapperMock.MockInstance as ICruiseRequest);

			// Verify
			Assert.AreEqual(velocityResponse, returnedResponse);
			VerifyAll();
		}
        private HtmlFragmentResponse GenerateView(ProjectStatusListAndExceptions projectStatusListAndExceptions, Hashtable velocityContext, string actionName, IRequest request, IServerSpecifier serverSpecifier)
        {
            ProjectGridSortColumn sortColumn = GetSortColumn(request);
            bool sortReverse = SortAscending(request);

            velocityContext["projectNameSortLink"] = GenerateSortLink(serverSpecifier, actionName, ProjectGridSortColumn.Name, sortColumn, sortReverse);
            velocityContext["buildStatusSortLink"] = GenerateSortLink(serverSpecifier, actionName, ProjectGridSortColumn.BuildStatus, sortColumn, sortReverse);
            velocityContext["lastBuildDateSortLink"] = GenerateSortLink(serverSpecifier, actionName, ProjectGridSortColumn.LastBuildDate, sortColumn, sortReverse);
            velocityContext["projectGrid"] = projectGrid.GenerateProjectGridRows(
                farmService, projectStatusListAndExceptions.StatusAndServerList, actionName, sortColumn, sortReverse);
            velocityContext["serverTimeNow"] = DateTime.Now;
            velocityContext["exceptions"] = projectStatusListAndExceptions.Exceptions;
            velocityContext["refreshButtonName"] = urlBuilder.BuildFormName(actionName);

            return viewGenerator.GenerateView(@"ProjectGrid.vm", velocityContext);
        }
		public void ShouldGenerateFarmServerProjectAndBuildLinksIfServerProjectAndBuildSpecified()
		{
			// Setup
			cruiseRequestMock.ExpectAndReturn("ServerName", "myServer");
			cruiseRequestMock.ExpectAndReturn("ServerName", "myServer");
			cruiseRequestMock.ExpectAndReturn("ProjectName", "myProject");
			cruiseRequestMock.ExpectAndReturn("ProjectName", "myProject");
			cruiseRequestMock.ExpectAndReturn("ProjectName", "myProject");
			cruiseRequestMock.ExpectAndReturn("BuildName", "myBuild");
			cruiseRequestMock.ExpectAndReturn("ServerSpecifier", serverSpecifier);
			cruiseRequestMock.ExpectAndReturn("ServerSpecifier", serverSpecifier);
			cruiseRequestMock.ExpectAndReturn("ProjectSpecifier", projectSpecifier);
			cruiseRequestMock.ExpectAndReturn("BuildSpecifier", buildSpecifier);
			cruiseRequestMock.ExpectAndReturn("Request", requestMock.MockInstance);
			requestMock.ExpectAndReturn("GetText", "", new object[] { "Category" });

            ProjectStatus ps = new ProjectStatus("myProject", "", null, 0, 0, null, DateTime.Now, null, null, DateTime.Now, null, "Queue 1", 1, new List<ParameterBase>());
			ProjectStatusOnServer[] psosa = new ProjectStatusOnServer[] { new ProjectStatusOnServer(ps, serverSpecifier) };
			ProjectStatusListAndExceptions pslae = new ProjectStatusListAndExceptions(psosa, new CruiseServerException[0]);
			farmServiceMock.ExpectAndReturn("GetProjectStatusListAndCaptureExceptions", pslae, serverSpecifier, null);

			expectedVelocityContext["serverName"] = "myServer";
			expectedVelocityContext["categoryName"] = "";
			expectedVelocityContext["projectName"] = "myProject";
			expectedVelocityContext["buildName"] = "myBuild";

			linkFactoryMock.ExpectAndReturn("CreateFarmLink", link1, "Dashboard", FarmReportFarmPlugin.ACTION_NAME);
			linkFactoryMock.ExpectAndReturn("CreateServerLink", link2, serverSpecifier, ServerReportServerPlugin.ACTION_NAME);
			linkFactoryMock.ExpectAndReturn("CreateProjectLink", link3, projectSpecifier, ProjectReportProjectPlugin.ACTION_NAME);
			linkFactoryMock.ExpectAndReturn("CreateBuildLink", link4, buildSpecifier, BuildReportBuildPlugin.ACTION_NAME);
			expectedVelocityContext["farmLink"] = link1;
			expectedVelocityContext["serverLink"] = link2;
			expectedVelocityContext["projectLink"] = link3;
			expectedVelocityContext["buildLink"] = link4;

			velocityViewGeneratorMock.ExpectAndReturn("GenerateView", response, "TopMenu.vm", new HashtableConstraint(expectedVelocityContext));

			// Execute & Verify
			Assert.AreEqual(response, viewBuilder.Execute());
			VerifyAll();
		}