|
| 1 | +/* |
| 2 | + * Copyright 2022 Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using Moq; |
| 18 | +using NUnit.Framework; |
| 19 | +using OptimizelySDK.ErrorHandler; |
| 20 | +using OptimizelySDK.Logger; |
| 21 | +using OptimizelySDK.Odp; |
| 22 | +using OptimizelySDK.Odp.Entity; |
| 23 | +using System.Collections.Generic; |
| 24 | +using System.Net; |
| 25 | + |
| 26 | +namespace OptimizelySDK.Tests.OdpTests |
| 27 | +{ |
| 28 | + [TestFixture] |
| 29 | + public class OdpEventApiManagerTest |
| 30 | + { |
| 31 | + private const string VALID_ODP_PUBLIC_KEY = "a-valid-odp-public-key"; |
| 32 | + private const string ODP_REST_API_HOST = "https://api.example.com"; |
| 33 | + |
| 34 | + private readonly List<OdpEvent> _odpEvents = new List<OdpEvent>(); |
| 35 | + |
| 36 | + private Mock<IErrorHandler> _mockErrorHandler; |
| 37 | + private Mock<ILogger> _mockLogger; |
| 38 | + |
| 39 | + [TestFixtureSetUp] |
| 40 | + public void Setup() |
| 41 | + { |
| 42 | + _mockErrorHandler = new Mock<IErrorHandler>(); |
| 43 | + _mockLogger = new Mock<ILogger>(); |
| 44 | + _mockLogger.Setup(i => i.Log(It.IsAny<LogLevel>(), It.IsAny<string>())); |
| 45 | + |
| 46 | + _odpEvents.Add(new OdpEvent("t1", "a1", |
| 47 | + new Dictionary<string, string> |
| 48 | + { |
| 49 | + { |
| 50 | + "id-key-1", "id-value-1" |
| 51 | + }, |
| 52 | + }, |
| 53 | + new Dictionary<string, object> |
| 54 | + { |
| 55 | + { |
| 56 | + "key11", "value-1" |
| 57 | + }, |
| 58 | + { |
| 59 | + "key12", true |
| 60 | + }, |
| 61 | + { |
| 62 | + "key13", 3.5 |
| 63 | + }, |
| 64 | + { |
| 65 | + "key14", null |
| 66 | + }, |
| 67 | + } |
| 68 | + )); |
| 69 | + _odpEvents.Add(new OdpEvent("t2", "a2", |
| 70 | + new Dictionary<string, string> |
| 71 | + { |
| 72 | + { |
| 73 | + "id-key-2", "id-value-2" |
| 74 | + }, |
| 75 | + }, new Dictionary<string, object> |
| 76 | + { |
| 77 | + { |
| 78 | + "key2", "value-2" |
| 79 | + }, |
| 80 | + } |
| 81 | + )); |
| 82 | + } |
| 83 | + |
| 84 | + [Test] |
| 85 | + public void ShouldSendEventsSuccessfullyAndNotSuggestRetry() |
| 86 | + { |
| 87 | + var httpClient = HttpClientTestUtil.MakeHttpClient(HttpStatusCode.OK); |
| 88 | + var manger = |
| 89 | + new OdpEventApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient); |
| 90 | + |
| 91 | + var shouldRetry = manger.SendEvents(VALID_ODP_PUBLIC_KEY, ODP_REST_API_HOST, |
| 92 | + _odpEvents); |
| 93 | + |
| 94 | + Assert.IsFalse(shouldRetry); |
| 95 | + } |
| 96 | + |
| 97 | + [Test] |
| 98 | + public void ShouldNotSuggestRetryFor400HttpResponse() |
| 99 | + { |
| 100 | + var httpClient = HttpClientTestUtil.MakeHttpClient(HttpStatusCode.BadRequest); |
| 101 | + var manger = |
| 102 | + new OdpEventApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient); |
| 103 | + |
| 104 | + var shouldRetry = manger.SendEvents(VALID_ODP_PUBLIC_KEY, ODP_REST_API_HOST, |
| 105 | + _odpEvents); |
| 106 | + |
| 107 | + Assert.IsFalse(shouldRetry); |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public void ShouldSuggestRetryFor500HttpResponse() |
| 112 | + { |
| 113 | + var httpClient = HttpClientTestUtil.MakeHttpClient(HttpStatusCode.InternalServerError); |
| 114 | + var manger = |
| 115 | + new OdpEventApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient); |
| 116 | + |
| 117 | + var shouldRetry = manger.SendEvents(VALID_ODP_PUBLIC_KEY, ODP_REST_API_HOST, |
| 118 | + _odpEvents); |
| 119 | + |
| 120 | + Assert.IsTrue(shouldRetry); |
| 121 | + } |
| 122 | + |
| 123 | + [Test] |
| 124 | + public void ShouldSuggestRetryForNetworkTimeout() { |
| 125 | + var httpClient = HttpClientTestUtil.MakeHttpClientWithTimeout(); |
| 126 | + var manger = |
| 127 | + new OdpEventApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient); |
| 128 | + |
| 129 | + var shouldRetry = manger.SendEvents(VALID_ODP_PUBLIC_KEY, ODP_REST_API_HOST, |
| 130 | + _odpEvents); |
| 131 | + |
| 132 | + Assert.IsTrue(shouldRetry);} |
| 133 | + } |
| 134 | +} |
0 commit comments