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