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

Move loadPackage to generic method, enable task validations #142

Merged
merged 1 commit into from
Apr 12, 2021
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 @@ -192,7 +192,7 @@ public void canRunTaskGenerateStructureWithOutParametersValidator() {
runner.withProjectDir(projectDir);
runner.build();

runner.withArguments("generateDrivenAdapter", "--type=" + "ASYNCEVENTBUS");
runner.withArguments("generateDrivenAdapter", "--type=" + "MONGODB");
runner.withProjectDir(projectDir);
runner.build();
}
Expand Down Expand Up @@ -330,7 +330,7 @@ public void canRunTaskGenerateDrivenAdapterRestConsumerCaseWithParameters() {

@Test
public void canRunTaskGenerateEntryPointGrahpqlApiCase() {
canRunTaskGenerateStructureWithOutParameters();
canRunTaskGenerateStructureReactiveProject();
String task = "generateEntryPoint";
String valueEntryPoint = "graphql";
String valuePathgqlEntryPoint = "/graphqlpath";
Expand Down Expand Up @@ -553,7 +553,7 @@ public void canRunTaskGenerateDrivenAdapterWithParameters() {

@Test
public void canRunTaskGenerateDrivenAdapterEventBusTest() {
canRunTaskGenerateStructureWithOutParameters();
canRunTaskGenerateStructureReactiveProject();
String task = "generateDrivenAdapter";
String valueDrivenAdapter = "ASYNCEVENTBUS";

Expand All @@ -580,9 +580,20 @@ public void canRunTaskGenerateDrivenAdapterEventBusTest() {
assertEquals(result.task(":" + task).getOutcome(), TaskOutcome.SUCCESS);
}

@Test(expected = Exception.class)
public void shouldFailTaskGenerateDrivenAdapterEventBusForNonReactiveTest() {
canRunTaskGenerateStructureWithOutParameters();
String task = "generateDrivenAdapter";
String valueDrivenAdapter = "ASYNCEVENTBUS";

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

@Test
public void canRunTaskGenerateEntryPointEventHandlerTest() {
canRunTaskGenerateStructureWithOutParameters();
canRunTaskGenerateStructureReactiveProject();
String task = "generateEntryPoint";
String valueDrivenAdapter = "ASYNCEVENTHANDLER";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package co.com.bancolombia.exceptions;

public class ValidationException extends CleanException {
public ValidationException(String message) {
super(message);
}
}
24 changes: 24 additions & 0 deletions src/main/java/co/com/bancolombia/factory/ModuleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import co.com.bancolombia.Constants;
import co.com.bancolombia.exceptions.ParamNotFoundException;
import co.com.bancolombia.exceptions.ValidationException;
import co.com.bancolombia.factory.validations.Validation;
import co.com.bancolombia.models.FileModel;
import co.com.bancolombia.models.TemplateDefinition;
import co.com.bancolombia.utils.FileUpdater;
Expand All @@ -16,6 +18,7 @@
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -51,6 +54,11 @@ public ModuleBuilder(Project project) {
params.put("objectMapperVersion", Constants.RCOMMONS_OBJECT_MAPPER_VERSION);
params.put("coberturaVersion", Constants.COBERTURA_VERSION);
params.put("lombokVersion", Constants.LOMBOK_VERSION);
try {
loadPackage();
} catch (IOException e) {
logger.warn("cannot read gradle.properties");
}
}

public void persist() throws IOException {
Expand Down Expand Up @@ -194,6 +202,22 @@ public Boolean isEnableLombok() {
return getABooleanProperty("lombok");
}

@SafeVarargs
public final <T extends Validation> void runValidations(Class<T>... validations)
throws ValidationException {
for (Iterator<Class<T>> it = Arrays.stream(validations).iterator(); it.hasNext(); ) {
try {
T validation = it.next().getDeclaredConstructor().newInstance();
validation.validate(this);
} catch (InstantiationException
| IllegalAccessException
| InvocationTargetException
| NoSuchMethodException e) {
logger.warn("Error instantiating validator: {}", e.getMessage());
}
}
}

private Boolean getABooleanProperty(String property) {
try {
String value = FileUtils.readProperties(project.getProjectDir().getPath(), property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.validations.ReactiveTypeValidation;
import java.io.IOException;

public class DrivenAdapterAsyncEventBus implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.loadPackage();
builder.runValidations(ReactiveTypeValidation.class);
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 @@ -9,7 +9,6 @@
public class DrivenAdapterGeneric implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.loadPackage();
String name = builder.getStringParam("task-param-name");
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
public class DrivenAdapterJPA implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.loadPackage();
builder.setupFromTemplate("driven-adapter/jpa-repository");
builder.appendToSettings("jpa-repository", "infrastructure/driven-adapters");
builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class DrivenAdapterKms implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
Logger logger = builder.getProject().getLogger();
builder.loadPackage();
String typePath = getPathType(builder.isReactive());
logger.lifecycle("Generating {}", typePath);
builder.setupFromTemplate("driven-adapter/" + typePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class DrivenAdapterMongoDB implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
Logger logger = builder.getProject().getLogger();
builder.loadPackage();
if (builder.isReactive()) {
logger.lifecycle("Generating for reactive project");
builder.setupFromTemplate("driven-adapter/mongo-reactive");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package co.com.bancolombia.factory.adapters;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.exceptions.InvalidTaskOptionException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.validations.ReactiveTypeValidation;
import java.io.IOException;
import org.gradle.api.logging.Logger;

Expand All @@ -12,17 +12,10 @@ public class DrivenAdapterR2dbcPostgreSQL implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
Logger logger = builder.getProject().getLogger();
builder.loadPackage();

if (builder.isReactive()) {
logger.lifecycle("Generating for reactive project");
builder.setupFromTemplate("driven-adapter/r2dbc-postgresql");
builder.appendDependencyToModule(
"app-service", "implementation project(':r2dbc-postgresql')");
builder.appendToSettings("r2dbc-postgresql", "infrastructure/driven-adapters");
} else {
throw new InvalidTaskOptionException(
"R2dbc postgresql Driven Adapter is only available in reactive projects");
}
builder.runValidations(ReactiveTypeValidation.class);
logger.lifecycle("Generating for reactive project");
builder.setupFromTemplate("driven-adapter/r2dbc-postgresql");
builder.appendDependencyToModule("app-service", "implementation project(':r2dbc-postgresql')");
builder.appendToSettings("r2dbc-postgresql", "infrastructure/driven-adapters");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package co.com.bancolombia.factory.adapters;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.validations.ReactiveTypeValidation;
import java.io.IOException;

public class DrivenAdapterRSocketRequester implements ModuleFactory {

@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.runValidations(ReactiveTypeValidation.class);
builder.appendToSettings("rsocket-requester", "infrastructure/driven-adapters");
builder.appendDependencyToModule("app-service", "implementation project(':rsocket-requester')");
builder.setupFromTemplate("driven-adapter/rsocket-requester");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class DrivenAdapterRedis implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
Logger logger = builder.getProject().getLogger();
builder.loadPackage();
String typePath = getPathType(builder.isReactive());
String modePath = getPathMode((Mode) builder.getParam(PARAM_MODE));
logger.lifecycle("Generating {} in {} mode", typePath, modePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class DrivenAdapterRestConsumer implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
Logger logger = builder.getProject().getLogger();
builder.loadPackage();
if (builder.isReactive()) {
logger.lifecycle("Generating rest-consumer for reactive project");
builder.setupFromTemplate("driven-adapter/consumer-rest/reactive-rest-consumer");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class DrivenAdapterS3 implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
Logger logger = builder.getProject().getLogger();
builder.loadPackage();
String typePath = getPathType(builder.isReactive());
logger.lifecycle("Generating {}", typePath);
builder.setupFromTemplate("driven-adapter/" + typePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
public class DrivenAdapterSecrets implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.loadPackage();
Logger logger = builder.getProject().getLogger();
String secretLibrary = "";
if (builder.isReactive()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static ModuleFactory getDrivenAdapterFactory(DrivenAdapterType type)
case REDIS:
return new DrivenAdapterRedis();
case RSOCKET:
return new DrivenAdapterRsocketRequester();
return new DrivenAdapterRSocketRequester();
case R2DBC:
return new DrivenAdapterR2dbcPostgreSQL();
case S3:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.validations.ReactiveTypeValidation;
import java.io.IOException;

public class EntryPointAsyncEventHandler implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.loadPackage();
builder.runValidations(ReactiveTypeValidation.class);
builder.setupFromTemplate("entry-point/async-event-handler");
builder.appendToSettings("async-event-handler", "infrastructure/entry-points");
builder.appendDependencyToModule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
public class EntryPointGeneric implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.loadPackage();
String name = builder.getStringParam("task-param-name");
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package co.com.bancolombia.factory.entrypoints;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.exceptions.InvalidTaskOptionException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.validations.ReactiveTypeValidation;
import java.io.IOException;

public class EntryPointGraphql implements ModuleFactory {

@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.loadPackage();
builder.runValidations(ReactiveTypeValidation.class);
String name = builder.getStringParam("task-param-pathgql");
if (!name.startsWith("/")) {
throw new IllegalArgumentException("The path must start with /");
}
if (!builder.isReactive()) {
builder.appendToSettings("graphql-api", "infrastructure/entry-points");
builder.appendToProperties("graphql.servlet").put("enabled", true).put("mapping", name);
builder
.appendToProperties("graphql.playground")
.put("mapping", "/playground")
.put("endpoint", name)
.put("enabled", true);
builder.appendDependencyToModule("app-service", "implementation project(':graphql-api')");
builder.setupFromTemplate("entry-point/graphql-api");
} else {
throw new InvalidTaskOptionException("Graphql API is only available on imperative projects");
}
builder.appendToSettings("graphql-api", "infrastructure/entry-points");
builder.appendToProperties("graphql.servlet").put("enabled", true).put("mapping", name);
builder
.appendToProperties("graphql.playground")
.put("mapping", "/playground")
.put("endpoint", name)
.put("enabled", true);
builder.appendDependencyToModule("app-service", "implementation project(':graphql-api')");
builder.setupFromTemplate("entry-point/graphql-api");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package co.com.bancolombia.factory.entrypoints;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.validations.ReactiveTypeValidation;
import java.io.IOException;

public class EntryPointRSocketResponder implements ModuleFactory {

@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.runValidations(ReactiveTypeValidation.class);
builder.appendToSettings("rsocket-responder", "infrastructure/entry-points");
builder.appendToProperties("spring.rsocket.server").put("port", 7000);
builder.appendDependencyToModule("app-service", "implementation project(':rsocket-responder')");
builder.setupFromTemplate("entry-point/rsocket-responder");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class EntryPointRestMvc implements ModuleFactory {

@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
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
Expand Up @@ -3,12 +3,13 @@
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.validations.ReactiveTypeValidation;
import java.io.IOException;

public class EntryPointRestWebflux implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.loadPackage();
builder.runValidations(ReactiveTypeValidation.class);
if (builder.getBooleanParam("task-param-router")) {
builder.setupFromTemplate("entry-point/rest-webflux/router-functions");
} else {
Expand Down
Loading