001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.jms.observer;
017
018import static javax.jms.Session.AUTO_ACKNOWLEDGE;
019import static org.mockito.Matchers.any;
020import static org.mockito.Matchers.eq;
021import static org.mockito.Mockito.mock;
022import static org.mockito.Mockito.verify;
023import static org.mockito.Mockito.when;
024import static org.springframework.test.util.ReflectionTestUtils.setField;
025
026import java.io.IOException;
027
028import javax.jms.Connection;
029import javax.jms.JMSException;
030import javax.jms.Message;
031import javax.jms.MessageProducer;
032
033import org.apache.activemq.ActiveMQConnectionFactory;
034
035import org.fcrepo.kernel.api.observer.FedoraEvent;
036
037import org.junit.Before;
038import org.junit.Test;
039import org.junit.runner.RunWith;
040import org.mockito.Mock;
041import org.mockito.runners.MockitoJUnitRunner;
042
043import com.google.common.eventbus.EventBus;
044
045/**
046 * <p>JMSTopicPublisherTest class.</p>
047 *
048 * @author awoods
049 */
050@RunWith(MockitoJUnitRunner.class)
051public class JMSTopicPublisherTest {
052
053    private JMSTopicPublisher testJMSTopicPublisher;
054
055    @Mock
056    private JMSEventMessageFactory mockEventFactory;
057
058    @Mock
059    private MessageProducer mockProducer;
060
061    @Mock
062    private ActiveMQConnectionFactory mockConnections;
063
064    @Mock
065    private EventBus mockBus;
066
067    @Mock
068    private javax.jms.Session mockJmsSession;
069
070    @Mock
071    private Connection mockConn;
072
073    @Before
074    public void setUp() {
075        testJMSTopicPublisher = new JMSTopicPublisher();
076        setField(testJMSTopicPublisher, "eventFactory", mockEventFactory);
077        setField(testJMSTopicPublisher, "producer", mockProducer);
078        setField(testJMSTopicPublisher, "connectionFactory", mockConnections);
079        setField(testJMSTopicPublisher, "eventBus", mockBus);
080    }
081
082    @Test
083    public void testAcquireConnections() throws JMSException {
084        when(mockConnections.createConnection()).thenReturn(mockConn);
085        when(mockConn.createSession(false, AUTO_ACKNOWLEDGE))
086                .thenReturn(mockJmsSession);
087        testJMSTopicPublisher.acquireConnections();
088        verify(mockBus).register(any());
089    }
090
091    @Test
092    public void testPublishJCREvent() throws IOException, JMSException {
093        final Message mockMsg = mock(Message.class);
094        final FedoraEvent mockEvent = mock(FedoraEvent.class);
095        when(mockEventFactory.getMessage(eq(mockEvent), any(javax.jms.Session.class))).thenReturn(mockMsg);
096        testJMSTopicPublisher.publishJCREvent(mockEvent);
097        verify(mockProducer).send(mockMsg);
098    }
099
100    @Test
101    public void testReleaseConnections() throws JMSException  {
102        setField(testJMSTopicPublisher, "connection", mockConn);
103        setField(testJMSTopicPublisher, "jmsSession", mockJmsSession);
104        testJMSTopicPublisher.releaseConnections();
105        verify(mockProducer).close();
106        verify(mockJmsSession).close();
107        verify(mockConn).close();
108        verify(mockBus).unregister(testJMSTopicPublisher);
109    }
110}