Skip to content

Commit

Permalink
add diagnosis submittion test. (Note: ExposureNotificationHandler.cs …
Browse files Browse the repository at this point in the history
…is modified by using #if USE_MOCK)

cocoa-mhlw#179 (comment)
  • Loading branch information
i-maruyama committed May 16, 2021
1 parent 1847252 commit b4e26e2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,11 @@ private async Task<DiagnosisSubmissionParameter> CreateSubmissionAsync(IEnumerab
// See if we can add the device verification
if (DependencyService.Get<IDeviceVerifier>() is IDeviceVerifier verifier)
{
#if USE_MOCK
//nop
#else
submission.DeviceVerificationPayload = await verifier?.VerifyAsync(submission);
#endif
}

loggerService.Info($"DeviceVerificationPayload is {(string.IsNullOrEmpty(submission.DeviceVerificationPayload) ? "null or empty" : "set")}.");
Expand Down
54 changes: 52 additions & 2 deletions Covid19Radar/Covid19Radar/Services/HttpDataServiceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,68 @@ async Task<List<TemporaryExposureKeyExportFileModel>> IHttpDataService.GetTempor
return Data();
}

// copy ./TestNativeImplementation.cs
private string[] UrlApi()
{
// "UrlApi" -> UrlApi=
// ".../api1/register1/" -> UrlApi=
string url = AppSettings.Instance.ApiUrlBase;
Regex r = new Regex("/r(egister)?[0-9]+");
Regex d = new Regex("/d(iagnosis)?[0-9]+");
string urlRegister = r.Match(url).Value;
url = r.Replace(url, "");
string urlDiagnosis = d.Match(url).Value;
url = d.Replace(url, "");
string urlApi = url;
return (new string[] { urlApi, urlRegister, urlDiagnosis });
}

// copy ./TestNativeImplementation.cs
private ushort NumberEndofSentence(string url)
{
Match match = Regex.Match(url, @"(?<d>\d+)$");
ushort dataVer = 0;
if (match.Success)
{
dataVer = Convert.ToUInt16(match.Groups["d"].Value);
}
return (dataVer);
}

async Task<bool> IHttpDataService.PostRegisterUserAsync()
{
Debug.WriteLine("HttpDataServiceMock::PostRegisterUserAsync called");
return await Task.FromResult(true);
switch (NumberEndofSentence(UrlApi()[1]))
{
case 1:
return await Task.FromResult(false);
case 0:
default:
return await Task.FromResult(true);
}
}

Task<HttpStatusCode> IHttpDataService.PutSelfExposureKeysAsync(DiagnosisSubmissionParameter request)
{
var code = HttpStatusCode.OK; // default. for PutSelfExposureKeys NG
var dataVer = NumberEndofSentence(UrlApi()[2]);
if (dataVer >= 100)
{
code = (HttpStatusCode)dataVer;
}
else
{
switch (dataVer)
{
case 1:
code = HttpStatusCode.NoContent; // for Successful PutSelfExposureKeys
break;
}
}
return Task.Factory.StartNew<HttpStatusCode>(() =>
{
Debug.WriteLine("HttpDataServiceMock::PutSelfExposureKeysAsync called");
return HttpStatusCode.OK;
return code;
});
}

Expand Down
41 changes: 31 additions & 10 deletions Covid19Radar/Covid19Radar/Services/TestNativeImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private ushort[] DataPreset(int dataVer)
0(default): two low-risk matches (default for v1.2.3)
1: one high-risk match and 2 low-risk matches
2: no match
3+: please add
*/
switch (dataVer)
{
Expand All @@ -82,30 +83,50 @@ private ushort[] DataPreset(int dataVer)
}
}

private ushort[] Data()
public string[] UrlApi()
{
// "UrlApi" -> UrlApi=
// ".../api1/register1/" -> UrlApi=
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+?)");
Regex r = new Regex("/r(egister)?[0-9]+");
Regex d = new Regex("/d(iagnosis)?[0-9]+");
string urlRegister = r.Match(url).Value;
url = r.Replace(url, "");
string urlDiagnosis = d.Match(url).Value;
url = d.Replace(url, "");
string urlApi = url;
return (new string[] { urlApi, urlRegister, urlDiagnosis });
}

public ushort NumberEndofSentence(string url)
{
Match match = Regex.Match(url, @"(?<d>\d+)$");
ushort dataVer = 0;
if (match.Success)
{
dataVer = Convert.ToUInt16(match.Groups["d"].Value);
}
return (DataPreset(dataVer));
return (dataVer);
}

private ushort[] Data()
{
string url = UrlApi()[0];
if (Regex.IsMatch(url, @"^(\d+,)+\d+,?$"))
{
return (url.Split(",").ToList().Select(x => Convert.ToUInt16(x)).ToArray());
}
return (DataPreset(NumberEndofSentence(url)));

}

public Task<(ExposureDetectionSummary summary, Func<Task<IEnumerable<ExposureInfo>>> getInfo)> DetectExposuresAsync(IEnumerable<string> files)
{
/* 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/api2" -> dataVer = 2
"https://API_URL_BASE/api" -> dataVer = 0 (default)
others -> dataVer = 0
others -> dataVer is the number at the end of the sentence
*/
var d = Data();
int i = 0;
Expand Down

0 comments on commit b4e26e2

Please sign in to comment.