From 475a6093078fdaabd20798835e9237fcda4bc956 Mon Sep 17 00:00:00 2001 From: ajs6f Date: Tue, 19 Sep 2017 15:08:08 -0400 Subject: [PATCH] Shortening route alteration --- .../fcrepo/FcrepoIndexerTestFramework.java | 24 ++++++ ...landoraFcrepoIndexerCreateBinaryTests.java | 82 ++++++++----------- 2 files changed, 56 insertions(+), 50 deletions(-) diff --git a/islandora-indexing-fcrepo/src/test/java/ca/islandora/alpaca/indexing/fcrepo/FcrepoIndexerTestFramework.java b/islandora-indexing-fcrepo/src/test/java/ca/islandora/alpaca/indexing/fcrepo/FcrepoIndexerTestFramework.java index dead0752..96d72f55 100644 --- a/islandora-indexing-fcrepo/src/test/java/ca/islandora/alpaca/indexing/fcrepo/FcrepoIndexerTestFramework.java +++ b/islandora-indexing-fcrepo/src/test/java/ca/islandora/alpaca/indexing/fcrepo/FcrepoIndexerTestFramework.java @@ -28,6 +28,7 @@ import org.apache.camel.EndpointInject; import org.apache.camel.Produce; import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.AdviceWithRouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.blueprint.CamelBlueprintTestSupport; import org.apache.commons.io.IOUtils; @@ -85,4 +86,27 @@ protected static Header h(final String key, final Object value) { protected String loadResource(final String resourceName) throws IOException { return IOUtils.toString(loadResourceAsStream(resourceName), UTF_8); } + + protected void alter(final String routeName, final UnsafeConsumer conf) throws Exception { + context.getRouteDefinition(routeName).adviceWith(context, new AbbreviatedAdviceWithRouteBuilder(conf)); + } + + static class AbbreviatedAdviceWithRouteBuilder extends AdviceWithRouteBuilder { + + public AbbreviatedAdviceWithRouteBuilder(final UnsafeConsumer conf) throws Exception { + super(); + this.configure = conf; + } + + private final UnsafeConsumer configure; + + @Override + public void configure() throws Exception { + configure.accept(this); + } + } + + interface UnsafeConsumer { + void accept(T input) throws Exception; + } } diff --git a/islandora-indexing-fcrepo/src/test/java/ca/islandora/alpaca/indexing/fcrepo/IslandoraFcrepoIndexerCreateBinaryTests.java b/islandora-indexing-fcrepo/src/test/java/ca/islandora/alpaca/indexing/fcrepo/IslandoraFcrepoIndexerCreateBinaryTests.java index bb21fa72..04a4baec 100644 --- a/islandora-indexing-fcrepo/src/test/java/ca/islandora/alpaca/indexing/fcrepo/IslandoraFcrepoIndexerCreateBinaryTests.java +++ b/islandora-indexing-fcrepo/src/test/java/ca/islandora/alpaca/indexing/fcrepo/IslandoraFcrepoIndexerCreateBinaryTests.java @@ -17,30 +17,22 @@ */ package ca.islandora.alpaca.indexing.fcrepo; -import org.apache.camel.builder.AdviceWithRouteBuilder; import org.apache.camel.http.common.HttpOperationFailedException; import org.junit.Test; -import org.junit.runner.RunWith; - -import com.googlecode.junittoolbox.ParallelRunner; /** * @author ajs6f * */ -@RunWith(ParallelRunner.class) public class IslandoraFcrepoIndexerCreateBinaryTests extends FcrepoIndexerTestFramework { private final static String routeName = "IslandoraFcrepoIndexerCreateBinary"; @Test public void testCreateBinaryFiltersBadEvents() throws Exception { - context.getRouteDefinition(routeName).adviceWith(context, new AdviceWithRouteBuilder() { - @Override - public void configure() throws Exception { - replaceFromWith("direct:start"); - mockEndpoints(); - } + alter(routeName, a-> { + a.replaceFromWith("direct:start"); + a.mockEndpoints(); }); getMockEndpoint("mock:result").expectedMessageCount(0); @@ -55,23 +47,20 @@ public void configure() throws Exception { @Test public void testCreateBinaryRoutesToMapOnSuccess() throws Exception { - context.getRouteDefinition(routeName).adviceWith(context, new AdviceWithRouteBuilder() { - @Override - public void configure() throws Exception { - replaceFromWith("direct:start"); - - // Mock milliner http endpoint and return canned response - interceptSendToEndpoint("http://*").skipSendToOriginalEndpoint().process(exchange -> { - exchange.getIn().removeHeaders("*"); - exchange.getIn().setHeader("Location", "http://localhost:8080/fcrepo/rest/foo"); - exchange.getIn().setHeader("Link", - "; rel=\"describedby\""); - exchange.getIn().setBody("http://localhost:8080/fcrepo/rest/foo"); - }); - - // Mock and skip final endpoint - mockEndpointsAndSkip("seda:islandora-indexing-fcrepo-map-binary"); - } + alter(routeName, a -> { + a.replaceFromWith("direct:start"); + + // Mock milliner http endpoint and return canned response + a.interceptSendToEndpoint("http://*").skipSendToOriginalEndpoint().process(exchange -> { + exchange.getIn().removeHeaders("*"); + exchange.getIn().setHeader("Location", "http://localhost:8080/fcrepo/rest/foo"); + exchange.getIn().setHeader("Link", + "; rel=\"describedby\""); + exchange.getIn().setBody("http://localhost:8080/fcrepo/rest/foo"); + }); + + // Mock and skip final endpoint + a.mockEndpointsAndSkip("seda:islandora-indexing-fcrepo-map-binary"); }); getMockEndpoint("mock:result").expectedMessageCount(0); @@ -85,18 +74,15 @@ public void configure() throws Exception { @Test public void testCreateBinaryPassthruOn409() throws Exception { - context.getRouteDefinition(routeName).adviceWith(context, new AdviceWithRouteBuilder() { - @Override - public void configure() throws Exception { - replaceFromWith("direct:start"); - mockEndpoints(); - - // Jam in a 409 right at the beginning - weaveAddFirst().process(exchange -> { - throw new HttpOperationFailedException("http://test.com", 409, "Conflict", null, null, - "Error message"); - }); - } + alter(routeName, a -> { + a.replaceFromWith("direct:start"); + a.mockEndpoints(); + + // Jam in a 409 right at the beginning + a.weaveAddFirst().process(exchange -> { + throw new HttpOperationFailedException("http://test.com", 409, "Conflict", null, null, + "Error message"); + }); }); getMockEndpoint("mock:result").expectedMessageCount(1); @@ -110,15 +96,12 @@ public void configure() throws Exception { @Test public void testCreateBinaryRoutesToDLQOnOtherExceptions() throws Exception { - context.getRouteDefinition(routeName).adviceWith(context, new AdviceWithRouteBuilder() { - @Override - public void configure() throws Exception { - replaceFromWith("direct:start"); - mockEndpoints(); - - // Jam in some other type of exception right at the beginning - weaveAddFirst().throwException(Exception.class, "Error Message"); - } + alter(routeName, a -> { + a.replaceFromWith("direct:start"); + a.mockEndpoints(); + + // Jam in some other type of exception right at the beginning + a.weaveAddFirst().throwException(Exception.class, "Error Message"); }); getMockEndpoint("mock:result").expectedMessageCount(0); @@ -129,5 +112,4 @@ public void configure() throws Exception { assertMockEndpointsSatisfied(); } - }