001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.camel;
017
018import static java.util.Collections.emptyMap;
019import static org.junit.Assert.assertEquals;
020import static org.junit.Assert.assertNull;
021
022import java.util.Map;
023
024import org.apache.camel.CamelContext;
025import org.apache.camel.Endpoint;
026import org.junit.Test;
027import org.junit.runner.RunWith;
028import org.mockito.Mock;
029import org.mockito.runners.MockitoJUnitRunner;
030
031/**
032 * @author ajs6f
033 */
034@RunWith(MockitoJUnitRunner.class)
035public class FcrepoComponentTest {
036
037    private static final String TEST_ENDPOINT_URI = "fcrepo:foo";
038
039    private static final Map<String, Object> EMPTY_MAP = emptyMap();
040
041    @Mock
042    private CamelContext mockContext;
043
044    @Test
045    public void testCreateEndpoint() {
046        final FcrepoComponent testComponent = new FcrepoComponent(mockContext);
047        final Endpoint testEndpoint = testComponent.createEndpoint(TEST_ENDPOINT_URI, "", EMPTY_MAP);
048        assertEquals(mockContext, testEndpoint.getCamelContext());
049        assertEquals(TEST_ENDPOINT_URI, testEndpoint.getEndpointUri());
050    }
051
052    @Test
053    public void testCreateEndpointFromConfig() {
054        final FcrepoConfiguration configuration = new FcrepoConfiguration();
055
056        configuration.setMetadata(false);
057
058        final FcrepoComponent testComponent = new FcrepoComponent(configuration);
059        final Endpoint testEndpoint = testComponent.createEndpoint(TEST_ENDPOINT_URI, "", EMPTY_MAP);
060        assertEquals(TEST_ENDPOINT_URI, testEndpoint.getEndpointUri());
061
062        assertEquals(false, testComponent.getConfiguration().getMetadata());
063    }
064
065    @Test
066    public void testPreConfiguredComponent() {
067        final FcrepoConfiguration config = new FcrepoConfiguration();
068        config.setAuthUsername("foo");
069        config.setAuthPassword("bar");
070        config.setAuthHost("baz");
071
072        final FcrepoComponent testComponent = new FcrepoComponent();
073
074        testComponent.setConfiguration(config);
075
076        final Endpoint testEndpoint = testComponent.createEndpoint(TEST_ENDPOINT_URI, "", EMPTY_MAP);
077
078        assertEquals(TEST_ENDPOINT_URI, testEndpoint.getEndpointUri());
079        assertEquals("foo", testComponent.getConfiguration().getAuthUsername());
080        assertEquals("bar", testComponent.getConfiguration().getAuthPassword());
081        assertEquals("baz", testComponent.getConfiguration().getAuthHost());
082    }
083
084    @Test
085    public void testPostConfiguredComponent() {
086        final FcrepoComponent testComponent = new FcrepoComponent();
087        final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
088        testComponent.setAuthUsername("foo");
089        testComponent.setAuthPassword("bar");
090        testComponent.setAuthHost("baz");
091        testComponent.setTransactionManager(txMgr);
092
093        final Endpoint testEndpoint = testComponent.createEndpoint(TEST_ENDPOINT_URI, "", EMPTY_MAP);
094
095        assertEquals(TEST_ENDPOINT_URI, testEndpoint.getEndpointUri());
096        assertEquals("foo", testComponent.getConfiguration().getAuthUsername());
097        assertEquals("bar", testComponent.getConfiguration().getAuthPassword());
098        assertEquals("baz", testComponent.getConfiguration().getAuthHost());
099        assertNull(testComponent.getConfiguration().getTransactionManager());
100        assertEquals(txMgr, testComponent.getTransactionManager());
101
102        testComponent.getConfiguration().setTransactionManager(txMgr);
103
104        assertEquals(txMgr, testComponent.getConfiguration().getTransactionManager());
105    }
106
107    @Test
108    public void testCreateEndpointFromDefaultConstructor() {
109        final FcrepoComponent testComponent = new FcrepoComponent();
110        final Endpoint testEndpoint = testComponent.createEndpoint(TEST_ENDPOINT_URI, "", EMPTY_MAP);
111        assertEquals(TEST_ENDPOINT_URI, testEndpoint.getEndpointUri());
112    }
113
114}