diff --git a/README.md b/README.md index b1801328..26fb102a 100644 --- a/README.md +++ b/README.md @@ -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" } ``` @@ -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] | diff --git a/gradle.properties b/gradle.properties index 6612de4f..02ddcfbb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ package=co.com.bancolombia -systemProp.version=1.6.3 +systemProp.version=1.6.4 diff --git a/src/main/java/co/com/bancolombia/Constants.java b/src/main/java/co/com/bancolombia/Constants.java index 67ffe888..78c10923 100644 --- a/src/main/java/co/com/bancolombia/Constants.java +++ b/src/main/java/co/com/bancolombia/Constants.java @@ -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 diff --git a/src/main/java/co/com/bancolombia/factory/ModuleBuilder.java b/src/main/java/co/com/bancolombia/factory/ModuleBuilder.java index 65c1bbfb..420d1685 100644 --- a/src/main/java/co/com/bancolombia/factory/ModuleBuilder.java +++ b/src/main/java/co/com/bancolombia/factory/ModuleBuilder.java @@ -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()); } @@ -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; diff --git a/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterAsyncEventBus.java b/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterAsyncEventBus.java index 771b7eb5..1f01c2ff 100644 --- a/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterAsyncEventBus.java +++ b/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterAsyncEventBus.java @@ -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; @@ -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')"); diff --git a/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterGeneric.java b/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterGeneric.java index 0f41609d..26c8a0cb 100644 --- a/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterGeneric.java +++ b/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterGeneric.java @@ -3,7 +3,6 @@ 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; @@ -11,7 +10,7 @@ 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 " + diff --git a/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterJPA.java b/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterJPA.java index 081dd616..21946670 100644 --- a/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterJPA.java +++ b/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterJPA.java @@ -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"); diff --git a/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterMongoDB.java b/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterMongoDB.java index dd1c830f..5683f916 100644 --- a/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterMongoDB.java +++ b/src/main/java/co/com/bancolombia/factory/adapters/DrivenAdapterMongoDB.java @@ -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"); diff --git a/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointGeneric.java b/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointGeneric.java index c0bce172..17db7369 100644 --- a/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointGeneric.java +++ b/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointGeneric.java @@ -3,7 +3,6 @@ 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; @@ -11,7 +10,7 @@ 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 " + diff --git a/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointRestMvc.java b/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointRestMvc.java index bcfab5b3..4aee36dd 100644 --- a/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointRestMvc.java +++ b/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointRestMvc.java @@ -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')"); diff --git a/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointRestWebflux.java b/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointRestWebflux.java index e319c88a..bb51e3fa 100644 --- a/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointRestWebflux.java +++ b/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointRestWebflux.java @@ -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; @@ -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')"); diff --git a/src/main/java/co/com/bancolombia/task/GenerateModelTask.java b/src/main/java/co/com/bancolombia/task/GenerateModelTask.java index 4b5d4b8f..2e38a471 100644 --- a/src/main/java/co/com/bancolombia/task/GenerateModelTask.java +++ b/src/main/java/co/com/bancolombia/task/GenerateModelTask.java @@ -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; @@ -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"); diff --git a/src/main/java/co/com/bancolombia/task/GenerateUseCaseTask.java b/src/main/java/co/com/bancolombia/task/GenerateUseCaseTask.java index 162e0c5f..54d24dc7 100644 --- a/src/main/java/co/com/bancolombia/task/GenerateUseCaseTask.java +++ b/src/main/java/co/com/bancolombia/task/GenerateUseCaseTask.java @@ -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; @@ -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"); diff --git a/src/main/java/co/com/bancolombia/task/ValidateStructureTask.java b/src/main/java/co/com/bancolombia/task/ValidateStructureTask.java index 4160eba6..5623ebf1 100644 --- a/src/main/java/co/com/bancolombia/task/ValidateStructureTask.java +++ b/src/main/java/co/com/bancolombia/task/ValidateStructureTask.java @@ -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()) { diff --git a/src/main/java/co/com/bancolombia/utils/FileUtils.java b/src/main/java/co/com/bancolombia/utils/FileUtils.java index 08e28c2a..b9d4bc77 100644 --- a/src/main/java/co/com/bancolombia/utils/FileUtils.java +++ b/src/main/java/co/com/bancolombia/utils/FileUtils.java @@ -50,13 +50,13 @@ public static List 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"); } } diff --git a/src/main/resources/driven-adapter/jpa-repository/jpa-repository-adapter.java.mustache b/src/main/resources/driven-adapter/jpa-repository/jpa-repository-adapter.java.mustache index bb7fb461..67bc642a 100644 --- a/src/main/resources/driven-adapter/jpa-repository/jpa-repository-adapter.java.mustache +++ b/src/main/resources/driven-adapter/jpa-repository/jpa-repository-adapter.java.mustache @@ -12,6 +12,6 @@ public class JPARepositoryAdapter extends AdapterOperations 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 } } diff --git a/src/main/resources/driven-adapter/mongo-reactive/build.gradle.mustache b/src/main/resources/driven-adapter/mongo-reactive/build.gradle.mustache new file mode 100644 index 00000000..9d520375 --- /dev/null +++ b/src/main/resources/driven-adapter/mongo-reactive/build.gradle.mustache @@ -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}}' +} diff --git a/src/main/resources/driven-adapter/mongo-reactive/config/db-secret.java.mustache b/src/main/resources/driven-adapter/mongo-reactive/config/db-secret.java.mustache new file mode 100644 index 00000000..b0278e31 --- /dev/null +++ b/src/main/resources/driven-adapter/mongo-reactive/config/db-secret.java.mustache @@ -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; +} diff --git a/src/main/resources/driven-adapter/mongo-reactive/config/mongo-config.java.mustache b/src/main/resources/driven-adapter/mongo-reactive/config/mongo-config.java.mustache new file mode 100644 index 00000000..39e90c85 --- /dev/null +++ b/src/main/resources/driven-adapter/mongo-reactive/config/mongo-config.java.mustache @@ -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); + } +} diff --git a/src/main/resources/driven-adapter/mongo-reactive/definition.json b/src/main/resources/driven-adapter/mongo-reactive/definition.json new file mode 100644 index 00000000..61546688 --- /dev/null +++ b/src/main/resources/driven-adapter/mongo-reactive/definition.json @@ -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" + } +} diff --git a/src/main/resources/driven-adapter/mongo-reactive/helper/adapter-operations.java.mustache b/src/main/resources/driven-adapter/mongo-reactive/helper/adapter-operations.java.mustache new file mode 100644 index 00000000..0cd2a340 --- /dev/null +++ b/src/main/resources/driven-adapter/mongo-reactive/helper/adapter-operations.java.mustache @@ -0,0 +1,77 @@ +package {{package}}.mongo.helper; + +import org.reactivecommons.utils.ObjectMapper; +import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor; +import org.springframework.data.repository.reactive.ReactiveCrudRepository; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.lang.reflect.ParameterizedType; +import java.util.function.Function; + +import static org.springframework.data.domain.Example.of; + +public abstract class AdapterOperations & ReactiveQueryByExampleExecutor> { + + protected R repository; + protected ObjectMapper mapper; + private final Class dataClass; + private final Function toEntityFn; + + @SuppressWarnings("unchecked") + public AdapterOperations(R repository, ObjectMapper mapper, Function toEntityFn) { + this.repository = repository; + this.mapper = mapper; + ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass(); + this.dataClass = (Class) genericSuperclass.getActualTypeArguments()[1]; + this.toEntityFn = toEntityFn; + } + + public Mono save(E entity) { + return Mono.just(entity) + .map(this::toData) + .flatMap(this::saveData) + .map(this::toEntity); + } + + public Flux saveAll(Flux entities) { + return doQueryMany(repository.saveAll(entities.map(this::toData))); + } + + public Mono findById(I id) { + return doQuery(repository.findById(id)); + } + + public Flux findByExample(E entity) { + return doQueryMany(repository.findAll(of(toData(entity)))); + } + + public Mono deleteById(I id) { + return repository.deleteById(id); + } + + public Flux findAll() { + return doQueryMany(repository.findAll()); + } + + protected Mono doQuery(Mono query) { + return query.map(this::toEntity); + } + + protected Flux doQueryMany(Flux query) { + return query.map(this::toEntity); + } + + protected Mono saveData(D data) { + return repository.save(data); + } + + protected D toData(E entity) { + return mapper.map(entity, dataClass); + } + + protected E toEntity(D data) { + return toEntityFn.apply(data); + } + +} diff --git a/src/main/resources/driven-adapter/mongo-reactive/mongo-repository-adapter.java.mustache b/src/main/resources/driven-adapter/mongo-reactive/mongo-repository-adapter.java.mustache new file mode 100644 index 00000000..941b8cda --- /dev/null +++ b/src/main/resources/driven-adapter/mongo-reactive/mongo-repository-adapter.java.mustache @@ -0,0 +1,17 @@ +package {{package}}.mongo; + +import {{package}}.mongo.helper.AdapterOperations; +import org.reactivecommons.utils.ObjectMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +@Repository +public class MongoRepositoryAdapter extends AdapterOperations +// implements ModelRepository from domain +{ + + @Autowired + public MongoRepositoryAdapter(MongoDBRepository repository, ObjectMapper mapper) { + super(repository, mapper, d -> mapper.map(d, Object.class/* change for domain model */)); // could be mapper.mapBuilder + } +} diff --git a/src/main/resources/driven-adapter/mongo-reactive/mongo-repository.java.mustache b/src/main/resources/driven-adapter/mongo-reactive/mongo-repository.java.mustache new file mode 100644 index 00000000..e7180551 --- /dev/null +++ b/src/main/resources/driven-adapter/mongo-reactive/mongo-repository.java.mustache @@ -0,0 +1,7 @@ +package {{package}}.mongo; + +import org.springframework.data.mongodb.repository.ReactiveMongoRepository; +import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor; + +interface MongoDBRepository extends ReactiveMongoRepository, ReactiveQueryByExampleExecutor { +} diff --git a/src/main/resources/driven-adapter/mongo-repository/mongo-repository-adapter.java.mustache b/src/main/resources/driven-adapter/mongo-repository/mongo-repository-adapter.java.mustache index ed10a6de..941b8cda 100644 --- a/src/main/resources/driven-adapter/mongo-repository/mongo-repository-adapter.java.mustache +++ b/src/main/resources/driven-adapter/mongo-repository/mongo-repository-adapter.java.mustache @@ -12,6 +12,6 @@ public class MongoRepositoryAdapter extends AdapterOperations mapper.mapBuilder(d, Object.class/* change for domain model */)); + super(repository, mapper, d -> mapper.map(d, Object.class/* change for domain model */)); // could be mapper.mapBuilder } } diff --git a/src/main/resources/structure/root/gradle.properties.mustache b/src/main/resources/structure/root/gradle.properties.mustache index de1d9f0b..ff8e0e6c 100644 --- a/src/main/resources/structure/root/gradle.properties.mustache +++ b/src/main/resources/structure/root/gradle.properties.mustache @@ -1,2 +1,3 @@ package={{package}} systemProp.version={{pluginVersion}} +reactive={{reactive}} diff --git a/src/test/java/co/com/bancolombia/task/GenerateDrivenAdapterTaskTest.java b/src/test/java/co/com/bancolombia/task/GenerateDrivenAdapterTaskTest.java index 448c407d..09c7a72d 100644 --- a/src/test/java/co/com/bancolombia/task/GenerateDrivenAdapterTaskTest.java +++ b/src/test/java/co/com/bancolombia/task/GenerateDrivenAdapterTaskTest.java @@ -9,7 +9,9 @@ import org.junit.Test; import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; import java.util.List; import static org.junit.Assert.assertEquals; @@ -20,6 +22,10 @@ public class GenerateDrivenAdapterTaskTest { @Before public void init() throws IOException, CleanException { + setup(GenerateStructureTask.ProjectType.IMPERATIVE); + } + + private void setup(GenerateStructureTask.ProjectType type) throws IOException, CleanException { Project project = ProjectBuilder.builder().withProjectDir(new File("build/unitTest")).build(); ProjectBuilder.builder() @@ -30,6 +36,7 @@ public void init() throws IOException, CleanException { project.getTasks().create("ca", GenerateStructureTask.class); GenerateStructureTask taskStructure = (GenerateStructureTask) project.getTasks().getByName("ca"); + taskStructure.setType(type); taskStructure.generateStructureTask(); project.getTasks().create("test", GenerateDrivenAdapterTask.class); @@ -124,6 +131,38 @@ public void generateDrivenAdapterMongoRepositoryWithSecrets() throws IOException assertTrue(new File("build/unitTest/applications/app-service/src/main/java/co/com/bancolombia/config/SecretsConfig.java").exists()); } + @Test + public void generateDrivenAdapterMongoRepositoryForNoProjectType() throws IOException, CleanException { + // Arrange + writeString(new File("build/unitTest/gradle.properties"), "package=co.com.bancolombia\nsystemProp.version=" + Constants.PLUGIN_VERSION + "\n"); + task.setType(ModuleFactoryDrivenAdapter.DrivenAdapterType.MONGODB); + // Act + task.generateDrivenAdapterTask(); + // Assert + assertTrue(new File("build/unitTest/infrastructure/driven-adapters/mongo-repository/build.gradle").exists()); + assertTrue(new File("build/unitTest/infrastructure/driven-adapters/mongo-repository/src/main/java/co/com/bancolombia/mongo/MongoDBRepository.java").exists()); + assertTrue(new File("build/unitTest/infrastructure/driven-adapters/mongo-repository/src/main/java/co/com/bancolombia/mongo/MongoRepositoryAdapter.java").exists()); + assertTrue(new File("build/unitTest/infrastructure/driven-adapters/mongo-repository/src/main/java/co/com/bancolombia/mongo/helper/AdapterOperations.java").exists()); + assertTrue(new File("build/unitTest/infrastructure/driven-adapters/mongo-repository/src/main/java/co/com/bancolombia/mongo/config/MongoDBSecret.java").exists()); + assertTrue(new File("build/unitTest/applications/app-service/src/main/java/co/com/bancolombia/config/MongoConfig.java").exists()); + } + + @Test + public void generateDrivenAdapterMongoRepositoryForReactive() throws IOException, CleanException { + // Arrange + setup(GenerateStructureTask.ProjectType.REACTIVE); + task.setType(ModuleFactoryDrivenAdapter.DrivenAdapterType.MONGODB); + // Act + task.generateDrivenAdapterTask(); + // Assert + assertTrue(new File("build/unitTest/infrastructure/driven-adapters/mongo-repository/build.gradle").exists()); + assertTrue(new File("build/unitTest/infrastructure/driven-adapters/mongo-repository/src/main/java/co/com/bancolombia/mongo/MongoDBRepository.java").exists()); + assertTrue(new File("build/unitTest/infrastructure/driven-adapters/mongo-repository/src/main/java/co/com/bancolombia/mongo/MongoRepositoryAdapter.java").exists()); + assertTrue(new File("build/unitTest/infrastructure/driven-adapters/mongo-repository/src/main/java/co/com/bancolombia/mongo/helper/AdapterOperations.java").exists()); + assertTrue(new File("build/unitTest/infrastructure/driven-adapters/mongo-repository/src/main/java/co/com/bancolombia/mongo/config/MongoDBSecret.java").exists()); + assertTrue(new File("build/unitTest/applications/app-service/src/main/java/co/com/bancolombia/config/MongoConfig.java").exists()); + } + @Test public void generateDrivenAdapterEventBus() throws IOException, CleanException { // Arrange @@ -144,4 +183,10 @@ public void shouldGetSecretOptions() { // Assert assertEquals(2, options.size()); } + + private void writeString(File file, String string) throws IOException { + try (Writer writer = new FileWriter(file)) { + writer.write(string); + } + } } diff --git a/src/test/java/co/com/bancolombia/utils/FileUtilsTest.java b/src/test/java/co/com/bancolombia/utils/FileUtilsTest.java index 350c79d4..69854d34 100644 --- a/src/test/java/co/com/bancolombia/utils/FileUtilsTest.java +++ b/src/test/java/co/com/bancolombia/utils/FileUtilsTest.java @@ -21,13 +21,13 @@ public class FileUtilsTest { @Test public void readPropertiesExist() throws Exception { String property = "package"; - assertEquals("co.com.bancolombia", FileUtils.readProperties(property)); + assertEquals("co.com.bancolombia", FileUtils.readProperties(".", property)); } @Test(expected = IOException.class) public void readPropertiesNonExists() throws Exception { String property = "packageName"; - FileUtils.readProperties(property); + FileUtils.readProperties("build/unitTest", property); } @Test