|
1 | 1 | using Microsoft.Extensions.Caching.Memory; |
2 | 2 | using Microsoft.Extensions.DependencyInjection; |
| 3 | +using Moq; |
3 | 4 | using NStack.SDK.Models; |
4 | | -using NStack.SDK.Repositories.Implementation; |
| 5 | +using NStack.SDK.Repositories; |
| 6 | +using NStack.SDK.Services; |
5 | 7 | using NStack.SDK.Services.Implementation; |
| 8 | +using NStack.Tests.Translations; |
6 | 9 | using NUnit.Framework; |
| 10 | +using RestSharp; |
7 | 11 | using System; |
| 12 | +using System.Linq; |
| 13 | +using System.Net; |
8 | 14 | using System.Threading.Tasks; |
9 | 15 |
|
10 | 16 | namespace NStack.SDK.Tests |
11 | 17 | { |
12 | 18 | public class NStackAppServiceTests |
13 | 19 | { |
14 | 20 | private NStackAppService _service; |
| 21 | + private Mock<INStackRepository> _repository; |
| 22 | + private Mock<INStackLocalizeService> _localizeService; |
| 23 | + private IMemoryCache _memoryCache; |
| 24 | + private DataMetaWrapper<TranslationData> _danish; |
| 25 | + private const int LanguageId = 42; |
| 26 | + private const string LanguageLocale = "da-DK"; |
15 | 27 |
|
16 | 28 | [SetUp] |
17 | 29 | public void Initialize() |
18 | 30 | { |
19 | | - var repository = new NStackRepository(new NStackConfiguration |
| 31 | + _repository = new Mock<INStackRepository>(MockBehavior.Strict); |
| 32 | + _repository.Setup(r => r.DoRequestAsync<DataAppOpenWrapper>(It.Is<IRestRequest>(s => s.Resource.EndsWith("api/v2/open")), It.IsAny<Action<HttpStatusCode>>())) |
| 33 | + .Returns(GetAppOpenMock); |
| 34 | + |
| 35 | + var danish = new TranslationData(); |
| 36 | + var defaultSection = new DefaultSection(); |
| 37 | + defaultSection.TryAdd("text", "Jeg er på dansk"); |
| 38 | + danish.TryAdd("default", defaultSection); |
| 39 | + |
| 40 | + _danish = new DataMetaWrapper<TranslationData> |
20 | 41 | { |
21 | | - ApiKey = "qd1GiPnq8sJuChbFxjOQxV9t1AN71oIMBuWF", |
22 | | - ApplicationId = "9vJhjXzSBUxBOuQx2B2mFIZSoa2aK4UJzt7y" |
23 | | - }); |
| 42 | + Data = danish, |
| 43 | + Meta = new MetaData |
| 44 | + { |
| 45 | + Language = new Language |
| 46 | + { |
| 47 | + Direction = LanguageDirection.LRM, |
| 48 | + Id = LanguageId, |
| 49 | + IsBestFit = true, |
| 50 | + IsDefault = true, |
| 51 | + Locale = LanguageLocale, |
| 52 | + Name = "Danish" |
| 53 | + }, |
| 54 | + Platform = new ResourcePlatform |
| 55 | + { |
| 56 | + Id = LanguageId, |
| 57 | + Slug = NStackPlatform.Web |
| 58 | + } |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + _localizeService = new Mock<INStackLocalizeService>(MockBehavior.Strict); |
| 63 | + _localizeService.Setup(r => r.GetResourceAsync<TranslationData>(It.Is<int>(id => id == LanguageId))) |
| 64 | + .Returns(Task.FromResult(_danish)); |
24 | 65 |
|
25 | 66 | var services = new ServiceCollection(); |
26 | 67 | services.AddMemoryCache(); |
27 | 68 | var serviceProvider = services.BuildServiceProvider(); |
28 | 69 |
|
29 | | - var localizationService = new NStackLocalizeService(repository); |
| 70 | + _memoryCache = serviceProvider.GetService<IMemoryCache>(); |
30 | 71 |
|
31 | | - _service = new NStackAppService(repository, localizationService, serviceProvider.GetService<IMemoryCache>()); |
| 72 | + _service = new NStackAppService(_repository.Object, _localizeService.Object, _memoryCache); |
32 | 73 | } |
33 | 74 |
|
| 75 | + #region GetResourceAsync |
34 | 76 | [Test] |
35 | | - public async Task DeleteMe() |
| 77 | + public void GetResourceAsyncNullLocaleThrowsException() |
36 | 78 | { |
37 | | - var userId = Guid.NewGuid(); |
| 79 | + var exception = Assert.ThrowsAsync<ArgumentException>(() => _service.GetResourceAsync(null, NStackPlatform.Web, "1.0.0")); |
38 | 80 |
|
39 | | - var data = await _service.AppOpenAsync(NStackPlatform.Backend, userId, "1.0.0", "development", true, false); |
40 | | - var data2 = await _service.AppOpenAsync(NStackPlatform.Backend, userId, "1.0.0", "development", true, false); |
| 81 | + Assert.AreEqual("locale", exception.ParamName); |
41 | 82 | } |
42 | 83 |
|
43 | 84 | [Test] |
44 | | - public async Task DeleteMe2() |
| 85 | + public void GetResourceAsyncEmptyLocaleThrowsException() |
45 | 86 | { |
46 | | - var data = await _service.GetResourceAsync("da-DK", NStackPlatform.Web, "1.0.0", "development", true, false); |
47 | | - var data2 = await _service.GetResourceAsync("da-DK", NStackPlatform.Web, "1.0.0", "development", true, false); |
| 87 | + var exception = Assert.ThrowsAsync<ArgumentException>(() => _service.GetResourceAsync(string.Empty, NStackPlatform.Web, "1.0.0")); |
| 88 | + |
| 89 | + Assert.AreEqual("locale", exception.ParamName); |
| 90 | + } |
| 91 | + |
| 92 | + [Test] |
| 93 | + public void GetResourceAsyncTrueDevelopmentAndProductionThrowsException() |
| 94 | + { |
| 95 | + Assert.ThrowsAsync<ArgumentException>(() => _service.GetResourceAsync("da-DK", NStackPlatform.Web, "1.0.0", developmentEnvironment: true, productionEnvironment: true)); |
| 96 | + } |
| 97 | + |
| 98 | + [Test] |
| 99 | + public async Task GetResourceAsyncFetchesLocalization() |
| 100 | + { |
| 101 | + ResetCounters(); |
| 102 | + |
| 103 | + DataMetaWrapper<TranslationData> resource = await _service.GetResourceAsync<TranslationData>(LanguageLocale, NStackPlatform.Web, "1.0.0"); |
| 104 | + |
| 105 | + Assert.AreEqual(_danish, resource); |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public async Task GetResourceAsyncDoesntFetchLocalizationOnSecondCall() |
| 110 | + { |
| 111 | + ResetCounters(); |
| 112 | + |
| 113 | + DataMetaWrapper<TranslationData> resource = await _service.GetResourceAsync<TranslationData>(LanguageLocale, NStackPlatform.Web, "1.0.0"); |
| 114 | + DataMetaWrapper<TranslationData> resource2 = await _service.GetResourceAsync<TranslationData>(LanguageLocale, NStackPlatform.Web, "1.0.0"); |
| 115 | + |
| 116 | + _repository.Verify(s => s.DoRequestAsync<DataAppOpenWrapper>(It.IsAny<IRestRequest>(), It.IsAny<Action<HttpStatusCode>>()), Times.Once()); |
| 117 | + _localizeService.Verify(s => s.GetResourceAsync<TranslationData>(LanguageId), Times.Once()); |
| 118 | + } |
| 119 | + |
| 120 | + [Test] |
| 121 | + public async Task GetResourceAsyncFetchLocalizationOnSecondCallOnExpiry() |
| 122 | + { |
| 123 | + ResetCounters(); |
| 124 | + |
| 125 | + DataMetaWrapper<TranslationData> resource = await _service.GetResourceAsync<TranslationData>(LanguageLocale, NStackPlatform.Web, "1.0.0"); |
| 126 | + |
| 127 | + _memoryCache.Set<DateTime>("nstack-last-updated", DateTime.UtcNow.AddHours(-2)); |
| 128 | + |
| 129 | + DataMetaWrapper<TranslationData> resource2 = await _service.GetResourceAsync<TranslationData>(LanguageLocale, NStackPlatform.Web, "1.0.0"); |
| 130 | + |
| 131 | + _repository.Verify(s => s.DoRequestAsync<DataAppOpenWrapper>(It.IsAny<IRestRequest>(), It.IsAny<Action<HttpStatusCode>>()), Times.Exactly(2)); |
| 132 | + _localizeService.Verify(s => s.GetResourceAsync<TranslationData>(LanguageId), Times.Exactly(2)); |
| 133 | + } |
| 134 | + |
| 135 | + [Test] |
| 136 | + public async Task GetResourceAsyncHonoursShouldUpdate() |
| 137 | + { |
| 138 | + ResetCounters(); |
| 139 | + |
| 140 | + DataMetaWrapper<TranslationData> resource = await _service.GetResourceAsync<TranslationData>(LanguageLocale, NStackPlatform.Web, "1.0.0"); |
| 141 | + |
| 142 | + _memoryCache.Set<DateTime>("nstack-last-updated", DateTime.UtcNow.AddHours(-2)); |
| 143 | + |
| 144 | + DataMetaWrapper<TranslationData> resource2 = await _service.GetResourceAsync<TranslationData>(LanguageLocale, NStackPlatform.Web, "1.0.0"); |
| 145 | + |
| 146 | + _memoryCache.Set<DateTime>("nstack-last-updated", DateTime.UtcNow.AddHours(-2)); |
| 147 | + |
| 148 | + DataMetaWrapper<TranslationData> resource3 = await _service.GetResourceAsync<TranslationData>(LanguageLocale, NStackPlatform.Web, "1.0.0"); |
| 149 | + |
| 150 | + _repository.Verify(s => s.DoRequestAsync<DataAppOpenWrapper>(It.IsAny<IRestRequest>(), It.IsAny<Action<HttpStatusCode>>()), Times.Exactly(3)); |
| 151 | + _localizeService.Verify(s => s.GetResourceAsync<TranslationData>(LanguageId), Times.Exactly(2)); |
| 152 | + } |
| 153 | + #endregion |
| 154 | + |
| 155 | + #region GetDefaultResourceAsync |
| 156 | + [Test] |
| 157 | + public void GetDefaultResourceAsyncTrueDevelopmentAndProductionThrowsException() |
| 158 | + { |
| 159 | + Assert.ThrowsAsync<ArgumentException>(() => _service.GetDefaultResourceAsync(NStackPlatform.Web, "1.0.0", developmentEnvironment: true, productionEnvironment: true)); |
| 160 | + } |
| 161 | + |
| 162 | + [Test] |
| 163 | + public async Task GetDefaultResourceAsyncFetchesLocalization() |
| 164 | + { |
| 165 | + ResetCounters(); |
| 166 | + |
| 167 | + DataMetaWrapper<TranslationData> resource = await _service.GetDefaultResourceAsync<TranslationData>(NStackPlatform.Web, "1.0.0"); |
| 168 | + |
| 169 | + Assert.AreEqual(_danish, resource); |
| 170 | + } |
| 171 | + |
| 172 | + [Test] |
| 173 | + public async Task GetDefaultResourceAsyncDoesntFetchLocalizationOnSecondCall() |
| 174 | + { |
| 175 | + ResetCounters(); |
| 176 | + |
| 177 | + DataMetaWrapper<TranslationData> resource = await _service.GetDefaultResourceAsync<TranslationData>(NStackPlatform.Web, "1.0.0"); |
| 178 | + DataMetaWrapper<TranslationData> resource2 = await _service.GetDefaultResourceAsync<TranslationData>(NStackPlatform.Web, "1.0.0"); |
| 179 | + |
| 180 | + _repository.Verify(s => s.DoRequestAsync<DataAppOpenWrapper>(It.IsAny<IRestRequest>(), It.IsAny<Action<HttpStatusCode>>()), Times.Once()); |
| 181 | + _localizeService.Verify(s => s.GetResourceAsync<TranslationData>(LanguageId), Times.Once()); |
| 182 | + } |
| 183 | + |
| 184 | + [Test] |
| 185 | + public async Task GetDefaultResourceAsyncFetchLocalizationOnSecondCallOnExpiry() |
| 186 | + { |
| 187 | + ResetCounters(); |
| 188 | + |
| 189 | + DataMetaWrapper<TranslationData> resource = await _service.GetDefaultResourceAsync<TranslationData>(NStackPlatform.Web, "1.0.0"); |
| 190 | + |
| 191 | + _memoryCache.Set<DateTime>("nstack-last-updated", DateTime.UtcNow.AddHours(-2)); |
| 192 | + |
| 193 | + DataMetaWrapper<TranslationData> resource2 = await _service.GetDefaultResourceAsync<TranslationData>(NStackPlatform.Web, "1.0.0"); |
| 194 | + |
| 195 | + _repository.Verify(s => s.DoRequestAsync<DataAppOpenWrapper>(It.IsAny<IRestRequest>(), It.IsAny<Action<HttpStatusCode>>()), Times.Exactly(2)); |
| 196 | + _localizeService.Verify(s => s.GetResourceAsync<TranslationData>(LanguageId), Times.Exactly(2)); |
| 197 | + } |
| 198 | + |
| 199 | + [Test] |
| 200 | + public async Task GetDefaultResourceAsyncHonoursShouldUpdate() |
| 201 | + { |
| 202 | + ResetCounters(); |
| 203 | + |
| 204 | + DataMetaWrapper<TranslationData> resource = await _service.GetDefaultResourceAsync<TranslationData>(NStackPlatform.Web, "1.0.0"); |
| 205 | + |
| 206 | + _memoryCache.Set<DateTime>("nstack-last-updated", DateTime.UtcNow.AddHours(-2)); |
| 207 | + |
| 208 | + DataMetaWrapper<TranslationData> resource2 = await _service.GetDefaultResourceAsync<TranslationData>(NStackPlatform.Web, "1.0.0"); |
| 209 | + |
| 210 | + _memoryCache.Set<DateTime>("nstack-last-updated", DateTime.UtcNow.AddHours(-2)); |
| 211 | + |
| 212 | + DataMetaWrapper<TranslationData> resource3 = await _service.GetDefaultResourceAsync<TranslationData>(NStackPlatform.Web, "1.0.0"); |
| 213 | + |
| 214 | + _repository.Verify(s => s.DoRequestAsync<DataAppOpenWrapper>(It.IsAny<IRestRequest>(), It.IsAny<Action<HttpStatusCode>>()), Times.Exactly(3)); |
| 215 | + _localizeService.Verify(s => s.GetResourceAsync<TranslationData>(LanguageId), Times.Exactly(2)); |
| 216 | + } |
| 217 | + #endregion |
| 218 | + |
| 219 | + private void ResetCounters() |
| 220 | + { |
| 221 | + _memoryCache.Remove("nstack-last-updated"); |
| 222 | + _memoryCache.Remove($"nstack-localization-{LanguageLocale}"); |
| 223 | + _memoryCache.Remove("nstack-localization-default"); |
| 224 | + appOpenCount = 0; |
| 225 | + } |
| 226 | + |
| 227 | + private static int appOpenCount = 0; |
| 228 | + private Task<DataAppOpenWrapper> GetAppOpenMock() |
| 229 | + { |
| 230 | + return Task.FromResult(new DataAppOpenWrapper |
| 231 | + { |
| 232 | + Data = new AppOpenData |
| 233 | + { |
| 234 | + Count = 1, |
| 235 | + CreatedAt = DateTime.UtcNow, |
| 236 | + Localize = new [] |
| 237 | + { |
| 238 | + new ResourceData |
| 239 | + { |
| 240 | + Id = LanguageId, |
| 241 | + Language = new Language |
| 242 | + { |
| 243 | + Direction = LanguageDirection.LRM, |
| 244 | + Id = LanguageId, |
| 245 | + IsBestFit = true, |
| 246 | + IsDefault = true, |
| 247 | + Locale = LanguageLocale, |
| 248 | + Name = "Danish" |
| 249 | + }, |
| 250 | + LastUpdatedAt = DateTime.UtcNow.AddMonths(-1), |
| 251 | + ShouldUpdate = appOpenCount++ < 2, |
| 252 | + Url = "https://nstack.io" |
| 253 | + } |
| 254 | + }, |
| 255 | + Platform = NStackPlatform.Web, |
| 256 | + Terms = Enumerable.Empty<AppOpenTerms>() |
| 257 | + }, |
| 258 | + Meta = new AppOpenMetaData |
| 259 | + { |
| 260 | + AcceptLanguage = null |
| 261 | + } |
| 262 | + }); |
48 | 263 | } |
49 | 264 | } |
50 | 265 | } |
0 commit comments