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 org.junit.Assert.assertEquals;
019import static org.junit.Assert.assertNotNull;
020
021import org.apache.camel.Processor;
022import org.apache.camel.Producer;
023import org.apache.camel.RuntimeCamelException;
024import org.junit.Test;
025import org.junit.runner.RunWith;
026import org.mockito.Mock;
027import org.mockito.runners.MockitoJUnitRunner;
028
029/**
030 * @author ajs6f
031 */
032@RunWith(MockitoJUnitRunner.class)
033public class FcrepoEndpointTest {
034
035    private final String FCREPO_URI = "fcrepo:foo/rest";
036
037    private final String FCREPO_PATH = "foo/rest";
038
039    @Mock
040    private FcrepoComponent mockContext;
041
042    @Mock
043    private Processor mockProcessor;
044
045    private FcrepoConfiguration testConfig = new FcrepoConfiguration();
046
047    @Test(expected = RuntimeCamelException.class)
048    public void testNoConsumerCanBeCreated() throws RuntimeCamelException {
049        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
050        testEndpoint.createConsumer(mockProcessor);
051    }
052
053    @Test
054    public void testCreateTransactionTemplate() {
055        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
056        assertNotNull(testEndpoint.createTransactionTemplate());
057        testEndpoint.setTransactionManager(new FcrepoTransactionManager());
058        assertNotNull(testEndpoint.createTransactionTemplate());
059    }
060
061    @Test
062    public void testCreateProducer() {
063        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
064        final Producer testProducer = testEndpoint.createProducer();
065        assertEquals(testEndpoint, testProducer.getEndpoint());
066    }
067
068    @Test
069    public void testBaseUrl() {
070        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
071        assertEquals(testEndpoint.getBaseUrl(), FCREPO_PATH);
072    }
073
074    @Test
075    public void testBaseUrlWithScheme1() {
076        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint("fcrepo:localhost:8080/rest", "localhost:8080/rest",
077                mockContext, testConfig);
078        assertEquals("http://localhost:8080/rest", testEndpoint.getBaseUrlWithScheme());
079    }
080
081    @Test
082    public void testBaseUrlWithScheme2() {
083        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint("fcrepo:https://localhost/rest",
084                "https://localhost/rest",
085                mockContext, testConfig);
086        assertEquals("https://localhost/rest", testEndpoint.getBaseUrlWithScheme());
087    }
088
089    @Test
090    public void testBaseUrlWithScheme3() {
091        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint("fcrepo://localhost:443/rest",
092                "localhost:443/rest",
093                mockContext, testConfig);
094        assertEquals("https://localhost:443/rest", testEndpoint.getBaseUrlWithScheme());
095    }
096
097    @Test
098    public void testBaseUrlWithScheme4() {
099        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint("fcrepo:http://localhost:8080/rest",
100                "http://localhost:8080/rest",
101                mockContext, testConfig);
102        assertEquals("http://localhost:8080/rest", testEndpoint.getBaseUrlWithScheme());
103    }
104
105    @Test
106    public void testConfiguration() {
107        final FcrepoConfiguration config = new FcrepoConfiguration();
108        config.setTombstone(true);
109
110        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
111        assertEquals(false, testEndpoint.getTombstone());
112        testEndpoint.setConfiguration(config);
113        assertEquals(true, testEndpoint.getTombstone());
114    }
115
116    @Test
117    public void testTombstone() {
118        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
119        assertEquals(false, testEndpoint.getTombstone());
120        testEndpoint.setTombstone(true);
121        assertEquals(true, testEndpoint.getTombstone());
122    }
123
124    @Test
125    public void testTransform() {
126        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
127        final String transform = "default";
128        assertEquals(null, testEndpoint.getTransform());
129        testEndpoint.setTransform(transform);
130        assertEquals(transform, testEndpoint.getTransform());
131    }
132
133    @Test
134    public void testThrowExceptionOnFailure() {
135        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
136        assertEquals(true, testEndpoint.getThrowExceptionOnFailure());
137        testEndpoint.setThrowExceptionOnFailure(false);
138        assertEquals(false, testEndpoint.getThrowExceptionOnFailure());
139    }
140
141    @Test
142    public void testFixity() {
143        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
144        assertEquals(false, testEndpoint.getFixity());
145        testEndpoint.setFixity(true);
146        assertEquals(true, testEndpoint.getFixity());
147    }
148
149    @Test
150    public void testMetadata() {
151        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
152        assertEquals(true, testEndpoint.getMetadata());
153        testEndpoint.setMetadata(false);
154        assertEquals(false, testEndpoint.getMetadata());
155    }
156
157    @Test
158    public void testAuthHost() {
159        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
160        final String authHost = "example.org";
161        assertEquals(null, testEndpoint.getAuthHost());
162        testEndpoint.setAuthHost(authHost);
163        assertEquals(authHost, testEndpoint.getAuthHost());
164    }
165
166    @Test
167    public void testAuthUser() {
168        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
169        final String authUser = "fedoraAdmin";
170        assertEquals(null, testEndpoint.getAuthUsername());
171        testEndpoint.setAuthUsername(authUser);
172        assertEquals(authUser, testEndpoint.getAuthUsername());
173    }
174
175    @Test
176    public void testAuthPassword() {
177        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
178        final String authPassword = "foo";
179        assertEquals(null, testEndpoint.getAuthPassword());
180        testEndpoint.setAuthPassword(authPassword);
181        assertEquals(authPassword, testEndpoint.getAuthPassword());
182    }
183
184    @Test
185    public void testAccept() {
186        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
187        final String accept1 = "application/rdf+xml";
188        final String accept2 = "text/turtle";
189        final String accept3 = "application/ld+json";
190        final String accept4 = "application/sparql-update";
191        assertEquals(null, testEndpoint.getAccept());
192        testEndpoint.setAccept("application/rdf xml");
193        assertEquals(accept1, testEndpoint.getAccept());
194        testEndpoint.setAccept(accept2);
195        assertEquals(accept2, testEndpoint.getAccept());
196        testEndpoint.setAccept(accept3);
197        assertEquals(accept3, testEndpoint.getAccept());
198        testEndpoint.setAccept(accept4);
199        assertEquals(accept4, testEndpoint.getAccept());
200    }
201
202    @Test
203    public void testContentType() {
204        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
205        final String contentType1 = "application/rdf+xml";
206        final String contentType2 = "text/turtle";
207        final String contentType3 = "application/ld+json";
208        final String contentType4 = "application/sparql-update";
209        assertEquals(null, testEndpoint.getContentType());
210        testEndpoint.setContentType("application/rdf xml");
211        assertEquals(contentType1, testEndpoint.getContentType());
212        testEndpoint.setContentType(contentType2);
213        assertEquals(contentType2, testEndpoint.getContentType());
214        testEndpoint.setContentType(contentType3);
215        assertEquals(contentType3, testEndpoint.getContentType());
216        testEndpoint.setContentType(contentType4);
217        assertEquals(contentType4, testEndpoint.getContentType());
218    }
219
220    @Test
221    public void testPreferOmit() {
222        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
223        final String omit1 = "PreferContainment";
224        final String omit2 = "http://www.w3.org/ns/ldp#PreferMembership";
225        final String omit3 = "http://www.w3.org/ns/ldp#PreferMinimalContainer " +
226                             "http://www.w3.org/ns/ldp#PreferContainment";
227        assertEquals(null, testEndpoint.getPreferOmit());
228        testEndpoint.setPreferOmit(omit1);
229        assertEquals(omit1, testEndpoint.getPreferOmit());
230        testEndpoint.setPreferOmit(omit2);
231        assertEquals(omit2, testEndpoint.getPreferOmit());
232        testEndpoint.setPreferOmit(omit3);
233        assertEquals(omit3, testEndpoint.getPreferOmit());
234    }
235
236    @Test
237    public void testPreferInclude() {
238        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
239        final String include1 = "PreferContainment";
240        final String include2 = "http://www.w3.org/ns/ldp#PreferMembership";
241        final String include3 = "http://www.w3.org/ns/ldp#PreferMinimalContainer " +
242                                "http://www.w3.org/ns/ldp#PreferContainment";
243        assertEquals(null, testEndpoint.getPreferInclude());
244        testEndpoint.setPreferInclude(include1);
245        assertEquals(include1, testEndpoint.getPreferInclude());
246        testEndpoint.setPreferInclude(include2);
247        assertEquals(include2, testEndpoint.getPreferInclude());
248        testEndpoint.setPreferInclude(include3);
249        assertEquals(include3, testEndpoint.getPreferInclude());
250    }
251
252    @Test
253    public void testSingleton() {
254        final FcrepoEndpoint testEndpoint = new FcrepoEndpoint(FCREPO_URI, FCREPO_PATH, mockContext, testConfig);
255        assertEquals(true, testEndpoint.isSingleton());
256    }
257
258}