001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.camel.audit.triplestore;
019
020import static org.apache.camel.util.ObjectHelper.loadResourceAsStream;
021import static org.fcrepo.camel.audit.triplestore.AuditSparqlProcessor.AUDIT;
022import static org.fcrepo.camel.audit.triplestore.AuditSparqlProcessor.PREMIS;
023
024import java.util.Properties;
025
026import org.apache.camel.EndpointInject;
027import org.apache.camel.Exchange;
028import org.apache.camel.Produce;
029import org.apache.camel.ProducerTemplate;
030import org.apache.camel.builder.AdviceWithRouteBuilder;
031import org.apache.camel.component.mock.MockEndpoint;
032import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
033
034import org.junit.Test;
035
036/**
037 * Test the route workflow.
038 *
039 * @author escowles
040 * @author Aaron Coburn
041 * @since 2015-04-10
042 */
043public class RouteTest extends CamelBlueprintTestSupport {
044
045    @EndpointInject(uri = "mock:result")
046    protected MockEndpoint resultEndpoint;
047
048    @Produce(uri = "direct:start")
049    protected ProducerTemplate template;
050
051    private static final String baseURL = "http://localhost/rest";
052    private static final String fileID = "/file1";
053    private static final String auditContainer = "/audit";
054    private static final String eventDate = "2015-04-06T22:45:20Z";
055    private static final String userID = "fedo raAdmin";
056    private static final String userAgent = "CLAW client/1.0";
057
058    @Override
059    protected String getBlueprintDescriptor() {
060        return "/OSGI-INF/blueprint/blueprint-test.xml";
061    }
062
063    @Override
064    protected Properties useOverridePropertiesWithPropertiesComponent() {
065         final Properties props = new Properties();
066         props.put("filter.containers", baseURL + auditContainer);
067         props.put("input.stream", "seda:foo");
068         return props;
069    }
070
071    @Test
072    public void testWithoutJms() throws Exception {
073
074        context.getRouteDefinition("AuditFcrepoRouter").adviceWith(context, new AdviceWithRouteBuilder() {
075            @Override
076            public void configure() throws Exception {
077                replaceFromWith("direct:start");
078            }
079        });
080
081        context.getRouteDefinition("AuditEventRouter").adviceWith(context, new AdviceWithRouteBuilder() {
082            @Override
083            public void configure() throws Exception {
084                mockEndpointsAndSkip("http*");
085                weaveAddLast().to("mock:result");
086            }
087        });
088
089        resultEndpoint.expectedMessageCount(2);
090        resultEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded");
091        resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST");
092        resultEndpoint.expectedHeaderReceived(AuditHeaders.EVENT_BASE_URI, "http://example.com/event");
093
094        template.sendBody(loadResourceAsStream("event_delete_binary.json"));
095        template.sendBody(loadResourceAsStream("event_delete_resource.json"));
096        template.sendBody(loadResourceAsStream("event_audit_resource.json"));
097        template.sendBody(loadResourceAsStream("event_audit_update.json"));
098
099        assertMockEndpointsSatisfied();
100        final String body = (String)resultEndpoint.assertExchangeReceived(0).getIn().getBody();
101        assertTrue("Event type not found!",
102            body.contains("<" + PREMIS + "hasEventType> <" + AUDIT + "contentRemoval>"));
103        assertTrue("Object link not found!",
104            body.contains("<" + PREMIS + "hasEventRelatedObject> <" + baseURL + fileID + ">"));
105    }
106}