Skip to content

Commit

Permalink
Applied resharper.
Browse files Browse the repository at this point in the history
  • Loading branch information
lvukadinovic-ib committed Nov 7, 2024
1 parent 5d56904 commit 8a83ad2
Show file tree
Hide file tree
Showing 7 changed files with 2,853 additions and 2,704 deletions.
385 changes: 194 additions & 191 deletions ApiClient.Tests/Api/ApiTest.cs

Large diffs are not rendered by default.

1,763 changes: 896 additions & 867 deletions ApiClient.Tests/Api/EmailApiTest.cs

Large diffs are not rendered by default.

1,515 changes: 764 additions & 751 deletions ApiClient.Tests/Api/SmsApiTest.cs

Large diffs are not rendered by default.

1,571 changes: 840 additions & 731 deletions ApiClient.Tests/Api/TfaApiTest.cs

Large diffs are not rendered by default.

133 changes: 66 additions & 67 deletions ApiClient.Tests/ApiExceptionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,47 @@
using Infobip.Api.Client.Api;
using Infobip.Api.Client.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using ApiException = Infobip.Api.Client.ApiException;

namespace ApiClient.Tests
namespace ApiClient.Tests;

[TestClass]
public class ApiExceptionTest : ApiTest
{
[TestClass]
public class ApiExceptionTest : ApiTest
{
protected const string SMS_SEND_TEXT_ADVANCED_ENDPOINT = "/sms/2/text/advanced";
protected const string SMS_SEND_TEXT_ADVANCED_ENDPOINT = "/sms/2/text/advanced";

internal static readonly Tuple<int, string, string, string>[] ErrorResponses =
{
Tuple.Create(400, "BAD_REQUEST", "Bad Request", "[messages[0].destinations : size must be between 1 and 2147483647]"),
Tuple.Create(401, "UNAUTHORIZED", "Unauthorized", "Invalid login details"),
Tuple.Create(403, "UNAUTHORIZED", "Forbidden", "Unauthorized access"),
Tuple.Create(404, "NOT_FOUND", "Not Found", "Requested URL not found: /sms/2/text/advanced"),
Tuple.Create(429, "TOO_MANY_REQUESTS", "Too Many Requests", "Too many requests"),
Tuple.Create(500, "GENERAL_ERROR", "Internal Server Error", "Something went wrong. Please contact support."),
Tuple.Create(503, "0", "Service Unavailable", "Error processing email validation request! Please try again"),
};
internal static readonly Tuple<int, string, string, string>[] ErrorResponses =
{
Tuple.Create(400, "BAD_REQUEST", "Bad Request",
"[messages[0].destinations : size must be between 1 and 2147483647]"),
Tuple.Create(401, "UNAUTHORIZED", "Unauthorized", "Invalid login details"),
Tuple.Create(403, "UNAUTHORIZED", "Forbidden", "Unauthorized access"),
Tuple.Create(404, "NOT_FOUND", "Not Found", "Requested URL not found: /sms/2/text/advanced"),
Tuple.Create(429, "TOO_MANY_REQUESTS", "Too Many Requests", "Too many requests"),
Tuple.Create(500, "GENERAL_ERROR", "Internal Server Error", "Something went wrong. Please contact support."),
Tuple.Create(503, "0", "Service Unavailable", "Error processing email validation request! Please try again")
};

[DataTestMethod]
[DataRow(0)]
[DataRow(1)]
[DataRow(2)]
[DataRow(3)]
[DataRow(4)]
[DataRow(5)]
[DataRow(6)]
public void ErrorResponseTest(int errorResponseIndex)
{
int httpCode = ErrorResponses[errorResponseIndex].Item1;
string messageId = ErrorResponses[errorResponseIndex].Item2;
string errorPhrase = ErrorResponses[errorResponseIndex].Item3;
string errorText = ErrorResponses[errorResponseIndex].Item4;
[DataTestMethod]
[DataRow(0)]
[DataRow(1)]
[DataRow(2)]
[DataRow(3)]
[DataRow(4)]
[DataRow(5)]
[DataRow(6)]
public void ErrorResponseTest(int errorResponseIndex)
{
var httpCode = ErrorResponses[errorResponseIndex].Item1;
var messageId = ErrorResponses[errorResponseIndex].Item2;
var errorPhrase = ErrorResponses[errorResponseIndex].Item3;
var errorText = ErrorResponses[errorResponseIndex].Item4;

string to = "41793026727";
string from = "InfoSMS";
string message = "This is a sample message";
var to = "41793026727";
var from = "InfoSMS";
var message = "This is a sample message";

string responseJson = $@"
var responseJson = $@"
{{
""requestError"": {{
""serviceException"": {{
Expand All @@ -54,7 +52,7 @@ public void ErrorResponseTest(int errorResponseIndex)
}}
}}";

string expectedRequest = $@"
var expectedRequest = $@"
{{
""messages"": [
{{
Expand All @@ -72,40 +70,41 @@ public void ErrorResponseTest(int errorResponseIndex)
""includeSmsCountInResponse"":false
}}";

Dictionary<string, string> responseHeaders = new Dictionary<string, string>()
{
{ "Server", "SMS,API" },
{ "X-Request-ID", "1608758729810312842" },
{ "Content-Type", "application/json; charset=utf-8" }
};

SetUpPostRequest(SMS_SEND_TEXT_ADVANCED_ENDPOINT, expectedRequest, responseJson, httpCode);
var responseHeaders = new Dictionary<string, string>
{
{ "Server", "SMS,API" },
{ "X-Request-ID", "1608758729810312842" },
{ "Content-Type", "application/json; charset=utf-8" }
};

var smsApi = new SmsApi(this.configuration);
SetUpPostRequest(SMS_SEND_TEXT_ADVANCED_ENDPOINT, expectedRequest, responseJson, httpCode);

var request = new SmsAdvancedTextualRequest(
messages: new List<SmsTextualMessage> {
new SmsTextualMessage(
from: from, text: message, destinations: new List<SmsDestination>{ new SmsDestination(to: to) }
)
}
);
var smsApi = new SmsApi(configuration);

try
{
var result = smsApi.SendSmsMessage(request);
}
catch (ApiException ex)
var request = new SmsAdvancedTextualRequest(
messages: new List<SmsTextualMessage>
{
Assert.AreEqual(httpCode, ex.ErrorCode);
Assert.IsNotNull(ex.ErrorContent);
Assert.AreEqual(responseJson, ex.ErrorContent);
Assert.IsInstanceOfType(ex, typeof(ApiException));
Assert.IsTrue(ex.Message.Contains(errorPhrase));
Assert.IsTrue(ex.ErrorContent.ToString()?.Contains(messageId) == true);
Assert.IsTrue(ex.ErrorContent.ToString()?.Contains(errorText) == true);
Assert.IsTrue(responseHeaders.All(h => ex.Headers.ContainsKey(h.Key) && ex.Headers[h.Key].First().Equals(h.Value)));
new(
from: from, text: message, destinations: new List<SmsDestination> { new(to: to) }
)
}
);

try
{
var result = smsApi.SendSmsMessage(request);
}
catch (ApiException ex)
{
Assert.AreEqual(httpCode, ex.ErrorCode);
Assert.IsNotNull(ex.ErrorContent);
Assert.AreEqual(responseJson, ex.ErrorContent);
Assert.IsInstanceOfType(ex, typeof(ApiException));
Assert.IsTrue(ex.Message.Contains(errorPhrase));
Assert.IsTrue(ex.ErrorContent.ToString()?.Contains(messageId) == true);
Assert.IsTrue(ex.ErrorContent.ToString()?.Contains(errorText) == true);
Assert.IsTrue(responseHeaders.All(h =>
ex.Headers.ContainsKey(h.Key) && ex.Headers[h.Key].First().Equals(h.Value)));
}
}
}
}
129 changes: 64 additions & 65 deletions ApiClient.Tests/DateTimeSerializationTest.cs
Original file line number Diff line number Diff line change
@@ -1,78 +1,77 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;

namespace ApiClient.Tests
namespace ApiClient.Tests;

[TestClass]
public class DateTimeSerializationTest
{
[TestClass]
public class DateTimeSerializationTest
{
internal static readonly DateTimeOffset[] DateTimeValues = new[] {
new DateTimeOffset(2035, 8, 18, 12, 8, 42, 777, new TimeSpan(0, 0, 0)),
new DateTimeOffset(2035, 8, 18, 13, 8, 42, 777, new TimeSpan(1, 0, 0)),
new DateTimeOffset(2035, 8, 18, 11, 8, 42, 777, new TimeSpan(-1, 0, 0)),
new DateTimeOffset(2035, 8, 18, 17, 8, 42, 777, new TimeSpan(5, 0, 0)),
new DateTimeOffset(2035, 8, 18, 7, 8, 42, 777, new TimeSpan(-5, 0, 0)),
new DateTimeOffset(2035, 8, 18, 13, 38, 42, 777, new TimeSpan(1, 30, 0)),
new DateTimeOffset(2035, 8, 18, 10, 38, 42, 777, new TimeSpan(-1, -30, 0)),
new DateTimeOffset(2035, 8, 18, 17, 38, 42, 777, new TimeSpan(5, 30, 0)),
new DateTimeOffset(2035, 8, 18, 6, 38, 42, 777, new TimeSpan(-5, -30, 0))
};
private const string EXPECTED_DATETIME_FORMAT = "yyyy-MM-ddTHH:mm:ss.fffzzzz";
private const string EXPECTED_DATE_FORMAT = "yyyy-MM-dd";
private const string EXPECTED_DATE = "2035-08-18";
private const long EXPECTED_TICKS = 642066048000000000;

const string EXPECTED_DATETIME_FORMAT = "yyyy-MM-ddTHH:mm:ss.fffzzzz";
const string EXPECTED_DATE_FORMAT = "yyyy-MM-dd";
const string EXPECTED_DATE = "2035-08-18";
const long EXPECTED_TICKS = 642066048000000000;
internal static readonly DateTimeOffset[] DateTimeValues =
{
new DateTimeOffset(2035, 8, 18, 12, 8, 42, 777, new TimeSpan(0, 0, 0)),
new DateTimeOffset(2035, 8, 18, 13, 8, 42, 777, new TimeSpan(1, 0, 0)),
new DateTimeOffset(2035, 8, 18, 11, 8, 42, 777, new TimeSpan(-1, 0, 0)),
new DateTimeOffset(2035, 8, 18, 17, 8, 42, 777, new TimeSpan(5, 0, 0)),
new DateTimeOffset(2035, 8, 18, 7, 8, 42, 777, new TimeSpan(-5, 0, 0)),
new DateTimeOffset(2035, 8, 18, 13, 38, 42, 777, new TimeSpan(1, 30, 0)),
new DateTimeOffset(2035, 8, 18, 10, 38, 42, 777, new TimeSpan(-1, -30, 0)),
new DateTimeOffset(2035, 8, 18, 17, 38, 42, 777, new TimeSpan(5, 30, 0)),
new DateTimeOffset(2035, 8, 18, 6, 38, 42, 777, new TimeSpan(-5, -30, 0))
};

[DataRow("2035-08-18T12:08:42.777+0000", 0)]
[DataRow("2035-08-18T13:08:42.777+0100", 1)]
[DataRow("2035-08-18T11:08:42.777-0100", 2)]
[DataRow("2035-08-18T17:08:42.777+0500", 3)]
[DataRow("2035-08-18T07:08:42.777-0500", 4)]
[DataRow("2035-08-18T13:38:42.777+0130", 5)]
[DataRow("2035-08-18T10:38:42.777-0130", 6)]
[DataRow("2035-08-18T17:38:42.777+0530", 7)]
[DataRow("2035-08-18T06:38:42.777-0530", 8)]
[DataTestMethod]
public void DateTimeFormatDeserializationTest(string dateString, int dateValueIndex)
{
DateTimeOffset expected = DateTimeValues[dateValueIndex];
[DataRow("2035-08-18T12:08:42.777+0000", 0)]
[DataRow("2035-08-18T13:08:42.777+0100", 1)]
[DataRow("2035-08-18T11:08:42.777-0100", 2)]
[DataRow("2035-08-18T17:08:42.777+0500", 3)]
[DataRow("2035-08-18T07:08:42.777-0500", 4)]
[DataRow("2035-08-18T13:38:42.777+0130", 5)]
[DataRow("2035-08-18T10:38:42.777-0130", 6)]
[DataRow("2035-08-18T17:38:42.777+0530", 7)]
[DataRow("2035-08-18T06:38:42.777-0530", 8)]
[DataTestMethod]
public void DateTimeFormatDeserializationTest(string dateString, int dateValueIndex)
{
var expected = DateTimeValues[dateValueIndex];

string jsonDate = $"{{\"date\":\"{dateString}\"}}";
var jsonDate = $"{{\"date\":\"{dateString}\"}}";

TestObject actual = JsonConvert.DeserializeObject<TestObject>(jsonDate)!;
var actual = JsonConvert.DeserializeObject<TestObject>(jsonDate)!;

Assert.AreEqual(expected, actual.Date);
Assert.AreEqual(expected.Ticks, actual.Date.Ticks);
Assert.AreEqual(expected.Offset, actual.Date.Offset);
Assert.AreEqual(EXPECTED_DATE, actual.Date.ToString(EXPECTED_DATE_FORMAT));
Assert.AreEqual(EXPECTED_TICKS, actual.Date.Date.Ticks);
Assert.AreEqual(expected.ToString(), actual.Date.ToString());
Assert.AreEqual(expected.ToString(EXPECTED_DATETIME_FORMAT), actual.Date.ToString(EXPECTED_DATETIME_FORMAT));
}
Assert.AreEqual(expected, actual.Date);
Assert.AreEqual(expected.Ticks, actual.Date.Ticks);
Assert.AreEqual(expected.Offset, actual.Date.Offset);
Assert.AreEqual(EXPECTED_DATE, actual.Date.ToString(EXPECTED_DATE_FORMAT));
Assert.AreEqual(EXPECTED_TICKS, actual.Date.Date.Ticks);
Assert.AreEqual(expected.ToString(), actual.Date.ToString());
Assert.AreEqual(expected.ToString(EXPECTED_DATETIME_FORMAT), actual.Date.ToString(EXPECTED_DATETIME_FORMAT));
}

[DataRow(0, "2035-08-18T12:08:42.777+00:00")]
[DataRow(1, "2035-08-18T13:08:42.777+01:00")]
[DataRow(2, "2035-08-18T11:08:42.777-01:00")]
[DataRow(3, "2035-08-18T17:08:42.777+05:00")]
[DataRow(4, "2035-08-18T07:08:42.777-05:00")]
[DataRow(5, "2035-08-18T13:38:42.777+01:30")]
[DataRow(6, "2035-08-18T10:38:42.777-01:30")]
[DataRow(7, "2035-08-18T17:38:42.777+05:30")]
[DataRow(8, "2035-08-18T06:38:42.777-05:30")]
[DataTestMethod]
public void DateTimeFormatSerializationTest(int dateValueIndex, string expected)
{
DateTimeOffset initialDate = DateTimeValues[dateValueIndex];
[DataRow(0, "2035-08-18T12:08:42.777+00:00")]
[DataRow(1, "2035-08-18T13:08:42.777+01:00")]
[DataRow(2, "2035-08-18T11:08:42.777-01:00")]
[DataRow(3, "2035-08-18T17:08:42.777+05:00")]
[DataRow(4, "2035-08-18T07:08:42.777-05:00")]
[DataRow(5, "2035-08-18T13:38:42.777+01:30")]
[DataRow(6, "2035-08-18T10:38:42.777-01:30")]
[DataRow(7, "2035-08-18T17:38:42.777+05:30")]
[DataRow(8, "2035-08-18T06:38:42.777-05:30")]
[DataTestMethod]
public void DateTimeFormatSerializationTest(int dateValueIndex, string expected)
{
var initialDate = DateTimeValues[dateValueIndex];

string actual = JsonConvert.SerializeObject(initialDate);
var actual = JsonConvert.SerializeObject(initialDate);

Assert.AreEqual($"\"{expected}\"", actual);
}
Assert.AreEqual($"\"{expected}\"", actual);
}

public class TestObject
{
public DateTimeOffset Date { get; set; }
}
public class TestObject
{
public DateTimeOffset Date { get; set; }
}
}
}
61 changes: 29 additions & 32 deletions ApiClient.Tests/GeneralSetupTest.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net;
using ApiClient.Tests.Api;
using Infobip.Api.Client;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RestSharp;

namespace ApiClient.Tests
namespace ApiClient.Tests;

[TestClass]
public class GeneralSetupTest : ApiTest
{
[TestClass]
public class GeneralSetupTest : ApiTest
{
protected const string SMS_SEND_TEXT_ADVANCED_ENDPOINT = "/sms/2/text/advanced";
protected const string SMS_SEND_TEXT_ADVANCED_ENDPOINT = "/sms/2/text/advanced";

[TestMethod]
public void UsingExampleFromDocs()
{
string givenRequest = @"
[TestMethod]
public void UsingExampleFromDocs()
{
var givenRequest = @"
{
""messages"": [
{
Expand All @@ -31,7 +29,7 @@ public void UsingExampleFromDocs()
]
}";

string expectedResponse = @"
var expectedResponse = @"
{
""bulkId"": ""2034072219640523072"",
""messages"": [
Expand All @@ -49,25 +47,24 @@ public void UsingExampleFromDocs()
]
}";

SetUpPostRequest(SMS_SEND_TEXT_ADVANCED_ENDPOINT, new Dictionary<string, string> { { "param1", "val1" } }, givenRequest, expectedResponse, 200);
SetUpPostRequest(SMS_SEND_TEXT_ADVANCED_ENDPOINT, new Dictionary<string, string> { { "param1", "val1" } },
givenRequest, expectedResponse, 200);

var client = new RestClient(configuration!.BasePath + SMS_SEND_TEXT_ADVANCED_ENDPOINT)
{
UserAgent = "infobip-api-client-csharp/" + Configuration.Version,
Timeout = -1
};
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", configuration.ApiKeyWithPrefix);
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddHeader("Accept", "application/json");
request.AddQueryParameter("param1", "val1");
request.AddParameter("application/json", givenRequest, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual(expectedResponse, response.Content);
var client = new RestClient(configuration!.BasePath + SMS_SEND_TEXT_ADVANCED_ENDPOINT)
{
UserAgent = "infobip-api-client-csharp/" + Configuration.Version,
Timeout = -1
};
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", configuration.ApiKeyWithPrefix);
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddHeader("Accept", "application/json");
request.AddQueryParameter("param1", "val1");
request.AddParameter("application/json", givenRequest, ParameterType.RequestBody);
var response = client.Execute(request);

}
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual(expectedResponse, response.Content);
}
}
}

0 comments on commit 8a83ad2

Please sign in to comment.