use of quickfix.Message in project camel by apache.
the class QuickfixjConsumerTest method setExceptionOnInOutExchange.
@Test
public void setExceptionOnInOutExchange() throws Exception {
org.apache.camel.Message mockCamelOutMessage = Mockito.mock(org.apache.camel.Message.class);
org.apache.camel.Message mockCamelInMessage = Mockito.mock(org.apache.camel.Message.class);
SessionID mockSessionId = Mockito.mock(SessionID.class);
QuickfixjConsumer consumer = Mockito.spy(new QuickfixjConsumer(mockEndpoint, mockProcessor));
Mockito.doReturn(null).when(consumer).getSession(mockSessionId);
Mockito.when(mockExchange.getPattern()).thenReturn(ExchangePattern.InOut);
Mockito.when(mockExchange.hasOut()).thenReturn(true);
Mockito.when(mockExchange.getOut()).thenReturn(mockCamelOutMessage);
Message outboundFixMessage = new Message();
Mockito.when(mockCamelOutMessage.getBody(Message.class)).thenReturn(outboundFixMessage);
Mockito.when(mockExchange.getIn()).thenReturn(mockCamelInMessage);
Mockito.when(mockCamelInMessage.getHeader("SessionID", SessionID.class)).thenReturn(mockSessionId);
consumer.start();
// Simulate a message from the FIX engine
consumer.onExchange(mockExchange);
Mockito.verify(mockExchange).setException(Matchers.isA(IllegalStateException.class));
}
use of quickfix.Message in project camel by apache.
the class QuickfixjConsumerTest method setUp.
@Before
public void setUp() {
mockExchange = Mockito.mock(Exchange.class);
org.apache.camel.Message mockCamelMessage = Mockito.mock(org.apache.camel.Message.class);
Mockito.when(mockExchange.getIn()).thenReturn(mockCamelMessage);
inboundFixMessage = new Message();
inboundFixMessage.getHeader().setString(BeginString.FIELD, FixVersions.BEGINSTRING_FIX44);
inboundFixMessage.getHeader().setString(SenderCompID.FIELD, "SENDER");
inboundFixMessage.getHeader().setString(TargetCompID.FIELD, "TARGET");
Mockito.when(mockCamelMessage.getBody(quickfix.Message.class)).thenReturn(inboundFixMessage);
mockProcessor = Mockito.mock(Processor.class);
mockEndpoint = Mockito.mock(QuickfixjEndpoint.class);
Mockito.when(mockEndpoint.createExchange(ExchangePattern.InOnly)).thenReturn(mockExchange);
}
use of quickfix.Message in project camel by apache.
the class QuickfixjProducer method sendMessage.
void sendMessage(Exchange exchange, org.apache.camel.Message camelMessage) throws Exception {
Message message = camelMessage.getBody(Message.class);
log.debug("Sending FIX message: {}", message);
SessionID messageSessionID = getEndpoint().getSessionID();
if (messageSessionID == null) {
messageSessionID = MessageUtils.getSessionID(message);
}
Session session = getSession(messageSessionID);
if (session == null) {
throw new IllegalStateException("Unknown session: " + messageSessionID);
}
Callable<Message> callable = null;
if (exchange.getPattern().isOutCapable()) {
MessageCorrelator messageCorrelator = getEndpoint().getEngine().getMessageCorrelator();
callable = messageCorrelator.getReply(getEndpoint().getSessionID(), exchange);
}
if (!session.send(message)) {
throw new CannotSendException("Cannot send FIX message: " + message.toString());
}
if (callable != null) {
Message reply = callable.call();
exchange.getOut().getHeaders().putAll(camelMessage.getHeaders());
exchange.getOut().setBody(reply);
}
}
use of quickfix.Message in project camel by apache.
the class QuickfixjProducerTest method processInOutExchangeSendUnsuccessful.
@Test
public void processInOutExchangeSendUnsuccessful() throws Exception {
Mockito.when(mockExchange.getPattern()).thenReturn(ExchangePattern.InOut);
Mockito.when(mockExchange.getProperty(QuickfixjProducer.CORRELATION_CRITERIA_KEY)).thenReturn(new MessagePredicate(sessionID, MsgType.EMAIL));
Mockito.when(mockExchange.getProperty(QuickfixjProducer.CORRELATION_TIMEOUT_KEY, 1000L, Long.class)).thenReturn(5000L);
org.apache.camel.Message mockOutboundCamelMessage = Mockito.mock(org.apache.camel.Message.class);
Mockito.when(mockExchange.getOut()).thenReturn(mockOutboundCamelMessage);
final Message outboundFixMessage = new Email();
outboundFixMessage.getHeader().setString(SenderCompID.FIELD, "TARGET");
outboundFixMessage.getHeader().setString(TargetCompID.FIELD, "SENDER");
Session mockSession = Mockito.spy(TestSupport.createSession(sessionID));
Mockito.doReturn(mockSession).when(producer).getSession(MessageUtils.getSessionID(inboundFixMessage));
Mockito.doAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
try {
quickfixjEngine.getMessageCorrelator().onEvent(QuickfixjEventCategory.AppMessageReceived, sessionID, outboundFixMessage);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}, 10);
return false;
}
}).when(mockSession).send(Matchers.isA(Message.class));
producer.process(mockExchange);
Mockito.verify(mockOutboundCamelMessage, Mockito.never()).setBody(Matchers.isA(Message.class));
Mockito.verify(mockSession).send(inboundFixMessage);
Mockito.verify(mockExchange).setException(Matchers.isA(CannotSendException.class));
}
use of quickfix.Message in project camel by apache.
the class QuickfixjEngineTest method doLogonEventsTest.
private void doLogonEventsTest(SessionID acceptorSessionID, SessionID initiatorSessionID, QuickfixjEngine quickfixjEngine) throws Exception {
final List<EventRecord> events = new ArrayList<EventRecord>();
final CountDownLatch logonLatch = new CountDownLatch(2);
QuickfixjEventListener logonListener = new QuickfixjEventListener() {
@Override
public synchronized void onEvent(QuickfixjEventCategory eventCategory, SessionID sessionID, Message message) {
events.add(new EventRecord(eventCategory, sessionID, message));
if (eventCategory == QuickfixjEventCategory.SessionLogon) {
logonLatch.countDown();
}
}
};
quickfixjEngine.addEventListener(logonListener);
quickfixjEngine.start();
assertTrue("Logons not completed", logonLatch.await(5000, TimeUnit.MILLISECONDS));
quickfixjEngine.removeEventListener(logonListener);
assertThat(events.size(), is(8));
// The session events will arrive out of order as the event callbacks happen in the context of different threads so that the asserts
// below must cater for that, that's do not assert on the order of the arrived events but just do assert on their existence. for this
// to work we have've defined a relaxed comparison about the messages being sent, see the EventRecord.equals() method
assertTrue(events.contains(new EventRecord(QuickfixjEventCategory.SessionCreated, acceptorSessionID, null)));
assertTrue(events.contains(new EventRecord(QuickfixjEventCategory.SessionCreated, initiatorSessionID, null)));
assertTrue(events.contains(new EventRecord(QuickfixjEventCategory.AdminMessageSent, initiatorSessionID, new Message())));
assertTrue(events.contains(new EventRecord(QuickfixjEventCategory.AdminMessageReceived, acceptorSessionID, new Message())));
assertTrue(events.contains(new EventRecord(QuickfixjEventCategory.AdminMessageSent, acceptorSessionID, new Message())));
assertTrue(events.contains(new EventRecord(QuickfixjEventCategory.AdminMessageReceived, initiatorSessionID, new Message())));
assertTrue(events.contains(new EventRecord(QuickfixjEventCategory.SessionLogon, initiatorSessionID, null)));
assertTrue(events.contains(new EventRecord(QuickfixjEventCategory.SessionLogon, acceptorSessionID, null)));
}
Aggregations