Skip to content

Commit

Permalink
Add come laboratory template tests for white label client
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Sep 9, 2023
1 parent 160df9f commit 71f0b01
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 13 deletions.
29 changes: 25 additions & 4 deletions src/CSharp/EasyMicroservices.Cores.AspCore.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
using EasyMicroservices.ServiceContracts;
using EasyMicroservices.Cores.Database.Managers;
using EasyMicroservices.Cores.Tests.Contracts.Common;
using EasyMicroservices.Cores.Tests.Laboratories;
using EasyMicroservices.ServiceContracts;
using Microsoft.AspNetCore.TestHost;
using Newtonsoft.Json;

namespace EasyMicroservices.Cores.AspCore.Tests
{
public class BasicTests
public class BasicTests : WhiteLabelLaboratoryTest
{
protected TestServer _testServer;
public BasicTests()
{
var webBuilder = new WebHostBuilder();
webBuilder.UseStartup<Startup>();

base.OnInitialize().Wait();
_testServer = new TestServer(webBuilder);
}

[Fact]
public async Task Get_EndpointsReturnSuccessAndCorrectContentType()
public async Task<string> Get_EndpointsReturnSuccessAndCorrectContentType()
{
var client = _testServer.CreateClient();
var data = await client.GetStringAsync($"api/user/getall");
var result = JsonConvert.DeserializeObject<MessageContract>(data);
Assert.True(result);
return data;
}

[Fact]
Expand All @@ -42,5 +46,22 @@ public async Task InternalErrorTest()
Assert.False(result);
Assert.Contains(result.Error.StackTrace, x => x.Contains("UserController.cs"));
}

[Fact]
public async Task AddUser()
{
var client = _testServer.CreateClient();
var data = await client.PostAsJsonAsync($"api/user/Add", new UpdateUserContract()
{
UserName = "Ali",
UniqueIdentity = "1-2"
});
var result = JsonConvert.DeserializeObject<MessageContract>(await data.Content.ReadAsStringAsync());
Assert.True(result);
var getAllRespone = await Get_EndpointsReturnSuccessAndCorrectContentType();
var users = JsonConvert.DeserializeObject<ListMessageContract<UpdateUserContract>>(getAllRespone);
Assert.True(users);
Assert.True(users.Result.All(x => DefaultUniqueIdentityManager.DecodeUniqueIdentity(x.UniqueIdentity).Length > 2));
}
}
}
10 changes: 1 addition & 9 deletions src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped((serviceProvider) => new UnitOfWork(serviceProvider).GetContractLogic<UserEntity, UserEntity, UserEntity, UserEntity>());
services.AddTransient(serviceProvider => new MyTestContext(serviceProvider.GetService<IEntityFrameworkCoreDatabaseBuilder>()));
services.AddScoped<IEntityFrameworkCoreDatabaseBuilder>(serviceProvider => new DatabaseBuilder());
//services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
// .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
// options =>
// {
// options.LoginPath = new PathString("/auth/login");
// options.AccessDeniedPath = new PathString("/auth/denied");
// });
//services.AddMvc().AddApplicationPart(typeof(UserController).Assembly).AddControllersAsServices();
}

public void Configure(IApplicationBuilder app)
Expand All @@ -41,7 +33,7 @@ public void Configure(IApplicationBuilder app)
{
endpoints.MapControllers();
});
app.Build<MyTestContext>();
app.Build<MyTestContext>().Wait();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Laboratory" Version="0.0.0.7" />
<PackageReference Include="EasyMicroservices.Mapper.CompileTimeMapper" Version="0.0.0.5" />
<PackageReference Include="EasyMicroservices.Mapper.SerializerMapper" Version="0.0.0.2" />
<PackageReference Include="EasyMicroservices.Serialization.Newtonsoft.Json" Version="0.0.0.3" />
Expand All @@ -24,6 +25,7 @@

<ItemGroup>
<ProjectReference Include="..\EasyMicroservices.Cores.AspCoreApi\EasyMicroservices.Cores.AspCoreApi.csproj" />
<ProjectReference Include="..\EasyMicroservices.Cores.AspEntityFrameworkCoreApi\EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj" />
<ProjectReference Include="..\EasyMicroservices.Cores.Contracts\EasyMicroservices.Cores.Contracts.csproj" />
<ProjectReference Include="..\EasyMicroservices.Cores.Database\EasyMicroservices.Cores.Database.csproj" />
<ProjectReference Include="..\EasyMicroservices.Cores.EntityFrameworkCore\EasyMicroservices.Cores.EntityFrameworkCore.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using EasyMicroservices.Laboratory.Engine;
using EasyMicroservices.Laboratory.Engine.Net.Http;
using System.Text;

namespace EasyMicroservices.Cores.Tests.Laboratories
{
public class WhiteLabelLaboratoryTest
{
const int Port = 6041;
string _routeAddress = "";
public static HttpClient HttpClient { get; set; } = new HttpClient();
public WhiteLabelLaboratoryTest()
{
_routeAddress = $"http://localhost:{Port}";
}

static bool _isInitialized = false;
static SemaphoreSlim Semaphore = new SemaphoreSlim(1);
protected async Task OnInitialize()
{
if (_isInitialized)
return;
try
{
await Semaphore.WaitAsync();
_isInitialized = true;

ResourceManager resourceManager = new ResourceManager();
HttpHandler httpHandler = new HttpHandler(resourceManager);
await httpHandler.Start(Port);
resourceManager.Append(@$"GET /api/WhiteLabel/GetAll HTTP/1.1
Host: localhost:{Port}
Accept: text/plain*RequestSkipBody*"
,
@"HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 0
{""isSuccess"":true,""result"":[{""id"":1,""name"":""ProjectName""},{""id"":2,""name"":""TenantName"",""parentId"":1}]}");


resourceManager.Append(@$"GET /api/Microservice/GetAll HTTP/1.1
Host: localhost:{Port}
Accept: text/plain*RequestSkipBody*"
,
@"HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 0
{""isSuccess"":true,""result"":[{""id"":1,""name"":""TextExample"",""description"":""Automatically added""}]}");


resourceManager.Append(@$"POST /api/Microservice/Add HTTP/1.1
Host: localhost:{Port}
Accept: text/plain*RequestSkipBody*"
,
@"HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 0
{""isSuccess"":true,""result"": 1}");

resourceManager.Append(@$"GET /api/MicroserviceContextTable/GetAll HTTP/1.1
Host: localhost:{Port}
Accept: text/plain*RequestSkipBody*"
,
@"HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 0
{""isSuccess"":true,""result"":[{""microserviceName"":""TextExample"",""microserviceId"":1,""contextName"":""MyTestContext"",""tableName"":""AddressEntity"",""contextTableId"":1},{""microserviceName"":""TextExample"",""microserviceId"":1,""contextName"":""MyTestContext"",""tableName"":""CompanyEntity"",""contextTableId"":2},{""microserviceName"":""TextExample"",""microserviceId"":1,""contextName"":""MyTestContext"",""tableName"":""ProfileEntity"",""contextTableId"":3},{""microserviceName"":""TextExample"",""microserviceId"":1,""contextName"":""MyTestContext"",""tableName"":""UserCompanyEntity"",""contextTableId"":4},{""microserviceName"":""TextExample"",""microserviceId"":1,""contextName"":""MyTestContext"",""tableName"":""UserEntity"",""contextTableId"":5}]}");

resourceManager.Append(@$"GET /api/ContextTable/GetAll HTTP/1.1
Host: localhost:{Port}
Accept: text/plain*RequestSkipBody*"
,
@"HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 0
{""isSuccess"":true,""result"":[{""id"":1,""tableName"":""AddressEntity"",""contextName"":""MyTestContext""},{""id"":2,""tableName"":""CompanyEntity"",""contextName"":""MyTestContext""},{""id"":3,""tableName"":""ProfileEntity"",""contextName"":""MyTestContext""},{""id"":4,""tableName"":""UserCompanyEntity"",""contextName"":""MyTestContext""},{""id"":5,""tableName"":""UserEntity"",""contextName"":""MyTestContext""}]}");

}
finally
{
Semaphore.Release();
}
}

[Fact]
public async Task WhiteLabelGetAllTest()
{
await OnInitialize();
var whiteLabelClient = new WhiteLables.GeneratedServices.WhiteLabelClient(_routeAddress, HttpClient);
var all = await whiteLabelClient.GetAllAsync();
Assert.True(all.IsSuccess);
Assert.True(all.Result.Count == 2);
Assert.True(all.Result.All(x => x.Name.HasValue()));
}

[Fact]
public async Task MicroserviceGetAllTest()
{
await OnInitialize();
var client = new WhiteLables.GeneratedServices.MicroserviceClient(_routeAddress, HttpClient);
var all = await client.GetAllAsync();
Assert.True(all.IsSuccess);
Assert.True(all.Result.Count == 1);
Assert.True(all.Result.All(x => x.Name == "TextExample"));
}

[Fact]
public async Task MicroserviceAddTest()
{
await OnInitialize();
var client = new WhiteLables.GeneratedServices.MicroserviceClient(_routeAddress, HttpClient);
var added = await client.AddAsync(new WhiteLables.GeneratedServices.MicroserviceContract()
{
Name = "TextExample",
Description = "Automatically added"
});
Assert.True(added.IsSuccess);
Assert.True(added.Result == 1);
}


[Fact]
public async Task MicroserviceContextTableGetAllTest()
{
await OnInitialize();
var client = new WhiteLables.GeneratedServices.MicroserviceContextTableClient(_routeAddress, HttpClient);
var all = await client.GetAllAsync();
Assert.True(all.IsSuccess);
Assert.True(all.Result.Count >= 5);
Assert.True(all.Result.All(x=>x.MicroserviceName == "TextExample"));
}

[Fact]
public async Task ContextTableGetAllTest()
{
await OnInitialize();
var client = new WhiteLables.GeneratedServices.ContextTableClient(_routeAddress, HttpClient);
var all = await client.GetAllAsync();
Assert.True(all.IsSuccess);
Assert.True(all.Result.Count >= 5);
}
}
}

0 comments on commit 71f0b01

Please sign in to comment.