Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Revert "micro-infra-camel module added."
Browse files Browse the repository at this point in the history
This reverts commit 8cf9664.
  • Loading branch information
mchmielarz committed Oct 2, 2014
1 parent af0df13 commit 8552159
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 1 deletion.
20 changes: 20 additions & 0 deletions micro-infra-camel/build.gradle
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.

Copy link
@marcingrzejszczak

marcingrzejszczak Oct 2, 2014

Contributor

Try to annotate with @CompileStatic instead of @TypedChecked

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.

Copy link
@marcingrzejszczak

marcingrzejszczak Oct 2, 2014

Contributor

Let's use Strings:
'Some string'
instead of GStrings
"Some GString"

correlationIdHeader = UUID.randomUUID().toString()
}
correlationIdHeader

This comment has been minimized.

Copy link
@marcingrzejszczak

marcingrzejszczak Oct 2, 2014

Contributor

Let's use explicit returns

}

private void setCorrelationIdHeaderIfMissing(Exchange exchange, String correlationIdHeader) {
def inboundMessage = exchange.getIn()

This comment has been minimized.

Copy link
@marcingrzejszczak

marcingrzejszczak Oct 2, 2014

Contributor

Let's use typing

instead of:

def message = exachange.in

let's use:

Type message = exchange.in
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.

Copy link
@marcingrzejszczak

marcingrzejszczak Oct 2, 2014

Contributor

Maybe we should write how to use it in the README.md

It would be the best to create a WIKI page for that how to migrate with the current solutions.

And btw let's add @CompileStatic everywhere it is possible


@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.

Copy link
@marcingrzejszczak

marcingrzejszczak Oct 2, 2014

Contributor

Private is unnecessary in specs - you can do a one lioner out of it together with @Autowired.


@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.

Copy link
@marcingrzejszczak

marcingrzejszczak Oct 2, 2014

Contributor

You can use @AutoCleanup instead of closing that expicitly:
http://ldaley.com/post/3133819681/spocks-autocleanup-feature

removeRouteDefinitions()
}

@DirtiesContext

This comment has been minimized.

Copy link
@marcingrzejszczak

marcingrzejszczak Oct 2, 2014

Contributor

We don't need to recreate the Spring Context each time. Let's create Spring context once and recreate it each time.

Each time you recreate a Spring context it takes some time to finish - at least a second. We need to limit this time as much as possible. That's why we shouldn't use DirtiesContext

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.

Copy link
@marcingrzejszczak

marcingrzejszczak Oct 2, 2014

Contributor

Could you please remove new liones and add indents


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.

Copy link
@marcingrzejszczak

marcingrzejszczak Oct 2, 2014

Contributor

That's unnecessary

}

}
4 changes: 3 additions & 1 deletion settings.gradle
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'

0 comments on commit 8552159

Please sign in to comment.