Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add driver adapter bin stash #222

Merged
merged 7 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,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"
}
}