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