Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Fixed mock http data service
Browse files Browse the repository at this point in the history
  • Loading branch information
cocoa-dev004 committed Jul 27, 2022
1 parent 5268c94 commit a403a52
Showing 1 changed file with 24 additions and 164 deletions.
188 changes: 24 additions & 164 deletions Covid19Radar/Covid19Radar/Services/HttpDataServiceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,137 +3,51 @@
* 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.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using Covid19Radar.Services.Logs;

namespace Covid19Radar.Services
{
class HttpDataServiceMock : IHttpDataService
public class HttpDataServiceMock : IHttpDataService
{
private readonly HttpClient downloadClient;
private readonly MockCommonUtils mockCommonUtils = new MockCommonUtils();
private readonly ILoggerService _loggerSerivce;

public HttpDataServiceMock(IHttpClientService httpClientService)
public HttpDataServiceMock(ILoggerService loggerService)
{
downloadClient = httpClientService.Create();
_loggerSerivce = loggerService;
}

// copy from ./HttpDataService.cs
private async Task<string> GetCdnAsync(string url, CancellationToken cancellationToken)
public Task<HttpStatusCode> PostRegisterUserAsync()
{
HttpResponseMessage result = await downloadClient.GetAsync(url, cancellationToken);
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;
}

public (string, string) GetCredentials()
{
return ("user-uuid", "secret");
}

public void RemoveCredentials()
{
}

private TemporaryExposureKeyExportFileModel CreateTestData(long created)
{
return new TemporaryExposureKeyExportFileModel()
{
Region = "440",
Url = "testUrl",
Created = created
};
}

private long CalcTimeAddDays(int day)
=> new DateTimeOffset(DateTime.UtcNow.AddDays(day)).ToUnixTimeMilliseconds();

private long CalcMidnightTimeAddDays(int day)
{
DateTime d = DateTime.UtcNow.AddDays(day);
// set 0 hour,1 min,2 sec,3 millisecond for debug
return new DateTimeOffset(new DateTime(d.Year, d.Month, d.Day, 0, 1, 2, 3)).ToUnixTimeMilliseconds();
}

enum PresetTekListType // PresetTekListData for Tek List
{
Nothing = 0, //nothing (default for v1.2.3)
RealTime = 1, // real time
MidnightTime = 2, // last night
// please add "YourDataType = <int>"
}

private List<TemporaryExposureKeyExportFileModel> PresetTekListData(int dataVersion)
{
switch ((PresetTekListType)dataVersion)
return Task.Factory.StartNew(() =>
{
case PresetTekListType.MidnightTime:
return new List<TemporaryExposureKeyExportFileModel> { CreateTestData(CalcMidnightTimeAddDays(-1)), CreateTestData(CalcMidnightTimeAddDays(0)) };
case PresetTekListType.RealTime:
return new List<TemporaryExposureKeyExportFileModel> { CreateTestData(CalcTimeAddDays(-1)), CreateTestData(CalcTimeAddDays(0)) };
case PresetTekListType.Nothing:
default:
return new List<TemporaryExposureKeyExportFileModel>();
}
return HttpStatusCode.OK;
});
}

async Task<HttpStatusCode> IHttpDataService.PostRegisterUserAsync()
public Task<HttpStatusCode> PutSelfExposureKeysAsync(DiagnosisSubmissionParameter request)
{
Debug.WriteLine("HttpDataServiceMock::PostRegisterUserAsync called");
var code = HttpStatusCode.OK;
var result = mockCommonUtils.GetRegisterDataType();
if (result >= 100)
{
code = (HttpStatusCode)result;
}
else
return Task.Factory.StartNew(() =>
{
if (result.Equals(1))
_loggerSerivce.StartMethod();
HttpStatusCode result;
try
{
code = HttpStatusCode.NoContent;
result = request.VerificationPayload switch
{
"99999910" => HttpStatusCode.NoContent,
"99999920" => HttpStatusCode.NotAcceptable,
_ => HttpStatusCode.ServiceUnavailable
};
}
}

return await Task.FromResult(code);
}

Task<HttpStatusCode> IHttpDataService.PutSelfExposureKeysAsync(DiagnosisSubmissionParameter request)
{
var code = HttpStatusCode.OK; // default. for PutSelfExposureKeys NG
var dataType = mockCommonUtils.GetDiagnosisDataType();
if (dataType >= 100) // HttpStatusCode >=100 by RFC2616#section-10
{
code = (HttpStatusCode)dataType;
}
else
{
switch (dataType)
finally
{
case 1:
code = HttpStatusCode.NoContent; // for Successful PutSelfExposureKeys
break;
_loggerSerivce.EndMethod();
}
}

Debug.WriteLine("HttpDataServiceMock::PutSelfExposureKeysAsync called");
return Task.FromResult(code);
return result;
});
}

public Task<ApiResponse<LogStorageSas>> GetLogStorageSas()
Expand All @@ -153,58 +67,4 @@ public Task<ApiResponse<string>> PutEventLog(V1EventLogRequest request)
});
}
}

public class MockCommonUtils
{
public string CdnUrlBase => AppSettings.Instance.CdnUrlBase;
public string ApiUrlBase => AppSettings.Instance.ApiUrlBase;

public bool IsDownloadRequired()
=> Regex.IsMatch(CdnUrlBase, @"^https://.*\..*\..*/$");

public bool IsDirectInput()
=> Regex.IsMatch(CdnUrlBase, @"^(\d+,)+\d+,*$");


private ushort NumberEndofSentence(string url)
{
Match match = Regex.Match(url, @"(?<d>\d+)$");
ushort number = 0;
if (match.Success)
{
number = Convert.ToUInt16(match.Groups["d"].Value);
}
return number;
}
public List<string> GetCreatedTimes()
=> CdnUrlBase.Split(",").ToList();
public ushort GetTekListDataType()
=> NumberEndofSentence(CdnUrlBase);
public string[] GetApiUrlSegment()
{
// "url/api" -> { "url/api", "", "" }
// "url/base/api/register1/diagnosis2" -> { "url/base/api", "/register1", "/diagnosis2" }
// "url/api1/r1/d2" -> { "url/api1", "/r1", "/d2" }
// "url/api1/d2/r1" -> { "url/api1", "/r1", "/d2" }
var url = ApiUrlBase;
var r = new Regex("/r(egister)?[0-9]+");
var d = new Regex("/d(iagnosis)?[0-9]+");
var urlRegister = r.Match(url).Value;
url = r.Replace(url, "");
var urlDiagnosis = d.Match(url).Value;
url = d.Replace(url, "");
var urlApi = url;
return new string[] { urlApi, urlRegister, urlDiagnosis };
}
public ushort GetDiagnosisDataType()
=> NumberEndofSentence(GetApiUrlSegment()[2]);
public ushort GetRegisterDataType()
=> NumberEndofSentence(GetApiUrlSegment()[1]);
public ushort GetApiDataType()
=> NumberEndofSentence(GetApiUrlSegment()[0]);
public bool IsDirectInputApi()
=> Regex.IsMatch(GetApiUrlSegment()[0], @"^(\d+,)+\d+,?$");
public List<string> GetApiStrings()
=> GetApiUrlSegment()[0].Split(",").ToList();
}
}

0 comments on commit a403a52

Please sign in to comment.