-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This reverts commit 8cf9664.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
description = 'Utility controlling presence of correlationId in message flow.' | ||
|
||
ext { | ||
camelVersion = '2.10.3' | ||
} | ||
|
||
dependencies { | ||
compile project(':micro-infra-spring-base') | ||
compile "org.apache.camel:camel-core:$camelVersion" | ||
|
||
testCompile("org.spockframework:spock-core:$spockVersion") { | ||
exclude group: 'org.codehaus.groovy', module: 'groovy-all' | ||
} | ||
testRuntime("org.spockframework:spock-spring:$spockVersion") { | ||
exclude group: 'org.spockframework', module: 'spock-core' | ||
} | ||
|
||
testCompile "org.apache.camel:camel-spring-javaconfig:$camelVersion" | ||
testCompile "org.apache.camel:camel-test-spring:$camelVersion" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.ofg.infrastructure.camel | ||
|
||
import static com.ofg.infrastructure.web.filter.correlationid.CorrelationIdHolder.CORRELATION_ID_HEADER | ||
import com.ofg.infrastructure.web.filter.correlationid.CorrelationIdUpdater | ||
import groovy.transform.TypeChecked | ||
import groovy.util.logging.Slf4j | ||
import org.apache.camel.Exchange | ||
import org.apache.camel.Processor | ||
|
||
@Slf4j | ||
@TypeChecked | ||
This comment has been minimized.
Sorry, something went wrong. |
||
class CorrelationIdInterceptor implements Processor { | ||
|
||
@Override | ||
void process(Exchange exchange) throws Exception { | ||
String correlationIdHeader = getCorrelationId(exchange) | ||
CorrelationIdUpdater.updateCorrelationId(correlationIdHeader) | ||
setCorrelationIdHeaderIfMissing(exchange, correlationIdHeader) | ||
} | ||
|
||
private String getCorrelationId(Exchange exchange) { | ||
String correlationIdHeader = exchange.getIn().getHeader(CORRELATION_ID_HEADER) | ||
if (!correlationIdHeader) { | ||
log.debug("No correlationId has been set in request inbound message. Creating new one.") | ||
This comment has been minimized.
Sorry, something went wrong.
marcingrzejszczak
Contributor
|
||
correlationIdHeader = UUID.randomUUID().toString() | ||
} | ||
correlationIdHeader | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
private void setCorrelationIdHeaderIfMissing(Exchange exchange, String correlationIdHeader) { | ||
def inboundMessage = exchange.getIn() | ||
This comment has been minimized.
Sorry, something went wrong.
marcingrzejszczak
Contributor
|
||
if (!inboundMessage.headers.containsKey(CORRELATION_ID_HEADER)) { | ||
log.debug("Setting correlationId [$correlationIdHeader] in header of inbound message") | ||
inboundMessage.setHeader(CORRELATION_ID_HEADER, correlationIdHeader) | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.ofg.infrastructure.camel | ||
|
||
import org.apache.camel.builder.RouteBuilder | ||
|
||
class CorrelationIdRouteBuilder extends RouteBuilder { | ||
This comment has been minimized.
Sorry, something went wrong.
marcingrzejszczak
Contributor
|
||
|
||
@Override | ||
void configure() throws Exception { | ||
def correlationIdInterceptor = new CorrelationIdInterceptor() | ||
interceptFrom().process(correlationIdInterceptor) | ||
interceptSendToEndpoint("*").process(correlationIdInterceptor) | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.ofg.infrastructure.camel | ||
|
||
import com.ofg.infrastructure.web.filter.correlationid.CorrelationIdHolder | ||
import org.springframework.test.annotation.DirtiesContext | ||
|
||
import static com.ofg.infrastructure.web.filter.correlationid.CorrelationIdHolder.* | ||
import groovy.util.logging.Slf4j | ||
import org.apache.camel.ProducerTemplate | ||
import org.apache.camel.builder.RouteBuilder | ||
import org.apache.camel.component.mock.MockEndpoint | ||
import org.apache.camel.impl.DefaultProducerTemplate | ||
import org.apache.camel.impl.InterceptSendToEndpoint | ||
import org.apache.camel.model.ModelCamelContext | ||
import org.apache.camel.spring.javaconfig.test.JavaConfigContextLoader | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.test.context.ContextConfiguration | ||
import spock.lang.Specification | ||
|
||
@ContextConfiguration( | ||
locations = "com.ofg.infrastructure.camel.CamelContextConfig", | ||
loader = JavaConfigContextLoader.class) | ||
@Slf4j | ||
class AcceptanceSpec extends Specification { | ||
|
||
@Autowired | ||
private ModelCamelContext camelContext | ||
This comment has been minimized.
Sorry, something went wrong.
marcingrzejszczak
Contributor
|
||
|
||
@Autowired | ||
private RouteBuilder routeBuilder | ||
|
||
private MockEndpoint resultEndpoint; | ||
private ProducerTemplate template; | ||
|
||
def setup() { | ||
camelContext.addRoutes(routeBuilder) | ||
resultEndpoint = ((InterceptSendToEndpoint)camelContext.getEndpoint("mock:result")).getDelegate() | ||
template = new DefaultProducerTemplate( | ||
camelContext, | ||
camelContext.getEndpoint("direct:start")) | ||
template.start() | ||
} | ||
|
||
def cleanup() { | ||
template?.stop() | ||
This comment has been minimized.
Sorry, something went wrong.
marcingrzejszczak
Contributor
|
||
removeRouteDefinitions() | ||
} | ||
|
||
@DirtiesContext | ||
This comment has been minimized.
Sorry, something went wrong.
marcingrzejszczak
Contributor
|
||
def "should set correlationId from header of input message"() { | ||
given: | ||
String correlationIdValue = UUID.randomUUID().toString(); | ||
|
||
when: | ||
template.sendBodyAndHeader("<message/>", CORRELATION_ID_HEADER, correlationIdValue); | ||
|
||
then: | ||
CorrelationIdHolder.get() == correlationIdValue | ||
} | ||
|
||
@DirtiesContext | ||
def "should set new correlationId if header in input message is empty"() { | ||
when: | ||
template.sendBody("<message/>"); | ||
|
||
then: | ||
CorrelationIdHolder.get() != null | ||
} | ||
|
||
@DirtiesContext | ||
def "should set correlationId in output message when it is missing on the input"() { | ||
when: | ||
template.sendBody("<message/>"); | ||
|
||
then: | ||
resultEndpoint.message(0).header(CORRELATION_ID_HEADER).isNotNull() | ||
} | ||
|
||
@DirtiesContext | ||
def "should copy correlationId from header of input message to the output"() { | ||
given: | ||
String correlationIdValue = UUID.randomUUID().toString(); | ||
|
||
when: | ||
template.sendBodyAndHeader("<message/>", CORRELATION_ID_HEADER, correlationIdValue); | ||
|
||
then: | ||
resultEndpoint.message(0).header(CORRELATION_ID_HEADER).isEqualTo(correlationIdValue) | ||
} | ||
|
||
private void removeRouteDefinitions() { | ||
def routeDefinitions = camelContext.getRouteDefinitions() | ||
camelContext.removeRouteDefinitions(routeDefinitions) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.ofg.infrastructure.camel | ||
|
||
import org.apache.camel.builder.RouteBuilder | ||
import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
class CamelContextConfig extends SingleRouteCamelConfiguration { | ||
|
||
@Bean | ||
@Override | ||
RouteBuilder route() { | ||
return new TestRouteBuilder() | ||
} | ||
|
||
private class TestRouteBuilder extends CorrelationIdRouteBuilder { | ||
public void configure() { | ||
super.configure() | ||
from("direct:start").to("mock:result").routeId("route-1"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.ofg.infrastructure.camel | ||
|
||
import com.ofg.infrastructure.web.filter.correlationid.CorrelationIdHolder | ||
import org.apache.camel.CamelContext | ||
import org.apache.camel.Exchange | ||
import org.apache.camel.impl.DefaultCamelContext | ||
import org.apache.camel.impl.DefaultExchange | ||
import spock.lang.Specification | ||
|
||
class CorrelationIdInterceptorSpec extends Specification { | ||
|
||
def "should set new correlationId header in request inbound message if missing"() { | ||
given: | ||
Exchange exchange = defaultExchange() | ||
|
||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
when: | ||
new CorrelationIdInterceptor().process(exchange) | ||
|
||
then: | ||
exchange.getIn().getHeader(CorrelationIdHolder.CORRELATION_ID_HEADER) != null | ||
} | ||
|
||
def "should set correlationId in holder from header of inbound message"() { | ||
given: | ||
Exchange exchange = defaultExchange() | ||
def correlationIdValue = UUID.randomUUID().toString() | ||
exchange.getIn().setHeader(CorrelationIdHolder.CORRELATION_ID_HEADER, correlationIdValue) | ||
|
||
when: | ||
new CorrelationIdInterceptor().process(exchange) | ||
|
||
then: | ||
CorrelationIdHolder.get() == correlationIdValue | ||
} | ||
|
||
Exchange defaultExchange() { | ||
CamelContext context = new DefaultCamelContext() | ||
return new DefaultExchange(context) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ofg.infrastructure.camel | ||
|
||
import org.apache.camel.model.InterceptFromDefinition | ||
import spock.lang.Specification | ||
|
||
class CorrelationIdRouteBuilderSpec extends Specification { | ||
|
||
def "builder defines correlationId interception on inbound messages"() { | ||
given: | ||
def builder = new CorrelationIdRouteBuilder() | ||
|
||
when: | ||
builder.configure() | ||
|
||
then: | ||
List<InterceptFromDefinition> interceptFroms = builder.getRouteCollection().interceptFroms | ||
interceptFroms.each {it.processRef()} | ||
|
||
true | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
include 'micro-infra-spring-base' | ||
include 'micro-infra-spring-reactor' | ||
include 'micro-infra-camel' | ||
include ':swagger:micro-infra-spring-swagger' | ||
include ':swagger:micro-infra-spring-swagger-ui' | ||
include 'micro-infra-spring' | ||
|
||
rootProject.name = 'micro-infra-spring-root' | ||
rootProject.name = 'micro-infra-spring-root' | ||
|
Try to annotate with @CompileStatic instead of @TypedChecked