Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-nikonov committed Sep 5, 2024
1 parent 344292e commit 5a3ff8d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
44 changes: 44 additions & 0 deletions clio.tests/ApplicationPackageListProviderTest.cs
Original file line number Diff line number Diff line change
@@ -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<IJsonConverter>();
var provider = new ApplicationPackageListProvider(jsonConverter);
Action act = () => provider.ParsePackageInfoResponse(responseData);
act.Should().Throw<Exception>();
}

[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<IJsonConverter>();
var provider = new ApplicationPackageListProvider(jsonConverter);
var result = provider.ParsePackageInfoResponse(responseData);
result.Should().HaveCount(packageCount);
}


}
}
24 changes: 17 additions & 7 deletions clio/Package/ApplicationPackageListProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public ApplicationPackageListProvider(IApplicationClient applicationClient, IJso
_serviceUrlBuilder = serviceUrlBuilder;
}

public ApplicationPackageListProvider() {
}

public ApplicationPackageListProvider(IJsonConverter jsonConverter) {
_jsonConverter = jsonConverter;
}

#endregion

#region Properties: Private
Expand All @@ -43,11 +50,11 @@ public ApplicationPackageListProvider(IApplicationClient applicationClient, IJso
private PackageInfo CreatePackageInfo(Dictionary<string, string> 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<string>());
return new PackageInfo(descriptor, string.Empty, Enumerable.Empty<string>());
}

#endregion
Expand All @@ -60,15 +67,18 @@ private PackageInfo CreatePackageInfo(Dictionary<string, string> package) {
public IEnumerable<PackageInfo> GetPackages(string scriptData) {
try {
string responseFormServer = _applicationClient.ExecutePostRequest(PackagesListServiceUrl, scriptData);
var json = _jsonConverter.CorrectJson(responseFormServer);
var packages = _jsonConverter.DeserializeObject<List<Dictionary<string, string>>>(json);
return packages.Select(CreatePackageInfo);

return ParsePackageInfoResponse(responseFormServer);
} catch (Exception e) {
return Array.Empty<PackageInfo>();
}
}

internal IEnumerable<PackageInfo> ParsePackageInfoResponse(string responseData) {
var json = _jsonConverter.CorrectJson(responseData);
var packages = _jsonConverter.DeserializeObject<List<Dictionary<string, string>>>(json);
return packages.Select(CreatePackageInfo);
}

#endregion

}
Expand Down

0 comments on commit 5a3ff8d

Please sign in to comment.