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.integration;
017
018import static org.fcrepo.camel.FcrepoProducer.DEFAULT_CONTENT_TYPE;
019
020import org.apache.camel.EndpointInject;
021import org.apache.camel.Exchange;
022import org.apache.camel.Produce;
023import org.apache.camel.ProducerTemplate;
024import org.apache.camel.builder.RouteBuilder;
025import org.apache.camel.component.mock.MockEndpoint;
026import org.apache.camel.test.junit4.CamelTestSupport;
027import org.junit.Test;
028import org.junit.runner.RunWith;
029import org.springframework.test.context.ContextConfiguration;
030import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
031
032/**
033 * Test the retrieved content-type from a fcrepo endpoint
034 * when the Accept type is put in a message header.
035 * @author Aaron Coburn
036 * @since November 7, 2014
037 */
038@RunWith(SpringJUnit4ClassRunner.class)
039@ContextConfiguration({"/spring-test/test-container.xml"})
040public class FcrepoContentTypeHeaderIT extends CamelTestSupport {
041
042    @EndpointInject(uri = "mock:result")
043    protected MockEndpoint resultEndpoint;
044
045    @Produce(uri = "direct:start")
046    protected ProducerTemplate template;
047
048    @Test
049    public void testContentTypeJson() throws InterruptedException {
050        resultEndpoint.expectedHeaderReceived("Content-Type", "application/ld+json");
051        resultEndpoint.expectedMessageCount(2);
052
053        template.sendBodyAndHeader(null, "Accept", "application/ld+json");
054        template.sendBodyAndHeader(null, Exchange.ACCEPT_CONTENT_TYPE, "application/ld+json");
055
056        resultEndpoint.assertIsSatisfied();
057    }
058
059    @Test
060    public void testContentTypeRdfXml() throws InterruptedException {
061        resultEndpoint.expectedHeaderReceived("Content-Type", "application/rdf+xml");
062        resultEndpoint.expectedMessageCount(2);
063
064        template.sendBodyAndHeader(null, "Accept", "application/rdf+xml");
065        template.sendBodyAndHeader(null, Exchange.ACCEPT_CONTENT_TYPE, "application/rdf+xml");
066
067        resultEndpoint.assertIsSatisfied();
068    }
069
070    @Test
071    public void testContentTypeNTriples() throws InterruptedException  {
072        resultEndpoint.expectedHeaderReceived("Content-Type", "application/n-triples");
073        resultEndpoint.expectedMessageCount(2);
074
075        template.sendBodyAndHeader(null, "Accept", "application/n-triples");
076        template.sendBodyAndHeader(null, Exchange.ACCEPT_CONTENT_TYPE, "application/n-triples");
077
078        resultEndpoint.assertIsSatisfied();
079    }
080
081    @Test
082    public void testContentTypeTurtle() throws InterruptedException {
083        resultEndpoint.expectedHeaderReceived("Content-Type", "text/turtle");
084        resultEndpoint.expectedMessageCount(2);
085
086        template.sendBodyAndHeader(null, "Accept", "text/turtle");
087        template.sendBodyAndHeader(null, Exchange.ACCEPT_CONTENT_TYPE, "text/turtle");
088
089        resultEndpoint.assertIsSatisfied();
090    }
091
092    @Test
093    public void testContentTypeN3() throws InterruptedException {
094        resultEndpoint.expectedHeaderReceived("Content-Type", "text/rdf+n3");
095        resultEndpoint.expectedMessageCount(2);
096
097        template.sendBodyAndHeader(null, "Accept", "text/rdf+n3");
098        template.sendBodyAndHeader(null, Exchange.ACCEPT_CONTENT_TYPE, "text/rdf+n3");
099
100        resultEndpoint.assertIsSatisfied();
101    }
102
103
104    @Test
105    public void testContentTypeDefault() throws InterruptedException {
106        resultEndpoint.expectedHeaderReceived("Content-Type", DEFAULT_CONTENT_TYPE);
107        resultEndpoint.expectedMessageCount(1);
108
109        template.sendBody(null);
110
111        resultEndpoint.assertIsSatisfied();
112    }
113
114
115    @Override
116    protected RouteBuilder createRouteBuilder() {
117        return new RouteBuilder() {
118            @Override
119            public void configure() {
120                final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri();
121
122                from("direct:start")
123                    .to(fcrepo_uri)
124                    .to("mock:result");
125            }
126        };
127    }
128}