Permalink
Browse files

Implement the MockRequestProxy and switch the tests over to use it

Should have a bit more reliable of tests now that I'm explicitly
grabbing the data from Hudson's API and saving it into the test itself

Idea gleaned from: http://martinfowler.com/bliki/SelfInitializingFake.html
  • Loading branch information...
1 parent 1f5bf34 commit 0c542699560c9e06763b6010d1b73badd3599041 @rtyler committed Sep 6, 2009
Showing with 114 additions and 2 deletions.
  1. +46 −0 Tests/Api.cs
  2. +64 −0 Tests/Base.cs
  3. +4 −2 Tests/RequestProxyTest.cs
View
@@ -11,6 +11,7 @@ public class ApiBase : Hudson.Tests.TestBase
{
#region "Member Variables"
protected Api api = null;
+ protected MockRequestProxy requestProxy = null;
#endregion
[SetUp]
@@ -28,18 +29,40 @@ public class ApiConstructorTests : Hudson.Tests.TestBase
public void BasicConstructor()
{
Api api = new Api();
+
+ Assert.IsNotNull(api);
}
[Test]
public void ParameterConstructor()
{
Api api = new Api("localhost", 8080);
+
+ Assert.IsNotNull(api);
+ }
+
+ [Test]
+ public void RequestProxyConstructor()
+ {
+ Api api = new Api(new Hudson.Internal.RequestProxy());
+
+ Assert.IsNotNull(api);
}
}
[TestFixture]
public class FetchProjectsTest : ApiBase
{
+ [SetUp]
+ public virtual void SetUp()
+ {
+ Dictionary<string, string> testData = new Dictionary<string, string>();
+ testData.Add("/api/json", "{\"assignedLabels\":[{}],\"mode\":\"NORMAL\",\"nodeDescription\":\"the master Hudson node\",\"nodeName\":\"\",\"numExecutors\":2,\"description\":null,\"jobs\":[{\"name\":\"Downstream\",\"url\":\"http://localhost:8080/job/Downstream/\",\"color\":\"blue\"},{\"name\":\"Hudson.NET\",\"url\":\"http://localhost:8080/job/Hudson.NET/\",\"color\":\"blue\"},{\"name\":\"Sleeper\",\"url\":\"http://localhost:8080/job/Sleeper/\",\"color\":\"blue\"}],\"primaryView\":{\"name\":\"All\",\"url\":\"http://localhost:8080/\"},\"slaveAgentPort\":0,\"useCrumbs\":false,\"useSecurity\":false,\"views\":[{\"name\":\"All\",\"url\":\"http://localhost:8080/\"},{\"name\":\"TestView\",\"url\":\"http://localhost:8080/view/TestView/\"}]}");
+
+ this.requestProxy = new Hudson.Tests.MockRequestProxy(testData);
+ this.api = new Api(this.requestProxy);
+ }
+
[Test]
public void SynchronousFetch()
{
@@ -53,6 +76,18 @@ public void SynchronousFetch()
[TestFixture]
public class FetchJobsTest : ApiBase
{
+ [SetUp]
+ public virtual void SetUp()
+ {
+ Dictionary<string, string> testData = new Dictionary<string, string>();
+ testData.Add("/api/json", "{\"assignedLabels\":[{}],\"mode\":\"NORMAL\",\"nodeDescription\":\"the master Hudson node\",\"nodeName\":\"\",\"numExecutors\":2,\"description\":null,\"jobs\":[{\"name\":\"Hudson.NET\",\"url\":\"http://localhost:8080/job/Hudson.NET/\",\"color\":\"blue\"}],\"primaryView\":{\"name\":\"All\",\"url\":\"http://localhost:8080/\"},\"slaveAgentPort\":0,\"useCrumbs\":false,\"useSecurity\":false,\"views\":[{\"name\":\"All\",\"url\":\"http://localhost:8080/\"},{\"name\":\"TestView\",\"url\":\"http://localhost:8080/view/TestView/\"}]}");
+ testData.Add("/job/Hudson.NET/api/json",
+ "{\"actions\":[{},{}],\"description\":\"Hudson.NET self-building project\",\"displayName\":\"Hudson.NET\",\"name\":\"Hudson.NET\",\"url\":\"http://localhost:8080/job/Hudson.NET/\",\"buildable\":true,\"builds\":[{\"number\":1,\"url\":\"http://localhost:8080/job/Hudson.NET/1/\"}],\"color\":\"blue\",\"firstBuild\":{\"number\":1,\"url\":\"http://localhost:8080/job/Hudson.NET/1/\"},\"healthReport\":[{\"description\":\"Test Result: 0 tests failing out of a total of 23 tests.\",\"iconUrl\":\"health-80plus.gif\",\"score\":100},{\"description\":\"Build stability: No recent builds failed.\",\"iconUrl\":\"health-80plus.gif\",\"score\":100}],\"inQueue\":false,\"keepDependencies\":false,\"lastBuild\":{\"number\":159,\"url\":\"http://localhost:8080/job/Hudson.NET/159/\"},\"lastCompletedBuild\":{\"number\":159,\"url\":\"http://localhost:8080/job/Hudson.NET/159/\"},\"lastFailedBuild\":{\"number\":149,\"url\":\"http://localhost:8080/job/Hudson.NET/149/\"},\"lastStableBuild\":{\"number\":159,\"url\":\"http://localhost:8080/job/Hudson.NET/159/\"},\"lastSuccessfulBuild\":{\"number\":159,\"url\":\"http://localhost:8080/job/Hudson.NET/159/\"},\"nextBuildNumber\":160,\"property\":[],\"queueItem\":null,\"downstreamProjects\":[],\"upstreamProjects\":[]}");
+
+ this.requestProxy = new Hudson.Tests.MockRequestProxy(testData);
+ this.api = new Api(this.requestProxy);
+ }
+
[Test]
public void SynchronousFetch()
{
@@ -71,6 +106,17 @@ public void SynchronousFetch()
[TestFixture]
public class FetchJobTest : ApiBase
{
+ [SetUp]
+ public virtual void SetUp()
+ {
+ Dictionary<string, string> testData = new Dictionary<string, string>();
+ testData.Add("/job/Hudson.NET/api/json",
+ "{\"actions\":[{},{}],\"description\":\"Hudson.NET self-building project\",\"displayName\":\"Hudson.NET\",\"name\":\"Hudson.NET\",\"url\":\"http://localhost:8080/job/Hudson.NET/\",\"buildable\":true,\"builds\":[{\"number\":1,\"url\":\"http://localhost:8080/job/Hudson.NET/1/\"}],\"color\":\"blue\",\"firstBuild\":{\"number\":1,\"url\":\"http://localhost:8080/job/Hudson.NET/1/\"},\"healthReport\":[{\"description\":\"Test Result: 0 tests failing out of a total of 23 tests.\",\"iconUrl\":\"health-80plus.gif\",\"score\":100},{\"description\":\"Build stability: No recent builds failed.\",\"iconUrl\":\"health-80plus.gif\",\"score\":100}],\"inQueue\":false,\"keepDependencies\":false,\"lastBuild\":{\"number\":159,\"url\":\"http://localhost:8080/job/Hudson.NET/159/\"},\"lastCompletedBuild\":{\"number\":159,\"url\":\"http://localhost:8080/job/Hudson.NET/159/\"},\"lastFailedBuild\":{\"number\":149,\"url\":\"http://localhost:8080/job/Hudson.NET/149/\"},\"lastStableBuild\":{\"number\":159,\"url\":\"http://localhost:8080/job/Hudson.NET/159/\"},\"lastSuccessfulBuild\":{\"number\":159,\"url\":\"http://localhost:8080/job/Hudson.NET/159/\"},\"nextBuildNumber\":160,\"property\":[],\"queueItem\":null,\"downstreamProjects\":[],\"upstreamProjects\":[]}");
+
+ this.requestProxy = new Hudson.Tests.MockRequestProxy(testData);
+ this.api = new Api(this.requestProxy);
+ }
+
[Test]
public void SynchronousFetch()
{
View
@@ -19,4 +19,68 @@ public void SetUp()
this.json = new JavaScriptSerializer();
}
}
+
+ public class BadMockRequestException : Exception
+ {
+ public BadMockRequestException() : base() {}
+ public BadMockRequestException(string message) : base(message) {}
+ }
+
+ public class MockRequestProxy : Hudson.Internal.IRequestProxy
+ {
+ protected bool shouldUseSSL = false;
+ protected JavaScriptSerializer json = null;
+ protected Dictionary<string, string> testData = null;
+
+ #region "Public Constructors"
+ public MockRequestProxy(Dictionary<string, string> testData)
+ {
+ this.testData = testData;
+ this.json = new JavaScriptSerializer();
+ }
+ #endregion
+
+
+ #region "Public Properties"
+ public string ProtocolPrefix
+ {
+ get { return "mock"; }
+ }
+
+ public int Port
+ {
+ get { return 666; }
+ }
+
+ public bool useSSL
+ {
+ get { return this.shouldUseSSL; }
+ set { this.shouldUseSSL = value; }
+ }
+ #endregion
+
+ #region "Public Methods"
+ public T Execute<T>(string endPoint)
+ {
+ if (this.testData.ContainsKey(endPoint) == false)
+ {
+ throw new BadMockRequestException(String.Format(
+ "I don't have any data for {0}", endPoint));
+ }
+ Console.WriteLine(this.testData[endPoint]);
+ return this.json.Deserialize<T>(this.testData[endPoint]);
+ }
+
+ public Dictionary<string, object> Execute(string endPoint)
+ {
+ if (this.testData.ContainsKey(endPoint) == false)
+ {
+ throw new BadMockRequestException(String.Format(
+ "I don't have any data for {0}", endPoint));
+ }
+ return this.json.DeserializeObject(this.testData[endPoint])
+ as Dictionary<string, object>;
+ }
+ #endregion
+ }
}
@@ -91,7 +91,8 @@ public void ExecuteToDownServer()
}
}
- [Test]
+ // Disabling this test for now, until I have a good Hudson test runner
+ //[Test]
public void ExecuteSimpleRequest()
{
RequestProxy req = new RequestProxy("localhost");
@@ -106,7 +107,8 @@ public void ExecuteSimpleRequest()
Assert.IsTrue(response.ContainsKey("jobs"), "Response doesn't contain 'jobs'");
}
- [Test]
+ // Disabling this test for now, until I have a good Hudson test runner
+ //[Test]
public void GenericsExecuteJobTest()
{
RequestProxy req = new RequestProxy("localhost");

0 comments on commit 0c54269

Please sign in to comment.