Skip to content

Commit

Permalink
improved testability
Browse files Browse the repository at this point in the history
  • Loading branch information
mcweba committed Nov 29, 2016
1 parent d0bb20d commit 9decbd7
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 82 deletions.
21 changes: 15 additions & 6 deletions src/main/java/org/swisspush/logtransformer/LogTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import org.swisspush.logtransformer.logger.DefaultLogTransformLogger;
import org.swisspush.logtransformer.logger.LogTransformLogger;
import org.swisspush.logtransformer.strategy.TransformStrategy;
import org.swisspush.logtransformer.strategy.DefaultTransformStrategyFinder;
import org.swisspush.logtransformer.strategy.TransformStrategyFinder;
import org.swisspush.logtransformer.util.Configuration;

import java.util.List;

/**
* @author https://github.com/mcweba [Marc-Andre Weber]
*/
Expand All @@ -22,14 +21,22 @@ public class LogTransformer extends AbstractVerticle {
private final Logger log = LoggerFactory.getLogger(LogTransformer.class);
private LogTransformLogger logTransformLogger;
private TransformStrategyFinder transformStrategyFinder;
private boolean useDefaultTransformLogger = false;

public LogTransformer(){
useDefaultTransformLogger = true;
this(null, null);
}

public LogTransformer(LogTransformLogger logTransformLogger) {
this(logTransformLogger, null);
}

public LogTransformer(TransformStrategyFinder transformStrategyFinder) {
this(null, transformStrategyFinder);
}

public LogTransformer(LogTransformLogger logTransformLogger, TransformStrategyFinder transformStrategyFinder) {
this.logTransformLogger = logTransformLogger;
this.transformStrategyFinder = transformStrategyFinder;
}

@Override
Expand All @@ -40,11 +47,13 @@ public void start(Future<Void> future) {
Configuration modConfig = Configuration.fromJsonObject(config());
log.info("Starting LogTransformer module with configuration: " + modConfig);

if(useDefaultTransformLogger){
if(this.logTransformLogger == null){
this.logTransformLogger = new DefaultLogTransformLogger(vertx, modConfig.getLoggerName());
}

transformStrategyFinder = new TransformStrategyFinder(vertx, modConfig.getStrategyHeader());
if(this.transformStrategyFinder == null) {
this.transformStrategyFinder = new DefaultTransformStrategyFinder(vertx, modConfig.getStrategyHeader());
}

eb.consumer(modConfig.getAddress(), event -> {
TransformStrategy strategy = transformStrategyFinder.findTransformStrategy(event.headers());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.swisspush.logtransformer.strategy;

import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

/**
* Default implementation of the {@link TransformStrategyFinder}
*
* @author https://github.com/mcweba [Marc-Andre Weber]
*/
public class DefaultTransformStrategyFinder implements TransformStrategyFinder {

private final String strategyHeader;
private final Logger log = LoggerFactory.getLogger(DefaultTransformStrategyFinder.class);

private Vertx vertx;

private DoNothingTransformStrategy doNothingTransformStrategy;
private SplitStorageExpandLogStrategy splitStorageExpandLogStrategy;

public DefaultTransformStrategyFinder(Vertx vertx, String strategyHeader) {
this.vertx = vertx;
this.strategyHeader = strategyHeader;
}

/**
* Returns the corresponding {@link TransformStrategy} implementation based on the provided header strategy property.
*
* @param headers the headers containing the name of the strategy
* @return returns the corresponding {@link TransformStrategy} implementation or {@link DoNothingTransformStrategy} when no matching strategy was found
*/
@Override
public TransformStrategy findTransformStrategy(MultiMap headers){
String strategy = headers.get(strategyHeader);

if(isEmpty(strategy)){
return getDoNothingTransformStrategy();
} else if("SplitStorageExpandLogStrategy".equalsIgnoreCase(strategy)){
return getSplitStorageExpandLogStrategy();
}

log.warn("No log transform strategy found for value '" + strategy + "'. Using DoNothingTransformStrategy instead");
return getDoNothingTransformStrategy();
}

private DoNothingTransformStrategy getDoNothingTransformStrategy(){
if(doNothingTransformStrategy == null){
doNothingTransformStrategy = new DoNothingTransformStrategy(vertx);
}
return doNothingTransformStrategy;
}

private SplitStorageExpandLogStrategy getSplitStorageExpandLogStrategy(){
if(splitStorageExpandLogStrategy == null){
splitStorageExpandLogStrategy = new SplitStorageExpandLogStrategy(vertx);
}
return splitStorageExpandLogStrategy;
}

private boolean isEmpty(String stringToTest){
if(stringToTest == null){
return true;
}
String trimmed = stringToTest.trim();
return trimmed.length() == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public DoNothingTransformStrategy(Vertx vertx) {

@Override
public void transformLog(String logToTransform, Handler<AsyncResult<List<String>>> resultHandler) {
vertx.executeBlocking(future -> {
future.complete(Collections.singletonList(logToTransform));
}, resultHandler);
vertx.executeBlocking(future -> future.complete(Collections.singletonList(logToTransform)), resultHandler);
}

}
Original file line number Diff line number Diff line change
@@ -1,68 +1,19 @@
package org.swisspush.logtransformer.strategy;

import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

/**
* Manages the mapping of the {@link TransformStrategy} implementations to their strategy name (metadata)
*
* @author https://github.com/mcweba [Marc-Andre Weber]
*/
public class TransformStrategyFinder {

private final String strategyHeader;
private final Logger log = LoggerFactory.getLogger(TransformStrategyFinder.class);

private Vertx vertx;

private DoNothingTransformStrategy doNothingTransformStrategy;
private SplitStorageExpandLogStrategy splitStorageExpandLogStrategy;

public TransformStrategyFinder(Vertx vertx, String strategyHeader) {
this.vertx = vertx;
this.strategyHeader = strategyHeader;
}
public interface TransformStrategyFinder {

/**
* Returns the corresponding {@link TransformStrategy} implementation based on the provided header strategy property.
*
* @param headers the headers containing the name of the strategy
* @return returns the corresponding {@link TransformStrategy} implementation or {@link DoNothingTransformStrategy} when no matching strategy was found
* @return returns the corresponding {@link TransformStrategy} implementation
*/
public TransformStrategy findTransformStrategy(MultiMap headers){
String strategy = headers.get(strategyHeader);

if(isEmpty(strategy)){
return getDoNothingTransformStrategy();
} else if("SplitStorageExpandLogStrategy".equalsIgnoreCase(strategy)){
return getSplitStorageExpandLogStrategy();
}

log.warn("No log transform strategy found for value '" + strategy + "'. Using DoNothingTransformStrategy instead");
return getDoNothingTransformStrategy();
}

private DoNothingTransformStrategy getDoNothingTransformStrategy(){
if(doNothingTransformStrategy == null){
doNothingTransformStrategy = new DoNothingTransformStrategy(vertx);
}
return doNothingTransformStrategy;
}

private SplitStorageExpandLogStrategy getSplitStorageExpandLogStrategy(){
if(splitStorageExpandLogStrategy == null){
splitStorageExpandLogStrategy = new SplitStorageExpandLogStrategy(vertx);
}
return splitStorageExpandLogStrategy;
}

private boolean isEmpty(String stringToTest){
if(stringToTest == null){
return true;
}
String trimmed = stringToTest.trim();
return trimmed.length() == 0;
}
TransformStrategy findTransformStrategy(MultiMap headers);
}
Loading

0 comments on commit 9decbd7

Please sign in to comment.