Skip to content

Commit

Permalink
Add driver adapter bin stash (#222)
Browse files Browse the repository at this point in the history
* add bin-stash to CA
  • Loading branch information
jakspok authored Mar 30, 2022
1 parent 194aebf commit 8e30339
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,66 @@ public void shouldGenerateDynamoDBDrivenAdapterInJavaReactive() {
assertEquals(result.task(":" + task).getOutcome(), TaskOutcome.SUCCESS);
}

@Test
public void shouldGenerateBinStashDrivenAdapterInJavaReactive() {
canRunTaskGenerateStructureReactiveProject();
String task = "generateDrivenAdapter";
String type = "BINSTASH";

runner.withArguments(task, "--type=" + type);
runner.withProjectDir(projectDir);
BuildResult result = runner.build();

assertTrue(
new File(
"build/functionalTest/infrastructure/driven-adapters/bin-stash/src/main/java/co/com/bancolombia/binstash/config/BinStashCacheConfig.java")
.exists());
assertTrue(
new File("build/functionalTest/infrastructure/driven-adapters/bin-stash/build.gradle")
.exists());
assertEquals(result.task(":" + task).getOutcome(), TaskOutcome.SUCCESS);
}

@Test
public void shouldGenerateBinStashDrivenAdapterInJavaNoReactive() {
canRunTaskGenerateStructureWithOutParameters();
String task = "generateDrivenAdapter";
String type = "BINSTASH";

runner.withArguments(task, "--type=" + type);
runner.withProjectDir(projectDir);
BuildResult result = runner.build();

assertTrue(
new File(
"build/functionalTest/infrastructure/driven-adapters/bin-stash/src/main/java/co/com/bancolombia/binstash/config/BinStashCacheConfig.java")
.exists());
assertTrue(
new File("build/functionalTest/infrastructure/driven-adapters/bin-stash/build.gradle")
.exists());
assertEquals(result.task(":" + task).getOutcome(), TaskOutcome.SUCCESS);
}

@Test
public void shouldGenerateBinStashDrivenAdapterInKotlin() throws IOException {
canRunTaskGenerateStructureKotlinWithOutParameters();
String task = "generateDrivenAdapter";
String type = "BINSTASH";

runner.withArguments(task, "--type=" + type);
runner.withProjectDir(projectDir);
BuildResult result = runner.build();

assertTrue(
new File(
"build/functionalTest/infrastructure/driven-adapters/bin-stash/src/main/kotlin/co/com/bancolombia/binstash/config/BinStashCacheConfig.kt")
.exists());
assertTrue(
new File("build/functionalTest/infrastructure/driven-adapters/bin-stash/build.gradle.kts")
.exists());
assertEquals(result.task(":" + task).getOutcome(), TaskOutcome.SUCCESS);
}

private void writeString(File file, String string) throws IOException {
try (Writer writer = new FileWriter(file)) {
writer.write(string);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package co.com.bancolombia.factory.adapters;

import static co.com.bancolombia.utils.Utils.buildImplementationFromProject;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.commons.ObjectMapperFactory;
import java.io.IOException;

public class DrivenAdapterBinStash implements ModuleFactory {

@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {

CacheMode cacheMode = (CacheMode) builder.getParam("task-param-cache-mode");
builder.setupFromTemplate("driven-adapter/bin-stash");

builder.addParam("include-local", cacheMode.equals(CacheMode.LOCAL));
builder.addParam("include-hybrid", cacheMode.equals(CacheMode.HYBRID));
builder.addParam("include-centralized", cacheMode.equals(CacheMode.CENTRALIZED));
builder.appendToSettings("bin-stash", "infrastructure/driven-adapters");
String dependency = buildImplementationFromProject(builder.isKotlin(), ":bin-stash");
builder.appendDependencyToModule("app-service", dependency);

builder.appendToProperties("stash.memory").put("maxSize", "10000");
builder.appendToProperties("stash.redis").put("host", "myredis.host");
builder.appendToProperties("stash.redis").put("port", "6379");
builder.appendToProperties("stash.redis").put("database", "0");
builder.appendToProperties("stash.redis").put("password", "mypwd");

new ObjectMapperFactory().buildModule(builder);
}

public enum CacheMode {
LOCAL,
HYBRID,
CENTRALIZED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public static ModuleFactory getDrivenAdapterFactory(DrivenAdapterType type)
return new DrivenAdapterKtorClient();
case DYNAMODB:
return new DrivenAdapterDynamoDB();
case BINSTASH:
return new DrivenAdapterBinStash();
default:
throw new InvalidTaskOptionException("Driven Adapter type invalid");
}
Expand All @@ -55,6 +57,7 @@ public enum DrivenAdapterType {
S3,
MQ,
KTOR,
DYNAMODB
DYNAMODB,
BINSTASH
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import co.com.bancolombia.Constants.BooleanOption;
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.adapters.DrivenAdapterBinStash;
import co.com.bancolombia.factory.adapters.DrivenAdapterRedis;
import co.com.bancolombia.factory.adapters.ModuleFactoryDrivenAdapter;
import co.com.bancolombia.factory.adapters.ModuleFactoryDrivenAdapter.DrivenAdapterType;
Expand All @@ -19,6 +20,8 @@ public class GenerateDrivenAdapterTask extends CleanArchitectureDefaultTask {
private String name;
private String url = "http://localhost:8080";
private DrivenAdapterRedis.Mode mode = DrivenAdapterRedis.Mode.TEMPLATE;
private DrivenAdapterBinStash.CacheMode cacheMode = DrivenAdapterBinStash.CacheMode.LOCAL;

private BooleanOption secret = BooleanOption.FALSE;

@Option(option = "type", description = "Set type of driven adapter to be generated")
Expand Down Expand Up @@ -56,6 +59,11 @@ public List<BooleanOption> getSecretOptions() {
return Arrays.asList(BooleanOption.values());
}

@Option(option = "cache-mode", description = "Set value for cache type")
public void setcacheMode(DrivenAdapterBinStash.CacheMode cacheMode) {
this.cacheMode = cacheMode;
}

@TaskAction
public void generateDrivenAdapterTask() throws IOException, CleanException {
if (type == null) {
Expand All @@ -69,6 +77,7 @@ public void generateDrivenAdapterTask() throws IOException, CleanException {
logger.lifecycle("Clean Architecture plugin version: {}", Utils.getVersionPlugin());
logger.lifecycle("Driven Adapter type: {}", type);
builder.addParam("task-param-name", name);
builder.addParam("task-param-cache-mode", cacheMode);
builder.addParam("include-secret", secret == BooleanOption.TRUE);
builder.addParam(DrivenAdapterRedis.PARAM_MODE, mode);
builder.addParam("lombok", builder.isEnableLombok());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package {{package}}.binstash.config;

import co.com.bancolombia.binstash.model.api.ObjectCache;
import org.springframework.context.annotation.Bean;

{{#include-local}}
import co.com.bancolombia.binstash.LocalCacheFactory;
{{/include-local}}

{{#include-centralized}}
import co.com.bancolombia.binstash.CentralizedCacheFactory;
{{/include-centralized}}

{{#include-hibrid}}
import co.com.bancolombia.binstash.HybridCacheFactory;
import co.com.bancolombia.binstash.model.SyncRule;
import java.util.Collections;
import java.util.List;
{{/include-hibrid}}

@Configuration
public class BinStashConfiguration {
{{#include-local}}
@Bean
public ObjectCache<Object/*Replace for domain model*/> objectCache(LocalCacheFactory<Object> localCacheFactory) {
return localCacheFactory.newObjectCache();
}
{{/include-local}}

{{#include-centralized }}
@Bean
public ObjectCache<Object/*Replace for domain model*/> objectCache(CentralizedCacheFactory<Object> centralizedCacheFactory) {
return centralizedCacheFactory.newObjectCache();
}
{{/include-centralized }}

{{#include-hybrid}}
@Bean
public List<SyncRule> cacheSyncRules() {
SyncRule simpleSyncRule = (keyArg, syncType) -> true;
return Collections.singletonList(simpleSyncRule);
}

@Bean
public ObjectCache<Object/*Replace for domain model*/> objectCache(HybridCacheFactory<Object/*Replace for domain model*/> cacheFactory,
List<SyncRule> cacheSyncRules) {
return cacheFactory.newObjectCache(cacheSyncRules);
}
{{/include-hybrid}}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package {{package}}.binstash.config

import co.com.bancolombia.binstash.model.api.ObjectCache
import org.springframework.context.annotation.Bean
{{#include-local}}
import co.com.bancolombia.binstash.LocalCacheFactory
{{/include-local}}
{{#include-centralized}}
import co.com.bancolombia.binstash.CentralizedCacheFactory
{{/include-centralized}}
{{#include-hibrid}}
import co.com.bancolombia.binstash.HybridCacheFactory
import co.com.bancolombia.binstash.model.SyncRule
import java.util.Collections
import java.util.List
{{/include-hibrid}}

@Configuration
open class BinStashConfiguration {
{{#include-local}}
@Bean
open fun objectCache(localCacheFactory: LocalCacheFactory<Any?>): ObjectCache<Any?>? {
return localCacheFactory.newObjectCache()
}
{{/include-local}}

{{#include-centralized }}
@Bean
open fun objectCache(centralizedCacheFactory: CentralizedCacheFactory<Any?>): ObjectCache<Any?>? {
return centralizedCacheFactory.newObjectCache()
}
{{/include-centralized }}

{{#include-hybrid}}
@Bean
open fun cacheSyncRules(): List<SyncRule?>? {
val simpleSyncRule = SyncRule { keyArg, syncType -> true }
return Collections.singletonList(simpleSyncRule)
}

@Bean
fun objectCache(
cacheFactory: HybridCacheFactory<Any?>,
cacheSyncRules: List<SyncRule?>?
): ObjectCache<Any?>? {
return cacheFactory.newObjectCache(cacheSyncRules)
}
{{/include-hybrid}}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
dependencies {
implementation project(':model')
{{#include-local}}
implementation 'com.github.bancolombia:bin-stash-local:1.0.3'
{{/include-local}}

{{#include-centralized }}
implementation 'com.github.bancolombia:bin-stash-centralized:1.0.3'
{{/include-centralized }}

{{#include-hybrid}}
implementation 'com.github.bancolombia:bin-stash-hybrid:1.0.3'
{{/include-hybrid}}
}
12 changes: 12 additions & 0 deletions src/main/resources/driven-adapter/bin-stash/build.gradle.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
dependencies {
implementation project(':model')
{{#include-local}}
implementation 'com.github.bancolombia:bin-stash-local:1.0.3'
{{/include-local}}
{{#include-centralized }}
implementation 'com.github.bancolombia:bin-stash-centralized:1.0.3'
{{/include-centralized }}
{{#include-hybrid}}
implementation 'com.github.bancolombia:bin-stash-hybrid:1.0.3'
{{/include-hybrid}}
}
12 changes: 12 additions & 0 deletions src/main/resources/driven-adapter/bin-stash/definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"folders": [],
"files": {},
"java": {
"driven-adapter/bin-stash/bin-stash-local-cache-config.java.mustache": "infrastructure/driven-adapters/bin-stash/src/main/{{language}}/{{packagePath}}/binstash/config/BinStashCacheConfig.java",
"driven-adapter/bin-stash/build.gradle.mustache": "infrastructure/driven-adapters/bin-stash/build.gradle"
},
"kotlin": {
"driven-adapter/bin-stash/bin-stash-local-cache-config.kt.mustache": "infrastructure/driven-adapters/bin-stash/src/main/{{language}}/{{packagePath}}/binstash/config/BinStashCacheConfig.kt",
"driven-adapter/bin-stash/build.gradle.kts.mustache": "infrastructure/driven-adapters/bin-stash/build.gradle.kts"
}
}

0 comments on commit 8e30339

Please sign in to comment.