Skip to content

Commit

Permalink
Shortening route alteration
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Sep 19, 2017
1 parent d2308d9 commit 475a609
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AdviceWithRouteBuilder> conf) throws Exception {
context.getRouteDefinition(routeName).adviceWith(context, new AbbreviatedAdviceWithRouteBuilder(conf));
}

static class AbbreviatedAdviceWithRouteBuilder extends AdviceWithRouteBuilder {

public AbbreviatedAdviceWithRouteBuilder(final UnsafeConsumer<AdviceWithRouteBuilder> conf) throws Exception {
super();
this.configure = conf;
}

private final UnsafeConsumer<AdviceWithRouteBuilder> configure;

@Override
public void configure() throws Exception {
configure.accept(this);
}
}

interface UnsafeConsumer<T> {
void accept(T input) throws Exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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",
"<http://localhost:8080/fcrepo/rest/foo/fcr:metadata>; 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",
"<http://localhost:8080/fcrepo/rest/foo/fcr:metadata>; 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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -129,5 +112,4 @@ public void configure() throws Exception {

assertMockEndpointsSatisfied();
}

}

0 comments on commit 475a609

Please sign in to comment.