From 6109a187338464b871cfe8ef005046ef43d676cb Mon Sep 17 00:00:00 2001 From: Ivan Kamkin <234-Ivan.Kamkin@users.noreply.git.saltov.dynabic.com> Date: Fri, 28 Nov 2025 13:51:35 +0500 Subject: [PATCH 1/6] Split tests for .net and old .netframework --- .github/workflows/net-framework.yml | 17 +-- .gitignore | 2 +- Makefile | 1 + NetFrameworkTests/ApiExceptionTests.cs | 31 ++++ ...BarCode.Cloud.Sdk.NetFrameworkTests.csproj | 24 ++++ NetFrameworkTests/Configuration.template.json | 4 + NetFrameworkTests/ConfigurationTests.cs | 135 ++++++++++++++++++ NetFrameworkTests/GenerateAndThenRecognize.cs | 46 ++++++ NetFrameworkTests/GenerateTests.cs | 74 ++++++++++ NetFrameworkTests/JwtAuthTests.cs | 54 +++++++ NetFrameworkTests/RecognizeTests.cs | 90 ++++++++++++ NetFrameworkTests/ScanTests.cs | 75 ++++++++++ NetFrameworkTests/TestsBase.cs | 115 +++++++++++++++ NetFrameworkTests/nuget.config | 16 +++ .../test_data/Test_PostGenerateMultiple.png | Bin 0 -> 8589 bytes Tests/Aspose.BarCode.Cloud.Sdk.Tests.csproj | 4 +- Tests/Configuration.template.json | 4 +- 17 files changed, 677 insertions(+), 15 deletions(-) create mode 100644 NetFrameworkTests/ApiExceptionTests.cs create mode 100644 NetFrameworkTests/Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.csproj create mode 100644 NetFrameworkTests/Configuration.template.json create mode 100644 NetFrameworkTests/ConfigurationTests.cs create mode 100644 NetFrameworkTests/GenerateAndThenRecognize.cs create mode 100644 NetFrameworkTests/GenerateTests.cs create mode 100644 NetFrameworkTests/JwtAuthTests.cs create mode 100644 NetFrameworkTests/RecognizeTests.cs create mode 100644 NetFrameworkTests/ScanTests.cs create mode 100644 NetFrameworkTests/TestsBase.cs create mode 100644 NetFrameworkTests/nuget.config create mode 100644 NetFrameworkTests/test_data/Test_PostGenerateMultiple.png diff --git a/.github/workflows/net-framework.yml b/.github/workflows/net-framework.yml index 4c75c05..4502598 100644 --- a/.github/workflows/net-framework.yml +++ b/.github/workflows/net-framework.yml @@ -28,25 +28,26 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Setup .NET 10 SDK - uses: actions/setup-dotnet@v4 - with: - dotnet-version: '10.0.x' - - name: Setup MSBuild uses: microsoft/setup-msbuild@v1.1 - name: Setup VSTest uses: darenm/Setup-VSTest@v1.2 + - name: Pack nuget + run: | + msbuild -restore -t:clean,rebuild,pack -p:Configuration=Release + copy src\bin\Release\*.nupkg NetFrameworkTests + - name: Build the Solution - run: msbuild -restore -p:Configuration=Release + run: msbuild NetFrameworkTests\Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.csproj -restore -p:Configuration=Release + - name: Test with VSTest run: | $ErrorActionPreference = "Stop" - VSTest.Console.exe /Framework:".NETFramework,Version=${{ matrix.net-version }}" /ResultsDirectory:Tests\Results /Logger:"trx;LogFileName=test.log" Tests\bin\Release\${{ matrix.framework }}\Aspose.BarCode.Cloud.Sdk.Tests.dll - if( ([xml](Get-Content Tests\Results\test.log)).TestRun.ResultSummary.Counters.total -ne 25 ){ throw "Not all tests were explored or added new tests" } + VSTest.Console.exe /Framework:".NETFramework,Version=${{ matrix.net-version }}" /ResultsDirectory:NetFrameworkTests\Results /Logger:"trx;LogFileName=test.log" NetFrameworkTests\bin\Release\${{ matrix.framework }}\Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.dll + if( ([xml](Get-Content NetFrameworkTests\Results\test.log)).TestRun.ResultSummary.Counters.total -ne 21 ){ throw "Not all tests were explored or added new tests" } env: TEST_CONFIGURATION_JWT_TOKEN: ${{ secrets.TEST_CONFIGURATION_ACCESS_TOKEN }} diff --git a/.gitignore b/.gitignore index 71d9dce..879a55d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ _ReSharper.* bin obj .vs -Tests/Configuration*.json +Configuration.json *.DotSettings *.binlog snippets_test/ diff --git a/Makefile b/Makefile index a63ada9..e3255a0 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ format: dotnet restore ./Aspose.BarCode.Cloud.Sdk.sln dotnet format ./Aspose.BarCode.Cloud.Sdk.sln --no-restore dotnet format --include ./snippets/ + dotnet format --include ./NetFrameworkTests/ # Trim white space in comments find . -iname "*.cs" -exec sed -i -e 's_[[:space:]]*$$__' {} \; diff --git a/NetFrameworkTests/ApiExceptionTests.cs b/NetFrameworkTests/ApiExceptionTests.cs new file mode 100644 index 0000000..157eaa7 --- /dev/null +++ b/NetFrameworkTests/ApiExceptionTests.cs @@ -0,0 +1,31 @@ +using Aspose.BarCode.Cloud.Sdk.Api; +using Aspose.BarCode.Cloud.Sdk.Model; + +using NUnit.Framework; + +namespace Aspose.BarCode.Cloud.Sdk.Tests +{ + [TestFixture] + public class ApiExceptionTests : TestsBase + { + + [Test] + [Category("AsyncTests")] + public void GetBarcodeGenerateAsyncTestThrows() + { + // Arrange + GenerateApi api = new GenerateApi(clientId: "client id", clientSecret: "client secret"); + + // Acts + ApiException ex = Assert.ThrowsAsync( + async () => + { + await api.GenerateAsync(data: "Very sample text", + barcodeType: EncodeBarcodeType.Code128, imageFormat: BarcodeImageFormat.Png); + }); + + Assert.AreEqual(400, ex!.ErrorCode); + Assert.AreEqual("{\"error\":\"invalid_client\"}: ", ex.Message); + } + } +} diff --git a/NetFrameworkTests/Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.csproj b/NetFrameworkTests/Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.csproj new file mode 100644 index 0000000..34e6a91 --- /dev/null +++ b/NetFrameworkTests/Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.csproj @@ -0,0 +1,24 @@ + + + + net462;net480;net481 + true + true + 8.0 + + + + + + + + + + + + + + + + + diff --git a/NetFrameworkTests/Configuration.template.json b/NetFrameworkTests/Configuration.template.json new file mode 100644 index 0000000..fb5ee04 --- /dev/null +++ b/NetFrameworkTests/Configuration.template.json @@ -0,0 +1,4 @@ +{ + "ClientId": "Client Id from https://dashboard.aspose.cloud/applications", + "ClientSecret": "Client Secret from https://dashboard.aspose.cloud/applications" +} diff --git a/NetFrameworkTests/ConfigurationTests.cs b/NetFrameworkTests/ConfigurationTests.cs new file mode 100644 index 0000000..3360344 --- /dev/null +++ b/NetFrameworkTests/ConfigurationTests.cs @@ -0,0 +1,135 @@ +using System.Collections.Generic; +using System.IO; +using Aspose.BarCode.Cloud.Sdk.Api; +using System.Text.Json; +using NUnit.Framework; + +namespace Aspose.BarCode.Cloud.Sdk.Tests +{ + [TestFixture] + public class ConfigurationTests + { + private readonly Dictionary _headers = new Dictionary + { + ["User-Agent"] = "Awesome SDK" + }; + + + [Test] + public void CanChangeApiBaseUrl() + { + Configuration config = new Configuration + { + ApiBaseUrl = "http://localhost:47972" + }; + + Assert.AreEqual("http://localhost:47972/v4.0", config.GetApiRootUrl()); + } + + + [Test] + public void CanChangeDefaultHeaders() + { + // ReSharper disable once UseObjectOrCollectionInitializer + Configuration config = new Configuration(); + config.DefaultHeaders["User-Agent"] = "Awesome SDK"; + + Assert.AreEqual("Awesome SDK", config.DefaultHeaders["User-Agent"]); + } + + + [Test] + public void CanSetAuthTypeAndTokenTest() + { + Configuration config = new Configuration + { + JwtToken = "Test JWT token" + }; + + Assert.AreEqual("Test JWT token", config.JwtToken); + Assert.AreEqual(AuthType.ExternalAuth, config.AuthType); + } + + + [Test] + public void CanSetDebugMode() + { + Configuration config = new Configuration + { + DebugMode = true + }; + + Assert.AreEqual(true, config.DebugMode); + } + + + [Test] + public void CanSetDefaultHeaders() + { + Configuration config = new Configuration + { + DefaultHeaders = _headers + }; + + Assert.AreEqual("Awesome SDK", config.DefaultHeaders["User-Agent"]); + } + + + [Test] + public void DefaultParamsTest() + { + Configuration config = new Configuration(); + + Assert.AreEqual("https://api.aspose.cloud", config.ApiBaseUrl); + Assert.AreEqual("https://api.aspose.cloud/v4.0", config.GetApiRootUrl()); + Assert.AreEqual("https://id.aspose.cloud/connect/token", config.TokenUrl); + Assert.AreEqual(false, config.DebugMode); + } + + + [Test] + public void DeserializeTest() + { + using FileStream file = File.OpenRead(Path.Combine( + TestContext.CurrentContext.TestDirectory, + "..", "..", "..", + "Configuration.template.json")); + var config = JsonSerializer.Deserialize(file); + + Assert.IsNotNull(config); + Assert.AreEqual("Client Secret from https://dashboard.aspose.cloud/applications", config.ClientSecret); + Assert.AreEqual("Client Id from https://dashboard.aspose.cloud/applications", config.ClientId); + Assert.AreEqual(AuthType.JWT, config.AuthType); + } + + + [Test] + public void GetApiVersionTest() + { + Configuration config = new Configuration(); + + Assert.AreEqual("4.0", config.ApiVersion); + } + + + [Test] + public void SerializationTest() + { + Configuration config = new Configuration(); + + Assert.AreEqual( + "{\"" + + "ApiBaseUrl\":\"https://api.aspose.cloud\",\"" + + "TokenUrl\":\"https://id.aspose.cloud/connect/token\",\"" + + "ClientSecret\":null,\"" + + "ClientId\":null,\"" + + "JwtToken\":null,\"" + + "DebugMode\":false,\"" + + "AuthType\":\"JWT\",\"" + + "ApiVersion\":\"4.0\",\"" + + "DefaultHeaders\":{}" + + "}", + JsonSerializer.Serialize(config)); + } + } +} diff --git a/NetFrameworkTests/GenerateAndThenRecognize.cs b/NetFrameworkTests/GenerateAndThenRecognize.cs new file mode 100644 index 0000000..b78722d --- /dev/null +++ b/NetFrameworkTests/GenerateAndThenRecognize.cs @@ -0,0 +1,46 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Aspose.BarCode.Cloud.Sdk.Api; +using Aspose.BarCode.Cloud.Sdk.Interfaces; +using Aspose.BarCode.Cloud.Sdk.Model; + +using NUnit.Framework; + +namespace Aspose.BarCode.Cloud.Sdk.Tests +{ + [TestFixture] + public class GenerateAndThenRecognize : TestsBase + { + [SetUp] + public void Init() + { + _generateApi = new GenerateApi(TestConfiguration); + _scanApi = new ScanApi(TestConfiguration); + } + + private IGenerateApi _generateApi; + private IScanApi _scanApi; + + [Test] + [Category("AsyncTests")] + public async Task GenerateAndThenRecognizeAsyncTest() + { + Stream generatedImage = await _generateApi.GenerateAsync(EncodeBarcodeType.QR, "Test"); + + BarcodeResponseList recognized = await _scanApi.ScanMultipartAsync(generatedImage); + + Assert.AreEqual(1, recognized.Barcodes.Count); + Assert.AreEqual(DecodeBarcodeType.QR.ToString(), recognized.Barcodes.First().Type); + Assert.AreEqual("Test", recognized.Barcodes.First().BarcodeValue); + Assert.AreEqual( + "{\"barcodes\":[{" + + "\"barcodeValue\":\"Test\"," + + "\"type\":\"QR\"," + + "\"region\":[{\"x\":7,\"y\":6},{\"x\":49,\"y\":6},{\"x\":48,\"y\":48},{\"x\":6,\"y\":49}],\"checksum\":null}]}", + recognized.ToString() + ); + } + } +} diff --git a/NetFrameworkTests/GenerateTests.cs b/NetFrameworkTests/GenerateTests.cs new file mode 100644 index 0000000..5020dcb --- /dev/null +++ b/NetFrameworkTests/GenerateTests.cs @@ -0,0 +1,74 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Aspose.BarCode.Cloud.Sdk.Api; +using Aspose.BarCode.Cloud.Sdk.Interfaces; +using Aspose.BarCode.Cloud.Sdk.Model; + +using NUnit.Framework; + +namespace Aspose.BarCode.Cloud.Sdk.Tests +{ + [TestFixture] + public class GenerateTests : TestsBase + { + private IGenerateApi _api; + + [SetUp] + public void Init() + { + _api = new GenerateApi(TestConfiguration); + } + + [Test] + public async Task TestBarcodeGenerateBarcodeTypeGet() + { + + Stream response = await _api.GenerateAsync(EncodeBarcodeType.Code128, "Hello!"); + + long contentLength = response.Length; + Assert.True(contentLength > 0, "Content length is zero or negative"); + } + + [Test] + public async Task TestBarcodeGenerateBodyPost() + { + // Test case for barcode_generate_body_post + // Generate barcode from params in body + BarcodeImageParams imageParams = new BarcodeImageParams + { + ImageFormat = BarcodeImageFormat.Jpeg + }; + + EncodeData encodeData = new EncodeData() + { + Data = "VGVzdA==", + DataType = EncodeDataType.Base64Bytes + }; + + GenerateParams generatorParams = new GenerateParams() + { + BarcodeType = EncodeBarcodeType.QR, + EncodeData = encodeData, + BarcodeImageParams = imageParams + }; + + + Stream response = await _api.GenerateBodyAsync(generatorParams); + + long contentLength = response.Length; + Assert.True(contentLength > 0, "Content length is zero or negative"); + } + + [Test] + public async Task TestBarcodeGenerateMultipartPost() + { + + Stream response = await _api.GenerateMultipartAsync(EncodeBarcodeType.QR, "54657374", dataType: EncodeDataType.HexBytes); + + long contentLength = response.Length; + Assert.True(contentLength > 0, "Content length is zero or negative"); + } + + } +} diff --git a/NetFrameworkTests/JwtAuthTests.cs b/NetFrameworkTests/JwtAuthTests.cs new file mode 100644 index 0000000..27ac391 --- /dev/null +++ b/NetFrameworkTests/JwtAuthTests.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net.Http; +using System.Text.Json; +using System.Threading.Tasks; +using Aspose.BarCode.Cloud.Sdk.Api; +using Aspose.BarCode.Cloud.Sdk.Model; +using NUnit.Framework; + +namespace Aspose.BarCode.Cloud.Sdk.Tests +{ + [TestFixture] + public class JwtAuthTests : TestsBase + { + private async Task FetchToken() + { + Dictionary formParams = new Dictionary + { + ["grant_type"] = "client_credentials", + // ReSharper disable once RedundantToStringCall + ["client_id"] = TestConfiguration.ClientId.ToString(), + // ReSharper disable once RedundantToStringCall + ["client_secret"] = TestConfiguration.ClientSecret.ToString() + }; + FormUrlEncodedContent formContent = new FormUrlEncodedContent(formParams); + HttpResponseMessage response = await new HttpClient().PostAsync(TestConfiguration.TokenUrl, formContent); + response.EnsureSuccessStatusCode(); + JsonDocument json = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); + string accessToken = json.RootElement.GetProperty("access_token").GetString(); + return accessToken; + } + + [Test] + public async Task CanUseExternalTokenWithAsync() + { + if (TestConfiguration.AuthType != AuthType.JWT) + { + Assert.Ignore($"Unsupported TestConfiguration.AuthType={TestConfiguration.AuthType}"); + } + + Configuration configWithToken = new Configuration + { + ApiBaseUrl = TestConfiguration.ApiBaseUrl, + TokenUrl = TestConfiguration.TokenUrl, + JwtToken = await FetchToken() + }; + + GenerateApi api = new GenerateApi(configWithToken); + using Stream generated = await api.GenerateAsync(EncodeBarcodeType.QR, "Test"); + Assert.Greater(generated.Length, 0); + } + } +} diff --git a/NetFrameworkTests/RecognizeTests.cs b/NetFrameworkTests/RecognizeTests.cs new file mode 100644 index 0000000..fd5c206 --- /dev/null +++ b/NetFrameworkTests/RecognizeTests.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using Aspose.BarCode.Cloud.Sdk.Api; +using Aspose.BarCode.Cloud.Sdk.Interfaces; +using Aspose.BarCode.Cloud.Sdk.Model; +using NUnit.Framework; + +namespace Aspose.BarCode.Cloud.Sdk.Tests +{ + [TestFixture] + public class RecognizeTests : TestsBase + { + private IRecognizeApi _api; + + [SetUp] + public void Init() + { + _api = new RecognizeApi(TestConfiguration); + } + + [TearDown] + public void Cleanup() + { + } + + [Test] + public async Task RecognizeBase64AsyncTest() + { + // Arrange + using Stream image = GetTestImage("Test_PostGenerateMultiple.png"); + + byte[] buffer = new byte[image.Length]; + _ = await image.ReadAsync(buffer, 0, buffer.Length); + + // Act + BarcodeResponseList response = await _api.RecognizeBase64Async( + new RecognizeBase64Request() + { + RecognitionImageKind = RecognitionImageKind.ClearImage, + RecognitionMode = RecognitionMode.Normal, + BarcodeTypes = new List { DecodeBarcodeType.QR }, + FileBase64 = Convert.ToBase64String(buffer) + } + + ); + + // Assert + Assert.AreEqual(1, response.Barcodes.Count); + Assert.AreEqual(DecodeBarcodeType.QR.ToString(), response.Barcodes[0].Type); + Assert.AreEqual("Hello world!", response.Barcodes[0].BarcodeValue); + } + + [Test] + public async Task RecognizeMultipartAsyncTest() + { + // Arrange + using Stream image = GetTestImage("Test_PostGenerateMultiple.png"); + + // Act + BarcodeResponseList response = await _api.RecognizeMultipartAsync(DecodeBarcodeType.QR, image, + recognitionImageKind: RecognitionImageKind.ClearImage, recognitionMode: RecognitionMode.Normal); + + // Assert + Assert.AreEqual(1, response.Barcodes.Count); + Assert.AreEqual(DecodeBarcodeType.QR.ToString(), response.Barcodes[0].Type); + Assert.AreEqual("Hello world!", response.Barcodes[0].BarcodeValue); + } + + [Test] + public async Task RecognizeAsyncTest() + { + // Arrange + using Stream image = GetTestImage("Test_PostGenerateMultiple.png"); + + // Act + BarcodeResponseList response = await _api.RecognizeAsync(DecodeBarcodeType.QR, + "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png", + recognitionImageKind: RecognitionImageKind.ClearImage, + recognitionMode: RecognitionMode.Normal); + + // Assert + Assert.AreEqual(1, response.Barcodes.Count); + Assert.AreEqual(DecodeBarcodeType.QR.ToString(), response.Barcodes[0].Type); + Assert.AreEqual("http://en.m.wikipedia.org", response.Barcodes[0].BarcodeValue); + } + + } +} diff --git a/NetFrameworkTests/ScanTests.cs b/NetFrameworkTests/ScanTests.cs new file mode 100644 index 0000000..d8555bf --- /dev/null +++ b/NetFrameworkTests/ScanTests.cs @@ -0,0 +1,75 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Aspose.BarCode.Cloud.Sdk.Api; +using Aspose.BarCode.Cloud.Sdk.Interfaces; +using Aspose.BarCode.Cloud.Sdk.Model; + +using NUnit.Framework; + +namespace Aspose.BarCode.Cloud.Sdk.Tests +{ + [TestFixture] + public class ScanTests : TestsBase + { + private IScanApi _api; + + [SetUp] + public void Init() + { + _api = new ScanApi(TestConfiguration); + } + + + [Test] + public async Task ScanBase64AsyncTest() + { + // Arrange + using Stream image = GetTestImage("Test_PostGenerateMultiple.png"); + + byte[] buffer = new byte[image.Length]; + + _ = await image.ReadAsync(buffer, 0, buffer.Length); + // Act + BarcodeResponseList response = await _api.ScanBase64Async( + new ScanBase64Request() + { + FileBase64 = Convert.ToBase64String(buffer) + } + ); + + // Assert + Assert.AreEqual(2, response.Barcodes.Count); + Assert.AreEqual(DecodeBarcodeType.QR.ToString(), response.Barcodes[0].Type); + Assert.AreEqual("Hello world!", response.Barcodes[0].BarcodeValue); + } + + [Test] + public async Task ScanAsyncTest() + { + // Act + BarcodeResponseList response = await _api.ScanAsync("https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png"); + + // Assert + Assert.AreEqual(1, response.Barcodes.Count); + Assert.AreEqual(DecodeBarcodeType.QR.ToString(), response.Barcodes[0].Type); + Assert.AreEqual("http://en.m.wikipedia.org", response.Barcodes[0].BarcodeValue); + } + + [Test] + public async Task ScanMultipartAsyncTest() + { + // Arrange + using Stream image = GetTestImage("Test_PostGenerateMultiple.png"); + + // Act + BarcodeResponseList response = await _api.ScanMultipartAsync(image); + + // Assert + Assert.AreEqual(2, response.Barcodes.Count); + Assert.AreEqual(DecodeBarcodeType.QR.ToString(), response.Barcodes[0].Type); + Assert.AreEqual("Hello world!", response.Barcodes[0].BarcodeValue); + } + + } +} diff --git a/NetFrameworkTests/TestsBase.cs b/NetFrameworkTests/TestsBase.cs new file mode 100644 index 0000000..f5cad09 --- /dev/null +++ b/NetFrameworkTests/TestsBase.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using Aspose.BarCode.Cloud.Sdk.Api; +using System.Text.Json; +using NUnit.Framework; + +namespace Aspose.BarCode.Cloud.Sdk.Tests +{ + public class TestsBase + { + private const string ENV_NAME_PREFIX = "TEST_CONFIGURATION_"; + + /// + /// Temp folder path + /// + protected static readonly string TempFolderPath = Path.Combine( + "BarcodeTests", + Guid.NewGuid().ToString() + ).Replace("\\", "/"); + + protected TestsBase() + { + TestConfiguration = LoadTestConfiguration(); + } + + internal Configuration TestConfiguration { get; } + + private static Configuration LoadTestConfiguration() + { + string configFilename = Path.GetFullPath(Path.Combine( + TestContext.CurrentContext.TestDirectory, + "..", "..", "..", + "Configuration.json")); + if (File.Exists(configFilename)) + { + using FileStream file = File.OpenRead(configFilename); + return JsonSerializer.Deserialize(file) + ?? throw new Exception("Configuration file is empty or invalid"); + } + Configuration config = LoadFromEnv(); + return config; + } + + private static Configuration LoadFromEnv() + { + // Serialize default config to JSON then to a dictionary + string jsonStr = JsonSerializer.Serialize(new Configuration()); + var dict = JsonSerializer.Deserialize>(jsonStr); + + if (dict == null) + throw new Exception("Default configuration invalid"); + + foreach (var key in new List(dict.Keys)) + { + var value = dict[key]; + + // Accept strings and nulls (JSON null becomes null here) + if (value != null && !(value is string)) + continue; + + string envName = $"{ENV_NAME_PREFIX}{CamelCaseToUpper(key)}"; + string envValue = Environment.GetEnvironmentVariable(envName); + if (!string.IsNullOrEmpty(envValue)) + { + dict[key] = envValue; + } + } + // Serialize back and then deserialize to Configuration + string updatedJson = JsonSerializer.Serialize(dict); + var config = JsonSerializer.Deserialize(updatedJson); + if (config == null) + throw new Exception("Failed to create Configuration from environment variables"); + return config; + } + + private static string CamelCaseToUpper(string name) + { + StringBuilder result = new StringBuilder(); + bool prevCharWasLower = false; + foreach (char c in name) + { + if (char.IsUpper(c)) + { + if (prevCharWasLower) + { + result.Append('_'); + } + } + + result.Append(char.ToUpperInvariant(c)); + + prevCharWasLower = char.IsLower(c); + } + + return result.ToString(); + } + + protected static string TestFilePath(string fileName) + { + return Path.GetFullPath(Path.Combine( + TestContext.CurrentContext.TestDirectory, + "..", "..", "..", + "test_data", + fileName)); + } + + protected static Stream GetTestImage(string fileName) + { + string filePath = TestFilePath(fileName); + return File.Open(filePath, FileMode.Open, FileAccess.Read); + } + } +} diff --git a/NetFrameworkTests/nuget.config b/NetFrameworkTests/nuget.config new file mode 100644 index 0000000..048733c --- /dev/null +++ b/NetFrameworkTests/nuget.config @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/NetFrameworkTests/test_data/Test_PostGenerateMultiple.png b/NetFrameworkTests/test_data/Test_PostGenerateMultiple.png new file mode 100644 index 0000000000000000000000000000000000000000..46b0fc5c11c314450ac591796fb27201753920ff GIT binary patch literal 8589 zcmeHNYfuwc6b{p=qJyN`aYPxD={Sm=Q9}WzRsumm0z)gMwW8I6X+W`3<0ub9LRN7m z)M@RCjPfQ$p(u`E#0JYFsSP$mfuxC#1Y#0j1TlsLmL!nP?sivF#~*$D=uH3YkKN7O zbM86ke&=NO-tXR%A8utYn7?d3g+f`7oRshpg%Yp}|7{DLgWpw$*RR1J^A08LJVK!a zD~T&W#k-hAp}fUPPS~{VXsx{lJk`vWZmA2nbozMRysWsuf;p!H8Y+VVkG~PvAQ7Fj z?q5AxFK_+h{u?tx@5Z0u>Q^fDc9 z?(sLvXuG_U2@`wjz~HOet+BE$kXLBN9d-OVYx|H(dgZxD@uK(ks#&9W@AL4Eg3$%a zaN%WtK+mM67#D0R@&w_HHh&{1-v;dnRyx@u+KPiTBPRsCC_I~)c(rJ<%_E}`Vfrn8 zo3WB0vd33V}L3>~kMsEov6&~7Ue!W}w^1*sb9 zP(iLxj>G1$^kCnkX;K_zO>a6s*QT6t0}5;(l3mhB)Z#Zxw?Q;jrzK3xkq>iWZ~HYm z^5YOl(}HNN-Drt>p>>^(my`Iozkd9|%qDFQQEB42{$%|Jq6c4tb}Gx~WHp<^V#+Ws`q8$g-EnOoiNZuLhSPs?L;oWw-*nWF=i@m2VKK9bqynIu4vt{8Eo~v;MU=NM9 z372jzEkBY>b|k?eD~J4dkgpE%^+l94PK$&!&Q+5gFs;Tw?3bnd^|Q8PTUV5rBdSD-(jkOaFPWr^&9 z5sv~x>@^R;L8eSnsXyWLZURz0b1@zO8@;zB=yee~GkVS{*juQ`1dx1W`Y}j%$9f$( z)3pO!_#__*u<5B@fqd!&k6)ZhJ*5?Doz&%OrtMB&@34679B#XB@hM4w{5Q5MhzM7Mce zwlgQyTtNh5LZqlr*}9KnhH5^;F2OZ0ZQ80XOA0huuPPuMLGG`*X~#M~2AqQ@c8@V- zVbCH+Pe!}h+5_Ycc2`&(00v{e#KnX|wD;$J0gfTfm^xsaJHr5G`Y~2Q8`NTphj6^| zk;#4r24uMU;<-`QiFVP}G+|J?%&>r@S?}5a3Rl|i{Zh@%~y$?ANd>fi*1{xa_)8XJ1i^J8w5Sv!99*Lle zW#|S1;P;Aio=()L?m0ZV?s1(cti9Ax&d+7NV{PD1v}6u^#EX998&dk&j-C6k&_bbAH3mS5T6-Y)xq0yr<-Me#q3; literal 0 HcmV?d00001 diff --git a/Tests/Aspose.BarCode.Cloud.Sdk.Tests.csproj b/Tests/Aspose.BarCode.Cloud.Sdk.Tests.csproj index 671baca..d013cf7 100644 --- a/Tests/Aspose.BarCode.Cloud.Sdk.Tests.csproj +++ b/Tests/Aspose.BarCode.Cloud.Sdk.Tests.csproj @@ -1,11 +1,9 @@ - net462;net480;net481;net8.0;net9.0;net10.0 + net8.0;net9.0;net10.0 true - NU1510 true - 8.0 diff --git a/Tests/Configuration.template.json b/Tests/Configuration.template.json index 7c22e70..fb5ee04 100644 --- a/Tests/Configuration.template.json +++ b/Tests/Configuration.template.json @@ -1,6 +1,4 @@ { "ClientId": "Client Id from https://dashboard.aspose.cloud/applications", - "ClientSecret": "Client Secret from https://dashboard.aspose.cloud/applications", - "ApiBaseUrl": "https://api.aspose.cloud", - "TokenUrl": "https://api.aspose.cloud/connect/token" + "ClientSecret": "Client Secret from https://dashboard.aspose.cloud/applications" } From 99ae327d93485498e28962cf2a38278bd5c81fe3 Mon Sep 17 00:00:00 2001 From: Ivan Kamkin <234-Ivan.Kamkin@users.noreply.git.saltov.dynabic.com> Date: Fri, 28 Nov 2025 14:08:45 +0500 Subject: [PATCH 2/6] Fixes --- .github/workflows/net-framework.yml | 8 ++++---- Tests/Aspose.BarCode.Cloud.Sdk.Tests.csproj | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/net-framework.yml b/.github/workflows/net-framework.yml index 4502598..5477c07 100644 --- a/.github/workflows/net-framework.yml +++ b/.github/workflows/net-framework.yml @@ -36,13 +36,13 @@ jobs: - name: Pack nuget run: | - msbuild -restore -t:clean,rebuild,pack -p:Configuration=Release - copy src\bin\Release\*.nupkg NetFrameworkTests + docker build . --file Dockerfile -t aspose-barcode-cloud-dotnet:test-nuget + $containerId = docker run -di --rm aspose-barcode-cloud-dotnet:test-nuget + docker cp "$containerId:/packed/*.nupkg" NetFrameworkTests - - name: Build the Solution + - name: Build test project run: msbuild NetFrameworkTests\Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.csproj -restore -p:Configuration=Release - - name: Test with VSTest run: | $ErrorActionPreference = "Stop" diff --git a/Tests/Aspose.BarCode.Cloud.Sdk.Tests.csproj b/Tests/Aspose.BarCode.Cloud.Sdk.Tests.csproj index d013cf7..316ef8d 100644 --- a/Tests/Aspose.BarCode.Cloud.Sdk.Tests.csproj +++ b/Tests/Aspose.BarCode.Cloud.Sdk.Tests.csproj @@ -15,7 +15,6 @@ - From 44104915235401aee081d9de2f754c18acc88bba Mon Sep 17 00:00:00 2001 From: Ivan Kamkin <234-Ivan.Kamkin@users.noreply.git.saltov.dynabic.com> Date: Fri, 28 Nov 2025 14:18:07 +0500 Subject: [PATCH 3/6] Fix ps script in ci --- .github/workflows/net-framework.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/net-framework.yml b/.github/workflows/net-framework.yml index 5477c07..77a1a1e 100644 --- a/.github/workflows/net-framework.yml +++ b/.github/workflows/net-framework.yml @@ -38,7 +38,7 @@ jobs: run: | docker build . --file Dockerfile -t aspose-barcode-cloud-dotnet:test-nuget $containerId = docker run -di --rm aspose-barcode-cloud-dotnet:test-nuget - docker cp "$containerId:/packed/*.nupkg" NetFrameworkTests + docker cp "${containerId}:/packed/*.nupkg" NetFrameworkTests - name: Build test project run: msbuild NetFrameworkTests\Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.csproj -restore -p:Configuration=Release From 97934687500f0a97ac888f946e2ca0adebb8b1c0 Mon Sep 17 00:00:00 2001 From: Ivan Kamkin <234-Ivan.Kamkin@users.noreply.git.saltov.dynabic.com> Date: Fri, 28 Nov 2025 14:28:10 +0500 Subject: [PATCH 4/6] Change build nuget to local --- .github/workflows/net-framework.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/net-framework.yml b/.github/workflows/net-framework.yml index 77a1a1e..580c863 100644 --- a/.github/workflows/net-framework.yml +++ b/.github/workflows/net-framework.yml @@ -28,6 +28,11 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Setup .NET 10 SDK + uses: actions/setup-dotnet@v4 + with: + dotnet-version: '10.0.x' + - name: Setup MSBuild uses: microsoft/setup-msbuild@v1.1 @@ -36,9 +41,8 @@ jobs: - name: Pack nuget run: | - docker build . --file Dockerfile -t aspose-barcode-cloud-dotnet:test-nuget - $containerId = docker run -di --rm aspose-barcode-cloud-dotnet:test-nuget - docker cp "${containerId}:/packed/*.nupkg" NetFrameworkTests + msbuild -restore -t:clean,rebuild,pack -p:Configuration=Release + copy src\bin\Release\*.nupkg NetFrameworkTests - name: Build test project run: msbuild NetFrameworkTests\Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.csproj -restore -p:Configuration=Release From eeac5e3e2cb89e0bb46d463b10fa83390c5357aa Mon Sep 17 00:00:00 2001 From: Ivan Kamkin <234-Ivan.Kamkin@users.noreply.git.saltov.dynabic.com> Date: Fri, 28 Nov 2025 17:24:54 +0500 Subject: [PATCH 5/6] test files as links --- NetFrameworkTests/ApiExceptionTests.cs | 31 ---- ...BarCode.Cloud.Sdk.NetFrameworkTests.csproj | 26 ++-- NetFrameworkTests/ConfigurationTests.cs | 135 ------------------ NetFrameworkTests/GenerateAndThenRecognize.cs | 46 ------ NetFrameworkTests/GenerateTests.cs | 74 ---------- NetFrameworkTests/JwtAuthTests.cs | 54 ------- NetFrameworkTests/RecognizeTests.cs | 90 ------------ NetFrameworkTests/ScanTests.cs | 75 ---------- NetFrameworkTests/TestsBase.cs | 115 --------------- 9 files changed, 18 insertions(+), 628 deletions(-) delete mode 100644 NetFrameworkTests/ApiExceptionTests.cs delete mode 100644 NetFrameworkTests/ConfigurationTests.cs delete mode 100644 NetFrameworkTests/GenerateAndThenRecognize.cs delete mode 100644 NetFrameworkTests/GenerateTests.cs delete mode 100644 NetFrameworkTests/JwtAuthTests.cs delete mode 100644 NetFrameworkTests/RecognizeTests.cs delete mode 100644 NetFrameworkTests/ScanTests.cs delete mode 100644 NetFrameworkTests/TestsBase.cs diff --git a/NetFrameworkTests/ApiExceptionTests.cs b/NetFrameworkTests/ApiExceptionTests.cs deleted file mode 100644 index 157eaa7..0000000 --- a/NetFrameworkTests/ApiExceptionTests.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Aspose.BarCode.Cloud.Sdk.Api; -using Aspose.BarCode.Cloud.Sdk.Model; - -using NUnit.Framework; - -namespace Aspose.BarCode.Cloud.Sdk.Tests -{ - [TestFixture] - public class ApiExceptionTests : TestsBase - { - - [Test] - [Category("AsyncTests")] - public void GetBarcodeGenerateAsyncTestThrows() - { - // Arrange - GenerateApi api = new GenerateApi(clientId: "client id", clientSecret: "client secret"); - - // Acts - ApiException ex = Assert.ThrowsAsync( - async () => - { - await api.GenerateAsync(data: "Very sample text", - barcodeType: EncodeBarcodeType.Code128, imageFormat: BarcodeImageFormat.Png); - }); - - Assert.AreEqual(400, ex!.ErrorCode); - Assert.AreEqual("{\"error\":\"invalid_client\"}: ", ex.Message); - } - } -} diff --git a/NetFrameworkTests/Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.csproj b/NetFrameworkTests/Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.csproj index 34e6a91..7808e8b 100644 --- a/NetFrameworkTests/Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.csproj +++ b/NetFrameworkTests/Aspose.BarCode.Cloud.Sdk.NetFrameworkTests.csproj @@ -7,16 +7,26 @@ 8.0 - - - + + + - - - - - + + + + + + + + + + + + + + + diff --git a/NetFrameworkTests/ConfigurationTests.cs b/NetFrameworkTests/ConfigurationTests.cs deleted file mode 100644 index 3360344..0000000 --- a/NetFrameworkTests/ConfigurationTests.cs +++ /dev/null @@ -1,135 +0,0 @@ -using System.Collections.Generic; -using System.IO; -using Aspose.BarCode.Cloud.Sdk.Api; -using System.Text.Json; -using NUnit.Framework; - -namespace Aspose.BarCode.Cloud.Sdk.Tests -{ - [TestFixture] - public class ConfigurationTests - { - private readonly Dictionary _headers = new Dictionary - { - ["User-Agent"] = "Awesome SDK" - }; - - - [Test] - public void CanChangeApiBaseUrl() - { - Configuration config = new Configuration - { - ApiBaseUrl = "http://localhost:47972" - }; - - Assert.AreEqual("http://localhost:47972/v4.0", config.GetApiRootUrl()); - } - - - [Test] - public void CanChangeDefaultHeaders() - { - // ReSharper disable once UseObjectOrCollectionInitializer - Configuration config = new Configuration(); - config.DefaultHeaders["User-Agent"] = "Awesome SDK"; - - Assert.AreEqual("Awesome SDK", config.DefaultHeaders["User-Agent"]); - } - - - [Test] - public void CanSetAuthTypeAndTokenTest() - { - Configuration config = new Configuration - { - JwtToken = "Test JWT token" - }; - - Assert.AreEqual("Test JWT token", config.JwtToken); - Assert.AreEqual(AuthType.ExternalAuth, config.AuthType); - } - - - [Test] - public void CanSetDebugMode() - { - Configuration config = new Configuration - { - DebugMode = true - }; - - Assert.AreEqual(true, config.DebugMode); - } - - - [Test] - public void CanSetDefaultHeaders() - { - Configuration config = new Configuration - { - DefaultHeaders = _headers - }; - - Assert.AreEqual("Awesome SDK", config.DefaultHeaders["User-Agent"]); - } - - - [Test] - public void DefaultParamsTest() - { - Configuration config = new Configuration(); - - Assert.AreEqual("https://api.aspose.cloud", config.ApiBaseUrl); - Assert.AreEqual("https://api.aspose.cloud/v4.0", config.GetApiRootUrl()); - Assert.AreEqual("https://id.aspose.cloud/connect/token", config.TokenUrl); - Assert.AreEqual(false, config.DebugMode); - } - - - [Test] - public void DeserializeTest() - { - using FileStream file = File.OpenRead(Path.Combine( - TestContext.CurrentContext.TestDirectory, - "..", "..", "..", - "Configuration.template.json")); - var config = JsonSerializer.Deserialize(file); - - Assert.IsNotNull(config); - Assert.AreEqual("Client Secret from https://dashboard.aspose.cloud/applications", config.ClientSecret); - Assert.AreEqual("Client Id from https://dashboard.aspose.cloud/applications", config.ClientId); - Assert.AreEqual(AuthType.JWT, config.AuthType); - } - - - [Test] - public void GetApiVersionTest() - { - Configuration config = new Configuration(); - - Assert.AreEqual("4.0", config.ApiVersion); - } - - - [Test] - public void SerializationTest() - { - Configuration config = new Configuration(); - - Assert.AreEqual( - "{\"" + - "ApiBaseUrl\":\"https://api.aspose.cloud\",\"" + - "TokenUrl\":\"https://id.aspose.cloud/connect/token\",\"" + - "ClientSecret\":null,\"" + - "ClientId\":null,\"" + - "JwtToken\":null,\"" + - "DebugMode\":false,\"" + - "AuthType\":\"JWT\",\"" + - "ApiVersion\":\"4.0\",\"" + - "DefaultHeaders\":{}" + - "}", - JsonSerializer.Serialize(config)); - } - } -} diff --git a/NetFrameworkTests/GenerateAndThenRecognize.cs b/NetFrameworkTests/GenerateAndThenRecognize.cs deleted file mode 100644 index b78722d..0000000 --- a/NetFrameworkTests/GenerateAndThenRecognize.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Aspose.BarCode.Cloud.Sdk.Api; -using Aspose.BarCode.Cloud.Sdk.Interfaces; -using Aspose.BarCode.Cloud.Sdk.Model; - -using NUnit.Framework; - -namespace Aspose.BarCode.Cloud.Sdk.Tests -{ - [TestFixture] - public class GenerateAndThenRecognize : TestsBase - { - [SetUp] - public void Init() - { - _generateApi = new GenerateApi(TestConfiguration); - _scanApi = new ScanApi(TestConfiguration); - } - - private IGenerateApi _generateApi; - private IScanApi _scanApi; - - [Test] - [Category("AsyncTests")] - public async Task GenerateAndThenRecognizeAsyncTest() - { - Stream generatedImage = await _generateApi.GenerateAsync(EncodeBarcodeType.QR, "Test"); - - BarcodeResponseList recognized = await _scanApi.ScanMultipartAsync(generatedImage); - - Assert.AreEqual(1, recognized.Barcodes.Count); - Assert.AreEqual(DecodeBarcodeType.QR.ToString(), recognized.Barcodes.First().Type); - Assert.AreEqual("Test", recognized.Barcodes.First().BarcodeValue); - Assert.AreEqual( - "{\"barcodes\":[{" + - "\"barcodeValue\":\"Test\"," + - "\"type\":\"QR\"," + - "\"region\":[{\"x\":7,\"y\":6},{\"x\":49,\"y\":6},{\"x\":48,\"y\":48},{\"x\":6,\"y\":49}],\"checksum\":null}]}", - recognized.ToString() - ); - } - } -} diff --git a/NetFrameworkTests/GenerateTests.cs b/NetFrameworkTests/GenerateTests.cs deleted file mode 100644 index 5020dcb..0000000 --- a/NetFrameworkTests/GenerateTests.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; -using Aspose.BarCode.Cloud.Sdk.Api; -using Aspose.BarCode.Cloud.Sdk.Interfaces; -using Aspose.BarCode.Cloud.Sdk.Model; - -using NUnit.Framework; - -namespace Aspose.BarCode.Cloud.Sdk.Tests -{ - [TestFixture] - public class GenerateTests : TestsBase - { - private IGenerateApi _api; - - [SetUp] - public void Init() - { - _api = new GenerateApi(TestConfiguration); - } - - [Test] - public async Task TestBarcodeGenerateBarcodeTypeGet() - { - - Stream response = await _api.GenerateAsync(EncodeBarcodeType.Code128, "Hello!"); - - long contentLength = response.Length; - Assert.True(contentLength > 0, "Content length is zero or negative"); - } - - [Test] - public async Task TestBarcodeGenerateBodyPost() - { - // Test case for barcode_generate_body_post - // Generate barcode from params in body - BarcodeImageParams imageParams = new BarcodeImageParams - { - ImageFormat = BarcodeImageFormat.Jpeg - }; - - EncodeData encodeData = new EncodeData() - { - Data = "VGVzdA==", - DataType = EncodeDataType.Base64Bytes - }; - - GenerateParams generatorParams = new GenerateParams() - { - BarcodeType = EncodeBarcodeType.QR, - EncodeData = encodeData, - BarcodeImageParams = imageParams - }; - - - Stream response = await _api.GenerateBodyAsync(generatorParams); - - long contentLength = response.Length; - Assert.True(contentLength > 0, "Content length is zero or negative"); - } - - [Test] - public async Task TestBarcodeGenerateMultipartPost() - { - - Stream response = await _api.GenerateMultipartAsync(EncodeBarcodeType.QR, "54657374", dataType: EncodeDataType.HexBytes); - - long contentLength = response.Length; - Assert.True(contentLength > 0, "Content length is zero or negative"); - } - - } -} diff --git a/NetFrameworkTests/JwtAuthTests.cs b/NetFrameworkTests/JwtAuthTests.cs deleted file mode 100644 index 27ac391..0000000 --- a/NetFrameworkTests/JwtAuthTests.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Net.Http; -using System.Text.Json; -using System.Threading.Tasks; -using Aspose.BarCode.Cloud.Sdk.Api; -using Aspose.BarCode.Cloud.Sdk.Model; -using NUnit.Framework; - -namespace Aspose.BarCode.Cloud.Sdk.Tests -{ - [TestFixture] - public class JwtAuthTests : TestsBase - { - private async Task FetchToken() - { - Dictionary formParams = new Dictionary - { - ["grant_type"] = "client_credentials", - // ReSharper disable once RedundantToStringCall - ["client_id"] = TestConfiguration.ClientId.ToString(), - // ReSharper disable once RedundantToStringCall - ["client_secret"] = TestConfiguration.ClientSecret.ToString() - }; - FormUrlEncodedContent formContent = new FormUrlEncodedContent(formParams); - HttpResponseMessage response = await new HttpClient().PostAsync(TestConfiguration.TokenUrl, formContent); - response.EnsureSuccessStatusCode(); - JsonDocument json = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); - string accessToken = json.RootElement.GetProperty("access_token").GetString(); - return accessToken; - } - - [Test] - public async Task CanUseExternalTokenWithAsync() - { - if (TestConfiguration.AuthType != AuthType.JWT) - { - Assert.Ignore($"Unsupported TestConfiguration.AuthType={TestConfiguration.AuthType}"); - } - - Configuration configWithToken = new Configuration - { - ApiBaseUrl = TestConfiguration.ApiBaseUrl, - TokenUrl = TestConfiguration.TokenUrl, - JwtToken = await FetchToken() - }; - - GenerateApi api = new GenerateApi(configWithToken); - using Stream generated = await api.GenerateAsync(EncodeBarcodeType.QR, "Test"); - Assert.Greater(generated.Length, 0); - } - } -} diff --git a/NetFrameworkTests/RecognizeTests.cs b/NetFrameworkTests/RecognizeTests.cs deleted file mode 100644 index fd5c206..0000000 --- a/NetFrameworkTests/RecognizeTests.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading.Tasks; -using Aspose.BarCode.Cloud.Sdk.Api; -using Aspose.BarCode.Cloud.Sdk.Interfaces; -using Aspose.BarCode.Cloud.Sdk.Model; -using NUnit.Framework; - -namespace Aspose.BarCode.Cloud.Sdk.Tests -{ - [TestFixture] - public class RecognizeTests : TestsBase - { - private IRecognizeApi _api; - - [SetUp] - public void Init() - { - _api = new RecognizeApi(TestConfiguration); - } - - [TearDown] - public void Cleanup() - { - } - - [Test] - public async Task RecognizeBase64AsyncTest() - { - // Arrange - using Stream image = GetTestImage("Test_PostGenerateMultiple.png"); - - byte[] buffer = new byte[image.Length]; - _ = await image.ReadAsync(buffer, 0, buffer.Length); - - // Act - BarcodeResponseList response = await _api.RecognizeBase64Async( - new RecognizeBase64Request() - { - RecognitionImageKind = RecognitionImageKind.ClearImage, - RecognitionMode = RecognitionMode.Normal, - BarcodeTypes = new List { DecodeBarcodeType.QR }, - FileBase64 = Convert.ToBase64String(buffer) - } - - ); - - // Assert - Assert.AreEqual(1, response.Barcodes.Count); - Assert.AreEqual(DecodeBarcodeType.QR.ToString(), response.Barcodes[0].Type); - Assert.AreEqual("Hello world!", response.Barcodes[0].BarcodeValue); - } - - [Test] - public async Task RecognizeMultipartAsyncTest() - { - // Arrange - using Stream image = GetTestImage("Test_PostGenerateMultiple.png"); - - // Act - BarcodeResponseList response = await _api.RecognizeMultipartAsync(DecodeBarcodeType.QR, image, - recognitionImageKind: RecognitionImageKind.ClearImage, recognitionMode: RecognitionMode.Normal); - - // Assert - Assert.AreEqual(1, response.Barcodes.Count); - Assert.AreEqual(DecodeBarcodeType.QR.ToString(), response.Barcodes[0].Type); - Assert.AreEqual("Hello world!", response.Barcodes[0].BarcodeValue); - } - - [Test] - public async Task RecognizeAsyncTest() - { - // Arrange - using Stream image = GetTestImage("Test_PostGenerateMultiple.png"); - - // Act - BarcodeResponseList response = await _api.RecognizeAsync(DecodeBarcodeType.QR, - "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png", - recognitionImageKind: RecognitionImageKind.ClearImage, - recognitionMode: RecognitionMode.Normal); - - // Assert - Assert.AreEqual(1, response.Barcodes.Count); - Assert.AreEqual(DecodeBarcodeType.QR.ToString(), response.Barcodes[0].Type); - Assert.AreEqual("http://en.m.wikipedia.org", response.Barcodes[0].BarcodeValue); - } - - } -} diff --git a/NetFrameworkTests/ScanTests.cs b/NetFrameworkTests/ScanTests.cs deleted file mode 100644 index d8555bf..0000000 --- a/NetFrameworkTests/ScanTests.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; -using Aspose.BarCode.Cloud.Sdk.Api; -using Aspose.BarCode.Cloud.Sdk.Interfaces; -using Aspose.BarCode.Cloud.Sdk.Model; - -using NUnit.Framework; - -namespace Aspose.BarCode.Cloud.Sdk.Tests -{ - [TestFixture] - public class ScanTests : TestsBase - { - private IScanApi _api; - - [SetUp] - public void Init() - { - _api = new ScanApi(TestConfiguration); - } - - - [Test] - public async Task ScanBase64AsyncTest() - { - // Arrange - using Stream image = GetTestImage("Test_PostGenerateMultiple.png"); - - byte[] buffer = new byte[image.Length]; - - _ = await image.ReadAsync(buffer, 0, buffer.Length); - // Act - BarcodeResponseList response = await _api.ScanBase64Async( - new ScanBase64Request() - { - FileBase64 = Convert.ToBase64String(buffer) - } - ); - - // Assert - Assert.AreEqual(2, response.Barcodes.Count); - Assert.AreEqual(DecodeBarcodeType.QR.ToString(), response.Barcodes[0].Type); - Assert.AreEqual("Hello world!", response.Barcodes[0].BarcodeValue); - } - - [Test] - public async Task ScanAsyncTest() - { - // Act - BarcodeResponseList response = await _api.ScanAsync("https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png"); - - // Assert - Assert.AreEqual(1, response.Barcodes.Count); - Assert.AreEqual(DecodeBarcodeType.QR.ToString(), response.Barcodes[0].Type); - Assert.AreEqual("http://en.m.wikipedia.org", response.Barcodes[0].BarcodeValue); - } - - [Test] - public async Task ScanMultipartAsyncTest() - { - // Arrange - using Stream image = GetTestImage("Test_PostGenerateMultiple.png"); - - // Act - BarcodeResponseList response = await _api.ScanMultipartAsync(image); - - // Assert - Assert.AreEqual(2, response.Barcodes.Count); - Assert.AreEqual(DecodeBarcodeType.QR.ToString(), response.Barcodes[0].Type); - Assert.AreEqual("Hello world!", response.Barcodes[0].BarcodeValue); - } - - } -} diff --git a/NetFrameworkTests/TestsBase.cs b/NetFrameworkTests/TestsBase.cs deleted file mode 100644 index f5cad09..0000000 --- a/NetFrameworkTests/TestsBase.cs +++ /dev/null @@ -1,115 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using Aspose.BarCode.Cloud.Sdk.Api; -using System.Text.Json; -using NUnit.Framework; - -namespace Aspose.BarCode.Cloud.Sdk.Tests -{ - public class TestsBase - { - private const string ENV_NAME_PREFIX = "TEST_CONFIGURATION_"; - - /// - /// Temp folder path - /// - protected static readonly string TempFolderPath = Path.Combine( - "BarcodeTests", - Guid.NewGuid().ToString() - ).Replace("\\", "/"); - - protected TestsBase() - { - TestConfiguration = LoadTestConfiguration(); - } - - internal Configuration TestConfiguration { get; } - - private static Configuration LoadTestConfiguration() - { - string configFilename = Path.GetFullPath(Path.Combine( - TestContext.CurrentContext.TestDirectory, - "..", "..", "..", - "Configuration.json")); - if (File.Exists(configFilename)) - { - using FileStream file = File.OpenRead(configFilename); - return JsonSerializer.Deserialize(file) - ?? throw new Exception("Configuration file is empty or invalid"); - } - Configuration config = LoadFromEnv(); - return config; - } - - private static Configuration LoadFromEnv() - { - // Serialize default config to JSON then to a dictionary - string jsonStr = JsonSerializer.Serialize(new Configuration()); - var dict = JsonSerializer.Deserialize>(jsonStr); - - if (dict == null) - throw new Exception("Default configuration invalid"); - - foreach (var key in new List(dict.Keys)) - { - var value = dict[key]; - - // Accept strings and nulls (JSON null becomes null here) - if (value != null && !(value is string)) - continue; - - string envName = $"{ENV_NAME_PREFIX}{CamelCaseToUpper(key)}"; - string envValue = Environment.GetEnvironmentVariable(envName); - if (!string.IsNullOrEmpty(envValue)) - { - dict[key] = envValue; - } - } - // Serialize back and then deserialize to Configuration - string updatedJson = JsonSerializer.Serialize(dict); - var config = JsonSerializer.Deserialize(updatedJson); - if (config == null) - throw new Exception("Failed to create Configuration from environment variables"); - return config; - } - - private static string CamelCaseToUpper(string name) - { - StringBuilder result = new StringBuilder(); - bool prevCharWasLower = false; - foreach (char c in name) - { - if (char.IsUpper(c)) - { - if (prevCharWasLower) - { - result.Append('_'); - } - } - - result.Append(char.ToUpperInvariant(c)); - - prevCharWasLower = char.IsLower(c); - } - - return result.ToString(); - } - - protected static string TestFilePath(string fileName) - { - return Path.GetFullPath(Path.Combine( - TestContext.CurrentContext.TestDirectory, - "..", "..", "..", - "test_data", - fileName)); - } - - protected static Stream GetTestImage(string fileName) - { - string filePath = TestFilePath(fileName); - return File.Open(filePath, FileMode.Open, FileAccess.Read); - } - } -} From 0c0631a4c664f455e55f798fbb12fd88549a3703 Mon Sep 17 00:00:00 2001 From: ivankamkin <109517020+ivankamkin@users.noreply.github.com> Date: Fri, 28 Nov 2025 18:10:38 +0500 Subject: [PATCH 6/6] Update Makefile Co-authored-by: Denis Averin <59285247+Denis-Averin@users.noreply.github.com> --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index e3255a0..bf87c4f 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,7 @@ init: format: dotnet restore ./Aspose.BarCode.Cloud.Sdk.sln dotnet format ./Aspose.BarCode.Cloud.Sdk.sln --no-restore - dotnet format --include ./snippets/ - dotnet format --include ./NetFrameworkTests/ + dotnet format --include ./snippets/ --include ./NetFrameworkTests/ # Trim white space in comments find . -iname "*.cs" -exec sed -i -e 's_[[:space:]]*$$__' {} \;