XmlServerReportActionTest.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » UnitTests » WebDashboard » Plugins » FarmReport » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » UnitTests » WebDashboard » Plugins » FarmReport » XmlServerReportActionTest.cs
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using NMock;
using NUnit.Framework;
using ThoughtWorks.CruiseControl.Remote;
using ThoughtWorks.CruiseControl.UnitTests.UnitTestUtils;
using ThoughtWorks.CruiseControl.WebDashboard.MVC;
using ThoughtWorks.CruiseControl.WebDashboard.Plugins.FarmReport;
using ThoughtWorks.CruiseControl.WebDashboard.ServerConnection;

namespace ThoughtWorks.CruiseControl.UnitTests.WebDashboard.Plugins.FarmReport{
  [TestFixture]
  public class XmlServerReportActionTest
  {
    private DynamicMock mockFarmService;
        private XmlServerReportAction reportAction;

    private readonly DateTime LastBuildTime = new DateTime(2005, 7, 1, 12, 12, 12);
    private readonly DateTime NextBuildTime = new DateTime(2005, 7, 2, 13, 13, 13);

    [SetUp]
    public void SetUp()
    {
      mockFarmService = new DynamicMock(typeof (IFarmService));
            reportAction = new XmlServerReportAction((IFarmService)mockFarmService.MockInstance);
    }

    [Test]
    public void ReturnsAnXmlResponse()
    {
            mockFarmService.ExpectAndReturn("GetCruiseServerSnapshotListAndExceptions",
                                      new CruiseServerSnapshotListAndExceptions(new CruiseServerSnapshotOnServer[0], new CruiseServerException[0]),
                                            (string)null);

      IResponse response = reportAction.Execute(null);
      Assert.IsNotNull(response);
      Assert.AreEqual(typeof (XmlFragmentResponse), response.GetType());

      mockFarmService.Verify();
    }

    [Test]
    public void WhenNoCruiseServerSnapshotEntriesAreReturnedByTheFarmServiceTheXmlContainsJustRootNodes()
    {
            mockFarmService.ExpectAndReturn("GetCruiseServerSnapshotListAndExceptions",
                                      new CruiseServerSnapshotListAndExceptions(new CruiseServerSnapshotOnServer[0], new CruiseServerException[0]),
                                            (string)null);
      XmlFragmentResponse response = (XmlFragmentResponse) reportAction.Execute(null);
      string xml = response.ResponseFragment;

            Assert.AreEqual("<CruiseControl><Projects /><Queues /></CruiseControl>", xml);

      mockFarmService.Verify();
    }

    [Test]
    public void WhenOneCruiseServerSnapshotIsReturnedThisIsContainedInTheReturnedXml()
    {
      CruiseServerSnapshot cruiseServerSnapshot = CreateCruiseServerSnapshot();

      CruiseServerSnapshotOnServer cruiseServerSnapshotOnServer = new CruiseServerSnapshotOnServer(cruiseServerSnapshot, null);
            mockFarmService.ExpectAndReturn("GetCruiseServerSnapshotListAndExceptions",
                                      new CruiseServerSnapshotListAndExceptions(new CruiseServerSnapshotOnServer[] {cruiseServerSnapshotOnServer}, new CruiseServerException[0]),
                                            (string)null);

      XmlFragmentResponse response = (XmlFragmentResponse) reportAction.Execute(null);
      string xml = response.ResponseFragment;

            // cannot just compare the xml string, since we correctly expect the string to vary based on the
      // timezone in which this code is executing
      XmlDocument doc = XPathAssert.LoadAsDocument(xml);
            XPathAssert.Matches(doc, "/CruiseControl/Projects/Project/@name", "HelloWorld");
            XPathAssert.Matches(doc, "/CruiseControl/Projects/Project/@activity", "Sleeping");
            XPathAssert.Matches(doc, "/CruiseControl/Projects/Project/@lastBuildStatus", "Success");
            XPathAssert.Matches(doc, "/CruiseControl/Projects/Project/@lastBuildLabel", "build_7");
            XPathAssert.Matches(doc, "/CruiseControl/Projects/Project/@lastBuildTime", LastBuildTime);
            XPathAssert.Matches(doc, "/CruiseControl/Projects/Project/@nextBuildTime", NextBuildTime);
            XPathAssert.Matches(doc, "/CruiseControl/Projects/Project/@webUrl", "http://blah");
            XPathAssert.Matches(doc, "/CruiseControl/Projects/Project/@category", "category");
            XPathAssert.Matches(doc, "/CruiseControl/Projects/Project/@serverName", Environment.MachineName);

            XPathAssert.Matches(doc, "/CruiseControl/Queues/Queue/@name", "Queue1");
            XPathAssert.Matches(doc, "/CruiseControl/Queues/Queue/Request/@projectName", "HelloWorld");
            XPathAssert.Matches(doc, "/CruiseControl/Queues/Queue/Request/@activity", "CheckingModifications");

      mockFarmService.Verify();
    }
        
    [Test]
    public void ReturnedXmlValidatesAgainstSchema()
    {
      CruiseServerSnapshot cruiseServerSnapshot = CreateCruiseServerSnapshot();

      CruiseServerSnapshotOnServer cruiseServerSnapshotOnServer = new CruiseServerSnapshotOnServer(cruiseServerSnapshot, null);
            mockFarmService.ExpectAndReturn("GetCruiseServerSnapshotListAndExceptions",
                                      new CruiseServerSnapshotListAndExceptions(new CruiseServerSnapshotOnServer[] {cruiseServerSnapshotOnServer}, new CruiseServerException[0]),
                                            (string)null);

      XmlFragmentResponse response = (XmlFragmentResponse) reportAction.Execute(null);
      string xml = response.ResponseFragment;

            XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
            xmlReaderSettings.Schemas.Add(ReadSchemaFromResources("XmlServerReportActionSchema.xsd"));
            XmlReader rdr = XmlReader.Create(new StringReader(xml), xmlReaderSettings);
      while (rdr.Read())
      {
      }

      mockFarmService.Verify();
    }

    private CruiseServerSnapshot CreateCruiseServerSnapshot()
    {
        ProjectStatus[] projectStatuses = new ProjectStatus[]
            {
                new ProjectStatus("HelloWorld", "category", ProjectActivity.Sleeping, IntegrationStatus.Success,
                                  ProjectIntegratorState.Running,
                                  "http://blah", LastBuildTime, "build_8", "build_7",
                                  NextBuildTime,"", "", 0)
            };
            QueueSetSnapshot snapshot = new QueueSetSnapshot();
            snapshot.Queues.Add(new QueueSnapshot("Queue1"));
            snapshot.Queues[0].Requests.Add(new QueuedRequestSnapshot("HelloWorld", ProjectActivity.CheckingModifications));

            return new CruiseServerSnapshot(projectStatuses, snapshot);
    }

    private XmlSchema ReadSchemaFromResources(string filename)
    {
      using (Stream s = ResourceUtil.LoadResource(GetType(), filename))
      {
        return XmlSchema.Read(s, null);
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.