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 FcrepoContainerPatchIT 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:title") 053 protected MockEndpoint titleEndpoint; 054 055 @EndpointInject(uri = "mock:verifyGone") 056 protected MockEndpoint goneEndpoint; 057 058 @EndpointInject(uri = "mock:operation") 059 protected MockEndpoint operationEndpoint; 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 testGetContainer() throws InterruptedException { 069 // Assertions 070 createdEndpoint.expectedMessageCount(1); 071 createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201); 072 073 operationEndpoint.expectedMessageCount(3); 074 operationEndpoint.expectedBodiesReceived(null, null, null); 075 operationEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204); 076 077 titleEndpoint.expectedMessageCount(3); 078 titleEndpoint.expectedBodiesReceivedInAnyOrder( 079 "some title & other", "some title & other", "some other title"); 080 titleEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml"); 081 titleEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200); 082 083 filteredEndpoint.expectedMessageCount(2); 084 filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml"); 085 filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200); 086 087 goneEndpoint.expectedMessageCount(1); 088 goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410); 089 090 notFoundEndpoint.expectedMessageCount(1); 091 notFoundEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 404); 092 093 final Map<String, Object> headers = new HashMap<>(); 094 headers.put(Exchange.HTTP_METHOD, "POST"); 095 headers.put(Exchange.CONTENT_TYPE, "text/turtle"); 096 097 final String fullPath = template.requestBodyAndHeaders( 098 "direct:create", 099 FcrepoTestUtils.getTurtleDocument(), 100 headers, String.class); 101 102 // Strip off the baseUrl to get the resource path 103 final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), ""); 104 105 template.sendBodyAndHeader("direct:title", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier); 106 107 final String patchDoc = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n\n" + 108 "INSERT { <> dc:title \"some other title\" } WHERE {}"; 109 110 template.sendBodyAndHeader("direct:patch", patchDoc, FcrepoHeaders.FCREPO_IDENTIFIER, identifier); 111 112 template.sendBodyAndHeader("direct:title", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier); 113 114 115 template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier); 116 117 // Confirm that assertions passed 118 createdEndpoint.assertIsSatisfied(); 119 filteredEndpoint.assertIsSatisfied(); 120 titleEndpoint.assertIsSatisfied(); 121 goneEndpoint.assertIsSatisfied(); 122 notFoundEndpoint.assertIsSatisfied(); 123 operationEndpoint.assertIsSatisfied(); 124 125 // Check deleted container 126 for (Exchange exchange : goneEndpoint.getExchanges()) { 127 Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html")); 128 Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Gone")); 129 } 130 131 for (Exchange exchange : notFoundEndpoint.getExchanges()) { 132 Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html")); 133 Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Not Found")); 134 } 135 } 136 137 @Override 138 protected RouteBuilder createRouteBuilder() { 139 return new RouteBuilder() { 140 @Override 141 public void configure() { 142 143 final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri(); 144 145 final Namespaces ns = new Namespaces("rdf", RdfNamespaces.RDF); 146 ns.add("dc", "http://purl.org/dc/elements/1.1/"); 147 148 from("direct:create") 149 .to(fcrepo_uri) 150 .to("mock:created"); 151 152 from("direct:patch") 153 .setHeader(Exchange.HTTP_METHOD, constant("PATCH")) 154 .to(fcrepo_uri) 155 .to("mock:operation"); 156 157 from("direct:title") 158 .to(fcrepo_uri) 159 .convertBodyTo(org.w3c.dom.Document.class) 160 .filter().xpath( 161 "/rdf:RDF/rdf:Description/rdf:type" + 162 "[@rdf:resource='" + RdfNamespaces.REPOSITORY + "Container']", ns) 163 .to("mock:filter") 164 .split().xpath( 165 "/rdf:RDF/rdf:Description/dc:title/text()", ns) 166 .to("mock:title"); 167 168 from("direct:delete") 169 .setHeader(Exchange.HTTP_METHOD, constant("DELETE")) 170 .to(fcrepo_uri) 171 .to("mock:operation") 172 .setHeader(Exchange.HTTP_METHOD, constant("GET")) 173 .to(fcrepo_uri + "?throwExceptionOnFailure=false") 174 .to("mock:verifyGone") 175 .setHeader(Exchange.HTTP_METHOD, constant("DELETE")) 176 .to(fcrepo_uri + "?tombstone=true") 177 .to("mock:operation") 178 .setHeader(Exchange.HTTP_METHOD, constant("GET")) 179 .to(fcrepo_uri + "?throwExceptionOnFailure=false") 180 .to("mock:verifyNotFound"); 181 } 182 }; 183 } 184}