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 an RDF resource 039 * @author Aaron Coburn 040 * @since Dec 26, 2014 041 */ 042@RunWith(SpringJUnit4ClassRunner.class) 043@ContextConfiguration({"/spring-test/test-container.xml"}) 044public class FcrepoContainerHeadIT 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:container") 053 protected MockEndpoint containerEndpoint; 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:verifyNotFound") 062 protected MockEndpoint notFoundEndpoint; 063 064 @Produce(uri = "direct:filter") 065 protected ProducerTemplate template; 066 067 @Test 068 public void testHeadContainer() throws InterruptedException { 069 // Assertions 070 createdEndpoint.expectedMessageCount(2); 071 createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201); 072 073 containerEndpoint.expectedMessageCount(1); 074 containerEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200); 075 076 filteredEndpoint.expectedMessageCount(1); 077 filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml"); 078 filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200); 079 080 deletedEndpoint.expectedMessageCount(4); 081 deletedEndpoint.allMessages().body().equals(null); 082 deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204); 083 084 goneEndpoint.expectedMessageCount(2); 085 goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410); 086 087 notFoundEndpoint.expectedMessageCount(2); 088 notFoundEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 404); 089 090 final Map<String, Object> headers = new HashMap<>(); 091 headers.put(Exchange.HTTP_METHOD, "POST"); 092 headers.put(Exchange.CONTENT_TYPE, "text/turtle"); 093 094 final String fullPath = template.requestBodyAndHeaders( 095 "direct:create", 096 FcrepoTestUtils.getTurtleDocument(), 097 headers, String.class); 098 099 // Strip off the baseUrl to get the resource path 100 final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), ""); 101 final String binary = "/file"; 102 headers.clear(); 103 headers.put(Exchange.HTTP_METHOD, "PUT"); 104 headers.put(Exchange.CONTENT_TYPE, "text/plain"); 105 headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary); 106 107 template.sendBodyAndHeaders("direct:create", FcrepoTestUtils.getTextDocument(), headers); 108 109 template.sendBodyAndHeader("direct:head", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier); 110 template.sendBodyAndHeader("direct:head", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary); 111 112 template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary); 113 template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier); 114 115 // Confirm that assertions passed 116 createdEndpoint.assertIsSatisfied(); 117 filteredEndpoint.assertIsSatisfied(); 118 containerEndpoint.assertIsSatisfied(); 119 goneEndpoint.assertIsSatisfied(); 120 notFoundEndpoint.assertIsSatisfied(); 121 deletedEndpoint.assertIsSatisfied(); 122 123 // skip first message, as we've already extracted the body 124 Assert.assertEquals(FcrepoTestUtils.getFcrepoBaseUrl() + identifier + binary, 125 createdEndpoint.getExchanges().get(1).getIn().getBody(String.class)); 126 127 // Check deleted container 128 for (Exchange exchange : goneEndpoint.getExchanges()) { 129 Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html")); 130 Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Gone")); 131 } 132 133 for (Exchange exchange : notFoundEndpoint.getExchanges()) { 134 Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html")); 135 Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Not Found")); 136 } 137 } 138 139 @Override 140 protected RouteBuilder createRouteBuilder() { 141 return new RouteBuilder() { 142 @Override 143 public void configure() { 144 145 final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri(); 146 147 final Namespaces ns = new Namespaces("rdf", RdfNamespaces.RDF); 148 149 from("direct:create") 150 .to(fcrepo_uri) 151 .to("mock:created"); 152 153 from("direct:head") 154 .to(fcrepo_uri) 155 .filter().xpath( 156 "/rdf:RDF/rdf:Description/rdf:type" + 157 "[@rdf:resource='" + RdfNamespaces.REPOSITORY + "Container']", ns) 158 .to("mock:filter") 159 .setHeader(Exchange.HTTP_METHOD, constant("HEAD")) 160 .to(fcrepo_uri) 161 .to("mock:container"); 162 163 from("direct:delete") 164 .setHeader(Exchange.HTTP_METHOD, constant("DELETE")) 165 .to(fcrepo_uri) 166 .to("mock:deleted") 167 .setHeader(Exchange.HTTP_METHOD, constant("GET")) 168 .to(fcrepo_uri + "?throwExceptionOnFailure=false") 169 .to("mock:verifyGone") 170 .setHeader(Exchange.HTTP_METHOD, constant("DELETE")) 171 .to(fcrepo_uri + "?tombstone=true") 172 .to("mock:deleted") 173 .setHeader(Exchange.HTTP_METHOD, constant("GET")) 174 .to(fcrepo_uri + "?throwExceptionOnFailure=false") 175 .to("mock:verifyNotFound"); 176 } 177 }; 178 } 179}