Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Net;
using System.Net.Http;
Expand All @@ -24,7 +23,7 @@ internal class SomeDomainErrorObjectException : Exception
{
public MyDomainErrorEnum Error { get; }

public SomeDomainErrorObjectException(MyDomainErrorEnum error, Exception innerException = null) : base(
public SomeDomainErrorObjectException(MyDomainErrorEnum error, Exception? innerException = null) : base(
error.ToString(), innerException)
{
Error = error;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Net;
using System.Net.Http;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -26,7 +25,7 @@ internal class MyDomainErrorObjectException : Exception
{
public MyDomainErrorObject Error { get; }

public MyDomainErrorObjectException(MyDomainErrorObject error, Exception innerException = null) : base(
public MyDomainErrorObjectException(MyDomainErrorObject error, Exception? innerException = null) : base(
error.ToString(), innerException)
{
Error = error;
Expand All @@ -42,8 +41,8 @@ public interface IMyApiErrorObjectClient

internal class MyDomainExceptionObjectMapper : AbstractDomainExceptionMapper
{
protected override Exception CreateException(HttpResponseMessage httpResponseMessage, object data,
Exception innerException)
protected override Exception CreateException(HttpResponseMessage httpResponseMessage, object? data,
Exception? innerException)
{
var domainError = data is MyApiErrorResponse errorResponse && errorResponse.Code == MyApiError.Bar
? new MyDomainErrorObject(MyDomainErrorEnum.DomainBar)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
namespace Activout.RestClient.Newtonsoft.Json.Test.DomainExceptions
{
public enum MyApiError
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
namespace Activout.RestClient.Newtonsoft.Json.Test.DomainExceptions
{
public class MyApiErrorResponse
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using Activout.RestClient.DomainExceptions;

namespace Activout.RestClient.Newtonsoft.Json.Test.DomainExceptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
namespace Activout.RestClient.Newtonsoft.Json.Test.DomainExceptions
{
public enum MyDomainErrorEnum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System.Net;
using Activout.RestClient.DomainExceptions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#nullable disable
using System.Collections.Generic;

namespace Activout.RestClient.Newtonsoft.Json.Test.MovieReviews
{
public class ErrorResponse
{
public List<Error> Errors { get; set; }
public List<Error> Errors { get; init; } = [];

public class Error
{
public string Message { get; set; }
public int Code { get; set; }
public string? Message { get; init; }
public int Code { get; init; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#nullable disable
using System.Diagnostics.CodeAnalysis;

namespace Activout.RestClient.Newtonsoft.Json.Test.MovieReviews
{
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
public class Movie
{
public string Title { get; set; }
public string? Title { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
namespace Activout.RestClient.Newtonsoft.Json.Test.MovieReviews
{
public class Review
Expand All @@ -9,9 +8,9 @@ public Review(int stars, string text)
Text = text;
}

public string MovieId { get; set; }
public string ReviewId { get; set; }
public int Stars { get; set; }
public string Text { get; set; }
public string? MovieId { get; init; }
public string? ReviewId { get; init; }
public int Stars { get; init; }
public string Text { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#nullable disable
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http;
using System.Text;
using Activout.RestClient.Newtonsoft.Json;
using Activout.RestClient.Serialization.Implementation;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
Expand All @@ -16,7 +14,7 @@ namespace Activout.RestClient.Newtonsoft.Json.Test
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
public class Data
{
public string Value { get; set; }
public string? Value { get; init; }
}

public interface IClient
Expand Down
37 changes: 6 additions & 31 deletions Activout.RestClient.Newtonsoft.Json.Test/RestClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#nullable disable
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Activout.RestClient.Helpers.Implementation;
using Activout.RestClient.Newtonsoft.Json.Test.MovieReviews;
Expand Down Expand Up @@ -50,32 +48,6 @@ private IMovieReviewService CreateMovieReviewService()
.Build<IMovieReviewService>();
}

[Fact]
public async Task TestErrorAsyncWithOldTaskConverter()
{
// arrange
ExpectGetAllReviewsAndReturnError();

var reviewSvc = CreateRestClientBuilder()
.With(new TaskConverterFactory())
.Build<IMovieReviewService>();

// act
var aggregateException =
await Assert.ThrowsAsync<AggregateException>(() => reviewSvc.GetAllReviews(MovieId));

// assert
_mockHttp.VerifyNoOutstandingExpectation();

Assert.IsType<RestClientException>(aggregateException.InnerException);
var exception = (RestClientException)aggregateException.InnerException;

Assert.Equal(HttpStatusCode.NotFound, exception.StatusCode);
var error = exception.GetErrorResponse<ErrorResponse>();
Assert.Equal(34, error.Errors[0].Code);
Assert.Equal("Sorry, that page does not exist", error.Errors[0].Message);
}

[Fact]
public async Task TestErrorAsync()
{
Expand All @@ -93,6 +65,7 @@ public async Task TestErrorAsync()

Assert.Equal(HttpStatusCode.NotFound, exception.StatusCode);
var error = exception.GetErrorResponse<ErrorResponse>();
Assert.NotNull(error);
Assert.Equal(34, error.Errors[0].Code);
Assert.Equal("Sorry, that page does not exist", error.Errors[0].Message);
}
Expand All @@ -101,7 +74,7 @@ private void ExpectGetAllReviewsAndReturnError(string movieId = MovieId)
{
_mockHttp
.Expect(HttpMethod.Get, $"{BaseUri}/movies/{movieId}/reviews")
.Respond(HttpStatusCode.NotFound, request => new StringContent(JsonConvert.SerializeObject(new
.Respond(HttpStatusCode.NotFound, _ => new StringContent(JsonConvert.SerializeObject(new
{
Errors = new object[]
{
Expand Down Expand Up @@ -137,13 +110,15 @@ public void TestErrorSync()
var exception = (RestClientException)aggregateException.GetBaseException();
Assert.Equal(HttpStatusCode.NotFound, exception.StatusCode);

Assert.NotNull(exception.ErrorResponse);
dynamic dynamicError = exception.ErrorResponse;
string message = dynamicError.Errors[0].Message;
int code = dynamicError.Errors[0].Code;
Assert.Equal(34, code);
Assert.Equal("Sorry, that page does not exist", message);

var error = exception.GetErrorResponse<ErrorResponse>();
Assert.NotNull(error);
Assert.Equal(34, error.Errors[0].Code);
Assert.Equal("Sorry, that page does not exist", error.Errors[0].Message);
}
Expand Down Expand Up @@ -222,7 +197,7 @@ public void TestPutSync()
var reviewId = "*REVIEW_ID*";
_mockHttp
.When(HttpMethod.Put, $"{BaseUri}/movies/{movieId}/reviews/{reviewId}")
.Respond(request => request.Content);
.Respond(request => request.Content!);

var reviewSvc = CreateMovieReviewService();

Expand Down Expand Up @@ -251,7 +226,7 @@ public void TestPatchSync()
var reviewId = "*REVIEW_ID*";
_mockHttp
.When(HttpMethod.Patch, $"{BaseUri}/movies/{movieId}/reviews/{reviewId}")
.Respond(request => request.Content);
.Respond(request => request.Content!);

var reviewSvc = CreateMovieReviewService();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Net.Http;
using System.Text;
Expand Down Expand Up @@ -53,7 +52,7 @@ public async Task TestSerializationOrder(int order, string expectedValue)

public class SerializationOrderModel
{
public string MyValue { get; set; }
public string? MyValue { get; init; }
}

public interface ISerializationOrderClient
Expand Down
19 changes: 6 additions & 13 deletions Activout.RestClient.Newtonsoft.Json.Test/SimpleValueObjectTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Net;
using System.Net.Http;
Expand All @@ -22,9 +21,9 @@ public MySimpleValueObject(string value)

public class Stuff
{
public MySimpleValueObject FooBar { get; set; }
public MySimpleValueObject? FooBar { get; init; }

public int? NullableInteger { get; set; }
public int? NullableInteger { get; init; }
}

public interface IValueObjectClient
Expand All @@ -37,16 +36,10 @@ public interface IValueObjectClient

public class SimpleValueObjectTest
{
public SimpleValueObjectTest()
{
_restClientFactory = Services.CreateRestClientFactory();
_mockHttp = new MockHttpMessageHandler();
}

private const string BaseUri = "https://example.com/api/";

private readonly IRestClientFactory _restClientFactory;
private readonly MockHttpMessageHandler _mockHttp;
private readonly IRestClientFactory _restClientFactory = Services.CreateRestClientFactory();
private readonly MockHttpMessageHandler _mockHttp = new();

[Fact]
public async Task TestSimpleValueObjectSerialization()
Expand Down Expand Up @@ -97,7 +90,7 @@ public void TestSimpleValueObjectDeserialization()

// Assert
_mockHttp.VerifyNoOutstandingExpectation();
Assert.Equal("foobar", result.FooBar.Value);
Assert.Equal("foobar", result.FooBar?.Value);
}

[Fact]
Expand All @@ -108,7 +101,7 @@ public void TestSimpleValueObjectDeserializationWithNulls()
.Expect(BaseUri)
.Respond(new StringContent(JsonConvert.SerializeObject(new
{
FooBar = (string)null,
FooBar = (string?)null,
NullableInteger = (int?)null
}),
Encoding.UTF8,
Expand Down
Loading