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