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 com.google.common.base.Strings.isNullOrEmpty;
021import static java.time.Instant.ofEpochMilli;
022import static java.util.Collections.singleton;
023import static org.fcrepo.jms.DefaultMessageFactory.BASE_URL_HEADER_NAME;
024import static org.fcrepo.jms.DefaultMessageFactory.EVENT_TYPE_HEADER_NAME;
025import static org.fcrepo.jms.DefaultMessageFactory.IDENTIFIER_HEADER_NAME;
026import static org.fcrepo.jms.DefaultMessageFactory.RESOURCE_TYPE_HEADER_NAME;
027import static org.fcrepo.jms.DefaultMessageFactory.TIMESTAMP_HEADER_NAME;
028import static org.fcrepo.jms.DefaultMessageFactory.USER_AGENT_HEADER_NAME;
029import static org.fcrepo.jms.DefaultMessageFactory.USER_HEADER_NAME;
030import static org.fcrepo.jms.DefaultMessageFactory.EVENT_ID_HEADER_NAME;
031import static org.fcrepo.kernel.api.observer.OptionalValues.BASE_URL;
032import static org.fcrepo.kernel.api.observer.OptionalValues.USER_AGENT;
033import static org.junit.Assert.assertEquals;
034import static org.mockito.Matchers.anyString;
035import static org.mockito.Mockito.when;
036
037import java.util.HashMap;
038import java.util.Map;
039import java.util.Set;
040
041import javax.jms.JMSException;
042import javax.jms.Message;
043import javax.jms.Session;
044
045import org.apache.activemq.command.ActiveMQTextMessage;
046
047import org.fcrepo.kernel.api.observer.EventType;
048import org.fcrepo.kernel.api.observer.FedoraEvent;
049
050import org.junit.Before;
051import org.junit.Test;
052import org.junit.runner.RunWith;
053import org.mockito.Mock;
054import org.mockito.runners.MockitoJUnitRunner;
055
056/**
057 * <p>DefaultMessageFactoryTest class.</p>
058 *
059 * @author ajs6f
060 */
061@RunWith(MockitoJUnitRunner.class)
062public class DefaultMessageFactoryTest {
063
064    @Mock
065    private Session mockSession;
066
067    @Mock
068    private FedoraEvent mockEvent;
069
070    private DefaultMessageFactory testDefaultMessageFactory;
071
072    @Before
073    public void setUp() throws JMSException {
074        when(mockSession.createTextMessage(anyString())).thenReturn(new ActiveMQTextMessage());
075        testDefaultMessageFactory = new DefaultMessageFactory();
076    }
077
078    @Test
079    public void testBuildMessage() throws JMSException {
080        final String testPath = "/path/to/resource";
081        final Message msg = doTestBuildMessage("base-url", "Test UserAgent", testPath);
082        assertEquals("Got wrong identifier in message!", testPath, msg.getStringProperty(IDENTIFIER_HEADER_NAME));
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        final Map<String, String> info = new HashMap<>();
096        if (baseUrl != null) {
097            info.put(BASE_URL, baseUrl);
098        }
099        if (userAgent != null) {
100            info.put(USER_AGENT, userAgent);
101        }
102        when(mockEvent.getInfo()).thenReturn(info);
103        when(mockEvent.getDate()).thenReturn(ofEpochMilli(testDate));
104        final String testUser = "testUser";
105        when(mockEvent.getUserID()).thenReturn(testUser);
106        when(mockEvent.getPath()).thenReturn(id);
107        final Set<EventType> testTypes = singleton(EventType.RESOURCE_CREATION);
108        final String testReturnType = EventType.RESOURCE_CREATION.getType();
109        when(mockEvent.getTypes()).thenReturn(testTypes);
110        final String prop = "test-type";
111        when(mockEvent.getResourceTypes()).thenReturn(singleton(prop));
112        final String eventID = "abcdefg12345678";
113        when(mockEvent.getEventID()).thenReturn(eventID);
114
115        final Message msg = testDefaultMessageFactory.getMessage(mockEvent, mockSession);
116
117        String trimmedBaseUrl = baseUrl;
118        while (!isNullOrEmpty(trimmedBaseUrl) && trimmedBaseUrl.endsWith("/")) {
119            trimmedBaseUrl = trimmedBaseUrl.substring(0, trimmedBaseUrl.length() - 1);
120        }
121
122        assertEquals("Got wrong date in message!", testDate, (Long) msg.getLongProperty(TIMESTAMP_HEADER_NAME));
123        assertEquals("Got wrong type in message!", testReturnType, msg.getStringProperty(EVENT_TYPE_HEADER_NAME));
124        assertEquals("Got wrong base-url in message", trimmedBaseUrl, msg.getStringProperty(BASE_URL_HEADER_NAME));
125        assertEquals("Got wrong resource type in message", prop, msg.getStringProperty(RESOURCE_TYPE_HEADER_NAME));
126        assertEquals("Got wrong userID in message", testUser, msg.getStringProperty(USER_HEADER_NAME));
127        assertEquals("Got wrong userAgent in message", userAgent, msg.getStringProperty(USER_AGENT_HEADER_NAME));
128        assertEquals("Got wrong eventID in message", eventID, msg.getStringProperty(EVENT_ID_HEADER_NAME));
129        return msg;
130    }
131
132}