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.junit.Assert.assertEquals; 032import static org.mockito.ArgumentMatchers.anyString; 033import static org.mockito.Mockito.when; 034 035import java.net.URI; 036import java.util.Set; 037 038import javax.jms.JMSException; 039import javax.jms.Message; 040import javax.jms.Session; 041 042import org.apache.activemq.command.ActiveMQTextMessage; 043 044import org.fcrepo.kernel.api.observer.Event; 045import org.fcrepo.kernel.api.observer.EventType; 046 047import org.junit.Before; 048import org.junit.Test; 049import org.junit.runner.RunWith; 050import org.mockito.Mock; 051import org.mockito.junit.MockitoJUnitRunner; 052 053/** 054 * <p>DefaultMessageFactoryTest class.</p> 055 * 056 * @author ajs6f 057 */ 058@RunWith(MockitoJUnitRunner.class) 059public class DefaultMessageFactoryTest { 060 061 @Mock 062 private Session mockSession; 063 064 @Mock 065 private Event mockEvent; 066 067 private DefaultMessageFactory testDefaultMessageFactory; 068 069 @Before 070 public void setUp() throws JMSException { 071 when(mockSession.createTextMessage(anyString())).thenReturn(new ActiveMQTextMessage()); 072 testDefaultMessageFactory = new DefaultMessageFactory(); 073 } 074 075 @Test 076 public void testBuildMessage() throws JMSException { 077 final String testPath = "/path/to/resource"; 078 final Message msg = doTestBuildMessage("base-url", "Test UserAgent", testPath); 079 assertEquals("Got wrong identifier in message!", testPath, msg.getStringProperty(IDENTIFIER_HEADER_NAME)); 080 } 081 082 @Test 083 public void testBuildMessageNullUrl() throws JMSException { 084 final String testPath = "/path/to/resource"; 085 final Message msg = doTestBuildMessage(null, null, testPath); 086 assertEquals("Got wrong identifier in message!", testPath, msg.getStringProperty(IDENTIFIER_HEADER_NAME)); 087 } 088 089 private Message doTestBuildMessage(final String baseUrl, final String userAgent, final String id) 090 throws JMSException { 091 final Long testDate = 46647758568747L; 092 093 when(mockEvent.getBaseUrl()).thenReturn(baseUrl); 094 when(mockEvent.getUserAgent()).thenReturn(userAgent); 095 when(mockEvent.getDate()).thenReturn(ofEpochMilli(testDate)); 096 final String testUser = "testUser"; 097 when(mockEvent.getUserID()).thenReturn(testUser); 098 when(mockEvent.getUserURI()).thenReturn(URI.create("http://localhost:8080/fcrepo/" + testUser)); 099 when(mockEvent.getPath()).thenReturn(id); 100 final Set<EventType> testTypes = singleton(EventType.RESOURCE_CREATION); 101 final String testReturnType = EventType.RESOURCE_CREATION.getType(); 102 when(mockEvent.getTypes()).thenReturn(testTypes); 103 final String prop = "test-type"; 104 when(mockEvent.getResourceTypes()).thenReturn(singleton(prop)); 105 final String eventID = "abcdefg12345678"; 106 when(mockEvent.getEventID()).thenReturn(eventID); 107 108 final Message msg = testDefaultMessageFactory.getMessage(mockEvent, mockSession); 109 110 String trimmedBaseUrl = baseUrl; 111 while (!isNullOrEmpty(trimmedBaseUrl) && trimmedBaseUrl.endsWith("/")) { 112 trimmedBaseUrl = trimmedBaseUrl.substring(0, trimmedBaseUrl.length() - 1); 113 } 114 115 assertEquals("Got wrong date in message!", testDate, (Long) msg.getLongProperty(TIMESTAMP_HEADER_NAME)); 116 assertEquals("Got wrong type in message!", testReturnType, msg.getStringProperty(EVENT_TYPE_HEADER_NAME)); 117 assertEquals("Got wrong base-url in message", trimmedBaseUrl, msg.getStringProperty(BASE_URL_HEADER_NAME)); 118 assertEquals("Got wrong resource type in message", prop, msg.getStringProperty(RESOURCE_TYPE_HEADER_NAME)); 119 assertEquals("Got wrong userID in message", testUser, msg.getStringProperty(USER_HEADER_NAME)); 120 assertEquals("Got wrong userAgent in message", userAgent, msg.getStringProperty(USER_AGENT_HEADER_NAME)); 121 assertEquals("Got wrong eventID in message", eventID, msg.getStringProperty(EVENT_ID_HEADER_NAME)); 122 return msg; 123 } 124 125}