From 5a3ff8d4b45f373e12d5bfa6d97d69e6a85da51a Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 5 Sep 2024 21:51:41 +0200 Subject: [PATCH] add tests --- .../ApplicationPackageListProviderTest.cs | 44 +++++++++++++++++++ .../Package/ApplicationPackageListProvider.cs | 24 +++++++--- 2 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 clio.tests/ApplicationPackageListProviderTest.cs diff --git a/clio.tests/ApplicationPackageListProviderTest.cs b/clio.tests/ApplicationPackageListProviderTest.cs new file mode 100644 index 00000000..26ec1e75 --- /dev/null +++ b/clio.tests/ApplicationPackageListProviderTest.cs @@ -0,0 +1,44 @@ +using Autofac; +using Clio.Common; +using Clio.Package; +using Clio.Tests.Command; +using FluentAssertions; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Clio.Tests +{ + [TestFixture] + internal class ApplicationPackageListProviderTest: BaseClioModuleTests + { + [TestCase("")] + [TestCase("{}")] + public void CreatePackageInfo_ThrowExceprionIfResponseIsIncorect(string responseData) { + IJsonConverter jsonConverter = Container.Resolve(); + var provider = new ApplicationPackageListProvider(jsonConverter); + Action act = () => provider.ParsePackageInfoResponse(responseData); + act.Should().Throw(); + } + + [TestCase("[]", 0)] + [TestCase("[{\"Name\":\"X\"}]", 1)] + [TestCase("[{\"Name\":\"X\", \"Maintainer\":\"Y\" }]", 1)] + [TestCase("[{\"Name\":\"X\", \"Maintainer\":\"Y\" },{\"Name\":\"X\", \"Maintainer\":\"Y\" }]", 2)] + [TestCase("[{\"Name\":\"X\", \"Maintainer\":\"Y\" },{\"Name\":\"X\", \"Maintainer\":\"Y\" },{\"Name\":\"X\", \"Maintainer\":\"Y\" }]", 3)] + [TestCase("[{\"Name\":\"X\", \"Maintainer\":\"Y\", \"UId\":\"00000000-0000-0000-0000-000000000001\", \"Version\":\"1.0.0\" }]", 1)] + [TestCase("[{\"Name\":\"X\", \"Maintainer\":\"Y\", \"UId\":\"00000000-0000-0000-0000-000000000001\", \"Version\":\"1.0.0\" },{\"Name\":\"X\", \"Maintainer\":\"Y\", \"UId\":\"00000000-0000-0000-0000-000000000002\", \"Version\":\"1.1.0\" }]", 2)] + [TestCase("[{\"Name\":\"X\", \"Maintainer\":\"Y\", \"UId\":\"00000000-0000-0000-0000-000000000001\", \"Version\":\"1.0.0\" },{\"Name\":\"X\", \"Maintainer\":\"Y\", \"UId\":\"00000000-0000-0000-0000-000000000002\", \"Version\":\"1.1.0\" },{\"Name\":\"X\", \"Maintainer\":\"Y\", \"UId\":\"00000000-0000-0000-0000-000000000003\", \"Version\":\"2.0.0\" }]", 3)] + public void CreatePackageInfo_ReturnCorrectPackagesIfResponseCorrect(string responseData, int packageCount) { + IJsonConverter jsonConverter = Container.Resolve(); + var provider = new ApplicationPackageListProvider(jsonConverter); + var result = provider.ParsePackageInfoResponse(responseData); + result.Should().HaveCount(packageCount); + } + + + } +} diff --git a/clio/Package/ApplicationPackageListProvider.cs b/clio/Package/ApplicationPackageListProvider.cs index 2dd6d602..b4217917 100644 --- a/clio/Package/ApplicationPackageListProvider.cs +++ b/clio/Package/ApplicationPackageListProvider.cs @@ -31,6 +31,13 @@ public ApplicationPackageListProvider(IApplicationClient applicationClient, IJso _serviceUrlBuilder = serviceUrlBuilder; } + public ApplicationPackageListProvider() { + } + + public ApplicationPackageListProvider(IJsonConverter jsonConverter) { + _jsonConverter = jsonConverter; + } + #endregion #region Properties: Private @@ -43,11 +50,11 @@ public ApplicationPackageListProvider(IApplicationClient applicationClient, IJso private PackageInfo CreatePackageInfo(Dictionary package) { var descriptor = new PackageDescriptor { Name = package["Name"], - Maintainer = package["Maintainer"], UId = Guid.Parse(package["UId"]), - PackageVersion = package["Version"] + Maintainer = package.ContainsKey("Maintainer") ? package["Maintainer"] : string.Empty, + PackageVersion = package.ContainsKey("Version") ? package["Version"] : string.Empty }; - return new PackageInfo(descriptor,string.Empty, Enumerable.Empty()); + return new PackageInfo(descriptor, string.Empty, Enumerable.Empty()); } #endregion @@ -60,15 +67,18 @@ private PackageInfo CreatePackageInfo(Dictionary package) { public IEnumerable GetPackages(string scriptData) { try { string responseFormServer = _applicationClient.ExecutePostRequest(PackagesListServiceUrl, scriptData); - var json = _jsonConverter.CorrectJson(responseFormServer); - var packages = _jsonConverter.DeserializeObject>>(json); - return packages.Select(CreatePackageInfo); - + return ParsePackageInfoResponse(responseFormServer); } catch (Exception e) { return Array.Empty(); } } + internal IEnumerable ParsePackageInfoResponse(string responseData) { + var json = _jsonConverter.CorrectJson(responseData); + var packages = _jsonConverter.DeserializeObject>>(json); + return packages.Select(CreatePackageInfo); + } + #endregion }