Skip to content

Commit

Permalink
comment on the issue#179
Browse files Browse the repository at this point in the history
  • Loading branch information
i-maruyama committed May 15, 2021
1 parent cc4b4a4 commit 1847252
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 9 deletions.
121 changes: 116 additions & 5 deletions Covid19Radar/Covid19Radar/Services/HttpDataServiceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,45 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using Covid19Radar.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Linq;
using System.Net.Http;
using Covid19Radar.Common;

namespace Covid19Radar.Services
{
class HttpDataServiceMock : IHttpDataService
{
private readonly HttpClient downloadClient;
public HttpDataServiceMock(IHttpClientService httpClientService)
{
if (DownloadRequired())
{
downloadClient = httpClientService.Create();
}
}

// copy from ./HttpDataService.cs
private async Task<string> GetCdnAsync(string url, CancellationToken cancellationToken)
{
Task<HttpResponseMessage> response = downloadClient.GetAsync(url, cancellationToken);
HttpResponseMessage result = await response;
await result.Content.ReadAsStringAsync();

if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
return await result.Content.ReadAsStringAsync();
}
return null;
}

public Task MigrateFromUserData(UserDataModel userData)
{
return Task.CompletedTask;
Expand All @@ -37,13 +65,96 @@ Task<Stream> IHttpDataService.GetTemporaryExposureKey(string url, CancellationTo
});
}

Task<List<TemporaryExposureKeyExportFileModel>> IHttpDataService.GetTemporaryExposureKeyList(string region, CancellationToken cancellationToken)
private TemporaryExposureKeyExportFileModel TestDat(long created)
{
var tmp = new TemporaryExposureKeyExportFileModel();
tmp.Region = "440";
tmp.Url = "testUrl";
tmp.Created = created;
return (tmp);
}

private long TimeBefore(int day)
{
return (new DateTimeOffset(DateTime.UtcNow.ToLocalTime().AddDays(day)).ToUnixTimeMilliseconds());
}

private long NightBefore(int day)
{
DateTime d = DateTime.UtcNow.ToLocalTime().AddDays(day);
return (new DateTimeOffset(new DateTime(d.Year, d.Month, d.Day, 0, 1, 2, 3)).ToUnixTimeMilliseconds());
}

private List<TemporaryExposureKeyExportFileModel> DataPreset(int dataVer)
{
return Task.Factory.StartNew<List<TemporaryExposureKeyExportFileModel>>(() =>
/* DataPreset for TEK FileModel
0(default): nothing (default for v1.2.3)
1: real time
2: last night
please add
*/
switch (dataVer)
{
Debug.WriteLine("HttpDataServiceMock::GetTemporaryExposureKeyList called");
return new List<TemporaryExposureKeyExportFileModel>();
});
case 2:
return new List<TemporaryExposureKeyExportFileModel> { TestDat(NightBefore(-1)), TestDat(NightBefore(0)) };
case 1:
return new List<TemporaryExposureKeyExportFileModel> { TestDat(TimeBefore(-1)), TestDat(TimeBefore(0)) };
case 0:
default:
return new List<TemporaryExposureKeyExportFileModel>();
}
}

private List<TemporaryExposureKeyExportFileModel> Data()
{
int dataVer = -1;
string url = AppSettings.Instance.CdnUrlBase;
if (Regex.IsMatch(url, @"^(\d+,)+\d+,*$"))
{
return (url.Split(",").ToList().Select(x => TestDat(Convert.ToInt64(x))).ToList());
}
Match match = Regex.Match(url, @"https://CDN_URL_BASE/(?<d>\d+?)");
if (match.Success)
{
dataVer = Convert.ToUInt16(match.Groups["d"].Value);
}
return (DataPreset(dataVer));
}

private static bool DownloadRequired()
{
string url = AppSettings.Instance.CdnUrlBase;
return (Regex.IsMatch(url, @"^https://.*\..*\..*/$"));
}

async Task<List<TemporaryExposureKeyExportFileModel>> IHttpDataService.GetTemporaryExposureKeyList(string region, CancellationToken cancellationToken)
{
/* CdnUrlBase trick for Debug_Mock
"https://www.example.com/"(url with 2+ periods) -> download "url"+"c19r/440/list.json"
"1598022036649,1598022036751,1598022036826" -> direct input timestamps
"https://CDN_URL_BASE/2" -> dataVer = 2
"https://CDN_URL_BASE/" -> dataVer = 0 (default)
*/
if (DownloadRequired())
{
// copy from GetTemporaryExposureKeyList @ ./HttpDataService.cs and delete logger part
string container = AppSettings.Instance.BlobStorageContainerName;
string url = AppSettings.Instance.CdnUrlBase + $"{container}/{region}/list.json";
var result = await GetCdnAsync(url, cancellationToken);
if (result != null)
{
Debug.WriteLine("HttpDataServiceMock::GetTemporaryExposureKeyList downloaded");
return Utils.DeserializeFromJson<List<TemporaryExposureKeyExportFileModel>>(result);
}
else
{
Debug.WriteLine("HttpDataServiceMock::GetTemporaryExposureKeyList download failed");
return new List<TemporaryExposureKeyExportFileModel>();
}
}

Debug.WriteLine("HttpDataServiceMock::GetTemporaryExposureKeyList called");
return Data();
}

async Task<bool> IHttpDataService.PostRegisterUserAsync()
Expand Down
65 changes: 61 additions & 4 deletions Covid19Radar/Covid19Radar/Services/TestNativeImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Linq;
using Xamarin.Essentials;
using Xamarin.ExposureNotifications;

Expand Down Expand Up @@ -50,16 +52,71 @@ public async Task<IEnumerable<TemporaryExposureKey>> GetSelfTemporaryExposureKey
public Task<Status> GetStatusAsync()
=> Task.FromResult(Preferences.Get("fake_enabled", true) ? Status.Active : Status.Disabled);

private ushort[] DataPreset(int dataVer)
{
/* DataPreset for ExposureDetection
0(default): two low-risk matches (default for v1.2.3)
1: one high-risk match and 2 low-risk matches
2: no match
*/
switch (dataVer)
{
case 1:
return (
new ushort[] {10, 3, 27, 0, 0, // ExposureDetectionSummary
13, 15, 65, 27, (ushort)RiskLevel.High, // ExposureInfo 1st
10, 15, 65, 5, (ushort)RiskLevel.Medium, // ExposureInfo 2st
11, 5, 40, 3, (ushort)RiskLevel.Low, // ExposureInfo 3nd
});
case 2:
return (
new ushort[] {0, 0, 0, 0, 0, // ExposureDetectionSummary
});
case 0:
default:
return (
new ushort[] {10, 2, 5, 0, 0, // ExposureDetectionSummary
10, 15, 65, 5, (ushort)RiskLevel.Medium, // ExposureInfo 1st (RiskLevel.Medium=4)
11, 5, 40, 3, (ushort)RiskLevel.Low, // ExposureInfo 2nd(RiskLevel.Low=2)
});
}
}

private ushort[] Data()
{
string url = AppSettings.Instance.ApiUrlBase;
if (Regex.IsMatch(url, @"^(\d+,)+\d+,*$"))
{
return (url.Split(",").ToList().Select(x => Convert.ToUInt16(x)).ToArray());
}
int dataVer = -1;
Match match = Regex.Match(url, @"https://API_URL_BASE/api/(?<d>\d+?)");
if (match.Success)
{
dataVer = Convert.ToUInt16(match.Groups["d"].Value);
}
return (DataPreset(dataVer));

}

public Task<(ExposureDetectionSummary summary, Func<Task<IEnumerable<ExposureInfo>>> getInfo)> DetectExposuresAsync(IEnumerable<string> files)
{
var summary = new ExposureDetectionSummary(10, 2, 5);
/* ApiUrlBase trick for Debug_Mock
"10,2,5,0,0,10,15,65,5,4,11,5,40,3,2" -> direct input (the same with default)
"https://API_URL_BASE/api/2" -> dataVer = 2
"https://API_URL_BASE/api" -> dataVer = 0 (default)
others -> dataVer = 0
*/
var d = Data();
int i = 0;
var summary = new ExposureDetectionSummary(d[i++], d[i++], d[i++], new TimeSpan[d[i++]], d[i++]);

Task<IEnumerable<ExposureInfo>> GetInfo()
{
var info = new List<ExposureInfo>
var info = new List<ExposureInfo>();
while (i < d.Length)
{
new ExposureInfo (DateTime.UtcNow.AddDays(-10), TimeSpan.FromMinutes(15), 65, 5, RiskLevel.Medium),
new ExposureInfo (DateTime.UtcNow.AddDays(-11), TimeSpan.FromMinutes(5), 40, 3, RiskLevel.Low),
info.Add(new ExposureInfo(DateTime.UtcNow.AddDays(-d[i++]), TimeSpan.FromMinutes(d[i++]), d[i++], d[i++], (Xamarin.ExposureNotifications.RiskLevel)d[i++]));
};
return Task.FromResult<IEnumerable<ExposureInfo>>(info);
}
Expand Down

0 comments on commit 1847252

Please sign in to comment.