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.audit.triplestore.integration;
007
008import org.apache.camel.CamelContext;
009import org.apache.camel.Produce;
010import org.apache.camel.ProducerTemplate;
011import org.apache.camel.builder.AdviceWith;
012import org.apache.camel.component.mock.MockEndpoint;
013import org.apache.camel.model.ModelCamelContext;
014import org.apache.camel.spring.javaconfig.CamelConfiguration;
015import org.apache.jena.atlas.web.AuthScheme;
016import org.apache.jena.fuseki.main.FusekiServer;
017import org.apache.jena.query.Dataset;
018import org.apache.jena.query.DatasetFactory;
019import org.junit.After;
020import org.junit.Before;
021import org.junit.BeforeClass;
022import org.junit.Test;
023import org.junit.runner.RunWith;
024import org.slf4j.Logger;
025import org.springframework.beans.factory.annotation.Autowired;
026import org.springframework.context.annotation.ComponentScan;
027import org.springframework.context.annotation.Configuration;
028import org.springframework.test.annotation.DirtiesContext;
029import org.springframework.test.context.ContextConfiguration;
030import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
031import org.springframework.test.context.support.AnnotationConfigContextLoader;
032
033import static java.lang.Integer.parseInt;
034import static org.apache.camel.util.ObjectHelper.loadResourceAsStream;
035import static org.slf4j.LoggerFactory.getLogger;
036
037/**
038 * Test routing against a triple store with basic authentication
039 *
040 * @author Andy Pfister
041 * @since 2015-04-10
042 */
043@RunWith(SpringJUnit4ClassRunner.class)
044@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
045public class RouteAuthTestIT {
046
047    final private Logger logger = getLogger(RouteAuthTestIT.class);
048
049    private static FusekiServer server = null;
050
051    @Produce("direct:start")
052    protected ProducerTemplate template;
053
054    @Autowired
055    private CamelContext camelContext;
056
057    private static final String baseURL = "http://localhost/rest";
058    private static final String auditContainer = "/audit";
059
060    private static final String FUSEKI_PORT = System.getProperty(
061        "fuseki.dynamic.test.port", "8080"
062    );
063
064    @BeforeClass
065    public static void beforeClass() {
066        System.setProperty("audit.input.stream", "seda:foo");
067        System.setProperty("audit.filter.containers", baseURL + auditContainer);
068        System.setProperty("audit.enabled", "true");
069
070        System.setProperty("audit.triplestore.baseUrl", "http://localhost:" + FUSEKI_PORT + "/fuseki/test/update");
071        System.setProperty("audit.triplestore.authUsername", "admin");
072        System.setProperty("audit.triplestore.authPassword", "password");
073    }
074
075    @After
076    public void tearDownFuseki() throws Exception {
077        logger.info("Stopping EmbeddedFusekiServer");
078        server.stop();
079    }
080
081    @Before
082    public void setUpFuseki() throws Exception {
083        logger.info("Starting EmbeddedFusekiServer on port {}", FUSEKI_PORT);
084        final Dataset ds = DatasetFactory.createTxnMem();
085        server = FusekiServer.create()
086            .verbose(true)
087            .port(parseInt(FUSEKI_PORT))
088            .contextPath("/fuseki")
089            .add("/test", ds, true)
090            .passwordFile("src/test/resources/passwd")
091            .auth(AuthScheme.BASIC)
092            .build();
093        server.start();
094    }
095
096    @DirtiesContext
097    @Test
098    public void testBasicAuthFusekiShouldReceiveMessages() throws Exception {
099        final String fusekiEndpoint = "mock:http:localhost:" + FUSEKI_PORT + "/fuseki/test/update";
100        final var context = camelContext.adapt(ModelCamelContext.class);
101
102        AdviceWith.adviceWith(context, "AuditFcrepoRouter", a -> {
103            a.replaceFromWith("direct:start");
104        });
105
106        AdviceWith.adviceWith(context, "AuditEventRouter", a -> {
107            a.mockEndpoints("*");
108        });
109
110        final var fusekiMockEndpoint = MockEndpoint.resolve(camelContext, fusekiEndpoint);
111        fusekiMockEndpoint.expectedMessageCount(2);
112        fusekiMockEndpoint.setAssertPeriod(5000);
113
114        template.sendBody(loadResourceAsStream("event_delete_binary.json"));
115        template.sendBody(loadResourceAsStream("event_delete_resource.json"));
116
117        MockEndpoint.assertIsSatisfied(fusekiMockEndpoint);
118    }
119
120    @Configuration
121    @ComponentScan(basePackages = "org.fcrepo.camel.audit")
122    static class ContextConfig extends CamelConfiguration {
123
124    }
125}