|
| 1 | +package org.fixtradingcommunity; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import org.junit.Test; |
| 5 | +import org.junit.Before; |
| 6 | +import com.google.gson.*; |
| 7 | + |
| 8 | +public class MsgTest { |
| 9 | + static class Header { |
| 10 | + String BeginString; |
| 11 | + String MsgType; |
| 12 | + String MsgSeqNum; |
| 13 | + String SenderCompID; |
| 14 | + String TargetCompID; |
| 15 | + String SendingTime; |
| 16 | + } |
| 17 | + |
| 18 | + static class Trailer { |
| 19 | + } |
| 20 | + |
| 21 | + static class NoMDEntries { |
| 22 | + String MDEntryType; |
| 23 | + String MDEntryPx; |
| 24 | + String MDEntrySize; |
| 25 | + String MDEntryDate; |
| 26 | + String MDEntryTime; |
| 27 | + } |
| 28 | + |
| 29 | + static class MarketDataSnapshotFullRefresh { |
| 30 | + static class Body { |
| 31 | + String SecurityIDSource; |
| 32 | + String SecurityID; |
| 33 | + String MDReqID; |
| 34 | + NoMDEntries[] NoMDEntries; |
| 35 | + } |
| 36 | + |
| 37 | + Header Header; |
| 38 | + Body Body; |
| 39 | + Trailer Trailer; |
| 40 | + } |
| 41 | + |
| 42 | + private final static String data = String.join("\n", |
| 43 | + "{", |
| 44 | + "\"Header\": {", |
| 45 | + "\"BeginString\": \"FIXT.1.1\",", |
| 46 | + "\"MsgType\": \"W\",", |
| 47 | + "\"MsgSeqNum\": \"4567\",", |
| 48 | + "\"SenderCompID\": \"SENDER\",", |
| 49 | + "\"TargetCompID\": \"TARGET\",", |
| 50 | + "\"SendingTime\": \"20160802-21:14:38.717\"", |
| 51 | + "},", |
| 52 | + "\"Body\": {", |
| 53 | + "\"SecurityIDSource\": \"8\",", |
| 54 | + "\"SecurityID\": \"ESU6\",", |
| 55 | + "\"MDReqID\": \"789\",", |
| 56 | + "\"NoMDEntries\": [", |
| 57 | + "{ \"MDEntryType\": \"0\", \"MDEntryPx\": \"2179.75\", \"MDEntrySize\": \"175\", \"MDEntryDate\": \"20160812\", \"MDEntryTime\": \"21:14:38.688\"},", |
| 58 | + "{ \"MDEntryType\": \"1\", \"MDEntryPx\": \"2180.25\", \"MDEntrySize\": \"125\", \"MDEntryDate\": \"20160812\", \"MDEntryTime\": \"21:14:38.688\"}", |
| 59 | + "]", |
| 60 | + "},", |
| 61 | + "\"Trailer\": {", |
| 62 | + "}", |
| 63 | + "}" |
| 64 | + ); |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testJson() { |
| 68 | + // When the JSON is parsed |
| 69 | + MarketDataSnapshotFullRefresh msg = new Gson().fromJson(data, MarketDataSnapshotFullRefresh.class); |
| 70 | + |
| 71 | + // Then the header fields should be |
| 72 | + assertEquals("FIXT.1.1", msg.Header.BeginString); |
| 73 | + assertEquals("W", msg.Header.MsgType); |
| 74 | + assertEquals("4567", msg.Header.MsgSeqNum); |
| 75 | + assertEquals("SENDER", msg.Header.SenderCompID); |
| 76 | + assertEquals("TARGET", msg.Header.TargetCompID); |
| 77 | + assertEquals("20160802-21:14:38.717", msg.Header.SendingTime); |
| 78 | + |
| 79 | + // And the body fields should be |
| 80 | + assertEquals("8", msg.Body.SecurityIDSource); |
| 81 | + assertEquals("ESU6", msg.Body.SecurityID); |
| 82 | + assertEquals("789", msg.Body.MDReqID); |
| 83 | + |
| 84 | + // And the NoMDEntries repeating group should contain two entries |
| 85 | + assertEquals(2, msg.Body.NoMDEntries.length); |
| 86 | + |
| 87 | + // And the first entry should be |
| 88 | + assertEquals("0", msg.Body.NoMDEntries[0].MDEntryType); |
| 89 | + assertEquals("2179.75", msg.Body.NoMDEntries[0].MDEntryPx); |
| 90 | + assertEquals("175", msg.Body.NoMDEntries[0].MDEntrySize); |
| 91 | + assertEquals("20160812", msg.Body.NoMDEntries[0].MDEntryDate); |
| 92 | + assertEquals("21:14:38.688", msg.Body.NoMDEntries[0].MDEntryTime); |
| 93 | + |
| 94 | + // And the second entry should be |
| 95 | + assertEquals("1", msg.Body.NoMDEntries[1].MDEntryType); |
| 96 | + assertEquals("2180.25", msg.Body.NoMDEntries[1].MDEntryPx); |
| 97 | + assertEquals("125", msg.Body.NoMDEntries[1].MDEntrySize); |
| 98 | + assertEquals("20160812", msg.Body.NoMDEntries[1].MDEntryDate); |
| 99 | + assertEquals("21:14:38.688", msg.Body.NoMDEntries[1].MDEntryTime); |
| 100 | + } |
| 101 | +} |
0 commit comments