Skip to content

Commit

Permalink
Feature/reactive mongodb (#74)
Browse files Browse the repository at this point in the history
* create mongodb reactive driven adapter automatically for reactive projects

* create mongodb reactive driven adapter automatically for reactive projects
  • Loading branch information
juancgalvis authored Jun 26, 2020
1 parent 2333e5c commit b19b205
Show file tree
Hide file tree
Showing 27 changed files with 288 additions and 34 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To use the plugin you need Gradle version 5 or later, to start add the following

```groovy
plugins {
id "co.com.bancolombia.cleanArchitecture" version "1.6.3"
id "co.com.bancolombia.cleanArchitecture" version "1.6.4"
}
```

Expand Down Expand Up @@ -152,6 +152,19 @@ gradle generateDrivenAdapter --type=[drivenAdapterType]
gradle gda --type [drivenAdapterType]
```

When use ```MONGODB``` type please be sure that property 'reactive' is set correctly in gradle.properties
- For reactive projects should be:

```
reactive=true
```

- For imperative projects should be:

```
reactive=false
```

|Reference for drivenAdapterType|Name |Additional Options |
|-------------------------------|--------------------|---------------------|
|GENERIC |Empty Driven Adapter|--name [name] |
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
package=co.com.bancolombia
systemProp.version=1.6.3
systemProp.version=1.6.4
2 changes: 1 addition & 1 deletion src/main/java/co/com/bancolombia/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Constants {
public static final String SECRETS_VERSION = "2.1.0";
public static final String RCOMMONS_ASYNC_COMMONS_STARTER_VERSION = "0.4.7";
public static final String RCOMMONS_OBJECT_MAPPER_VERSION = "0.1.0";
public static final String PLUGIN_VERSION = "1.6.3";
public static final String PLUGIN_VERSION = "1.6.4";

public enum BooleanOption {
TRUE, FALSE
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/co/com/bancolombia/factory/ModuleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ public void addParam(String key, Object value) {
this.params.put(key, value);
}

public void loadPackage() throws IOException {
addParamPackage(FileUtils.readProperties(project.getProjectDir().getPath(), "package"));
}

public void addParamPackage(String packageName) {
logger.lifecycle("Project Package: {}", packageName.toLowerCase());
this.params.put("package", packageName.toLowerCase());
this.params.put("packagePath", packageName.replaceAll("\\.", "\\/").toLowerCase());
}
Expand Down Expand Up @@ -169,6 +174,18 @@ public Boolean getBooleanParam(String key) {
return (Boolean) params.get(key);
}

public Boolean isReactive() {
try {
String reactive = FileUtils.readProperties(project.getProjectDir().getPath(), "reactive");
return "true".equals(reactive);
} catch (IOException e) {
logger.warn(e.getMessage());
logger.lifecycle("WARN: variable reactive not present, if your project is reactive please add" +
" reactive=true to gradle.properties and relaunch this task");
return false;
}
}

private void updateFile(String path, FileUpdater updater) throws IOException {
FileModel current = files.get(path);
String content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package co.com.bancolombia.factory.adapters;

import co.com.bancolombia.utils.FileUtils;
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
Expand All @@ -10,7 +9,7 @@
public class DrivenAdapterAsyncEventBus implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.addParamPackage(FileUtils.readProperties("package"));
builder.loadPackage();
builder.setupFromTemplate("driven-adapter/async-event-bus");
builder.appendToSettings("async-event-bus", "infrastructure/driven-adapters");
builder.appendDependencyToModule("app-service", "implementation project(':async-event-bus')");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.utils.FileUtils;
import co.com.bancolombia.utils.Utils;

import java.io.IOException;

public class DrivenAdapterGeneric implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.addParamPackage(FileUtils.readProperties("package"));
builder.loadPackage();
String name = builder.getStringParam("task-param-name");
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("No name is set for GENERIC type, usage: gradle generateDrivenAdapter " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.utils.FileUtils;

import java.io.IOException;

public class DrivenAdapterJPA implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.addParamPackage(FileUtils.readProperties("package"));
builder.loadPackage();
builder.setupFromTemplate("driven-adapter/jpa-repository");
builder.appendToSettings("jpa-repository", "infrastructure/driven-adapters");
builder.appendToSettings("jpa-repository-commons", "infrastructure/helpers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.utils.FileUtils;
import org.gradle.api.logging.Logger;

import java.io.IOException;

public class DrivenAdapterMongoDB implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.addParamPackage(FileUtils.readProperties("package"));
builder.setupFromTemplate("driven-adapter/mongo-repository");
Logger logger = builder.getProject().getLogger();
builder.loadPackage();
if (builder.isReactive()) {
logger.lifecycle("Generating for reactive project");
builder.setupFromTemplate("driven-adapter/mongo-reactive");
} else {
logger.lifecycle("Generating for imperative project");
builder.setupFromTemplate("driven-adapter/mongo-repository");
}
builder.appendToSettings("mongo-repository", "infrastructure/driven-adapters");
builder.appendToProperties("spring.data.mongodb")
.put("uri", "mongodb://localhost:27017/test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.utils.FileUtils;
import co.com.bancolombia.utils.Utils;

import java.io.IOException;

public class EntryPointGeneric implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.addParamPackage(FileUtils.readProperties("package"));
builder.loadPackage();
String name = builder.getStringParam("task-param-name");
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("No name is set for GENERIC type, usage: gradle generateEntryPoint " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.utils.FileUtils;

import java.io.IOException;

public class EntryPointRestMvc implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.addParamPackage(FileUtils.readProperties("package"));
builder.loadPackage();
builder.setupFromTemplate("entry-point/rest-mvc");
builder.appendToSettings("api-rest", "infrastructure/entry-points");
builder.appendDependencyToModule("app-service", "implementation project(':api-rest')");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package co.com.bancolombia.factory.entrypoints;

import co.com.bancolombia.utils.FileUtils;
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
Expand All @@ -10,7 +9,7 @@
public class EntryPointRestWebflux implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.addParamPackage(FileUtils.readProperties("package"));
builder.loadPackage();
builder.setupFromTemplate("entry-point/rest-webflux");
builder.appendToSettings("reactive-web", "infrastructure/entry-points");
builder.appendDependencyToModule("app-service", "implementation project(':reactive-web')");
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/co/com/bancolombia/task/GenerateModelTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import co.com.bancolombia.exceptions.ParamNotFoundException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.utils.FileUtils;
import co.com.bancolombia.utils.Utils;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
Expand All @@ -27,12 +26,10 @@ public void generateModelTask() throws IOException, ParamNotFoundException {
if (name.isEmpty()) {
throw new IllegalArgumentException("No model name, usage: gradle generateModel --name [name]");
}
String packageName = FileUtils.readProperties("package");
name = Utils.capitalize(name);
logger.lifecycle("Clean Architecture plugin version: {}", Utils.getVersionPlugin());
logger.lifecycle("Project Package: {}", packageName);
logger.lifecycle("Model Name: {}", name);
builder.addParamPackage(packageName);
builder.loadPackage();
builder.addParam("modelName", name.toLowerCase());
builder.addParam("modelClassName", name);
builder.setupFromTemplate("model");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import co.com.bancolombia.exceptions.ParamNotFoundException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.utils.FileUtils;
import co.com.bancolombia.utils.Utils;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
Expand All @@ -28,14 +27,12 @@ public void generateUseCaseTask() throws IOException, ParamNotFoundException {
throw new IllegalArgumentException(
"No use case name, usage: gradle generateUseCase --name [name]");
}
String packageName = FileUtils.readProperties("package");
name = Utils.capitalize(name);
String className = refactorName(name);
String useCaseName = className.replace(USECASE_CLASS_NAME, "").toLowerCase();
logger.lifecycle("Clean Architecture plugin version: {}", Utils.getVersionPlugin());
logger.lifecycle("Project Package: {}", packageName);
logger.lifecycle("Use Case Name: {}", name);
builder.addParamPackage(packageName);
builder.loadPackage();
builder.addParam("useCaseName", useCaseName);
builder.addParam("useCaseClassName", className);
builder.setupFromTemplate("usecase");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ValidateStructureTask extends DefaultTask {
@TaskAction
public void validateStructureTask() throws IOException, CleanException {

String packageName = FileUtils.readProperties("package");
String packageName = FileUtils.readProperties(getProject().getProjectDir().getPath(), "package");
logger.lifecycle("Clean Architecture plugin version: {}", Utils.getVersionPlugin());
logger.lifecycle("Project Package: {}", packageName);
if (!validateModelLayer()) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/co/com/bancolombia/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public static List<File> finderSubProjects(String dirPath) {
return textFiles;
}

public static String readProperties(String variable) throws IOException {
public static String readProperties(String projectPath, String variable) throws IOException {
Properties properties = new Properties();
properties.load(new FileReader("gradle.properties"));
properties.load(new FileReader(projectPath + "/gradle.properties"));
if (properties.getProperty(variable) != null) {
return properties.getProperty(variable);
} else {
throw new IOException("No parameter" + variable + " in build.properties file");
throw new IOException("No parameter " + variable + " in gradle.properties file");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public class JPARepositoryAdapter extends AdapterOperations<Object/* change for
@Autowired
public JPARepositoryAdapter(JPARepository repository, ObjectMapper mapper) {
super(repository, mapper, d -> mapper.mapBuilder(d, Object.class/* change for domain model */)); // could be mapper.map
super(repository, mapper, d -> mapper.map(d, Object.class/* change for domain model */)); // could be mapper.mapBuilder
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dependencies {
implementation project(':model')
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
implementation 'org.reactivecommons.utils:object-mapper-api:{{objectMapperVersion}}'
runtime 'de.flapdoodle.embed:de.flapdoodle.embed.mongo' // TODO: remove this dependency to connect to real database
testCompile 'org.reactivecommons.utils:object-mapper:{{objectMapperVersion}}'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package {{package}}.mongo.config;

{{#include-secret}}
import lombok.Data;
{{/include-secret}}
{{^include-secret}}
import lombok.Builder;
import lombok.Getter;
{{/include-secret}}

{{#include-secret}}
@Data
{{/include-secret}}
{{^include-secret}}
@Builder
@Getter
{{/include-secret}}
public class MongoDBSecret {
private {{^include-secret}}final {{/include-secret}}String uri;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package {{package}}.config;

{{#include-secret}}
import co.com.bancolombia.commons.secretsmanager.exceptions.SecretException;
import co.com.bancolombia.commons.secretsmanager.manager.GenericManager;
import org.springframework.beans.factory.annotation.Value;
{{/include-secret}}
import {{package}}.mongo.config.MongoDBSecret;
import org.springframework.boot.autoconfigure.mongo.MongoClientFactory;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class MongoConfig {
{{#include-secret}}
@Bean
public MongoDBSecret dbSecret(@Value("${aws.secretName}") String secret, GenericManager manager)
throws SecretException {
return manager.getSecretModel(secret, MongoDBSecret.class);
}
{{/include-secret}}
{{^include-secret}}
@Bean
public MongoDBSecret dbSecret(Environment env) {
return MongoDBSecret.builder()
.uri(env.getProperty("spring.data.mongo.uri"))
.build();
}
{{/include-secret}}

@Bean
public MongoClientFactory mongoProperties(MongoDBSecret secret, Environment env) {
MongoProperties properties = new MongoProperties();
properties.setUri(secret.getUri());
return new MongoClientFactory(properties, env);
}
}
13 changes: 13 additions & 0 deletions src/main/resources/driven-adapter/mongo-reactive/definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"folders": [
"infrastructure/driven-adapters/mongo-repository/src/test/java/{{packagePath}}/mongo/helper"
],
"files": {
"driven-adapter/mongo-reactive/config/mongo-config.java.mustache": "applications/app-service/src/main/java/{{packagePath}}/config/MongoConfig.java",
"driven-adapter/mongo-reactive/config/db-secret.java.mustache": "infrastructure/driven-adapters/mongo-repository/src/main/java/{{packagePath}}/mongo/config/MongoDBSecret.java",
"driven-adapter/mongo-reactive/helper/adapter-operations.java.mustache": "infrastructure/driven-adapters/mongo-repository/src/main/java/{{packagePath}}/mongo/helper/AdapterOperations.java",
"driven-adapter/mongo-reactive/mongo-repository.java.mustache": "infrastructure/driven-adapters/mongo-repository/src/main/java/{{packagePath}}/mongo/MongoDBRepository.java",
"driven-adapter/mongo-reactive/mongo-repository-adapter.java.mustache": "infrastructure/driven-adapters/mongo-repository/src/main/java/{{packagePath}}/mongo/MongoRepositoryAdapter.java",
"driven-adapter/mongo-reactive/build.gradle.mustache": "infrastructure/driven-adapters/mongo-repository/build.gradle"
}
}
Loading

0 comments on commit b19b205

Please sign in to comment.