using EdiApiGateway.Services;
using FluentAssertions;
using System.Text;
using Xunit;
namespace EdiApiGateway.Tests.Services;
///
/// AcmsApiWrapper のユニットテスト
/// 注意: ACMSCTL1.DLL が配置されていない環境では DllNotFoundException が発生します
/// このテストはAPI定義の検証と、DLL配置後の動作確認を目的としています
///
public class AcmsApiWrapperTests
{
public AcmsApiWrapperTests()
{
// Shift-JISエンコーディングプロバイダーの登録(.NET Core/8 で必要)
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
[Fact]
public void ACMSCallRecv_関数定義_存在確認()
{
// Arrange
var methodInfo = typeof(AcmsApiWrapper).GetMethod("ACMSCallRecv");
// Assert
methodInfo.Should().NotBeNull("ACMSCallRecv メソッドが定義されている必要があります");
methodInfo!.IsStatic.Should().BeTrue("P/Invoke メソッドは static である必要があります");
methodInfo.IsPublic.Should().BeTrue("テストから呼び出せるように public である必要があります");
}
[Fact]
public void ACMSCallSend_関数定義_存在確認()
{
// Arrange
var methodInfo = typeof(AcmsApiWrapper).GetMethod("ACMSCallSend");
// Assert
methodInfo.Should().NotBeNull("ACMSCallSend メソッドが定義されている必要があります");
methodInfo!.IsStatic.Should().BeTrue();
methodInfo.IsPublic.Should().BeTrue();
}
[Fact]
public void ACMSCallSendNoDisc_関数定義_存在確認()
{
// Arrange
var methodInfo = typeof(AcmsApiWrapper).GetMethod("ACMSCallSendNoDisc");
// Assert
methodInfo.Should().NotBeNull("ACMSCallSendNoDisc メソッドが定義されている必要があります");
methodInfo!.IsStatic.Should().BeTrue();
methodInfo.IsPublic.Should().BeTrue();
}
[Fact]
public void ACMSCallRecvNoDisc_関数定義_存在確認()
{
// Arrange
var methodInfo = typeof(AcmsApiWrapper).GetMethod("ACMSCallRecvNoDisc");
// Assert
methodInfo.Should().NotBeNull("ACMSCallRecvNoDisc メソッドが定義されている必要があります");
methodInfo!.IsStatic.Should().BeTrue();
methodInfo.IsPublic.Should().BeTrue();
}
[Fact]
public void ACMSCallStartup_関数定義_存在確認()
{
// Arrange
var methodInfo = typeof(AcmsApiWrapper).GetMethod("ACMSCallStartup");
// Assert
methodInfo.Should().NotBeNull("ACMSCallStartup メソッドが定義されている必要があります");
methodInfo!.IsStatic.Should().BeTrue();
methodInfo.IsPublic.Should().BeTrue();
}
[Fact]
public void ACMSCallAdd_関数定義_存在確認()
{
// Arrange
var methodInfo = typeof(AcmsApiWrapper).GetMethod("ACMSCallAdd");
// Assert
methodInfo.Should().NotBeNull("ACMSCallAdd メソッドが定義されている必要があります");
methodInfo!.IsStatic.Should().BeTrue();
methodInfo.IsPublic.Should().BeTrue();
}
[Fact]
public void ACMSCallExecute_関数定義_存在確認()
{
// Arrange
var methodInfo = typeof(AcmsApiWrapper).GetMethod("ACMSCallExecute");
// Assert
methodInfo.Should().NotBeNull("ACMSCallExecute メソッドが定義されている必要があります");
methodInfo!.IsStatic.Should().BeTrue();
methodInfo.IsPublic.Should().BeTrue();
}
[Fact]
public void ACMSInitCallInfo_関数定義_存在確認()
{
// Arrange
var methodInfo = typeof(AcmsApiWrapper).GetMethod("ACMSInitCallInfo");
// Assert
methodInfo.Should().NotBeNull("ACMSInitCallInfo メソッドが定義されている必要があります");
methodInfo!.IsStatic.Should().BeTrue();
methodInfo.IsPublic.Should().BeTrue();
}
[Fact]
public void ACMSCallRecv_パラメータ定義_検証()
{
// Arrange
var methodInfo = typeof(AcmsApiWrapper).GetMethod("ACMSCallRecv");
// Assert
methodInfo.Should().NotBeNull();
var parameters = methodInfo!.GetParameters();
parameters.Should().HaveCount(6, "ACMSCallRecv は6つのパラメータを持つ必要があります");
parameters[0].Name.Should().Be("uid");
parameters[1].Name.Should().Be("fid");
parameters[2].Name.Should().Be("fname");
parameters[3].Name.Should().Be("cycle");
parameters[4].Name.Should().Be("denbun");
parameters[5].Name.Should().Be("msg");
}
[Fact]
public void ACMSCallSend_パラメータ定義_検証()
{
// Arrange
var methodInfo = typeof(AcmsApiWrapper).GetMethod("ACMSCallSend");
// Assert
methodInfo.Should().NotBeNull();
var parameters = methodInfo!.GetParameters();
parameters.Should().HaveCount(5, "ACMSCallSend は5つのパラメータを持つ必要があります");
parameters[0].Name.Should().Be("uid");
parameters[1].Name.Should().Be("fid");
parameters[2].Name.Should().Be("fname");
parameters[3].Name.Should().Be("cycle");
parameters[4].Name.Should().Be("msg");
}
[Fact]
public void ACMSCallRecv_戻り値型_int検証()
{
// Arrange
var methodInfo = typeof(AcmsApiWrapper).GetMethod("ACMSCallRecv");
// Assert
methodInfo.Should().NotBeNull();
methodInfo!.ReturnType.Should().Be(typeof(int), "ACMS API は int 型のリターンコードを返す必要があります");
}
[Fact]
public void ACMSCallSend_戻り値型_int検証()
{
// Arrange
var methodInfo = typeof(AcmsApiWrapper).GetMethod("ACMSCallSend");
// Assert
methodInfo.Should().NotBeNull();
methodInfo!.ReturnType.Should().Be(typeof(int), "ACMS API は int 型のリターンコードを返す必要があります");
}
///
/// 統合テスト用のサンプルコード(実際のDLL配置環境で実行)
///
[Fact(Skip = "ACMSCTL1.DLL が配置されている環境でのみ実行可能")]
public void ACMSCallRecv_実DLL呼び出し_統合テスト()
{
// Arrange
string uid = "TRUSCO001";
string fid = "110000000000";
string fname = "test_recv.dat";
int cycle = 60;
int denbun = 0;
byte[] msgBuffer = AcmsApiWrapper.CreateMessageBuffer();
// Act
int result = AcmsApiWrapper.ACMSCallRecv(uid, fid, fname, cycle, denbun, msgBuffer);
// Assert
// 結果コードは環境依存(0=成功, 1-7=エラー)
result.Should().BeInRange(0, 7, "ACMS API のリターンコードは0-7の範囲である必要があります");
// メッセージデコード確認
string message = AcmsApiWrapper.DecodeMessage(msgBuffer);
message.Should().NotBeNull();
}
///
/// 統合テスト用のサンプルコード(実際のDLL配置環境で実行)
///
[Fact(Skip = "ACMSCTL1.DLL が配置されている環境でのみ実行可能")]
public void ACMSCallSend_実DLL呼び出し_統合テスト()
{
// Arrange
string uid = "TRUSCO001";
string fid = "110000000000";
string fname = "test_send.dat";
int cycle = 60;
byte[] msgBuffer = AcmsApiWrapper.CreateMessageBuffer();
// Act
int result = AcmsApiWrapper.ACMSCallSend(uid, fid, fname, cycle, msgBuffer);
// Assert
result.Should().BeInRange(0, 7, "ACMS API のリターンコードは0-7の範囲である必要があります");
// メッセージデコード確認
string message = AcmsApiWrapper.DecodeMessage(msgBuffer);
message.Should().NotBeNull();
}
///
/// バッチ処理用API定義の検証(実DLL配置環境でのみ実行)
///
[Fact(Skip = "ACMSCTL1.DLL が配置されている環境でのみ実行可能")]
public void ACMSCallStartup_実DLL呼び出し_統合テスト()
{
// Arrange
string uid = "TRUSCO001";
byte[] msgBuffer = AcmsApiWrapper.CreateMessageBuffer();
// Act
int result = AcmsApiWrapper.ACMSCallStartup(uid, msgBuffer);
// Assert
result.Should().BeInRange(0, 7);
// メッセージデコード確認
string message = AcmsApiWrapper.DecodeMessage(msgBuffer);
message.Should().NotBeNull();
}
///
/// CreateMessageBuffer メソッドのテスト
///
[Fact]
public void CreateMessageBuffer_正常作成_256バイトバッファ()
{
// Act
var buffer = AcmsApiWrapper.CreateMessageBuffer();
// Assert
buffer.Should().NotBeNull();
buffer.Should().BeOfType();
buffer.Length.Should().Be(256, "メッセージバッファは256バイトである必要があります");
}
///
/// DecodeMessage メソッドのテスト
///
[Fact]
public void DecodeMessage_Shift_JIS文字列_正常デコード()
{
// Arrange
var shiftJis = System.Text.Encoding.GetEncoding("Shift_JIS");
string originalMessage = "COM073: TCP-Z手順エラー";
byte[] buffer = new byte[256];
byte[] messageBytes = shiftJis.GetBytes(originalMessage);
Array.Copy(messageBytes, buffer, messageBytes.Length);
// Act
string decodedMessage = AcmsApiWrapper.DecodeMessage(buffer);
// Assert
decodedMessage.Should().Be(originalMessage);
}
///
/// DecodeMessage メソッドのテスト - null終端処理
///
[Fact]
public void DecodeMessage_null終端文字列_正常デコード()
{
// Arrange
var shiftJis = System.Text.Encoding.GetEncoding("Shift_JIS");
string originalMessage = "Success";
byte[] buffer = new byte[256];
byte[] messageBytes = shiftJis.GetBytes(originalMessage);
Array.Copy(messageBytes, buffer, messageBytes.Length);
// null終端が自動的に設定される(残りは0で初期化済み)
// Act
string decodedMessage = AcmsApiWrapper.DecodeMessage(buffer);
// Assert
decodedMessage.Should().Be(originalMessage);
decodedMessage.Length.Should().Be(originalMessage.Length, "null終端以降は含まれない");
}
}