Skip to content

Commit 1601ac9

Browse files
authored
fix: create unconfigured response message (#29)
1 parent 20518b5 commit 1601ac9

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

NetCore8583.Test/TestConfigParser.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,23 @@ public void TestSimpleCompositeTemplate()
252252
Assert.False(m.HasField(4));
253253
}
254254

255+
[Fact]
256+
public void TestCreatingResponseWithTypeNotInConfig()
257+
{
258+
var configXml = @"/Resources/issue36.xml";
259+
var mfact = Config(configXml);
260+
261+
var m = mfact.NewMessage(0x100);
262+
Assert.NotNull(m);
263+
Assert.True(m.HasField(2));
264+
Assert.True(m.HasField(3));
265+
266+
var r = mfact.CreateResponse(m);
267+
Assert.NotNull(r);
268+
Assert.True(r.HasField(2));
269+
Assert.True(r.HasField(3));
270+
}
271+
255272
[Fact]
256273
public void TestAllTypesHaveParseInfo()
257274
{

NetCore8583/MessageFactory.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,25 @@ protected T CreateIsoMessage(string isoHeader)
148148
return (T) new IsoMessage(isoHeader);
149149
}
150150

151+
/// <summary>
152+
/// Creates a new message of the specified type, with proper ISO header set.
153+
/// </summary>
154+
/// <param name="type">The message type, for example 0x200, 0x400, etc. used to define header if available</param>
155+
/// <returns></returns>
156+
protected T CreateIsoMessageWithType(int type)
157+
{
158+
T m;
159+
if (_binIsoHeaders.ContainsKey(type))
160+
m = CreateIsoMessageWithBinaryHeader(_binIsoHeaders[type]);
161+
else if (_isoHeaders.ContainsKey(type))
162+
m = CreateIsoMessage(_isoHeaders[type]);
163+
else
164+
m = CreateIsoMessage(string.Empty);
165+
166+
m.Type = type;
167+
return m;
168+
}
169+
151170
/// <summary>
152171
/// Creates a new message of the specified type, with optional trace and date values as well
153172
/// as any other values specified in a message template. If the factory is set to use binary
@@ -157,17 +176,7 @@ protected T CreateIsoMessage(string isoHeader)
157176
/// <returns></returns>
158177
public T NewMessage(int type)
159178
{
160-
var keyPresent = _binIsoHeaders.ContainsKey(type);
161-
sbyte[] val = null;
162-
var valStr = string.Empty;
163-
164-
if (keyPresent)
165-
val = _binIsoHeaders[type];
166-
else if (_isoHeaders.ContainsKey(type))
167-
valStr = _isoHeaders[type];
168-
169-
var m = keyPresent ? CreateIsoMessageWithBinaryHeader(val) : CreateIsoMessage(valStr);
170-
m.Type = type;
179+
T m = CreateIsoMessageWithType(type);
171180
m.Etx = Etx;
172181
m.Binary = UseBinaryMessages;
173182
m.EnforceSecondBitmap = EnforceSecondBitmap;
@@ -209,14 +218,13 @@ public T NewMessage(int type)
209218
/// <returns></returns>
210219
public T CreateResponse(T request, bool copyAllFields = true)
211220
{
212-
var resp = CreateIsoMessage(_isoHeaders[request.Type + 16]);
221+
var resp = CreateIsoMessageWithType(request.Type + 16);
213222
resp.Encoding = request.Encoding;
214223
resp.Binary = request.Binary;
215224
resp.BinBitmap = request.BinBitmap;
216-
resp.Type = request.Type + 16;
217225
resp.Etx = request.Etx;
218226
resp.EnforceSecondBitmap = request.EnforceSecondBitmap;
219-
IsoMessage templ = _typeTemplates[resp.Type];
227+
IsoMessage templ = _typeTemplates.ContainsKey(resp.Type) ? _typeTemplates[resp.Type] : null;
220228
if (templ == null)
221229
{
222230
for (var i = 2; i < 128; i++)

0 commit comments

Comments
 (0)