001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.camel.httpforwarding.integration;
007
008import com.github.tomakehurst.wiremock.WireMockServer;
009import com.github.tomakehurst.wiremock.client.WireMock;
010import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
011import org.apache.camel.CamelContext;
012import org.apache.camel.EndpointInject;
013import org.apache.camel.Produce;
014import org.apache.camel.ProducerTemplate;
015import org.apache.camel.builder.AdviceWith;
016import org.apache.camel.component.mock.MockEndpoint;
017import org.apache.camel.model.ModelCamelContext;
018import org.junit.After;
019import org.junit.Before;
020import org.junit.BeforeClass;
021import org.junit.Test;
022import org.junit.runner.RunWith;
023import org.slf4j.Logger;
024import org.springframework.beans.factory.annotation.Autowired;
025import org.springframework.test.annotation.DirtiesContext;
026import org.springframework.test.context.ContextConfiguration;
027import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
028import org.springframework.test.context.support.AnnotationConfigContextLoader;
029
030import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
031import static com.github.tomakehurst.wiremock.client.WireMock.ok;
032import static com.github.tomakehurst.wiremock.client.WireMock.post;
033import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
034import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
035import static java.lang.Integer.parseInt;
036import static org.slf4j.LoggerFactory.getLogger;
037
038/**
039 * Test the route workflow.
040 *
041 * @author Aaron Coburn
042 * @author Demian Katz
043 */
044@RunWith(SpringJUnit4ClassRunner.class)
045@ContextConfiguration(classes = {RouteUpdateIT.ContextConfig.class}, loader = AnnotationConfigContextLoader.class)
046public class RouteDeleteIT {
047
048    final private Logger logger = getLogger(RouteDeleteIT.class);
049
050    private static final String AS_NS = "https://www.w3.org/ns/activitystreams#";
051
052    private static final String MOCK_ENDPOINT = "/endpoint";
053
054    private static final String MOCKSERVER_PORT = System.getProperty(
055            "mockserver.dynamic.test.port", "8080");
056
057    private static final String FCREPO_PORT = System.getProperty(
058            "fcrepo.dynamic.test.port", "8080");
059
060    private static final String BASIC_AUTH_USERNAME = "fooUser";
061
062    private static final String BASIC_AUTH_PASSWORD = "barPass";
063
064    private String fullPath = "http://localhost:" + FCREPO_PORT + "/fcrepo/rest/" + "fake-identifier";
065
066    @EndpointInject("mock:result")
067    protected MockEndpoint resultEndpoint;
068
069    @Produce("direct:start")
070    protected ProducerTemplate template;
071
072    @Autowired
073    private CamelContext camelContext;
074
075    private WireMockServer mockServer;
076
077    @BeforeClass
078    public static void beforeClass() {
079        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
080        System.setProperty("http.enabled", "true");
081        System.setProperty("http.baseUrl", "http://localhost:" + MOCKSERVER_PORT + MOCK_ENDPOINT);
082        System.setProperty("http.authUsername", BASIC_AUTH_USERNAME);
083        System.setProperty("http.authPassword", BASIC_AUTH_PASSWORD);
084        System.setProperty("fcrepo.baseUrl", "http://localhost:" + FCREPO_PORT + "/fcrepo/rest");
085        System.setProperty("jms.brokerUrl", "tcp://localhost:" + jmsPort);
086        System.setProperty("http.input.stream", "direct:start");
087        System.setProperty("error.maxRedeliveries", "1");
088    }
089
090    @After
091    public void tearDownMockServer() throws Exception {
092        logger.info("Stopping HTTP Server");
093        mockServer.stop();
094    }
095
096    @Before
097    public void setUpMockServer() throws Exception {
098        mockServer = new WireMockServer(WireMockConfiguration.options().port(parseInt(MOCKSERVER_PORT)));
099        mockServer.start();
100    }
101
102    @DirtiesContext
103    @Test
104    public void testDeletedResourceWithEventBody() throws Exception {
105        final var mockServerEndpoint = "mock:http:localhost:" + MOCKSERVER_PORT + MOCK_ENDPOINT;
106        final var idMatcher = WireMock.matchingJsonPath("$.id", equalTo(fullPath));
107        final var typeMatcher = WireMock.matchingJsonPath("$.type", equalTo(AS_NS + "Delete"));
108
109        // have the http server return a 200; also test that basic auth works (for variety)
110        mockServer.stubFor(post(urlEqualTo(MOCK_ENDPOINT))
111            .withBasicAuth(BASIC_AUTH_USERNAME, BASIC_AUTH_PASSWORD)
112            .willReturn(ok()));
113
114        final var context = camelContext.adapt(ModelCamelContext.class);
115
116        AdviceWith.adviceWith(context, "FcrepoHttpRouter", a -> a.mockEndpoints("*"));
117        AdviceWith.adviceWith(context, "FcrepoHttpAddType", a -> a.mockEndpoints("*"));
118        AdviceWith.adviceWith(context, "FcrepoHttpSend", a -> a.mockEndpoints("*"));
119
120        final var mockServerMockEndpoint = MockEndpoint.resolve(camelContext, mockServerEndpoint);
121        mockServerMockEndpoint.expectedMessageCount(1);
122        final var updateEndpoint = MockEndpoint.resolve(camelContext, "mock://direct:send.to.http");
123        updateEndpoint.expectedMessageCount(1);
124
125        logger.info("fullPath={}", fullPath);
126        template.sendBodyAndHeader(
127            "direct:start", TestUtils.getEvent(fullPath, AS_NS + "Delete"), "org.fcrepo.jms.eventtype", AS_NS + "Delete"
128        );
129
130        mockServer.verify(1, postRequestedFor(urlEqualTo(MOCK_ENDPOINT))
131            .withRequestBody(idMatcher.and(typeMatcher)));
132        MockEndpoint.assertIsSatisfied(mockServerMockEndpoint, updateEndpoint);
133    }
134}