Skip to content

Commit 18eb880

Browse files
committed
test: 增加单元测试
1 parent 38fc7e9 commit 18eb880

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

test/UnitTestOpcUa/UnitTest1.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using Microsoft.Extensions.Configuration;
6+
using Opc.Ua;
7+
using Opc.Ua.Client;
8+
using Opc.Ua.Configuration;
9+
10+
namespace UnitTestOpcUa;
11+
12+
public class UnitTest1
13+
{
14+
[Fact]
15+
public async Task Connect_Ok()
16+
{
17+
var config = new ApplicationConfiguration()
18+
{
19+
ApplicationName = "KEPServerEX Client",
20+
ApplicationType = ApplicationType.Client,
21+
ApplicationUri = "urn:" + Utils.GetHostName() + ":KEPServerEXClient",
22+
SecurityConfiguration = new SecurityConfiguration
23+
{
24+
ApplicationCertificate = new CertificateIdentifier { StoreType = "X509Store", StorePath = "CurrentUser\\My" },
25+
TrustedPeerCertificates = new CertificateTrustList { StoreType = "Directory", StorePath = "OPC Foundation/CertificateStores/UA Applications" },
26+
RejectedCertificateStore = new CertificateTrustList { StoreType = "Directory", StorePath = "OPC Foundation/CertificateStores/RejectedCertificates" },
27+
AutoAcceptUntrustedCertificates = true // 仅用于测试环境
28+
},
29+
TransportConfigurations = new TransportConfigurationCollection(),
30+
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
31+
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
32+
};
33+
34+
//await config.Validate(ApplicationType.Client);
35+
36+
// 创建端点描述
37+
var endpointDescription = CoreClientUtils.SelectEndpoint("opc.tcp://127.0.0.1:49320", false);
38+
var endpointConfiguration = EndpointConfiguration.Create(config);
39+
var endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);
40+
41+
// 创建会话
42+
var identity = new UserIdentity(); // 匿名登录,或提供用户名密码
43+
var session = await Session.Create(
44+
config,
45+
endpoint,
46+
false,
47+
false,
48+
config.ApplicationName,
49+
60000,
50+
identity,
51+
null);
52+
}
53+
54+
[Fact]
55+
public async Task FindServersAsync()
56+
{
57+
// 配置与连接过程同前述基本客户端
58+
var application = new ApplicationInstance
59+
{
60+
ApplicationName = "OPC UA Basic Client",
61+
ApplicationType = ApplicationType.Client
62+
};
63+
var applicationConfiguration = new ApplicationConfiguration
64+
{
65+
ApplicationName = application.ApplicationName,
66+
ApplicationType = application.ApplicationType,
67+
ClientConfiguration = new ClientConfiguration()
68+
};
69+
var endpointURL = "opc.tcp://127.0.0.1:49320";
70+
var endpointDescription = CoreClientUtils.SelectEndpoint(applicationConfiguration, endpointURL, false);
71+
var endpointConfiguration = EndpointConfiguration.Create();
72+
var endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);
73+
74+
var session = await Session.Create(
75+
configuration: applicationConfiguration,
76+
endpoint: endpoint,
77+
updateBeforeConnect: false,
78+
sessionName: "Opc.Session.BootstrapBlazor",
79+
sessionTimeout: 60000,
80+
identity: null,
81+
preferredLocales: null);
82+
}
83+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="coverlet.collector" Version="6.0.2" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
13+
<PackageReference Include="xunit" Version="2.9.2" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\src\components\BootstrapBlazor.OpcUa\BootstrapBlazor.OpcUa.csproj" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<Using Include="Xunit" />
23+
</ItemGroup>
24+
25+
</Project>

0 commit comments

Comments
 (0)