Skip to content

Commit

Permalink
fix: create unconfigured response message (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
TadijaB authored Dec 16, 2024
1 parent 20518b5 commit 1601ac9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
17 changes: 17 additions & 0 deletions NetCore8583.Test/TestConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,23 @@ public void TestSimpleCompositeTemplate()
Assert.False(m.HasField(4));
}

[Fact]
public void TestCreatingResponseWithTypeNotInConfig()
{
var configXml = @"/Resources/issue36.xml";
var mfact = Config(configXml);

var m = mfact.NewMessage(0x100);
Assert.NotNull(m);
Assert.True(m.HasField(2));
Assert.True(m.HasField(3));

var r = mfact.CreateResponse(m);
Assert.NotNull(r);
Assert.True(r.HasField(2));
Assert.True(r.HasField(3));
}

[Fact]
public void TestAllTypesHaveParseInfo()
{
Expand Down
36 changes: 22 additions & 14 deletions NetCore8583/MessageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,25 @@ protected T CreateIsoMessage(string isoHeader)
return (T) new IsoMessage(isoHeader);
}

/// <summary>
/// Creates a new message of the specified type, with proper ISO header set.
/// </summary>
/// <param name="type">The message type, for example 0x200, 0x400, etc. used to define header if available</param>
/// <returns></returns>
protected T CreateIsoMessageWithType(int type)
{
T m;
if (_binIsoHeaders.ContainsKey(type))
m = CreateIsoMessageWithBinaryHeader(_binIsoHeaders[type]);
else if (_isoHeaders.ContainsKey(type))
m = CreateIsoMessage(_isoHeaders[type]);
else
m = CreateIsoMessage(string.Empty);

m.Type = type;
return m;
}

/// <summary>
/// Creates a new message of the specified type, with optional trace and date values as well
/// as any other values specified in a message template. If the factory is set to use binary
Expand All @@ -157,17 +176,7 @@ protected T CreateIsoMessage(string isoHeader)
/// <returns></returns>
public T NewMessage(int type)
{
var keyPresent = _binIsoHeaders.ContainsKey(type);
sbyte[] val = null;
var valStr = string.Empty;

if (keyPresent)
val = _binIsoHeaders[type];
else if (_isoHeaders.ContainsKey(type))
valStr = _isoHeaders[type];

var m = keyPresent ? CreateIsoMessageWithBinaryHeader(val) : CreateIsoMessage(valStr);
m.Type = type;
T m = CreateIsoMessageWithType(type);
m.Etx = Etx;
m.Binary = UseBinaryMessages;
m.EnforceSecondBitmap = EnforceSecondBitmap;
Expand Down Expand Up @@ -209,14 +218,13 @@ public T NewMessage(int type)
/// <returns></returns>
public T CreateResponse(T request, bool copyAllFields = true)
{
var resp = CreateIsoMessage(_isoHeaders[request.Type + 16]);
var resp = CreateIsoMessageWithType(request.Type + 16);
resp.Encoding = request.Encoding;
resp.Binary = request.Binary;
resp.BinBitmap = request.BinBitmap;
resp.Type = request.Type + 16;
resp.Etx = request.Etx;
resp.EnforceSecondBitmap = request.EnforceSecondBitmap;
IsoMessage templ = _typeTemplates[resp.Type];
IsoMessage templ = _typeTemplates.ContainsKey(resp.Type) ? _typeTemplates[resp.Type] : null;
if (templ == null)
{
for (var i = 2; i < 128; i++)
Expand Down

0 comments on commit 1601ac9

Please sign in to comment.