Skip to content

Commit

Permalink
feat: implement builder for Properties in CreateConversation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Dec 7, 2023
1 parent 4d012dd commit 7a8c4c1
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 328 deletions.
550 changes: 279 additions & 271 deletions Vonage.Common.Test/Client/VonageHttpClientTest.cs

Large diffs are not rendered by default.

30 changes: 10 additions & 20 deletions Vonage.Common.Test/Extensions/MaybeAssertionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,6 @@ public AndConstraint<MaybeAssertionExtensions<T>> Be(Maybe<T> expected)
return new AndConstraint<MaybeAssertionExtensions<T>>(this);
}

public AndConstraint<MaybeAssertionExtensions<T>> BeEquivalentTo(Maybe<T> expected)
{
Execute.Assertion
.WithExpectation($"Expected {this.Subject} to be equivalent to {expected}, ")
.Given(() => new {this.Subject, Expected = expected})
.ForCondition(data => data.Subject.IsSome == data.Subject.IsSome)
.FailWith($"States differs between {this.Subject} and {expected}.")
.Then
.Given(data => data.Subject.Merge(data.Expected, (s, e) => new {Subject = s, Expected = e}))
.ForCondition(
data => data.Match(some => EvaluateValueEquality(some.Subject, some.Expected), () => true))
.FailWith($"Value equality failed between {this.Subject} and {expected}.");
return new AndConstraint<MaybeAssertionExtensions<T>>(this);
}

public AndConstraint<MaybeAssertionExtensions<T>> BeNone()
{
Execute.Assertion
Expand All @@ -60,13 +45,18 @@ public AndConstraint<MaybeAssertionExtensions<T>> BeSome(Action<T> action)
public AndConstraint<MaybeAssertionExtensions<T>> BeSome(T expected)
{
Execute.Assertion
.WithExpectation("Expected {context:option} to be Some {0}{reason}, ", expected)
.Given(() => this.Subject)
.ForCondition(subject => subject.IsSome)
.WithExpectation($"Expected {this.Subject} to be equivalent to {expected}, ")
.Given(() => new {this.Subject, Expected = expected})
.ForCondition(data => data.Subject.IsSome)
.FailWith("but found to be None.")
.Then
.ForCondition(subject => subject.Equals(Maybe<T>.Some(expected)))
.FailWith("but found Some {0}.", this.Subject);
.Given(data => new
{
Subject = data.Subject.GetUnsafe(),
data.Expected,
})
.ForCondition(data => EvaluateValueEquality(data.Subject, data.Expected))
.FailWith($"but value equality failed between {this.Subject} and {expected}.");
return new AndConstraint<MaybeAssertionExtensions<T>>(this);
}

Expand Down
32 changes: 7 additions & 25 deletions Vonage.Common.Test/Extensions/ResultAssertionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,6 @@ public ResultAssertionExtensions(Result<T> subject) : base(subject)
{
}

public AndConstraint<ResultAssertionExtensions<T>> BeEquivalentTo(Result<T> expected)
{
Execute.Assertion
.WithExpectation($"Expected {this.Subject} to be equivalent to {expected}, ")
.Given(() => new {this.Subject, Expected = expected})
.ForCondition(data => data.Subject.IsSuccess == data.Subject.IsSuccess)
.FailWith($"States differs between {this.Subject} and {expected}.")
.Then
.Given(data => new
{
IsSuccess = data.Subject.IsSuccess && data.Expected.IsSuccess,
data.Subject,
data.Expected,
})
.ForCondition(data =>
data.IsSuccess
? EvaluateValueEquality(data.Subject.GetSuccessUnsafe(), data.Expected.GetSuccessUnsafe())
: EvaluateValueEquality(data.Subject.GetFailureUnsafe(), data.Expected.GetFailureUnsafe()))
.FailWith($"Value equality failed between {this.Subject} and {expected}.");
return new AndConstraint<ResultAssertionExtensions<T>>(this);
}

public AndConstraint<ResultAssertionExtensions<T>> BeFailure(Action<IResultFailure> action)
{
this.BuildFailureExpectation();
Expand Down Expand Up @@ -92,10 +70,14 @@ public AndConstraint<ResultAssertionExtensions<T>> BeSuccess(Action<T> action)

public AndConstraint<ResultAssertionExtensions<T>> BeSuccess(T expected)
{
this.BuildSuccessExpectation()
Execute.Assertion
.WithExpectation($"Expected {this.Subject} to be equivalent to {expected}, ")
.Given(() => new {this.Subject, Expected = expected})
.ForCondition(data => data.Subject.IsSuccess)
.FailWith("but found to be Failure.")
.Then
.ForCondition(subject => subject.Equals(Result<T>.FromSuccess(expected)))
.FailWith(this.BuildResultSuccessMessage());
.ForCondition(data => EvaluateValueEquality(data.Subject.GetSuccessUnsafe(), expected))
.FailWith($"Value equality failed between {this.Subject} and {expected}.");
return new AndConstraint<ResultAssertionExtensions<T>>(this);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Vonage.Common.Test.Extensions;
using Vonage.Conversations.CreateConversation;
using Xunit;
Expand Down Expand Up @@ -63,6 +64,15 @@ public class RequestBuilderTest
.Should()
.BeSuccess(new string('a', 50));

[Fact]
public void Build_ShouldSetImageUrl() =>
CreateConversationRequest.Build()
.WithImageUrl(new Uri("https://example.com"))
.Create()
.Map(request => request.ImageUrl)
.Should()
.BeSuccess(new Uri("https://example.com"));

[Fact]
public void Build_ShouldSetName() =>
CreateConversationRequest.Build()
Expand All @@ -73,12 +83,15 @@ public class RequestBuilderTest
.BeSuccess(new string('a', 100));

[Fact]
public void Build_ShouldSetUri() =>
public void Build_ShouldSetProperties() =>
CreateConversationRequest.Build()
.WithUri(new Uri("https://example.com"))
.WithProperties(new Properties(55, "Fake", "hello-there",
new Dictionary<string, string> {{"temp1", "123"}, {"temp12", "456"}}))
.Create()
.Map(request => request.Uri)
.Map(request => request.Properties)
.Should()
.BeSuccess(new Uri("https://example.com"));
.BeSuccess(properties =>
properties.Should().BeSome(new Properties(55, "Fake", "hello-there",
new Dictionary<string, string> {{"temp1", "123"}, {"temp12", "456"}})));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class SerializationTest
DateTimeOffset.Parse("2019-09-03T18:40:24.324Z", CultureInfo.InvariantCulture),
DateTimeOffset.Parse("2019-09-03T18:40:24.324Z", CultureInfo.InvariantCulture),
DateTimeOffset.Parse("2019-09-03T18:40:24.324Z", CultureInfo.InvariantCulture)));
response.Properties.Should().BeEquivalentTo(new Properties(60, "string",
response.Properties.Should().BeSome(new Properties(60, "string",
"string", new Dictionary<string, string>
{
{"property1", "string"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ namespace Vonage.Conversations.CreateConversation;
/// </summary>
public Maybe<string> DisplayName { get; internal init; }

/// <summary>
/// </summary>
public Maybe<Uri> ImageUrl { get; internal init; }

/// <summary>
/// </summary>
public Maybe<string> Name { get; internal init; }

/// <summary>
/// </summary>
public Maybe<Uri> Uri { get; internal init; }
public Maybe<Properties> Properties { get; internal init; }

/// <summary>
/// Initializes a builder for CreateConversationRequest.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal class CreateConversationRequestBuilder : IBuilderForOptional
{
private const int DisplayNameMaxLength = 50;
private const int NameMaxLength = 100;
private Maybe<Properties> properties;
private Maybe<string> name;
private Maybe<string> displayName;
private Maybe<Uri> uri;
Expand All @@ -18,7 +19,8 @@ internal class CreateConversationRequestBuilder : IBuilderForOptional
{
Name = this.name,
DisplayName = this.displayName,
Uri = this.uri,
ImageUrl = this.uri,
Properties = this.properties,
})
.Map(InputEvaluation<CreateConversationRequest>.Evaluate)
.Bind(evaluation => evaluation.WithRules(
Expand All @@ -33,15 +35,21 @@ public IBuilderForOptional WithDisplayName(string value)
return this;
}

public IBuilderForOptional WithImageUrl(Uri value)
{
this.uri = value;
return this;
}

public IBuilderForOptional WithName(string value)
{
this.name = value;
return this;
}

public IBuilderForOptional WithUri(Uri value)
public IBuilderForOptional WithProperties(Properties value)
{
this.uri = value;
this.properties = value;
return this;
}

Expand Down Expand Up @@ -79,6 +87,13 @@ public interface IBuilderForOptional : IVonageRequestBuilder<CreateConversationR
/// <returns>The builder.</returns>
IBuilderForOptional WithDisplayName(string value);

/// <summary>
/// Sets the Image Url.
/// </summary>
/// <param name="value">The Image Url.</param>
/// <returns>The builder.</returns>
IBuilderForOptional WithImageUrl(Uri value);

/// <summary>
/// Sets the Name
/// </summary>
Expand All @@ -87,9 +102,9 @@ public interface IBuilderForOptional : IVonageRequestBuilder<CreateConversationR
IBuilderForOptional WithName(string value);

/// <summary>
/// Sets the Uri
/// Sets the Properties.
/// </summary>
/// <param name="value">The uri.</param>
/// <param name="value">The properties.</param>
/// <returns>The builder.</returns>
IBuilderForOptional WithUri(Uri value);
IBuilderForOptional WithProperties(Properties value);
}

0 comments on commit 7a8c4c1

Please sign in to comment.