Skip to content

Commit 58dddb5

Browse files
author
Morten Turn Pedersen
authored
Merge pull request #97 from nstack-io/develop
1.3.1 release
2 parents d15e314 + 493fb14 commit 58dddb5

File tree

8 files changed

+428
-71
lines changed

8 files changed

+428
-71
lines changed

DemoNStack/DemoNStack/Controllers/NStackController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected NStackController(INStackAppService nStackAppService)
2020

2121
protected async Task<DataMetaWrapper<Translation>> GetTranslations()
2222
{
23-
return await NStackAppService.GetResourceAsync<Translation>(Request.GetCurrentLanguage(), NStackPlatform.Web, "1.3.0");
23+
return await NStackAppService.GetResourceAsync<Translation>(Request.GetCurrentLanguage(), NStackPlatform.Web, "1.3.1");
2424
}
2525

2626
protected Guid GetUserId()

DemoNStack/DemoNStack/DemoNStack.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="NStack.SDK" Version="1.3.0" />
8+
<PackageReference Include="NStack.SDK" Version="1.3.1" />
99
</ItemGroup>
1010

1111
</Project>

DemoNStack/DemoNStack/ViewComponents/NavbarViewComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public NavbarViewComponent(INStackAppService nStackAppService, IMemoryCache memo
2323

2424
public async Task<IViewComponentResult> InvokeAsync()
2525
{
26-
var res = await NStackAppService.GetResourceAsync<Translation>(Request.GetCurrentLanguage(), NStackPlatform.Web, "1.3.0");
26+
var res = await NStackAppService.GetResourceAsync<Translation>(Request.GetCurrentLanguage(), NStackPlatform.Web, "1.3.1");
2727

2828
var viewModel = new NavbarViewModel
2929
{
Binary file not shown.
Lines changed: 229 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,265 @@
11
using Microsoft.Extensions.Caching.Memory;
22
using Microsoft.Extensions.DependencyInjection;
3+
using Moq;
34
using NStack.SDK.Models;
4-
using NStack.SDK.Repositories.Implementation;
5+
using NStack.SDK.Repositories;
6+
using NStack.SDK.Services;
57
using NStack.SDK.Services.Implementation;
8+
using NStack.Tests.Translations;
69
using NUnit.Framework;
10+
using RestSharp;
711
using System;
12+
using System.Linq;
13+
using System.Net;
814
using System.Threading.Tasks;
915

1016
namespace NStack.SDK.Tests
1117
{
1218
public class NStackAppServiceTests
1319
{
1420
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";
1527

1628
[SetUp]
1729
public void Initialize()
1830
{
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>
2041
{
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));
2465

2566
var services = new ServiceCollection();
2667
services.AddMemoryCache();
2768
var serviceProvider = services.BuildServiceProvider();
2869

29-
var localizationService = new NStackLocalizeService(repository);
70+
_memoryCache = serviceProvider.GetService<IMemoryCache>();
3071

31-
_service = new NStackAppService(repository, localizationService, serviceProvider.GetService<IMemoryCache>());
72+
_service = new NStackAppService(_repository.Object, _localizeService.Object, _memoryCache);
3273
}
3374

75+
#region GetResourceAsync
3476
[Test]
35-
public async Task DeleteMe()
77+
public void GetResourceAsyncNullLocaleThrowsException()
3678
{
37-
var userId = Guid.NewGuid();
79+
var exception = Assert.ThrowsAsync<ArgumentException>(() => _service.GetResourceAsync(null, NStackPlatform.Web, "1.0.0"));
3880

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);
4182
}
4283

4384
[Test]
44-
public async Task DeleteMe2()
85+
public void GetResourceAsyncEmptyLocaleThrowsException()
4586
{
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+
});
48263
}
49264
}
50265
}

0 commit comments

Comments
 (0)