Skip to content

Commit 365f703

Browse files
authored
Merge pull request #69 from microsoft/tester-app
test: Added C# Tester Console App to verify backend services
2 parents 7c73683 + 011ca12 commit 365f703

18 files changed

+840
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ _textdb/
1717
##
1818
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
1919

20+
# .NET C#
21+
appsettings.json
22+
23+
2024
# User-specific files
2125
*.rsuser
2226
*.suo
91.8 KB
Loading
42.7 KB
Loading

Services/testapp/Choice.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT License.
3+
using System.ComponentModel;
4+
5+
namespace Tester.ConsoleApp
6+
{
7+
enum Choice
8+
{
9+
[Description("1. List Registered Documents")]
10+
ListRegisteredDocuments,
11+
[Description("2. Register Documents")]
12+
RegisterDocuments,
13+
[Description("3. Delete Registered Documents in Azure")]
14+
DeleteDocuments,
15+
[Description("4. Perform Gap Analysis")]
16+
GapAnalysis,
17+
[Description("5. Get All Gap Analysis Results")]
18+
GetAllGapAnalysisResults,
19+
[Description("6. Perform Benchmark Analysis")]
20+
BenchMarks,
21+
[Description("7. Get All Benchmark Analysis Results")]
22+
GetAllBenchMarksResults,
23+
[Description("8. Test API Connection")]
24+
TestApiConnection,
25+
[Description("9. Exit")]
26+
Exit
27+
}
28+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Security.Cryptography.X509Certificates;
5+
using Spectre.Console;
6+
7+
namespace Tester.ConsoleApp.Functions
8+
{
9+
public static class ApiConnection
10+
{
11+
public static async void TestConnection(Uri uri)
12+
{
13+
// Create a custom HttpClientHandler to handle SSL certificate validation
14+
var handler = new HttpClientHandler
15+
{
16+
ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) =>
17+
{
18+
// Here you can add custom logic to validate the certificate
19+
// For example, you can check the certificate thumbprint, issuer, etc.
20+
// Returning true will bypass the SSL certificate validation
21+
if (cert == null)
22+
{
23+
AnsiConsole.WriteLine("No Server Certificate Found");
24+
return false;
25+
}
26+
return ValidateCertificate(cert);
27+
}
28+
};
29+
30+
// Create an HttpClient using the custom handler
31+
using (var client = new HttpClient(handler))
32+
{
33+
// Set the base address of the API
34+
client.BaseAddress = uri;
35+
try
36+
{
37+
// Make a GET request to the API
38+
HttpResponseMessage response = await client.GetAsync("/api/endpoint");
39+
// Check if the response is successful
40+
if (response.IsSuccessStatusCode)
41+
{
42+
// Read the response content
43+
string content = await response.Content.ReadAsStringAsync();
44+
//AnsiConsole.WriteLine("\nTest Connection Response Content: " + content);
45+
}
46+
AnsiConsole.WriteLine("API Connection is successful.");
47+
}
48+
catch (Exception ex)
49+
{
50+
//AnsiConsole.WriteLine("\nTest Connection Failed with Exception (check your appsettings.json and services): " + ex.Message);
51+
AnsiConsole.WriteLine("Test Connection Failed with Exceptions. Check your appsettings.json and API services.");
52+
}
53+
}
54+
}
55+
56+
// Validate the SSL certificate
57+
static bool ValidateCertificate(X509Certificate2 cert)
58+
{
59+
// Add custom validation logic here
60+
// For example, check the certificate thumbprint, issuer, expiration date, etc.
61+
//AnsiConsole.WriteLine("Certificate Subject: " + cert.Subject);
62+
//AnsiConsole.WriteLine("Certificate Issuer: " + cert.Issuer);
63+
//AnsiConsole.WriteLine("Certificate Thumbprint: " + cert.Thumbprint);
64+
//AnsiConsole.WriteLine("Certificate Expiration: " + cert.NotAfter);
65+
//AnsiConsole.WriteLine(); // Write a new line
66+
67+
// Example: Validate the certificate thumbprint. This is just an example, you should use a valid thumbprint.
68+
string expectedThumbprint = "B89BB8B0BEF4B6CF59A472284B4F8F234525302B";
69+
if (cert.Thumbprint == expectedThumbprint)
70+
{
71+
return true;
72+
}
73+
74+
// Example: Validate the certificate issuer. This is just an example, you should use a valid issuer.
75+
string expectedIssuer = "Kubernetes Ingress Controller Fake Certificate";
76+
if (cert.Issuer == expectedIssuer)
77+
{
78+
return true;
79+
}
80+
81+
// Example: Validate the certificate expiration date
82+
if (DateTime.Now < cert.NotAfter)
83+
{
84+
return true;
85+
}
86+
87+
// If none of the validation checks pass, return false
88+
return false;
89+
}
90+
}
91+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
namespace Tester.ConsoleApp.Functions
5+
{
6+
public class AppConfig
7+
{
8+
public string JobOwner { get; set; }
9+
public string Type { get; set; }
10+
public string disclosureNumber { get; set; }
11+
public string disclosureName { get; set; }
12+
public string disclosureRequirement { get; set; }
13+
public string disclosureRequirementDetail { get; set; }
14+
public string disclosureAnnex { get; set; }
15+
16+
// Constructor to initialize properties
17+
public AppConfig(string myJobOwner, string myType, string myDisclosureNumber, string myDisclosureName, string myDisclosureRequirement, string myDisclosureRequirementDetail, string myDisclosureAnnex)
18+
{
19+
JobOwner = myJobOwner;
20+
Type = myType;
21+
disclosureNumber = myDisclosureNumber;
22+
disclosureName = myDisclosureName;
23+
disclosureRequirement = myDisclosureRequirement;
24+
disclosureRequirementDetail = myDisclosureRequirementDetail;
25+
disclosureAnnex = myDisclosureAnnex;
26+
}
27+
28+
// Parameterless constructor for deserialization
29+
public AppConfig()
30+
{
31+
JobOwner = string.Empty;
32+
Type = string.Empty;
33+
disclosureNumber = string.Empty;
34+
disclosureName = string.Empty;
35+
disclosureRequirement = string.Empty;
36+
disclosureRequirementDetail = string.Empty;
37+
disclosureAnnex = string.Empty;
38+
}
39+
}
40+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Extensions.Configuration;
5+
using Newtonsoft.Json;
6+
using RestSharp;
7+
using Spectre.Console;
8+
using System.Text;
9+
10+
namespace Tester.ConsoleApp.Functions
11+
{
12+
public static class BenchMarks
13+
{
14+
public static async Task PerformBenchmarks(Uri baseUri, IConfiguration config)
15+
{
16+
17+
var standardType = string.Empty;
18+
while (standardType != "CSRD" && standardType != "GRI")
19+
{
20+
standardType = AnsiConsole.Ask<string>("CSRD or GRI?:");
21+
if (standardType != "CSRD" && standardType != "GRI")
22+
{
23+
AnsiConsole.MarkupLine("[red]Invalid input. Valid values: 'CSRD' or 'GRI'.[/]");
24+
}
25+
}
26+
27+
AppConfig appConfig = new AppConfig();
28+
appConfig = Helpers.ConfigHelper.GetAppConfig(standardType, config);
29+
30+
var client = new HttpClient();
31+
client.BaseAddress = baseUri;
32+
33+
var myJobName = AnsiConsole.Ask<string>("Enter a Job Name for Benchmarks Analysis:");
34+
35+
// user input for documentIds
36+
var myDocumentIds = new List<string>();
37+
while (true)
38+
{
39+
var myDocumentId = AnsiConsole.Ask<string>("Enter a document ID or the word 'Finished' to end:");
40+
if (myDocumentId.Equals("Finished", StringComparison.OrdinalIgnoreCase))
41+
{
42+
break;
43+
}
44+
myDocumentIds.Add(myDocumentId);
45+
}
46+
var requestBody = new
47+
{
48+
jobName = myJobName,
49+
documentIds = myDocumentIds,
50+
jobOwner = config["AppConfig:JobOwner"] ?? string.Empty,
51+
disclosureNumber = config["AppConfig:disclosureNumber"] ?? string.Empty,
52+
disclosureName = config["AppConfig:disclosureName"] ?? string.Empty,
53+
disclosureRequirement = config["AppConfig:disclosureRequirement"] ?? string.Empty,
54+
disclosureRequirementDetail = config["AppConfig:disclosureRequirementDetail"] ?? string.Empty,
55+
disclosureAnnex = config["AppConfig:disclosureAnnex"] ?? string.Empty
56+
};
57+
58+
var json = JsonConvert.SerializeObject(requestBody);
59+
var content = new StringContent(json, Encoding.UTF8, "application/json");
60+
61+
var response = await client.PostAsync("ESRS/ESRSDisclosureBenchmarkOnQueue", content);
62+
63+
if (response.IsSuccessStatusCode)
64+
{
65+
var responseContent = await response.Content.ReadAsStringAsync();
66+
AnsiConsole.MarkupLine("[green]Benchmark documents request was successful.[/]");
67+
AnsiConsole.WriteLine(responseContent);
68+
}
69+
else
70+
{
71+
AnsiConsole.MarkupLine("[red]Benchmark documents request failed.[/]");
72+
AnsiConsole.WriteLine(response.ReasonPhrase ?? "No reason phrase provided.");
73+
}
74+
}
75+
76+
public static void GetAllBenchmarksResults(Uri uri)
77+
{
78+
var handler = new HttpClientHandler
79+
{
80+
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
81+
};
82+
83+
var client = new RestClient(new RestClientOptions(uri)
84+
{
85+
ConfigureMessageHandler = _ => handler
86+
});
87+
88+
//BaseUrl/ESRS/GetAllESRSBenchmarkResults
89+
var request = new RestRequest("ESRS/GetAllESRSBenchmarkResults", Method.Get);
90+
91+
RestResponse response = client.Execute(request);
92+
var content = response.Content;
93+
94+
AnsiConsole.WriteLine("GetAllESRSBenchmarkResults Response Status: " + response.StatusCode);
95+
AnsiConsole.WriteLine("GetAllESRSBenchmarkResults Response Content: " + content);
96+
97+
}
98+
99+
}
100+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Extensions.Configuration;
5+
using Tester.ConsoleApp.Functions;
6+
7+
namespace Tester.ConsoleApp.Helpers
8+
{
9+
public static class ConfigHelper
10+
{
11+
public static AppConfig GetAppConfig(string standardType, IConfiguration config)
12+
{
13+
var appConfig = new AppConfig();
14+
15+
if (standardType.Equals("CSRD", StringComparison.OrdinalIgnoreCase))
16+
{
17+
appConfig.JobOwner = config["CSRD:JobOwner"] ?? string.Empty;
18+
appConfig.Type = config["CSRD:Type"] ?? string.Empty;
19+
appConfig.disclosureName = config["CSRD:disclosureName"] ?? string.Empty;
20+
appConfig.disclosureNumber = config["CSRD:disclosureNumber"] ?? string.Empty;
21+
appConfig.disclosureRequirement = config["CSRD:disclosureRequirement"] ?? string.Empty;
22+
appConfig.disclosureRequirementDetail = config["CSRD:disclosureRequirementDetail"] ?? string.Empty;
23+
}
24+
else if (standardType.Equals("GRI", StringComparison.OrdinalIgnoreCase))
25+
{
26+
appConfig.JobOwner = config["GRI:JobOwner"] ?? string.Empty;
27+
appConfig.Type = config["GRI:Type"] ?? string.Empty;
28+
appConfig.disclosureName = config["GRI:disclosureName"] ?? string.Empty;
29+
appConfig.disclosureNumber = config["GRI:disclosureNumber"] ?? string.Empty;
30+
appConfig.disclosureRequirement = config["GRI:disclosureRequirement"] ?? string.Empty;
31+
appConfig.disclosureRequirementDetail = config["GRI:disclosureRequirementDetail"] ?? string.Empty;
32+
appConfig.disclosureAnnex = config["GRI:disclosureAnnex"] ?? string.Empty;
33+
}
34+
else
35+
{
36+
throw new ArgumentException($"Invalid input {standardType}. Valid values: 'CSRD' or 'GRI'.");
37+
}
38+
39+
return appConfig;
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)