Skip to content

Commit

Permalink
Merge branch 'release/v0.4.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Nov 28, 2019
2 parents 4b04045 + ad75b6a commit 2d15cfb
Show file tree
Hide file tree
Showing 39 changed files with 614 additions and 249 deletions.
2 changes: 2 additions & 0 deletions ResgateIO.Service.UnitTests/BaseHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void EnabledHandlers_AuthHandler_IsAuth()
Assert.Equal(HandlerTypes.Auth, handler.EnabledHandlers);
}

#pragma warning disable 0618
class EnabledHandlers_NewHandler_IsNew_Class : BaseHandler
{
public void New(INewRequest r) { }
Expand All @@ -70,6 +71,7 @@ public void EnabledHandlers_NewHandler_IsNew()
var handler = new EnabledHandlers_NewHandler_IsNew_Class();
Assert.Equal(HandlerTypes.New, handler.EnabledHandlers);
}
#pragma warning restore 0618

#region ApplyHandler
class ApplyHandler_ApplyChangeHandler_IsCalled_Class : BaseHandler
Expand Down
2 changes: 2 additions & 0 deletions ResgateIO.Service.UnitTests/DynamicHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ public async Task AuthMethod_MethodWithoutHandler_CallsMethodNotFound()
Assert.Equal("MethodNotFound", mock.Calls[0].Method);
}

#pragma warning disable 0618
[Fact]
public async Task New_WithHandler_IsCalled()
{
Expand Down Expand Up @@ -378,6 +379,7 @@ public async Task New_CallHandler_IsNotCalled()
Assert.Equal(1, newCalled);
Assert.Equal(0, callCalled);
}
#pragma warning restore 0618

[Fact]
public async Task ApplyChange_WithHandler_IsCalled()
Expand Down
24 changes: 24 additions & 0 deletions ResgateIO.Service.UnitTests/IAuthRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ public void Ok_WithNullResult_SendsNullResponse()
.AssertResult(null);
}

[Fact]
public void Resource_WithValidResourceID_SendsResourceResponse()
{
Service.AddHandler("model", new DynamicHandler().Auth(r => r.Resource("test.model")));
Service.Serve(Conn);
Conn.GetMsg().AssertSubject("system.reset");
string inbox = Conn.NATSRequest("auth.test.model.method", Test.Request);
Conn.GetMsg()
.AssertSubject(inbox)
.AssertResource("test.model");
}

[Fact]
public void Resource_WithInvalidResourceID_ThrowsArgumentException()
{
Service.AddHandler("model", new DynamicHandler().Auth(r => r.Resource("test..model")));
Service.Serve(Conn);
Conn.GetMsg().AssertSubject("system.reset");
string inbox = Conn.NATSRequest("auth.test.model.method", Test.Request);
Conn.GetMsg()
.AssertSubject(inbox)
.AssertError(ResError.CodeInternalError);
}

[Fact]
public void Error_SendsErrorResponse()
{
Expand Down
24 changes: 24 additions & 0 deletions ResgateIO.Service.UnitTests/ICallRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ public void Ok_WithNullResult_SendsNullResponse()
.AssertResult(null);
}

[Fact]
public void Resource_WithValidResourceID_SendsResourceResponse()
{
Service.AddHandler("model", new DynamicHandler().Call(r => r.Resource("test.model")));
Service.Serve(Conn);
Conn.GetMsg().AssertSubject("system.reset");
string inbox = Conn.NATSRequest("call.test.model.method", Test.Request);
Conn.GetMsg()
.AssertSubject(inbox)
.AssertResource("test.model");
}

[Fact]
public void Resource_WithInvalidResourceID_ThrowsArgumentException()
{
Service.AddHandler("model", new DynamicHandler().Call(r => r.Resource("test..model")));
Service.Serve(Conn);
Conn.GetMsg().AssertSubject("system.reset");
string inbox = Conn.NATSRequest("call.test.model.method", Test.Request);
Conn.GetMsg()
.AssertSubject(inbox)
.AssertError(ResError.CodeInternalError);
}

[Fact]
public void Error_SendsErrorResponse()
{
Expand Down
2 changes: 2 additions & 0 deletions ResgateIO.Service.UnitTests/INewRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Xunit;
using Xunit.Abstractions;

#pragma warning disable 618
namespace ResgateIO.Service.UnitTests
{
public class INewRequestTests : TestsBase
Expand Down Expand Up @@ -315,3 +316,4 @@ public void Timeout_WithNegativeDuration_ThrowsException()
}
}
}
#pragma warning restore 0618
50 changes: 50 additions & 0 deletions ResgateIO.Service.UnitTests/IResourceContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1895,5 +1895,55 @@ public void ResetEvent_UsingWith_SendsSystemResetEvent()
.AssertPayload(new { resources = new[] { "test.model" } });
}
#endregion

#region CloneWithQuery
[Fact]
public void CloneWithQuery_UsingRequest_ReturnsClone()
{
Service.AddHandler("model.$id", new DynamicHandler().Call(r =>
{
var clone = r.CloneWithQuery("foo=bar");
Assert.NotSame(r, clone);
Assert.Equal(r.Service, clone.Service);
Assert.Equal(r.ResourceName, clone.ResourceName);
Assert.Equal(r.Handler, clone.Handler);
Assert.Same(r.PathParams, clone.PathParams);
Assert.Equal("", r.Query);
Assert.Equal("foo=bar", clone.Query);
Assert.Equal(r.Group, clone.Group);
Assert.NotSame(r.Items, clone.Items);
r.Ok();
}));
Service.Serve(Conn);
Conn.GetMsg().AssertSubject("system.reset");
string inbox = Conn.NATSRequest("call.test.model.42.method", Test.Request);
Conn.GetMsg()
.AssertSubject(inbox)
.AssertResult(null);
}

[Fact]
public void CloneWithQuery_UsingWith_ReturnsClone()
{
AutoResetEvent ev = new AutoResetEvent(false);
Service.AddHandler("model.$id", new DynamicHandler());
Service.Serve(Conn);
Service.With("test.model.42", r =>
{
var clone = r.CloneWithQuery("foo=bar");
Assert.NotSame(r, clone);
Assert.Equal(r.Service, clone.Service);
Assert.Equal(r.ResourceName, clone.ResourceName);
Assert.Same(r.Handler, clone.Handler);
Assert.Same(r.PathParams, clone.PathParams);
Assert.Equal("", r.Query);
Assert.Equal("foo=bar", clone.Query);
Assert.Equal(r.Group, clone.Group);
Assert.NotSame(r.Items, clone.Items);
ev.Set();
});
Assert.True(ev.WaitOne(Test.TimeoutDuration), "callback was not called before timeout");
}
#endregion
}
}
42 changes: 32 additions & 10 deletions ResgateIO.Service.UnitTests/QueryEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,8 @@ public static IEnumerable<object[]> GetQueryRequestTestData()
"foo=error_after_event",
(Action<IQueryRequest>)(r =>
{
if (r != null)
{
r.ChangeEvent(new Dictionary<string, object>{ { "foo", "bar" } });
r.Error(Test.CustomError);
}
r?.ChangeEvent(new Dictionary<string, object>{ { "foo", "bar" } });
r?.Error(Test.CustomError);
}),
Test.CustomError
};
Expand All @@ -201,11 +198,8 @@ public static IEnumerable<object[]> GetQueryRequestTestData()
"foo=multiple_error",
(Action<IQueryRequest>)(r =>
{
if (r != null)
{
r.NotFound();
r.Error(Test.CustomError);
}
r?.NotFound();
r?.Error(Test.CustomError);
}),
ResError.NotFound
};
Expand All @@ -221,6 +215,34 @@ public static IEnumerable<object[]> GetQueryRequestTestData()
}),
ResError.NotFound
};
yield return new object[] {
"foo=model_response",
(Action<IQueryRequest>)(r => r?.Model(Test.Model)),
JToken.Parse("{\"model\":{\"id\":42,\"foo\":\"bar\"}}")
};
yield return new object[] {
"foo=event_with_model_response",
(Action<IQueryRequest>)(r =>
{
r?.ChangeEvent(new Dictionary<string, object>{ { "foo", "bar" } });
r?.Model(Test.Model);
}),
JToken.Parse("{\"model\":{\"id\":42,\"foo\":\"bar\"}}")
};
yield return new object[] {
"foo=collection_response",
(Action<IQueryRequest>)(r => r?.Collection(Test.Collection)),
JToken.Parse("{\"collection\":[42,\"foo\",null]}")
};
yield return new object[] {
"foo=event_with_collection_response",
(Action<IQueryRequest>)(r =>
{
r?.AddEvent("bar", 2);
r?.Collection(Test.Collection);
}),
JToken.Parse("{\"collection\":[42,\"foo\",null]}")
};
}

[Theory]
Expand Down
4 changes: 2 additions & 2 deletions ResgateIO.Service.UnitTests/RefTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public void SerializeRef_SerializesToCorrectJson(string rid, string json)
[InlineData("{\"foo\":\"bar\"}", null)]
[InlineData("{}", null)]
[InlineData("{\"rid\": null}", null)]
public void DeserializeRef_ValidJsonObject_DeserializesToCorrectResourceID(string json, string resourceId)
public void DeserializeRef_ValidJsonObject_DeserializesToCorrectResourceID(string json, string resourceID)
{
Ref r = JsonConvert.DeserializeObject<Ref>(json);
Assert.Equal(resourceId, r.ResourceID);
Assert.Equal(resourceID, r.ResourceID);
}
}
}
6 changes: 6 additions & 0 deletions ResgateIO.Service.UnitTests/ResServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public class ResServiceTests : TestsBase
{
public ResServiceTests(ITestOutputHelper output) : base(output) { }

[Fact]
public void ProtocolVersion_ReturnsCurrentVersion()
{
Assert.Equal("1.2.0", Service.ProtocolVersion);
}

[Fact]
public void Serve_NoException()
{
Expand Down
15 changes: 15 additions & 0 deletions ResgateIO.Service.UnitTests/mocks/MockMsg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,47 @@ public MockMsg AssertPathPayload(string path, object payload)
public MockMsg AssertResult()
{
AssertNoPath("error");
AssertNoPath("resource");
AssertPath("result");
return this;
}

public MockMsg AssertResult(object result)
{
AssertNoPath("error");
AssertNoPath("resource");
AssertPathPayload("result", result);
return this;
}

public MockMsg AssertResource(string resourceID)
{
AssertNoPath("result");
AssertNoPath("error");
AssertPathPayload("resource", new { rid = resourceID });
return this;
}

public MockMsg AssertError()
{
AssertNoPath("result");
AssertNoPath("resource");
AssertPath("error");
return this;
}

public MockMsg AssertError(string code)
{
AssertNoPath("result");
AssertNoPath("resource");
AssertPathPayload("error.code", code);
return this;
}

public MockMsg AssertError(string code, string message)
{
AssertNoPath("result");
AssertNoPath("resource");
AssertPathPayload("error.code", code);
AssertPathPayload("error.message", message);
return this;
Expand All @@ -129,6 +142,7 @@ public MockMsg AssertError(string code, string message)
public MockMsg AssertError(string code, string message, object data)
{
AssertNoPath("result");
AssertNoPath("resource");
AssertPathPayload("error.code", code);
AssertPathPayload("error.message", message);
AssertPathPayload("error.data", data);
Expand All @@ -138,6 +152,7 @@ public MockMsg AssertError(string code, string message, object data)
public MockMsg AssertError(ResError err)
{
AssertNoPath("result");
AssertNoPath("resource");
AssertPathPayload("error", err);
return this;
}
Expand Down
16 changes: 13 additions & 3 deletions ResgateIO.Service.UnitTests/mocks/MockRequest.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using NATS.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ResgateIO.Service
{
#pragma warning disable 618
public class MockRequest : IRequest, IAccessRequest, IGetRequest, ICallRequest, IAuthRequest, IModelRequest, ICollectionRequest, INewRequest
#pragma warning restore 618
{
public class Call
{
Expand Down Expand Up @@ -217,6 +216,11 @@ public void Ok(object result)
{
Calls.Add(new Call("Ok", new object[] { result }));
}

public void Resource(string resourceID)
{
Calls.Add(new Call("Resource", new object[] { resourceID }));
}

public void New(Ref rid)
{
Expand Down Expand Up @@ -317,5 +321,11 @@ public void RawResponse(byte[] data)
{
Calls.Add(new Call("RawResponse", new object[] { data }));
}

public IResourceContext CloneWithQuery(string query)
{
Calls.Add(new Call("CloneWithQuery", new object[] { query }));
return null;
}
}
}
4 changes: 4 additions & 0 deletions ResgateIO.Service/EventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class ErrorEventArgs: EventArgs
/// </summary>
public string Message { get; }

/// <summary>
/// Initializes a new instance of the ErrorEventArgs class.
/// </summary>
/// <param name="message">Error message.</param>
public ErrorEventArgs(string message)
{
Message = message;
Expand Down
Loading

0 comments on commit 2d15cfb

Please sign in to comment.