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.audit;
019
020import static org.mockito.Matchers.argThat;
021import static org.mockito.Mockito.mock;
022import static org.mockito.Mockito.verify;
023import static org.mockito.Mockito.when;
024import static org.mockito.MockitoAnnotations.initMocks;
025import static org.slf4j.LoggerFactory.getLogger;
026
027import org.fcrepo.kernel.api.observer.FedoraEvent;
028
029import org.junit.Before;
030import org.junit.Test;
031import org.mockito.ArgumentMatcher;
032import org.mockito.Mock;
033
034import ch.qos.logback.classic.Logger;
035import ch.qos.logback.classic.spi.ILoggingEvent;
036import ch.qos.logback.classic.spi.LoggingEvent;
037import ch.qos.logback.core.Appender;
038
039import com.google.common.eventbus.EventBus;
040
041/**
042 * <p>LogbackAuditorTest class.</p>
043 *
044 * @author eddies
045 */
046public class LogbackAuditorTest {
047
048    private final String jcrEventUserID = "jdoe";
049
050    private final String jcrEventPath = "/foo/bar";
051
052    @Mock
053    private Appender<ILoggingEvent> mockAppender;
054
055    @Before
056    public void setUp() {
057        initMocks(this);
058    }
059
060    @Test
061    public void testEventAuditing() throws Exception {
062        final Logger root =
063                (Logger) getLogger(LogbackAuditor.class);
064
065        when(mockAppender.getName()).thenReturn("MockAppender");
066
067        final FedoraEvent mockEvent = mock(FedoraEvent.class);
068        when(mockEvent.getUserID()).thenReturn(jcrEventUserID);
069        when(mockEvent.getPath()).thenReturn(jcrEventPath);
070
071        root.addAppender(mockAppender);
072
073        final EventBus eventBus = new EventBus("Test EventBus");
074        final Auditor auditor = new LogbackAuditor();
075        eventBus.register(auditor);
076        eventBus.post(mockEvent);
077
078        verify(mockAppender).doAppend(
079                (ILoggingEvent) argThat(new ArgumentMatcher<Object>() {
080
081                    @Override
082                    public boolean matches(final Object argument) {
083                        return ((LoggingEvent) argument).getFormattedMessage()
084                                .contains("jdoe /foo/bar");
085                    }
086                }));
087    }
088}