Last active
April 25, 2019 14:29
-
-
Save SpicySyntax/3fc2e4274362aefadfaadb4d8c1d17bf to your computer and use it in GitHub Desktop.
BaseTest.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
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Bentley.ConnectCoreLibs.Testing.Oidc; | |
using Bentley.CONNECT.UnitTest.Settings; | |
using ObjectDetection.Models; | |
using System.Net; | |
using System.Net.Http.Headers; | |
using Newtonsoft.Json; | |
namespace ObjectDetectionApiTests | |
{ | |
public abstract class TestsBase : IDisposable | |
{ | |
protected string HostBaseUrl => @"http://52.170.27.32/";//@"http://localhost:7071"; | |
protected string UploadUrl => $"{HostBaseUrl}api/upload-image"; | |
protected string ImageRecordsUrl => $"{HostBaseUrl}api/image-records"; | |
protected readonly string CrackFilePath = @"TestImages/crack5.JPG"; | |
protected readonly string NocrackFilePath = @"TestImages/nocrack8.JPG"; | |
protected readonly string TestCrackPicName = "test_crack_upload.jpg"; | |
protected readonly string TestNoCrackPicName = "test_nocrack_upload.jpg"; | |
protected Settings settings; | |
protected string testUserName; | |
protected string testPassword; | |
protected string testClientId; | |
protected string testClientSecret; | |
protected string oidcBaseUrl; | |
private string _jwtAccessToken; | |
protected HttpClient HttpClient = new HttpClient(); | |
protected TestsBase() | |
{ | |
// Do "global" initialization here; Called before every test method. | |
settings = new Settings(); | |
testUserName = settings.Local.GetVariable("test_username"); | |
testPassword = settings.Local.GetVariable("test_password"); | |
testClientId = settings.Local.GetVariable("test_client_id"); | |
testClientSecret = settings.Local.GetVariable("test_client_secret"); | |
oidcBaseUrl = settings.Local.GetVariable("oidc_base_url"); | |
} | |
protected async Task<string> GetJwtAccessToken() | |
{ | |
if (!string.IsNullOrEmpty(_jwtAccessToken)) | |
return _jwtAccessToken; | |
var user = new ImsUser(testUserName, testPassword); | |
var authority = oidcBaseUrl; | |
var flowExecutor = new ImsOidcExecutor(authority, user); | |
var flowSettings = new ImsOidcFlowSettings() | |
{ | |
ClientId = testClientId, //provide this from OIDC | |
Flow = GrantType.AuthorizationCode, //not limited to Auth Code | |
Scope = "openid email profile organization", | |
RedirectUri = "https://localhost:44360/signin-oidc", | |
ClientSecret = testClientSecret //provide this from OIDC | |
}; | |
var flowResult = await flowExecutor.ExecuteAuthCodeNoPkce(flowSettings); | |
_jwtAccessToken = flowResult.AccessToken; | |
return _jwtAccessToken; | |
} | |
protected async Task<HttpStatusCode> DeleteImageRecord(string imageBlobName) | |
{ | |
var imageRecordUri = $"{ImageRecordsUrl}?id={imageBlobName}"; | |
var resp = await HttpClient.DeleteAsync(imageRecordUri); | |
return resp.StatusCode; | |
} | |
protected async Task<bool> ImagePredictionCompletedCorrectly(string imageBlobName, ImageCategory imageCategory) | |
{ | |
var imageRecordUri = $"{ImageRecordsUrl}?id={imageBlobName}"; | |
var resp = await HttpClient.GetStringAsync(imageRecordUri); | |
var imageRecord = JsonConvert.DeserializeObject<ImageRecord>(resp); | |
if (imageRecord.Status != ImageStatus.Completed.Value) | |
return false; | |
// Check if it matches expected value | |
return imageRecord.Category == imageCategory.Value; | |
} | |
protected async Task<HttpResponseMessage> UploadImage(string url, byte[] imageData, string fileName) | |
{ | |
var requestContent = new MultipartFormDataContent(); | |
// here you can specify boundary if you need---^ | |
var imageContent = new ByteArrayContent(imageData); | |
imageContent.Headers.ContentType = | |
MediaTypeHeaderValue.Parse("image/jpeg"); | |
requestContent.Add(imageContent, "image", fileName); | |
return await HttpClient.PostAsync(url, requestContent); | |
} | |
public void Dispose() | |
{ | |
// Do "global" teardown here; Called after every test method. | |
} | |
} | |
} |
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
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Xunit; | |
using ObjectDetection.Models; | |
namespace ObjectDetectionApiTests | |
{ | |
public class ObjectDetectionTest: TestsBase | |
{ | |
private string _uploadCrackBlobName; | |
private string _uploadNoCrackBlobName; | |
[Fact] | |
public async void UploadRequestWithNoMultipartContentReturnsBadRequest() | |
{ | |
var token = await GetJwtAccessToken(); | |
HttpClient.SetBearerToken(token); | |
var resp = await HttpClient.PostAsync(UploadUrl, null); | |
Assert.Equal(HttpStatusCode.BadRequest, resp.StatusCode); | |
} | |
[Fact] | |
public async void CrackDetectionMainWorkflowSucceeds() | |
{ | |
var token = await GetJwtAccessToken(); | |
HttpClient.SetBearerToken(token); | |
await UploadFileSuccessReturnsBlobName(); | |
await ImageRecordsAreUpdatedWithPredictions(); | |
Assert.Equal(HttpStatusCode.OK, await DeleteImageRecord(_uploadCrackBlobName)); | |
Assert.Equal(HttpStatusCode.OK, await DeleteImageRecord(_uploadNoCrackBlobName)); | |
} | |
private async Task UploadFileSuccessReturnsBlobName() | |
{ | |
var fileBytes = await System.IO.File.ReadAllBytesAsync(CrackFilePath); | |
var resp = await UploadImage(UploadUrl, fileBytes, TestCrackPicName); | |
Assert.Equal(HttpStatusCode.Created, resp.StatusCode); | |
_uploadCrackBlobName = await resp.Content.ReadAsStringAsync(); | |
Assert.NotNull(_uploadCrackBlobName); | |
fileBytes = await System.IO.File.ReadAllBytesAsync(NocrackFilePath); | |
resp = await UploadImage(UploadUrl, fileBytes, TestNoCrackPicName); | |
Assert.Equal(HttpStatusCode.Created, resp.StatusCode); | |
_uploadNoCrackBlobName = await resp.Content.ReadAsStringAsync(); | |
Assert.NotNull(_uploadNoCrackBlobName); | |
} | |
private async Task ImageRecordsAreUpdatedWithPredictions() | |
{ | |
Assert.NotNull(_uploadCrackBlobName); | |
Assert.NotNull(_uploadNoCrackBlobName); | |
var secondsToWait = 60; | |
var sleepSeconds = 15; | |
var crackPredictionFinished = false; | |
var noCrackPredictionFinished = false; | |
while (secondsToWait > 0) | |
{ | |
if (!crackPredictionFinished) | |
{ | |
crackPredictionFinished = | |
await ImagePredictionCompletedCorrectly(_uploadCrackBlobName, ImageCategory.Crack); | |
} | |
if (!noCrackPredictionFinished) | |
{ | |
noCrackPredictionFinished = | |
await ImagePredictionCompletedCorrectly(_uploadNoCrackBlobName, ImageCategory.NoCrack); | |
} | |
if (crackPredictionFinished && noCrackPredictionFinished) | |
return; | |
await Task.Delay(sleepSeconds * 1000); | |
secondsToWait -= sleepSeconds; | |
} | |
Assert.True(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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<TargetFramework>netcoreapp2.2</TargetFramework> | |
<IsPackable>false</IsPackable> | |
<Platforms>x64</Platforms> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Bentley.ConnectCoreLibs.Testing.Oidc" Version="1.2.1" /> | |
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" /> | |
<PackageReference Include="Bentley.CONNECT.UnitTest.Settings" Version="2.0.6" /> | |
<PackageReference Include="xunit" Version="2.3.1" /> | |
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> | |
</ItemGroup> | |
<ItemGroup> | |
<Content Include="TestImages\**"> | |
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
</Content> | |
<None Update="localSettings.xml"> | |
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
</None> | |
</ItemGroup> | |
<ItemGroup> | |
<ProjectReference Include="..\ObjectDetectionFunctions\ObjectDetection.csproj" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment