Search in sources :

Example 1 with Message

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));
}
Also used : Message(quickfix.Message) SessionID(quickfix.SessionID) Test(org.junit.Test)

Example 2 with Message

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);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(quickfix.Message) Before(org.junit.Before)

Example 3 with Message

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);
    }
}
Also used : Message(quickfix.Message) SessionID(quickfix.SessionID) Session(quickfix.Session)

Example 4 with Message

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));
}
Also used : Email(quickfix.fix42.Email) Message(quickfix.Message) IOException(java.io.IOException) JMException(javax.management.JMException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Session(quickfix.Session) Test(org.junit.Test)

Example 5 with Message

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)));
}
Also used : Message(quickfix.Message) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) SessionID(quickfix.SessionID)

Aggregations

Message (quickfix.Message)17 SessionID (quickfix.SessionID)12 Test (org.junit.Test)8 Exchange (org.apache.camel.Exchange)7 DefaultExchange (org.apache.camel.impl.DefaultExchange)4 Session (quickfix.Session)4 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Before (org.junit.Before)3 Email (quickfix.fix42.Email)3 NoHops (quickfix.fix44.Message.Header.NoHops)3 IOException (java.io.IOException)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 JMException (javax.management.JMException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 DataDictionary (quickfix.DataDictionary)2 Converter (org.apache.camel.Converter)1 Processor (org.apache.camel.Processor)1 InvalidMessage (quickfix.InvalidMessage)1