Permalink
Browse files

Implemented logic for executing of Couch Commands using a RestClient.…

… Right now we are only handling POST operation commands. Will need to add more for the other command types.
  • Loading branch information...
1 parent 595deaa commit 26259bd44fbc7b6eaca79a6cd2b5b3a5dcebe2ad @dragan committed Aug 7, 2010
@@ -1,11 +1,81 @@
using System;
+using NSubstitute;
+using NUnit.Framework;
+using SineSignal.Ottoman.Specs.Framework;
+
+using SineSignal.Ottoman.Commands;
+using SineSignal.Ottoman.Http;
+
namespace SineSignal.Ottoman.Specs
{
public class CouchProxySpecs
{
- public CouchProxySpecs()
+ public class When_executing_a_post_couch_command : ConcernFor<CouchProxy>
+ {
+ private Employee entity1;
+ private IRestClient restClient;
+ private ICouchCommand couchCommand;
+ private ResultStub resultStub;
+ private RestResponse<ResultStub> restResponse;
+
+ protected override void Given()
+ {
+ entity1 = new Employee { Name = "Bob", Login = "boblogin" };
+ restClient = Fake<IRestClient>();
+ couchCommand = Fake<ICouchCommand>();
+ couchCommand.Route.Returns("some/path");
+ couchCommand.Operation.Returns(HttpMethod.Post);
+ couchCommand.Message.Returns(entity1);
+
+ restResponse = new RestResponse<ResultStub>
+ {
+ ContentType = "application/json",
+ ContentLength = 5,
+ Content = "{\"status\":\"completed\"}",
+ StatusCode = System.Net.HttpStatusCode.Created,
+ StatusDescription = System.Net.HttpStatusCode.Created.ToString(),
+ ContentDeserialized = new ResultStub()
+ };
+
+ restClient.Process<ResultStub>(Arg.Any<RestRequest>()).Returns(restResponse);
+ }
+
+ public override CouchProxy CreateSystemUnderTest()
+ {
+ return new CouchProxy(restClient);
+ }
+
+ protected override void When()
+ {
+ resultStub = Sut.Execute<ResultStub>(couchCommand);
+ }
+
+ [Test]
+ public void Should_call_process_on_rest_client_with_generated_rest_request()
+ {
+ restClient.Received().Process<ResultStub>(Arg.Is<RestRequest>(r => {
+ return r.Path == "some/path" &&
+ r.Method == HttpMethod.Post &&
+ (r.Payload is Employee && r.Payload == entity1);
+ }));
+ }
+
+ [Test]
+ public void Should_return_deserialized_object()
+ {
+ Assert.That(resultStub.Status, Is.EqualTo("completed"));
+ }
+ }
+
+ public class ResultStub
{
+ public string Status { get; private set; }
+
+ public ResultStub()
+ {
+ Status = "completed";
+ }
}
}
}
@@ -0,0 +1,29 @@
+using SineSignal.Ottoman.Commands;
+using SineSignal.Ottoman.Http;
+
+namespace SineSignal.Ottoman
+{
+ public class CouchProxy : ICouchProxy
+ {
+ public IRestClient RestClient { get; private set; }
+
+ public CouchProxy(IRestClient restClient)
+ {
+ RestClient = restClient;
+ }
+
+ public TResult Execute<TResult>(ICouchCommand couchCommand)
+ {
+ var restRequest = CreateRestRequestFrom(couchCommand);
+
+ RestResponse<TResult> restResponse = RestClient.Process<TResult>(restRequest);
+
+ return restResponse.ContentDeserialized;
+ }
+
+ private RestRequest CreateRestRequestFrom(ICouchCommand couchCommand)
+ {
+ return new RestRequest { Path = couchCommand.Route, Method = couchCommand.Operation, Payload = couchCommand.Message };
+ }
+ }
+}
@@ -0,0 +1,7 @@
+namespace SineSignal.Ottoman.Http
+{
+ public interface IRestClient
+ {
+ RestResponse<T> Process<T>(RestRequest restRequest);
+ }
+}
@@ -0,0 +1,9 @@
+namespace SineSignal.Ottoman.Http
+{
+ public class RestRequest
+ {
+ public string Path { get; set; }
+ public string Method { get; set; }
+ public object Payload { get; set; }
+ }
+}
@@ -0,0 +1,13 @@
+namespace SineSignal.Ottoman.Http
+{
+ public class RestResponse<T>
+ {
+ public RestRequest RestRequest { get; set; }
+ public string ContentType { get; set; }
+ public long ContentLength { get; set; }
+ public string Content { get; set; }
+ public System.Net.HttpStatusCode StatusCode { get; set; }
+ public string StatusDescription { get; set; }
+ public T ContentDeserialized { get; set; }
+ }
+}
@@ -46,6 +46,10 @@
<Compile Include="ICouchDatabase.cs" />
<Compile Include="ICouchDocumentSession.cs" />
<Compile Include="Commands\BulkDocsCommand.cs" />
+ <Compile Include="Http\RestRequest.cs" />
+ <Compile Include="Http\RestResponse.cs" />
+ <Compile Include="Http\IRestClient.cs" />
+ <Compile Include="CouchProxy.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />

0 comments on commit 26259bd

Please sign in to comment.