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.reindexing; 019 020import java.io.IOException; 021import java.util.HashMap; 022import java.util.Map; 023 024import org.apache.camel.EndpointInject; 025import org.apache.camel.Exchange; 026import org.apache.camel.Produce; 027import org.apache.camel.ProducerTemplate; 028import org.apache.camel.builder.RouteBuilder; 029import org.apache.camel.component.mock.MockEndpoint; 030import org.apache.camel.test.junit4.CamelTestSupport; 031import org.fcrepo.camel.FcrepoHeaders; 032 033import org.junit.Test; 034 035/** 036 * Test the route workflow. 037 * 038 * @author Aaron Coburn 039 * @since 2015-04-10 040 */ 041public class RestProcessorTest extends CamelTestSupport { 042 043 @EndpointInject(uri = "mock:result") 044 protected MockEndpoint resultEndpoint; 045 046 @Produce(uri = "direct:start") 047 protected ProducerTemplate template; 048 049 @Test 050 public void testRestProcessor() throws Exception { 051 052 resultEndpoint.expectedMessageCount(3); 053 resultEndpoint.expectedHeaderValuesReceivedInAnyOrder(FcrepoHeaders.FCREPO_IDENTIFIER, 054 "/", "/foo/bar", "/foo/bar"); 055 resultEndpoint.message(0).header(ReindexingHeaders.RECIPIENTS).isEqualTo(""); 056 resultEndpoint.message(1).header(ReindexingHeaders.RECIPIENTS).contains("broker:queue:bar"); 057 resultEndpoint.message(1).header(ReindexingHeaders.RECIPIENTS).contains("broker:queue:foo"); 058 resultEndpoint.message(2).header(ReindexingHeaders.RECIPIENTS).isEqualTo(""); 059 060 final Map<String, Object> headers = new HashMap<>(); 061 headers.put(Exchange.HTTP_PATH, "/"); 062 template.sendBodyAndHeaders("", headers); 063 064 headers.clear(); 065 headers.put(Exchange.HTTP_PATH, "/foo/bar"); 066 headers.put(ReindexingHeaders.RECIPIENTS, 067 "broker:queue:foo, broker:queue:bar,\t\nbroker:queue:foo "); 068 template.sendBodyAndHeaders("", headers); 069 070 headers.clear(); 071 headers.put(Exchange.HTTP_PATH, "/foo/bar"); 072 headers.put(ReindexingHeaders.RECIPIENTS, null); 073 template.sendBodyAndHeaders("", headers); 074 075 assertMockEndpointsSatisfied(); 076 } 077 078 @Test 079 public void testRestProcessorWithBody1() throws Exception { 080 final String body = "[\"broker:queue:foo\",\"broker:queue:bar\"]"; 081 082 resultEndpoint.expectedMessageCount(1); 083 resultEndpoint.message(0).header(ReindexingHeaders.RECIPIENTS).contains("broker:queue:bar"); 084 resultEndpoint.message(0).header(ReindexingHeaders.RECIPIENTS).contains("broker:queue:foo"); 085 086 final Map<String, Object> headers = new HashMap<>(); 087 headers.put(Exchange.CONTENT_TYPE, "application/json"); 088 headers.put(Exchange.HTTP_PATH, "/foo/bar"); 089 template.sendBodyAndHeaders(body, headers); 090 091 assertMockEndpointsSatisfied(); 092 } 093 094 @Test 095 public void testRestProcessorWithBody2() throws Exception { 096 final String body = "[\"broker:queue:foo\",\"broker:queue:bar\"]"; 097 098 resultEndpoint.expectedMessageCount(1); 099 resultEndpoint.message(0).header(ReindexingHeaders.RECIPIENTS).contains("broker:queue:bar"); 100 resultEndpoint.message(0).header(ReindexingHeaders.RECIPIENTS).contains("broker:queue:foo"); 101 resultEndpoint.message(0).header(ReindexingHeaders.RECIPIENTS).contains("broker:queue:baz"); 102 103 final Map<String, Object> headers = new HashMap<>(); 104 headers.put(Exchange.HTTP_PATH, "/foo/bar"); 105 headers.put(Exchange.CONTENT_TYPE, "application/json"); 106 headers.put(ReindexingHeaders.RECIPIENTS, "broker:queue:baz"); 107 template.sendBodyAndHeaders(body, headers); 108 109 assertMockEndpointsSatisfied(); 110 } 111 112 @Test 113 public void testRestProcessorWithNullBody() throws Exception { 114 115 resultEndpoint.expectedMessageCount(1); 116 resultEndpoint.message(0).header(ReindexingHeaders.RECIPIENTS).isEqualTo("broker:queue:baz"); 117 118 final Map<String, Object> headers = new HashMap<>(); 119 headers.put(Exchange.CONTENT_TYPE, "application/json"); 120 headers.put(Exchange.HTTP_PATH, "/foo/bar"); 121 headers.put(ReindexingHeaders.RECIPIENTS, "broker:queue:baz"); 122 template.sendBodyAndHeaders(null, headers); 123 124 assertMockEndpointsSatisfied(); 125 } 126 127 @Test 128 public void testRestProcessorWithNoContentType() throws Exception { 129 130 final String body = "[\"broker:queue:foo\",\"broker:queue:bar\"]"; 131 132 resultEndpoint.expectedMessageCount(1); 133 resultEndpoint.message(0).header(ReindexingHeaders.RECIPIENTS).isEqualTo("broker:queue:baz"); 134 135 final Map<String, Object> headers = new HashMap<>(); 136 headers.put(Exchange.HTTP_PATH, "/foo/bar"); 137 headers.put(ReindexingHeaders.RECIPIENTS, "broker:queue:baz"); 138 template.sendBodyAndHeaders(body, headers); 139 140 assertMockEndpointsSatisfied(); 141 } 142 143 @Test 144 public void testRestProcessorWithBadContentType() throws Exception { 145 146 final String body = "[\"broker:queue:foo\",\"broker:queue:bar\"]"; 147 148 resultEndpoint.expectedMessageCount(1); 149 resultEndpoint.message(0).header(ReindexingHeaders.RECIPIENTS).isEqualTo("broker:queue:baz"); 150 151 final Map<String, Object> headers = new HashMap<>(); 152 headers.put(Exchange.CONTENT_TYPE, "application/foo"); 153 headers.put(Exchange.HTTP_PATH, "/foo/bar"); 154 headers.put(ReindexingHeaders.RECIPIENTS, "broker:queue:baz"); 155 template.sendBodyAndHeaders(body, headers); 156 157 assertMockEndpointsSatisfied(); 158 } 159 160 161 @Test 162 public void testRestProcessorWithEmptyBody() throws Exception { 163 164 resultEndpoint.expectedMessageCount(1); 165 resultEndpoint.message(0).header(ReindexingHeaders.RECIPIENTS).isEqualTo("broker:queue:baz"); 166 167 final Map<String, Object> headers = new HashMap<>(); 168 headers.put(Exchange.CONTENT_TYPE, "application/json"); 169 headers.put(Exchange.HTTP_PATH, "/foo/bar"); 170 headers.put(ReindexingHeaders.RECIPIENTS, "broker:queue:baz"); 171 template.sendBodyAndHeaders(" ", headers); 172 173 assertMockEndpointsSatisfied(); 174 } 175 176 177 @Override 178 protected RouteBuilder createRouteBuilder() { 179 return new RouteBuilder() { 180 @Override 181 public void configure() throws IOException { 182 from("direct:start") 183 .process(new RestProcessor()) 184 .to("mock:result"); 185 } 186 }; 187 } 188}