001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.jms;
007
008import static com.google.common.base.Strings.isNullOrEmpty;
009import static java.time.Instant.ofEpochMilli;
010import static java.util.Collections.singleton;
011import static org.fcrepo.jms.DefaultMessageFactory.BASE_URL_HEADER_NAME;
012import static org.fcrepo.jms.DefaultMessageFactory.EVENT_TYPE_HEADER_NAME;
013import static org.fcrepo.jms.DefaultMessageFactory.IDENTIFIER_HEADER_NAME;
014import static org.fcrepo.jms.DefaultMessageFactory.RESOURCE_TYPE_HEADER_NAME;
015import static org.fcrepo.jms.DefaultMessageFactory.TIMESTAMP_HEADER_NAME;
016import static org.fcrepo.jms.DefaultMessageFactory.USER_AGENT_HEADER_NAME;
017import static org.fcrepo.jms.DefaultMessageFactory.USER_HEADER_NAME;
018import static org.fcrepo.jms.DefaultMessageFactory.EVENT_ID_HEADER_NAME;
019import static org.junit.Assert.assertEquals;
020import static org.mockito.ArgumentMatchers.anyString;
021import static org.mockito.Mockito.when;
022
023import java.net.URI;
024import java.util.Set;
025
026import javax.jms.JMSException;
027import javax.jms.Message;
028import javax.jms.Session;
029
030import org.apache.activemq.command.ActiveMQTextMessage;
031
032import org.fcrepo.kernel.api.observer.Event;
033import org.fcrepo.kernel.api.observer.EventType;
034
035import org.junit.Before;
036import org.junit.Test;
037import org.junit.runner.RunWith;
038import org.mockito.Mock;
039import org.mockito.junit.MockitoJUnitRunner;
040
041/**
042 * <p>DefaultMessageFactoryTest class.</p>
043 *
044 * @author ajs6f
045 */
046@RunWith(MockitoJUnitRunner.class)
047public class DefaultMessageFactoryTest {
048
049    @Mock
050    private Session mockSession;
051
052    @Mock
053    private Event mockEvent;
054
055    private DefaultMessageFactory testDefaultMessageFactory;
056
057    @Before
058    public void setUp() throws JMSException {
059        when(mockSession.createTextMessage(anyString())).thenReturn(new ActiveMQTextMessage());
060        testDefaultMessageFactory = new DefaultMessageFactory();
061    }
062
063    @Test
064    public void testBuildMessage() throws JMSException {
065        final String testPath = "/path/to/resource";
066        final Message msg = doTestBuildMessage("base-url", "Test UserAgent", testPath);
067        assertEquals("Got wrong identifier in message!", testPath, msg.getStringProperty(IDENTIFIER_HEADER_NAME));
068    }
069
070    @Test
071    public void testBuildMessageNullUrl() throws JMSException {
072        final String testPath = "/path/to/resource";
073        final Message msg = doTestBuildMessage(null, null, testPath);
074        assertEquals("Got wrong identifier in message!", testPath, msg.getStringProperty(IDENTIFIER_HEADER_NAME));
075    }
076
077    private Message doTestBuildMessage(final String baseUrl, final String userAgent, final String id)
078            throws JMSException {
079        final Long testDate = 46647758568747L;
080
081        when(mockEvent.getBaseUrl()).thenReturn(baseUrl);
082        when(mockEvent.getUserAgent()).thenReturn(userAgent);
083        when(mockEvent.getDate()).thenReturn(ofEpochMilli(testDate));
084        final String testUser = "testUser";
085        when(mockEvent.getUserID()).thenReturn(testUser);
086        when(mockEvent.getUserURI()).thenReturn(URI.create("http://localhost:8080/fcrepo/" + testUser));
087        when(mockEvent.getPath()).thenReturn(id);
088        final Set<EventType> testTypes = singleton(EventType.RESOURCE_CREATION);
089        final String testReturnType = EventType.RESOURCE_CREATION.getType();
090        when(mockEvent.getTypes()).thenReturn(testTypes);
091        final String prop = "test-type";
092        when(mockEvent.getResourceTypes()).thenReturn(singleton(prop));
093        final String eventID = "abcdefg12345678";
094        when(mockEvent.getEventID()).thenReturn(eventID);
095
096        final Message msg = testDefaultMessageFactory.getMessage(mockEvent, mockSession);
097
098        String trimmedBaseUrl = baseUrl;
099        while (!isNullOrEmpty(trimmedBaseUrl) && trimmedBaseUrl.endsWith("/")) {
100            trimmedBaseUrl = trimmedBaseUrl.substring(0, trimmedBaseUrl.length() - 1);
101        }
102
103        assertEquals("Got wrong date in message!", testDate, (Long) msg.getLongProperty(TIMESTAMP_HEADER_NAME));
104        assertEquals("Got wrong type in message!", testReturnType, msg.getStringProperty(EVENT_TYPE_HEADER_NAME));
105        assertEquals("Got wrong base-url in message", trimmedBaseUrl, msg.getStringProperty(BASE_URL_HEADER_NAME));
106        assertEquals("Got wrong resource type in message", prop, msg.getStringProperty(RESOURCE_TYPE_HEADER_NAME));
107        assertEquals("Got wrong userID in message", testUser, msg.getStringProperty(USER_HEADER_NAME));
108        assertEquals("Got wrong userAgent in message", userAgent, msg.getStringProperty(USER_AGENT_HEADER_NAME));
109        assertEquals("Got wrong eventID in message", eventID, msg.getStringProperty(EVENT_ID_HEADER_NAME));
110        return msg;
111    }
112
113}