-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat; implement CreateEvent in Conversations
- Loading branch information
Showing
12 changed files
with
468 additions
and
2 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
Vonage.Test/Conversations/CreateEvent/Data/ShouldDeserialize200-response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"id": 100, | ||
"type": "message", | ||
"from": "string", | ||
"body": { | ||
"message_type": "text", | ||
"text": "string" | ||
}, | ||
"timestamp": "2020-01-01T14:00:00.00Z", | ||
"_embedded": { | ||
"from_user": { | ||
"id": "USR-82e028d9-5201-4f1e-8188-604b2d3471ec", | ||
"name": "my_user_name", | ||
"display_name": "My User Name", | ||
"image_url": "https://example.com/image.png", | ||
"custom_data": { | ||
"field_1": "value_1", | ||
"field_2": "value_2" | ||
} | ||
}, | ||
"from_member": { | ||
"id": "string" | ||
} | ||
}, | ||
"_links": { | ||
"self": { | ||
"href": "https://api.nexmo.com/v0.1/conversations/CON-1234/events/100" | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
Vonage.Test/Conversations/CreateEvent/Data/ShouldSerialize-request.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "message:submitted", | ||
"body": { | ||
"event_id": "string" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
Vonage.Test/Conversations/CreateEvent/Data/ShouldSerializeWithFrom-request.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "message:submitted", | ||
"from": "string", | ||
"body": { | ||
"event_id": "string" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Vonage.Test.Common.Extensions; | ||
using WireMock.ResponseBuilders; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.Conversations.CreateEvent; | ||
|
||
[Trait("Category", "E2E")] | ||
public class E2ETest : E2EBase | ||
{ | ||
public E2ETest() : base(typeof(E2ETest).Namespace) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task CreateEvent() | ||
{ | ||
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create() | ||
.WithPath("/v1/conversations/CON-123/events") | ||
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue) | ||
.WithBody(this.Serialization.GetRequestJson(nameof(SerializationTest.ShouldSerialize))) | ||
.UsingPost()) | ||
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK) | ||
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200)))); | ||
await this.Helper.VonageClient.ConversationsClient | ||
.CreateEventAsync(SerializationTest.BuildRequest()) | ||
.Should() | ||
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateEventWithFrom() | ||
{ | ||
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create() | ||
.WithPath("/v1/conversations/CON-123/events") | ||
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue) | ||
.WithBody(this.Serialization.GetRequestJson(nameof(SerializationTest.ShouldSerializeWithFrom))) | ||
.UsingPost()) | ||
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK) | ||
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200)))); | ||
await this.Helper.VonageClient.ConversationsClient | ||
.CreateEventAsync(SerializationTest.BuildRequestWithFrom()) | ||
.Should() | ||
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
Vonage.Test/Conversations/CreateEvent/RequestBuilderTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System.Text.Json; | ||
using Vonage.Common.Monads; | ||
using Vonage.Conversations.CreateEvent; | ||
using Vonage.Test.Common.Extensions; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.Conversations.CreateEvent; | ||
|
||
[Trait("Category", "Request")] | ||
public class RequestBuilderTest | ||
{ | ||
private const string ValidConversationId = "CON-123"; | ||
private const string ValidType = "submitted"; | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
[InlineData(null)] | ||
public void Parse_ShouldReturnFailure_GivenConversationIdIsEmpty(string invalidId) => | ||
CreateEventRequest | ||
.Build() | ||
.WithConversationId(invalidId) | ||
.WithType(ValidType) | ||
.WithBody(BuildValidBody()) | ||
.Create() | ||
.Should() | ||
.BeParsingFailure("ConversationId cannot be null or whitespace."); | ||
|
||
[Fact] | ||
public void Build_ShouldSetConversationId() => | ||
CreateEventRequest | ||
.Build() | ||
.WithConversationId(ValidConversationId) | ||
.WithType(ValidType) | ||
.WithBody(BuildValidBody()) | ||
.Create() | ||
.Map(request => request.ConversationId) | ||
.Should() | ||
.BeSuccess(ValidConversationId); | ||
|
||
[Fact] | ||
public void Build_ShouldSetType() => | ||
CreateEventRequest | ||
.Build() | ||
.WithConversationId(ValidConversationId) | ||
.WithType(ValidType) | ||
.WithBody(BuildValidBody()) | ||
.Create() | ||
.Map(request => request.Type) | ||
.Should() | ||
.BeSuccess(ValidType); | ||
|
||
[Fact] | ||
public void Build_ShouldHaveNoFrom_GivenDefault() => | ||
CreateEventRequest | ||
.Build() | ||
.WithConversationId(ValidConversationId) | ||
.WithType(ValidType) | ||
.WithBody(BuildValidBody()) | ||
.Create() | ||
.Map(request => request.From) | ||
.Should() | ||
.BeSuccess(Maybe<string>.None); | ||
|
||
[Fact] | ||
public void Build_ShouldSetFrom() => | ||
CreateEventRequest | ||
.Build() | ||
.WithConversationId(ValidConversationId) | ||
.WithType(ValidType) | ||
.WithBody(BuildValidBody()) | ||
.WithFrom("from") | ||
.Create() | ||
.Map(request => request.From) | ||
.Should() | ||
.BeSuccess("from"); | ||
|
||
[Fact] | ||
public void Build_ShouldSetBody() => | ||
CreateEventRequest | ||
.Build() | ||
.WithConversationId(ValidConversationId) | ||
.WithType(ValidType) | ||
.WithBody(BuildValidBody()) | ||
.Create() | ||
.Map(request => request.Body) | ||
.Should() | ||
.BeSuccess(body => body.Should().Be(BuildValidBody())); | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
[InlineData(null)] | ||
public void Parse_ShouldReturnFailure_GivenTypeIsEmpty(string invalidType) => | ||
CreateEventRequest | ||
.Build() | ||
.WithConversationId(ValidType) | ||
.WithType(invalidType) | ||
.WithBody(BuildValidBody()) | ||
.Create() | ||
.Should() | ||
.BeParsingFailure("Type cannot be null or whitespace."); | ||
|
||
private static JsonElement BuildValidBody() => | ||
JsonSerializer.SerializeToElement(new {type = "action", value = "random"}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Text.Json; | ||
using Vonage.Conversations.CreateEvent; | ||
using Vonage.Test.Common.Extensions; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.Conversations.CreateEvent; | ||
|
||
[Trait("Category", "Request")] | ||
public class RequestTest | ||
{ | ||
[Fact] | ||
public void GetEndpointPath_ShouldReturnApiEndpoint() => | ||
CreateEventRequest | ||
.Build() | ||
.WithConversationId("CON-123") | ||
.WithType("submitted") | ||
.WithBody(JsonSerializer.SerializeToElement(new { })) | ||
.Create() | ||
.Map(request => request.GetEndpointPath()) | ||
.Should() | ||
.BeSuccess("/v1/conversations/CON-123/events"); | ||
} |
73 changes: 73 additions & 0 deletions
73
Vonage.Test/Conversations/CreateEvent/SerializationTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using FluentAssertions; | ||
using Vonage.Common; | ||
using Vonage.Common.Monads; | ||
using Vonage.Conversations; | ||
using Vonage.Conversations.CreateEvent; | ||
using Vonage.Serialization; | ||
using Vonage.Test.Common; | ||
using Vonage.Test.Common.Extensions; | ||
using Xunit; | ||
using JsonSerializer = System.Text.Json.JsonSerializer; | ||
|
||
namespace Vonage.Test.Conversations.CreateEvent; | ||
|
||
[Trait("Category", "Serialization")] | ||
public class SerializationTest | ||
{ | ||
private readonly SerializationTestHelper helper = new SerializationTestHelper( | ||
typeof(SerializationTest).Namespace, | ||
JsonSerializerBuilder.BuildWithSnakeCase()); | ||
|
||
[Fact] | ||
public void ShouldSerializeWithFrom() => | ||
BuildRequestWithFrom() | ||
.GetStringContent() | ||
.Should() | ||
.BeSuccess(this.helper.GetRequestJson()); | ||
|
||
internal static Result<CreateEventRequest> BuildRequestWithFrom() => | ||
CreateEventRequest.Build() | ||
.WithConversationId("CON-123") | ||
.WithType("message:submitted") | ||
.WithBody(new {event_id = "string"}) | ||
.WithFrom("string") | ||
.Create(); | ||
|
||
[Fact] | ||
public void ShouldSerialize() => | ||
BuildRequest() | ||
.GetStringContent() | ||
.Should() | ||
.BeSuccess(this.helper.GetRequestJson()); | ||
|
||
internal static Result<CreateEventRequest> BuildRequest() => | ||
CreateEventRequest.Build() | ||
.WithConversationId("CON-123") | ||
.WithType("message:submitted") | ||
.WithBody(new {event_id = "string"}) | ||
.Create(); | ||
|
||
[Fact] | ||
public void ShouldDeserialize200() => this.helper.Serializer | ||
.DeserializeObject<Event>(this.helper.GetResponseJson()) | ||
.Should() | ||
.BeSuccess(VerifyExpectedResponse); | ||
|
||
internal static void VerifyExpectedResponse(Event success) | ||
{ | ||
success.Id.Should().Be(100); | ||
success.Type.Should().Be("message"); | ||
success.From.Should().Be("string"); | ||
success.Body.Should().Be(JsonSerializer.SerializeToElement(new {message_type = "text", text = "string"})); | ||
success.Embedded.Member.Should().Be(new EmbeddedEventMember("string")); | ||
success.Embedded.User.Id.Should().Be("USR-82e028d9-5201-4f1e-8188-604b2d3471ec"); | ||
success.Embedded.User.Name.Should().Be("my_user_name"); | ||
success.Embedded.User.DisplayName.Should().Be("My User Name"); | ||
success.Embedded.User.ImageUrl.Should().Be("https://example.com/image.png"); | ||
success.Embedded.User.CustomData.Should() | ||
.Be(JsonSerializer.SerializeToElement(new {field_1 = "value_1", field_2 = "value_2"})); | ||
success.Links.Should() | ||
.Be(new Links(new HalLink(new Uri("https://api.nexmo.com/v0.1/conversations/CON-1234/events/100")))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.