This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #873 from keiji/make_check_version_service
Make CheckVersion as a service.
- Loading branch information
Showing
10 changed files
with
216 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Covid19Radar.Resources; | ||
using Covid19Radar.Services.Logs; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Covid19Radar.Services | ||
{ | ||
public interface ICheckVersionService | ||
{ | ||
public Task<bool> IsUpdateVersionExistAsync(); | ||
} | ||
|
||
public class CheckVersionServiceNop : ICheckVersionService | ||
{ | ||
public Task<bool> IsUpdateVersionExistAsync() | ||
=> Task.FromResult(false); | ||
} | ||
|
||
public class CheckVersionService : ICheckVersionService | ||
{ | ||
private readonly HttpClient _httpClient; | ||
private readonly IEssentialsService _essentialsService; | ||
private readonly ILoggerService _loggerService; | ||
|
||
public CheckVersionService( | ||
IHttpClientService httpClientService, | ||
IEssentialsService essentialsService, | ||
ILoggerService loggerService | ||
) | ||
{ | ||
_httpClient = httpClientService.Create(); | ||
_essentialsService = essentialsService; | ||
_loggerService = loggerService; | ||
} | ||
|
||
public async Task<bool> IsUpdateVersionExistAsync() | ||
{ | ||
_loggerService.StartMethod(); | ||
|
||
var uri = AppResources.UrlVersion; | ||
try | ||
{ | ||
var json = await _httpClient.GetStringAsync(uri); | ||
var key = _essentialsService.IsIos ? "ios" : "android"; | ||
var versionString = JObject.Parse(json).Value<string>(key); | ||
|
||
return new Version(versionString).CompareTo(new Version(_essentialsService.AppVersion)) > 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_loggerService.Exception("Failed to check version.", ex); | ||
} | ||
finally | ||
{ | ||
_loggerService.EndMethod(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
Covid19Radar/Tests/Covid19Radar.UnitTests/Files/check_version1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"version": "2.0.1", | ||
"android": "2.0.2", | ||
"ios": "2.0.3" | ||
} |
91 changes: 91 additions & 0 deletions
91
Covid19Radar/Tests/Covid19Radar.UnitTests/Services/CheckVersionServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
using System.IO; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
using Covid19Radar.Services; | ||
using Covid19Radar.Services.Logs; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Covid19Radar.UnitTests.Services | ||
{ | ||
public class CheckVersionServiceTests | ||
{ | ||
private const string JSON_VERSION1 = "check_version1.json"; | ||
|
||
#region Instance Properties | ||
|
||
private readonly MockRepository _mockRepository; | ||
private readonly Mock<ILoggerService> _mockLoggerService; | ||
private readonly Mock<IHttpClientService> _mockClientService; | ||
private readonly Mock<IEssentialsService> _mockEssentialsService; | ||
|
||
#endregion | ||
|
||
#region Constructors | ||
|
||
public CheckVersionServiceTests() | ||
{ | ||
_mockRepository = new MockRepository(MockBehavior.Default); | ||
_mockLoggerService = _mockRepository.Create<ILoggerService>(); | ||
_mockClientService = _mockRepository.Create<IHttpClientService>(); | ||
_mockEssentialsService = _mockRepository.Create<IEssentialsService>(); | ||
} | ||
|
||
#endregion | ||
|
||
#region Other Private Methods | ||
|
||
private CheckVersionService CreateService() | ||
{ | ||
return new CheckVersionService( | ||
_mockClientService.Object, | ||
_mockEssentialsService.Object, | ||
_mockLoggerService.Object | ||
); | ||
} | ||
|
||
private static string GetTestJson(string fileName) | ||
{ | ||
var path = TestDataUtils.GetLocalFilePath(fileName); | ||
using (var reader = File.OpenText(path)) | ||
{ | ||
return reader.ReadToEnd(); | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
[Theory] | ||
[InlineData(false, "2.0.1", true)] | ||
[InlineData(false, "2.0.2", false)] | ||
[InlineData(true, "2.0.1", true)] | ||
[InlineData(true, "2.0.2", true)] | ||
[InlineData(true, "2.0.3", false)] | ||
private async void IsUpdate(bool isIos, string version, bool expected) | ||
{ | ||
string content = GetTestJson(JSON_VERSION1); | ||
|
||
var jsonContent = new StringContent( | ||
content, | ||
Encoding.UTF8, | ||
"application/json" | ||
); | ||
var client = HttpClientUtils.CreateHttpClient(HttpStatusCode.OK, jsonContent); | ||
_mockClientService.Setup(x => x.Create()).Returns(client); | ||
|
||
_mockEssentialsService.Setup(x => x.IsIos).Returns(isIos); | ||
_mockEssentialsService.Setup(x => x.AppVersion).Returns(version); | ||
|
||
ICheckVersionService service = CreateService(); | ||
|
||
bool isUpdated = await service.IsUpdateVersionExistAsync(); | ||
|
||
Assert.Equal(expected, isUpdated); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters