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.headers;
017
018import static com.google.common.base.Strings.isNullOrEmpty;
019import static java.util.Collections.singleton;
020import static javax.jcr.observation.Event.NODE_ADDED;
021import static org.fcrepo.jms.headers.DefaultMessageFactory.BASE_URL_HEADER_NAME;
022import static org.fcrepo.jms.headers.DefaultMessageFactory.EVENT_TYPE_HEADER_NAME;
023import static org.fcrepo.jms.headers.DefaultMessageFactory.IDENTIFIER_HEADER_NAME;
024import static org.fcrepo.jms.headers.DefaultMessageFactory.PROPERTIES_HEADER_NAME;
025import static org.fcrepo.jms.headers.DefaultMessageFactory.TIMESTAMP_HEADER_NAME;
026import static org.fcrepo.jms.headers.DefaultMessageFactory.USER_AGENT_HEADER_NAME;
027import static org.fcrepo.jms.headers.DefaultMessageFactory.USER_HEADER_NAME;
028import static org.fcrepo.jms.headers.DefaultMessageFactory.EVENT_ID_HEADER_NAME;
029import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE;
030import static org.junit.Assert.assertEquals;
031import static org.mockito.Mockito.when;
032import static org.mockito.Mockito.doThrow;
033
034import java.util.Set;
035
036import javax.jms.JMSException;
037import javax.jms.Message;
038import javax.jms.Session;
039
040import org.apache.activemq.command.ActiveMQObjectMessage;
041import org.fcrepo.kernel.api.observer.FedoraEvent;
042import org.fcrepo.kernel.api.utils.EventType;
043
044import org.junit.Before;
045import org.junit.Test;
046import org.junit.runner.RunWith;
047import org.mockito.Mock;
048import org.mockito.runners.MockitoJUnitRunner;
049
050/**
051 * <p>DefaultMessageFactoryTest class.</p>
052 *
053 * @author ajs6f
054 */
055@RunWith(MockitoJUnitRunner.class)
056public class DefaultMessageFactoryTest {
057
058    @Mock
059    private Session mockSession;
060
061    @Mock
062    private FedoraEvent mockEvent;
063
064    private DefaultMessageFactory testDefaultMessageFactory;
065
066    @Before
067    public void setUp() throws JMSException {
068        when(mockSession.createMessage()).thenReturn(new ActiveMQObjectMessage());
069        testDefaultMessageFactory = new DefaultMessageFactory();
070    }
071
072    @Test
073    public void testBuildMessage() throws JMSException {
074        final String testPath = "/path/to/resource";
075        final Message msg = doTestBuildMessage("base-url", "Test UserAgent", testPath);
076        assertEquals("Got wrong identifier in message!", testPath, msg.getStringProperty(IDENTIFIER_HEADER_NAME));
077    }
078
079    @Test (expected = Exception.class)
080    public void testBuildMessageException() throws JMSException {
081        doThrow(Exception.class).when(mockEvent).getUserData();
082        testDefaultMessageFactory.getMessage(mockEvent, mockSession);
083    }
084
085    @Test
086    public void testBuildMessageNullUrl() throws JMSException {
087        final String testPath = "/path/to/resource";
088        final Message msg = doTestBuildMessage(null, null, testPath);
089        assertEquals("Got wrong identifier in message!", testPath, msg.getStringProperty(IDENTIFIER_HEADER_NAME));
090    }
091
092    private Message doTestBuildMessage(final String baseUrl, final String userAgent, final String id)
093            throws JMSException {
094        final Long testDate = 46647758568747L;
095        when(mockEvent.getDate()).thenReturn(testDate);
096
097        String url = null;
098        if (!isNullOrEmpty(baseUrl) || !isNullOrEmpty(userAgent)) {
099            url = "{\"baseURL\":\"" + baseUrl + "\",\"userAgent\":\"" + userAgent + "\"}";
100        }
101        when(mockEvent.getUserData()).thenReturn(url);
102        final String testUser = "testUser";
103        when(mockEvent.getUserID()).thenReturn(testUser);
104        when(mockEvent.getPath()).thenReturn(id);
105        final Set<EventType> testTypes = singleton(EventType.valueOf(NODE_ADDED));
106        final String testReturnType = REPOSITORY_NAMESPACE + EventType.valueOf(NODE_ADDED).toString();
107        when(mockEvent.getTypes()).thenReturn(testTypes);
108        final String prop = "test-property";
109        when(mockEvent.getProperties()).thenReturn(singleton(prop));
110        final String eventID = "abcdefg12345678";
111        when(mockEvent.getEventID()).thenReturn(eventID);
112
113        final Message msg = testDefaultMessageFactory.getMessage(mockEvent, mockSession);
114
115        String trimmedBaseUrl = baseUrl;
116        while (!isNullOrEmpty(trimmedBaseUrl) && trimmedBaseUrl.endsWith("/")) {
117            trimmedBaseUrl = trimmedBaseUrl.substring(0, trimmedBaseUrl.length() - 1);
118        }
119
120        assertEquals("Got wrong date in message!", testDate, (Long) msg.getLongProperty(TIMESTAMP_HEADER_NAME));
121        assertEquals("Got wrong type in message!", testReturnType, msg.getStringProperty(EVENT_TYPE_HEADER_NAME));
122        assertEquals("Got wrong base-url in message", trimmedBaseUrl, msg.getStringProperty(BASE_URL_HEADER_NAME));
123        assertEquals("Got wrong property in message", prop, msg.getStringProperty(PROPERTIES_HEADER_NAME));
124        assertEquals("Got wrong userID in message", testUser, msg.getStringProperty(USER_HEADER_NAME));
125        assertEquals("Got wrong userAgent in message", userAgent, msg.getStringProperty(USER_AGENT_HEADER_NAME));
126        assertEquals("Got wrong eventID in message", eventID, msg.getStringProperty(EVENT_ID_HEADER_NAME));
127        return msg;
128    }
129
130}