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