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 java.util.HashMap; 019import java.util.Map; 020 021import org.apache.camel.EndpointInject; 022import org.apache.camel.Exchange; 023import org.apache.camel.Produce; 024import org.apache.camel.ProducerTemplate; 025import org.apache.camel.builder.RouteBuilder; 026import org.apache.camel.builder.xml.Namespaces; 027import org.apache.camel.component.mock.MockEndpoint; 028import org.apache.camel.test.junit4.CamelTestSupport; 029import org.fcrepo.camel.FcrepoHeaders; 030import org.fcrepo.camel.RdfNamespaces; 031import org.junit.Assert; 032import org.junit.Test; 033import org.junit.runner.RunWith; 034import org.springframework.test.context.ContextConfiguration; 035import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 036 037/** 038 * Test adding a non-RDF resource 039 * @author Aaron Coburn 040 * @since November 7, 2014 041 */ 042@RunWith(SpringJUnit4ClassRunner.class) 043@ContextConfiguration({"/spring-test/test-container.xml"}) 044public class FcrepoBinaryGetIT extends CamelTestSupport { 045 046 @EndpointInject(uri = "mock:created") 047 protected MockEndpoint createdEndpoint; 048 049 @EndpointInject(uri = "mock:filter") 050 protected MockEndpoint filteredEndpoint; 051 052 @EndpointInject(uri = "mock:binary") 053 protected MockEndpoint binaryEndpoint; 054 055 @EndpointInject(uri = "mock:verifyGone") 056 protected MockEndpoint goneEndpoint; 057 058 @EndpointInject(uri = "mock:deleted") 059 protected MockEndpoint deletedEndpoint; 060 061 @EndpointInject(uri = "mock:fixity") 062 protected MockEndpoint fixityEndpoint; 063 064 @EndpointInject(uri = "mock:verifyNotFound") 065 protected MockEndpoint notFoundEndpoint; 066 067 @Produce(uri = "direct:filter") 068 protected ProducerTemplate template; 069 070 @Test 071 public void testGetBinary() throws InterruptedException { 072 // Assertions 073 createdEndpoint.expectedMessageCount(2); 074 createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201); 075 076 binaryEndpoint.expectedBodiesReceived(FcrepoTestUtils.getTextDocument()); 077 binaryEndpoint.expectedMessageCount(1); 078 binaryEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "text/plain"); 079 binaryEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200); 080 081 filteredEndpoint.expectedMessageCount(1); 082 filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml"); 083 filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200); 084 085 deletedEndpoint.expectedMessageCount(4); 086 deletedEndpoint.expectedBodiesReceived(null, null, null, null); 087 deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204); 088 089 goneEndpoint.expectedMessageCount(2); 090 goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410); 091 092 fixityEndpoint.expectedMessageCount(3); 093 fixityEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200); 094 095 notFoundEndpoint.expectedMessageCount(2); 096 notFoundEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 404); 097 098 final String binary = "/file"; 099 final Map<String, Object> headers = new HashMap<>(); 100 headers.put(Exchange.HTTP_METHOD, "POST"); 101 headers.put(Exchange.CONTENT_TYPE, "text/turtle"); 102 103 final String fullPath = template.requestBodyAndHeaders( 104 "direct:create", 105 FcrepoTestUtils.getTurtleDocument(), 106 headers, String.class); 107 108 // Strip off the baseUrl to get the resource path 109 final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), ""); 110 111 headers.clear(); 112 headers.put(Exchange.HTTP_METHOD, "PUT"); 113 headers.put(Exchange.CONTENT_TYPE, "text/plain"); 114 headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary); 115 116 template.sendBodyAndHeaders("direct:create", FcrepoTestUtils.getTextDocument(), headers); 117 118 template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier); 119 template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary); 120 121 template.sendBodyAndHeader("direct:getFixity", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary); 122 123 template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary); 124 template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier); 125 126 127 // Confirm that assertions passed 128 createdEndpoint.assertIsSatisfied(); 129 filteredEndpoint.assertIsSatisfied(); 130 binaryEndpoint.assertIsSatisfied(); 131 goneEndpoint.assertIsSatisfied(); 132 notFoundEndpoint.assertIsSatisfied(); 133 deletedEndpoint.assertIsSatisfied(); 134 fixityEndpoint.assertIsSatisfied(); 135 136 // Additional assertions 137 // skip first message, as we've already extracted the body 138 Assert.assertEquals(FcrepoTestUtils.getFcrepoBaseUrl() + identifier + binary, 139 createdEndpoint.getExchanges().get(1).getIn().getBody(String.class)); 140 141 // Check deleted container 142 for (Exchange exchange : goneEndpoint.getExchanges()) { 143 Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html")); 144 Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Gone")); 145 } 146 147 for (Exchange exchange : notFoundEndpoint.getExchanges()) { 148 Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html")); 149 Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Not Found")); 150 } 151 } 152 153 @Override 154 protected RouteBuilder createRouteBuilder() { 155 return new RouteBuilder() { 156 @Override 157 public void configure() { 158 159 final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri(); 160 161 final Namespaces ns = new Namespaces("rdf", RdfNamespaces.RDF); 162 ns.add("premis", RdfNamespaces.PREMIS); 163 ns.add("fedora", RdfNamespaces.REPOSITORY); 164 165 from("direct:create") 166 .to(fcrepo_uri) 167 .to("mock:created"); 168 169 from("direct:get") 170 .to(fcrepo_uri) 171 .filter().xpath( 172 "/rdf:RDF/rdf:Description/rdf:type" + 173 "[@rdf:resource='" + RdfNamespaces.REPOSITORY + "Binary']", ns) 174 .to("mock:filter") 175 .to(fcrepo_uri + "?metadata=false") 176 .to("mock:binary"); 177 178 from("direct:getFixity") 179 .to(fcrepo_uri + "?fixity=true") 180 .filter().xpath( 181 "/rdf:RDF/rdf:Description/rdf:type" + 182 "[@rdf:resource='" + RdfNamespaces.PREMIS + "Fixity']", ns) 183 .to("mock:fixity") 184 .filter().xpath( 185 "/rdf:RDF/rdf:Description/premis:hasMessageDigest" + 186 "[@rdf:resource='urn:sha1:12f68888e3beff267deae42ea86058c9c0565e36']", ns) 187 .to("mock:fixity") 188 .filter().xpath( 189 "/rdf:RDF/rdf:Description/premis:hasEventOutcome" + 190 "[text()='SUCCESS']", ns) 191 .to("mock:fixity"); 192 193 from("direct:delete") 194 .setHeader(Exchange.HTTP_METHOD, constant("DELETE")) 195 .to(fcrepo_uri) 196 .to("mock:deleted") 197 .setHeader(Exchange.HTTP_METHOD, constant("GET")) 198 .to(fcrepo_uri + "?throwExceptionOnFailure=false") 199 .to("mock:verifyGone") 200 .setHeader(Exchange.HTTP_METHOD, constant("DELETE")) 201 .to(fcrepo_uri + "?tombstone=true") 202 .to("mock:deleted") 203 .setHeader(Exchange.HTTP_METHOD, constant("GET")) 204 .to(fcrepo_uri + "?throwExceptionOnFailure=false") 205 .to("mock:verifyNotFound"); 206 } 207 }; 208 } 209}