Skip to content

Commit f15468e

Browse files
committed
Copied MessageUtilsTest from core to base.
1 parent e499f2a commit f15468e

File tree

1 file changed

+272
-0
lines changed

1 file changed

+272
-0
lines changed
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
/*******************************************************************************
2+
* Copyright (c) quickfixengine.org All rights reserved.
3+
*
4+
* This file is part of the QuickFIX FIX Engine
5+
*
6+
* This file may be distributed under the terms of the quickfixengine.org
7+
* license as defined by quickfixengine.org and appearing in the file
8+
* LICENSE included in the packaging of this file.
9+
*
10+
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
11+
* THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
12+
* PARTICULAR PURPOSE.
13+
*
14+
* See http://www.quickfixengine.org/LICENSE for licensing information.
15+
*
16+
* Contact ask@quickfixengine.org if any conditions of this licensing
17+
* are not clear to you.
18+
******************************************************************************/
19+
20+
package quickfix;
21+
22+
import org.junit.Test;
23+
24+
import quickfix.field.ApplVerID;
25+
import quickfix.field.BeginString;
26+
import quickfix.field.DefaultApplVerID;
27+
import quickfix.field.EmailThreadID;
28+
import quickfix.field.EmailType;
29+
import quickfix.field.EncryptMethod;
30+
import quickfix.field.HeartBtInt;
31+
import quickfix.field.MsgType;
32+
import quickfix.field.SenderCompID;
33+
import quickfix.field.Subject;
34+
import quickfix.field.TargetCompID;
35+
import quickfix.fix40.Logon;
36+
import quickfix.fix50.Email;
37+
38+
import static org.hamcrest.Matchers.instanceOf;
39+
import static org.hamcrest.Matchers.is;
40+
import static org.hamcrest.Matchers.notNullValue;
41+
import static org.hamcrest.MatcherAssert.assertThat;
42+
import static org.junit.Assert.assertEquals;
43+
import static org.junit.Assert.assertNull;
44+
import static org.junit.Assert.fail;
45+
import org.junit.Test;
46+
import static org.mockito.ArgumentMatchers.any;
47+
import static org.mockito.Mockito.mock;
48+
import static org.mockito.Mockito.when;
49+
50+
public class MessageUtilsTest {
51+
52+
@Test
53+
public void testGetStringField() throws Exception {
54+
String messageString = "8=FIX.4.2\0019=12\00135=X\001108=30\00110=049\001";
55+
assertEquals("wrong value", "FIX.4.2", MessageUtils.getStringField(messageString,
56+
BeginString.FIELD));
57+
assertEquals("wrong value", "X", MessageUtils.getStringField(messageString, MsgType.FIELD));
58+
assertNull(messageString, MessageUtils.getStringField(messageString, SenderCompID.FIELD));
59+
}
60+
61+
@Test
62+
public void testSessionIdFromMessage() throws Exception {
63+
Message message = new Logon();
64+
message.getHeader().setString(SenderCompID.FIELD, "TW");
65+
message.getHeader().setString(TargetCompID.FIELD, "ISLD");
66+
SessionID sessionID = MessageUtils.getSessionID(message);
67+
assertEquals(sessionID.getBeginString(), "FIX.4.0");
68+
assertEquals("TW", sessionID.getSenderCompID());
69+
assertEquals("ISLD", sessionID.getTargetCompID());
70+
}
71+
72+
@Test
73+
public void testReverseSessionIdFromMessage() throws Exception {
74+
Message message = new Logon();
75+
message.getHeader().setString(SenderCompID.FIELD, "TW");
76+
message.getHeader().setString(TargetCompID.FIELD, "ISLD");
77+
SessionID sessionID = MessageUtils.getReverseSessionID(message);
78+
assertEquals(sessionID.getBeginString(), "FIX.4.0");
79+
assertEquals("ISLD", sessionID.getSenderCompID());
80+
assertEquals("TW", sessionID.getTargetCompID());
81+
}
82+
83+
@Test
84+
public void testReverseSessionIdFromMessageWithMissingFields() throws Exception {
85+
Message message = new Logon();
86+
SessionID sessionID = MessageUtils.getReverseSessionID(message);
87+
assertEquals(sessionID.getBeginString(), "FIX.4.0");
88+
assertEquals(sessionID.getSenderCompID(), SessionID.NOT_SET);
89+
assertEquals(sessionID.getTargetCompID(), SessionID.NOT_SET);
90+
}
91+
92+
@Test
93+
public void testSessionIdFromRawMessage() throws Exception {
94+
String messageString = "8=FIX.4.0\0019=56\00135=A\00134=1\00149=TW\001" +
95+
"52=20060118-16:34:19\00156=ISLD\00198=0\001108=2\00110=223\001";
96+
SessionID sessionID = MessageUtils.getSessionID(messageString);
97+
assertEquals(sessionID.getBeginString(), "FIX.4.0");
98+
assertEquals("TW", sessionID.getSenderCompID());
99+
assertEquals("ISLD", sessionID.getTargetCompID());
100+
}
101+
102+
@Test
103+
public void testReverseSessionIdFromRawMessage() throws Exception {
104+
String messageString = "8=FIX.4.0\0019=56\00135=A\00134=1\00149=TW\00150=TWS\001" +
105+
"142=TWL\00152=20060118-16:34:19\00156=ISLD\00198=0\001108=2\00110=223\001";
106+
SessionID sessionID = MessageUtils.getReverseSessionID(messageString);
107+
assertEquals(sessionID.getBeginString(), "FIX.4.0");
108+
assertEquals("ISLD", sessionID.getSenderCompID());
109+
assertEquals("TW", sessionID.getTargetCompID());
110+
assertEquals("TWS", sessionID.getTargetSubID());
111+
assertEquals("TWL", sessionID.getTargetLocationID());
112+
}
113+
114+
@Test
115+
public void testMessageType() throws Exception {
116+
String messageString = "8=FIX.4.0\0019=56\00135=A\00134=1\00149=TW\001" +
117+
"52=20060118-16:34:19\00156=ISLD\00198=0\001108=2\00110=223\001";
118+
assertEquals("A", MessageUtils.getMessageType(messageString));
119+
}
120+
121+
@Test
122+
public void testMessageTypeError() throws Exception {
123+
String messageString = "8=FIX.4.0\0019=56\00134=1\00149=TW\001" +
124+
"52=20060118-16:34:19\00156=ISLD\00198=0\001108=2\00110=223\001";
125+
try {
126+
MessageUtils.getMessageType(messageString);
127+
fail("expected exception");
128+
} catch (InvalidMessage e) {
129+
// expected
130+
}
131+
}
132+
133+
@Test
134+
public void testMessageTypeError2() throws Exception {
135+
String messageString = "8=FIX.4.0\0019=56\00135=1";
136+
try {
137+
MessageUtils.getMessageType(messageString);
138+
fail("expected exception");
139+
} catch (InvalidMessage e) {
140+
// expected
141+
}
142+
}
143+
144+
@Test
145+
public void testGetNonexistentStringField() throws Exception {
146+
String messageString = "8=FIX.4.0\0019=56\00134=1\00149=TW\001" +
147+
"52=20060118-16:34:19\00156=ISLD\00198=0\001108=2\00110=223\001";
148+
assertNull(MessageUtils.getStringField(messageString, 35));
149+
}
150+
151+
@Test
152+
public void testGetStringFieldWithBadValue() throws Exception {
153+
String messageString = "8=FIX.4.0\0019=56\00134=1\00149=TW\001" +
154+
"52=20060118-16:34:19\00156=ISLD\00198=0\001108=2\00110=223";
155+
assertNull(MessageUtils.getStringField(messageString, 10));
156+
}
157+
158+
@Test
159+
public void testParse() throws Exception {
160+
Session mockSession = mock(Session.class);
161+
DataDictionaryProvider mockDataDictionaryProvider = mock(DataDictionaryProvider.class);
162+
when(mockSession.getDataDictionaryProvider()).thenReturn(mockDataDictionaryProvider);
163+
when(mockSession.getMessageFactory()).thenReturn(new quickfix.fix40.MessageFactory());
164+
String messageString = "8=FIX.4.0\0019=56\00135=A\00134=1\00149=TW\001" +
165+
"52=20060118-16:34:19\00156=ISLD\00198=0\001108=2\00110=223\001";
166+
167+
Message message = MessageSessionUtils.parse(mockSession, messageString);
168+
169+
assertThat(message, notNullValue());
170+
}
171+
172+
@Test
173+
public void testLegacyParse() throws Exception {
174+
String data = "8=FIX.4.4\0019=309\00135=8\00149=ASX\00156=CL1_FIX44\00134=4\001" +
175+
"52=20060324-01:05:58\00117=X-B-WOW-1494E9A0:58BD3F9D-1109\001150=D\001" +
176+
"39=0\00111=184271\00138=200\001198=1494E9A0:58BD3F9D\001526=4324\001" +
177+
"37=B-WOW-1494E9A0:58BD3F9D\00155=WOW\00154=1\001151=200\00114=0\00140=2\001" +
178+
"44=15\00159=1\0016=0\001453=3\001448=AAA35791\001447=D\001452=3\001448=8\001" +
179+
"447=D\001452=4\001448=FIX11\001447=D\001452=36\00160=20060320-03:34:29\00110=169\001";
180+
181+
Message message = MessageUtils.parse(new quickfix.fix40.MessageFactory(), DataDictionaryTest.getDictionary(), data);
182+
assertThat(message, notNullValue());
183+
}
184+
185+
@Test
186+
public void testParseFixt() throws Exception {
187+
Session mockSession = mock(Session.class);
188+
DataDictionaryProvider mockDataDictionaryProvider = mock(DataDictionaryProvider.class);
189+
when(mockSession.getDataDictionaryProvider()).thenReturn(mockDataDictionaryProvider);
190+
when(mockSession.getMessageFactory()).thenReturn(new quickfix.fix40.MessageFactory());
191+
192+
Email email = new Email(new EmailThreadID("THREAD_ID"), new EmailType(EmailType.NEW), new Subject("SUBJECT"));
193+
email.getHeader().setField(new ApplVerID(ApplVerID.FIX42));
194+
email.getHeader().setField(new SenderCompID("SENDER"));
195+
email.getHeader().setField(new TargetCompID("TARGET"));
196+
197+
Message message = MessageSessionUtils.parse(mockSession, email.toString());
198+
199+
assertThat(message, is(notNullValue()));
200+
assertThat(message, instanceOf(quickfix.fix40.Email.class));
201+
}
202+
203+
@Test
204+
public void testParseFixtLogon() throws Exception {
205+
Session mockSession = mock(Session.class);
206+
DataDictionaryProvider mockDataDictionaryProvider = mock(DataDictionaryProvider.class);
207+
when(mockSession.getDataDictionaryProvider()).thenReturn(mockDataDictionaryProvider);
208+
when(mockSession.getMessageFactory()).thenReturn(new DefaultMessageFactory());
209+
210+
quickfix.fixt11.Logon logon = new quickfix.fixt11.Logon(new EncryptMethod(EncryptMethod.NONE_OTHER), new HeartBtInt(30),
211+
new DefaultApplVerID(ApplVerID.FIX42));
212+
213+
Message message = MessageSessionUtils.parse(mockSession, logon.toString());
214+
215+
assertThat(message, is(notNullValue()));
216+
assertThat(message, instanceOf(quickfix.fixt11.Logon.class));
217+
}
218+
219+
@Test
220+
public void testParseFixtLogout() throws Exception {
221+
Session mockSession = mock(Session.class);
222+
DataDictionaryProvider mockDataDictionaryProvider = mock(DataDictionaryProvider.class);
223+
when(mockSession.getDataDictionaryProvider()).thenReturn(mockDataDictionaryProvider);
224+
when(mockSession.getMessageFactory()).thenReturn(new DefaultMessageFactory());
225+
226+
quickfix.fixt11.Logout logout = new quickfix.fixt11.Logout();
227+
228+
Message message = MessageSessionUtils.parse(mockSession, logout.toString());
229+
230+
assertThat(message, is(notNullValue()));
231+
assertThat(message, instanceOf(quickfix.fixt11.Logout.class));
232+
}
233+
234+
@Test
235+
public void testParseFix50() throws Exception {
236+
Session mockSession = mock(Session.class);
237+
DataDictionaryProvider mockDataDictionaryProvider = mock(DataDictionaryProvider.class);
238+
when(mockSession.getDataDictionaryProvider()).thenReturn(mockDataDictionaryProvider);
239+
when(mockSession.getMessageFactory()).thenReturn(new DefaultMessageFactory());
240+
241+
Email email = new Email(new EmailThreadID("THREAD_ID"), new EmailType(EmailType.NEW), new Subject("SUBJECT"));
242+
email.getHeader().setField(new ApplVerID(ApplVerID.FIX50));
243+
email.getHeader().setField(new SenderCompID("SENDER"));
244+
email.getHeader().setField(new TargetCompID("TARGET"));
245+
246+
Message message = MessageSessionUtils.parse(mockSession, email.toString());
247+
248+
assertThat(message, is(notNullValue()));
249+
assertThat(message, instanceOf(quickfix.fix50.Email.class));
250+
}
251+
252+
// QFJ-973
253+
@Test
254+
public void testParseMessageWithoutChecksumValidation() throws InvalidMessage {
255+
Session mockSession = mock(Session.class);
256+
when(mockSession.isValidateChecksum()).thenReturn(Boolean.FALSE);
257+
258+
DataDictionary dataDictionary = mock(DataDictionary.class);
259+
DataDictionaryProvider mockDataDictionaryProvider = mock(DataDictionaryProvider.class);
260+
when(mockDataDictionaryProvider.getSessionDataDictionary(any(String.class))).thenReturn(dataDictionary);
261+
when(mockSession.getDataDictionaryProvider()).thenReturn(mockDataDictionaryProvider);
262+
when(mockSession.getMessageFactory()).thenReturn(new quickfix.fix40.MessageFactory());
263+
264+
String messageString = "8=FIX.4.0\0019=56\00135=A\00134=1\00149=TW\001" +
265+
"52=20060118-16:34:19\00156=ISLD\00198=0\001108=2\00110=283\001";
266+
267+
Message message = MessageSessionUtils.parse(mockSession, messageString);
268+
269+
assertThat(message, notNullValue());
270+
}
271+
272+
}

0 commit comments

Comments
 (0)