Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pm): add pm client #32

Merged
merged 13 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Masa.Contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Isolation.UoW.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Isolation.UoW.EF.Web.Tests", "test\Masa.Contrib.Isolation.UoW.EF.Web.Tests\Masa.Contrib.Isolation.UoW.EF.Web.Tests.csproj", "{F4B8D71B-DAE4-4948-A1DC-42B42DE31D4F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.BasicAbility.Pm", "src\BasicAbility\Masa.Contrib.BasicAbility.Pm\Masa.Contrib.BasicAbility.Pm.csproj", "{118A91A6-6FBB-431A-B50D-076375AD6A63}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.BasicAbility.Pm", "src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Pm\Masa.BuildingBlocks.BasicAbility.Pm.csproj", "{AB6FAB84-5218-48A8-8F94-03B02ECD098F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.BasicAbility.Pm.Tests", "test\Masa.Contrib.BasicAbility.Pm.Tests\Masa.Contrib.BasicAbility.Pm.Tests.csproj", "{C4DDEA6E-D6AF-4890-9713-C2862403E57C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -604,6 +610,30 @@ Global
{F4B8D71B-DAE4-4948-A1DC-42B42DE31D4F}.Release|Any CPU.Build.0 = Release|Any CPU
{F4B8D71B-DAE4-4948-A1DC-42B42DE31D4F}.Release|x64.ActiveCfg = Release|Any CPU
{F4B8D71B-DAE4-4948-A1DC-42B42DE31D4F}.Release|x64.Build.0 = Release|Any CPU
{118A91A6-6FBB-431A-B50D-076375AD6A63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{118A91A6-6FBB-431A-B50D-076375AD6A63}.Debug|Any CPU.Build.0 = Debug|Any CPU
{118A91A6-6FBB-431A-B50D-076375AD6A63}.Debug|x64.ActiveCfg = Debug|Any CPU
{118A91A6-6FBB-431A-B50D-076375AD6A63}.Debug|x64.Build.0 = Debug|Any CPU
{118A91A6-6FBB-431A-B50D-076375AD6A63}.Release|Any CPU.ActiveCfg = Release|Any CPU
{118A91A6-6FBB-431A-B50D-076375AD6A63}.Release|Any CPU.Build.0 = Release|Any CPU
{118A91A6-6FBB-431A-B50D-076375AD6A63}.Release|x64.ActiveCfg = Release|Any CPU
{118A91A6-6FBB-431A-B50D-076375AD6A63}.Release|x64.Build.0 = Release|Any CPU
{AB6FAB84-5218-48A8-8F94-03B02ECD098F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB6FAB84-5218-48A8-8F94-03B02ECD098F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB6FAB84-5218-48A8-8F94-03B02ECD098F}.Debug|x64.ActiveCfg = Debug|Any CPU
{AB6FAB84-5218-48A8-8F94-03B02ECD098F}.Debug|x64.Build.0 = Debug|Any CPU
{AB6FAB84-5218-48A8-8F94-03B02ECD098F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB6FAB84-5218-48A8-8F94-03B02ECD098F}.Release|Any CPU.Build.0 = Release|Any CPU
{AB6FAB84-5218-48A8-8F94-03B02ECD098F}.Release|x64.ActiveCfg = Release|Any CPU
{AB6FAB84-5218-48A8-8F94-03B02ECD098F}.Release|x64.Build.0 = Release|Any CPU
{C4DDEA6E-D6AF-4890-9713-C2862403E57C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C4DDEA6E-D6AF-4890-9713-C2862403E57C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C4DDEA6E-D6AF-4890-9713-C2862403E57C}.Debug|x64.ActiveCfg = Debug|Any CPU
{C4DDEA6E-D6AF-4890-9713-C2862403E57C}.Debug|x64.Build.0 = Debug|Any CPU
{C4DDEA6E-D6AF-4890-9713-C2862403E57C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C4DDEA6E-D6AF-4890-9713-C2862403E57C}.Release|Any CPU.Build.0 = Release|Any CPU
{C4DDEA6E-D6AF-4890-9713-C2862403E57C}.Release|x64.ActiveCfg = Release|Any CPU
{C4DDEA6E-D6AF-4890-9713-C2862403E57C}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -681,6 +711,9 @@ Global
{F1826D84-1A16-41A4-A57B-F0DBB5A18745} = {38E6C400-90C0-493E-9266-C1602E229F1B}
{2E1EEC9C-37C9-4C03-A4BC-58306F8B1C38} = {38E6C400-90C0-493E-9266-C1602E229F1B}
{F4B8D71B-DAE4-4948-A1DC-42B42DE31D4F} = {38E6C400-90C0-493E-9266-C1602E229F1B}
{118A91A6-6FBB-431A-B50D-076375AD6A63} = {5DFAF4A2-ECB5-46E4-904D-1EA5F48B2D48}
{AB6FAB84-5218-48A8-8F94-03B02ECD098F} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
{C4DDEA6E-D6AF-4890-9713-C2862403E57C} = {38E6C400-90C0-493E-9266-C1602E229F1B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}
Expand Down
6 changes: 6 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Pm/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Masa.Contrib.BasicAbility.Pm;

public class Constants
{
internal const string DEFAULT_CLIENT_NAME = "masa.contrib.basicability.pm";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Utils.Caching.DistributedMemory" Version="0.4.0-preview.2" />
<PackageReference Include="Masa.Utils.Caching.Redis" Version="0.4.0-preview.2" />
<PackageReference Include="Masa.Utils.Caller.HttpClient" Version="0.4.0-preview.2" />
<PackageReference Include="Masa.Utils.Configuration.Json" Version="0.4.0-preview.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Pm\Masa.BuildingBlocks.BasicAbility.Pm.csproj" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Pm/PmClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Masa.Contrib.BasicAbility.Pm;

public class PmClient : IPmClient
{
public PmClient(ICallerProvider callerProvider)
{
EnvironmentService = new EnvironmentService(callerProvider);
ClusterService = new ClusterService(callerProvider);
ProjectService = new ProjectService(callerProvider);
AppService = new AppService(callerProvider);
}

public IProjectService ProjectService { get; init; }

public IEnvironmentService EnvironmentService { get; init; }

public IClusterService ClusterService { get; init; }

public IAppService AppService { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Masa.Contrib.BasicAbility.Pm.Service;

public class AppService : IAppService
{
private readonly ICallerProvider _callerProvider;

public AppService(ICallerProvider callerProvider)
{
_callerProvider = callerProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Masa.Contrib.BasicAbility.Pm.Service;

public class ClusterService : IClusterService
{
private readonly ICallerProvider _callerProvider;

public ClusterService(ICallerProvider callerProvider)
{
_callerProvider = callerProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Masa.Contrib.BasicAbility.Pm.Service;

public class EnvironmentService : IEnvironmentService
{
private readonly ICallerProvider _callerProvider;

public EnvironmentService(ICallerProvider callerProvider)
{
_callerProvider = callerProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Masa.Contrib.BasicAbility.Pm.Service;

public class ProjectService : IProjectService
{
private readonly ICallerProvider _callerProvider;

public ProjectService(ICallerProvider callerProvider)
{
_callerProvider = callerProvider;
}

public async Task<List<ProjectModel>> GetProjectListAsync(string envName)
{
var requestUri = $"api/v1/projectwithapps/{envName}";
var result = await _callerProvider.GetAsync<List<ProjectModel>>(requestUri);

return result ?? new List<ProjectModel>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Masa.Contrib.BasicAbility.Pm;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddPmClient(this IServiceCollection services, Action<CallerOptions> callerOptions)
{
if (services.Any(service => service.ServiceType.Equals(typeof(IPmClient))))
return services;

services.AddCaller(options =>
{
callerOptions.Invoke(options);
});

services.AddSingleton<IPmClient>(serviceProvider =>
{
var callProvider = serviceProvider.GetRequiredService<ICallerFactory>().CreateClient(DEFAULT_CLIENT_NAME);
var pmCaching = new PmClient(callProvider);
return pmCaching;
});

return services;
}
}
7 changes: 7 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Pm/_Imports.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
global using Masa.BuildingBlocks.BasicAbility.Pm;
global using Masa.BuildingBlocks.BasicAbility.Pm.Model;
global using Microsoft.Extensions.DependencyInjection;
global using Masa.Utils.Caller.Core;
global using static Masa.Contrib.BasicAbility.Pm.Constants;
global using Masa.Contrib.BasicAbility.Pm.Service;
global using Masa.BuildingBlocks.BasicAbility.Pm.Service;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\BasicAbility\Masa.Contrib.BasicAbility.Pm\Masa.Contrib.BasicAbility.Pm.csproj" />
</ItemGroup>

</Project>
133 changes: 133 additions & 0 deletions test/Masa.Contrib.BasicAbility.Pm.Tests/PmCachingTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using Masa.BuildingBlocks.BasicAbility.Pm;
using Masa.BuildingBlocks.BasicAbility.Pm.Model;
using Masa.Utils.Caching.Redis.Models;
using Masa.Utils.Caller.Core;
using Masa.Utils.Caller.HttpClient;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace Masa.Contrib.BasicAbility.Pm.Tests
{
[TestClass]
public class PmCachingTest
{
[TestMethod]
public void TestAddPmClient()
{
var services = new ServiceCollection();

services.AddPmClient(option =>
{
option.UseHttpClient(builder =>
{
builder.Name = "masa.contrib.basicability.pm";
builder.Configure = opt => opt.BaseAddress = new Uri("https://github.com");
});
});

var pmClient = services.BuildServiceProvider().GetRequiredService<IPmClient>();
Assert.IsNotNull(pmClient);
}

[TestMethod]
public void TestAddMultiplePmClient()
{
var services = new ServiceCollection();

services.AddPmClient(option =>
{
option.UseHttpClient(builder =>
{
builder.Name = "masa.contrib.basicability.pm";
builder.Configure = opt => opt.BaseAddress = new Uri("https://github.com");
});
});

services.AddPmClient(option =>
{
option.UseHttpClient(builder =>
{
builder.Name = "masa.contrib.basicability.pm";
builder.Configure = opt => opt.BaseAddress = new Uri("https://github.com");
});
});

var pmClient = services.BuildServiceProvider().GetRequiredService<IPmClient>();
Assert.IsNotNull(pmClient);
}


[TestMethod]
public async Task TestGetProjectAppsListAsync()
{
var list = new List<ProjectModel>()
{
new ProjectModel(1,"","",1,Guid.NewGuid())
};
string env = "development";
var requestUri = $"api/v1/projectwithapps/{env}";
var callerProvider = new Mock<ICallerProvider>();
callerProvider.Setup(provider => provider.GetAsync<List<ProjectModel>>(requestUri, default).Result).Returns(list).Verifiable();
var pmCaching = new PmClient(callerProvider.Object);

var data = await pmCaching.ProjectService.GetProjectListAsync(env);
callerProvider.Verify(provider => provider.GetAsync<List<ProjectModel>>(requestUri, default), Times.Once);
Assert.IsTrue(data.Count == 1);
}

[TestMethod]
public async Task TestGetProjectAppsList2Async()
{
List<ProjectModel> list = null!;
string env = "development";
var requestUri = $"api/v1/projectwithapps/{env}";
var callerProvider = new Mock<ICallerProvider>();
callerProvider.Setup(provider =>(provider.GetAsync<List<ProjectModel>>(It.IsAny<string>(), default))).ReturnsAsync(list).Verifiable();
var pmCaching = new PmClient(callerProvider.Object);

var data = await pmCaching.ProjectService.GetProjectListAsync(env);
callerProvider.Verify(provider => provider.GetAsync<List<ProjectModel>>(requestUri, default), Times.Once);

Assert.IsTrue(data.Count == 0);
}

[TestMethod]
public void TestGetEnvironmentService()
{
var callerProvider = new Mock<ICallerProvider>();
var pmCaching = new PmClient(callerProvider.Object);

var env = pmCaching.EnvironmentService;

Assert.IsNotNull(env);
}

[TestMethod]
public void TestGetClusterService()
{
var callerProvider = new Mock<ICallerProvider>();
var pmCaching = new PmClient(callerProvider.Object);

var env = pmCaching.ClusterService;

Assert.IsNotNull(env);
}

[TestMethod]
public void TestGetAppService()
{
var callerProvider = new Mock<ICallerProvider>();
var pmCaching = new PmClient(callerProvider.Object);

var env = pmCaching.AppService;

Assert.IsNotNull(env);
}
}
}