Skip to content

Commit

Permalink
[GeothermalResearchInstitute] Add DeviceServiceTests.
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoona committed Sep 16, 2019
1 parent 6196224 commit 9195b07
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,6 @@ public static void Init(TestContext testContext)
serviceProvider.GetRequiredService<ILogger<Server>>());
});
builder.AddTransient<AuthenticationServiceImpl>();
builder.AddSingleton(serviceProvider =>
{
GrpcEnvironment.SetLogger(serviceProvider.GetRequiredService<GrpcLoggerAdapater.GrpcLoggerAdapter>());
return new Server
{
Services =
{
AuthenticationService.BindService(serviceProvider.GetRequiredService<AuthenticationServiceImpl>()),
},
Ports =
{
new ServerPort(
"0.0.0.0",
config.GetValue<int>("core.port"),
ServerCredentials.Insecure),
},
};
});

builder.AddHostedService<GrpcHostedService>();
})
.Build();
}
Expand All @@ -115,7 +95,7 @@ public async Task LoginUser0Success()
DateTime.UtcNow.AddHours(1),
new Metadata(),
CancellationToken.None,
"127.0.0.1",
null,
null,
null,
(metadata) => Task.CompletedTask,
Expand Down Expand Up @@ -148,7 +128,7 @@ public async Task LoginUser1Success()
DateTime.UtcNow.AddHours(1),
new Metadata(),
CancellationToken.None,
"127.0.0.1",
null,
null,
null,
(metadata) => Task.CompletedTask,
Expand Down Expand Up @@ -181,7 +161,7 @@ public async Task LoginUser1Failed()
DateTime.UtcNow.AddHours(1),
new Metadata(),
CancellationToken.None,
"127.0.0.1",
null,
null,
null,
(metadata) => Task.CompletedTask,
Expand All @@ -207,7 +187,7 @@ public async Task LoginFailed()
DateTime.UtcNow.AddHours(1),
new Metadata(),
CancellationToken.None,
"127.0.0.1",
null,
null,
null,
(metadata) => Task.CompletedTask,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// <copyright file="DeviceServiceTests.cs" company="Shuai Zhang">
// Copyright Shuai Zhang. All rights reserved.
// Licensed under the GPLv3 license. See LICENSE file in the project root for full license information.
// </copyright>

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using GeothermalResearchInstitute.ServerConsole.GrpcService;
using GeothermalResearchInstitute.ServerConsole.Model;
using GeothermalResearchInstitute.v1;
using Google.Protobuf;
using Grpc.Core;
using Grpc.Core.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GeothermalResearchInstitute.ServerConsole.UnitTest
{
[TestClass]
public class DeviceServiceTests
{
private IHost Host { get; set; }

[TestInitialize]
public void Init()
{
this.Host = new HostBuilder()
.ConfigureHostConfiguration(builder =>
{
builder.AddInMemoryCollection(new Dictionary<string, string>
{
{ "Environment", "UnitTest" },
});
})
.ConfigureAppConfiguration(builder =>
{
builder.AddInMemoryCollection(new Dictionary<string, string>
{
{ "devices:0:id", "10:BF:48:79:B2:A4" },
{ "devices:0:name", "测试设备0" },
{ "devices:1:id", "BC:96:80:E6:70:16" },
{ "devices:1:name", "测试设备1" },
});
})
.ConfigureLogging((context, builder) =>
{
builder.AddProvider(new MsTestLoggerProvider());
})
.ConfigureServices((context, builder) =>
{
IConfiguration config = context.Configuration;

builder.AddDbContext<BjdireContext>(options => options.UseInMemoryDatabase("bjdire"));

// Configuration options.
builder.Configure<DeviceOptions>(context.Configuration);

// Grpc services.
builder.AddSingleton(serviceProvider =>
{
return new GrpcLoggerAdapater.GrpcLoggerAdapter(
serviceProvider.GetRequiredService<ILoggerFactory>(),
serviceProvider.GetRequiredService<ILogger<Server>>());
});
builder.AddSingleton<DeviceServiceImpl>();
})
.Build();
}

[TestCleanup]
public void Cleanup()
{
this.Host.Dispose();
this.Host = null;
}

[TestMethod]
public async Task ListDevicesTest()
{
var service = this.Host.Services.GetRequiredService<DeviceServiceImpl>();
var fakeServerCallContext = TestServerCallContext.Create(
nameof(service.ListDevices),
null,
DateTime.UtcNow.AddHours(1),
new Metadata(),
CancellationToken.None,
null,
null,
null,
(metadata) => Task.CompletedTask,
() => new WriteOptions(),
(writeOptions) => { });
var response = await service.ListDevices(new ListDevicesRequest(), fakeServerCallContext).ConfigureAwait(false);

CollectionAssert.AreEqual(
new List<Device>
{
new Device
{
Id = ByteString.CopyFrom(new byte[] { 0x10, 0xBF, 0x48, 0x79, 0xB2, 0xA4 }),
Name = "测试设备0",
},
new Device
{
Id = ByteString.CopyFrom(new byte[] { 0xBC, 0x96, 0x80, 0xE6, 0x70, 0x16 }),
Name = "测试设备1",
},
},
response.Devices);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,15 @@ public override Task<Device> GetDevice(GetDeviceRequest request, ServerCallConte
break;
case DeviceView.MetricsAndControl:
// TODO(zhangshuai.ustc): Loading it.
device.Metrics = new DeviceMetrics
if (this.metricsMap.TryGetValue(request.Id, out var metrics))
{
};
device.Metrics = new DeviceMetrics(metrics);
}
else
{
device.Metrics = new DeviceMetrics();
}

device.Controls = new DeviceControls
{
DevicePower = deviceAdditionalInformation.DevicePower,
Expand Down

0 comments on commit 9195b07

Please sign in to comment.