Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ODP REST API for Sending ODP Events #315

Merged
merged 14 commits into from
Oct 25, 2022
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
73 changes: 73 additions & 0 deletions OptimizelySDK.Tests/OdpTests/HttpClientTestUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2022 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Moq;
using Moq.Protected;
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace OptimizelySDK.Tests.OdpTests
{
/// <summary>
/// Shared utility methods used for stubbing HttpClient instances
/// </summary>
public static class HttpClientTestUtil
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Create an HttpClient instance that returns an expected response
/// </summary>
/// <param name="statusCode">Http Status Code to return</param>
/// <param name="content">Message body content to return</param>
/// <returns>HttpClient instance that will return desired HttpResponseMessage</returns>
public static HttpClient MakeHttpClient(HttpStatusCode statusCode,
string content = default
)
{
var response = new HttpResponseMessage(statusCode);

if (!string.IsNullOrWhiteSpace(content))
{
response.Content = new StringContent(content);
}

var mockedHandler = new Mock<HttpMessageHandler>();
mockedHandler.Protected().Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()).
ReturnsAsync(response);
return new HttpClient(mockedHandler.Object);
}

/// <summary>
/// Create an HttpClient instance that will timeout for SendAsync calls
/// </summary>
/// <returns>HttpClient instance that throw TimeoutException</returns>
public static HttpClient MakeHttpClientWithTimeout()
{
var mockedHandler = new Mock<HttpMessageHandler>();
mockedHandler.Protected().Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()).
Throws<TimeoutException>();
return new HttpClient(mockedHandler.Object);
}
}
}
1 change: 0 additions & 1 deletion OptimizelySDK.Tests/OdpTests/LruCacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using OptimizelySDK.Odp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace OptimizelySDK.Tests.OdpTests
Expand Down
134 changes: 134 additions & 0 deletions OptimizelySDK.Tests/OdpTests/OdpEventApiManagerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright 2022 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Moq;
using NUnit.Framework;
using OptimizelySDK.ErrorHandler;
using OptimizelySDK.Logger;
using OptimizelySDK.Odp;
using OptimizelySDK.Odp.Entity;
using System.Collections.Generic;
using System.Net;

namespace OptimizelySDK.Tests.OdpTests
{
[TestFixture]
public class OdpEventApiManagerTest
{
private const string VALID_ODP_PUBLIC_KEY = "a-valid-odp-public-key";
private const string ODP_REST_API_HOST = "https://api.example.com";

private readonly List<OdpEvent> _odpEvents = new List<OdpEvent>();

private Mock<IErrorHandler> _mockErrorHandler;
private Mock<ILogger> _mockLogger;

[TestFixtureSetUp]
public void Setup()
{
_mockErrorHandler = new Mock<IErrorHandler>();
_mockLogger = new Mock<ILogger>();
_mockLogger.Setup(i => i.Log(It.IsAny<LogLevel>(), It.IsAny<string>()));

_odpEvents.Add(new OdpEvent("t1", "a1",
new Dictionary<string, string>
{
{
"id-key-1", "id-value-1"
},
},
new Dictionary<string, object>
{
{
"key11", "value-1"
},
{
"key12", true
},
{
"key13", 3.5
},
{
"key14", null
},
}
));
_odpEvents.Add(new OdpEvent("t2", "a2",
new Dictionary<string, string>
{
{
"id-key-2", "id-value-2"
},
}, new Dictionary<string, object>
{
{
"key2", "value-2"
},
}
));
}

[Test]
public void ShouldSendEventsSuccessfullyAndNotSuggestRetry()
{
var httpClient = HttpClientTestUtil.MakeHttpClient(HttpStatusCode.OK);
var manger =
new OdpEventApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient);

var shouldRetry = manger.SendEvents(VALID_ODP_PUBLIC_KEY, ODP_REST_API_HOST,
_odpEvents);

Assert.IsFalse(shouldRetry);
}

[Test]
public void ShouldNotSuggestRetryFor400HttpResponse()
{
var httpClient = HttpClientTestUtil.MakeHttpClient(HttpStatusCode.BadRequest);
var manger =
new OdpEventApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient);

var shouldRetry = manger.SendEvents(VALID_ODP_PUBLIC_KEY, ODP_REST_API_HOST,
_odpEvents);

Assert.IsFalse(shouldRetry);
}

[Test]
public void ShouldSuggestRetryFor500HttpResponse()
{
var httpClient = HttpClientTestUtil.MakeHttpClient(HttpStatusCode.InternalServerError);
var manger =
new OdpEventApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient);

var shouldRetry = manger.SendEvents(VALID_ODP_PUBLIC_KEY, ODP_REST_API_HOST,
_odpEvents);

Assert.IsTrue(shouldRetry);
}

[Test]
public void ShouldSuggestRetryForNetworkTimeout() {
var httpClient = HttpClientTestUtil.MakeHttpClientWithTimeout();
var manger =
new OdpEventApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient);

var shouldRetry = manger.SendEvents(VALID_ODP_PUBLIC_KEY, ODP_REST_API_HOST,
_odpEvents);

Assert.IsTrue(shouldRetry);}
}
}
Loading