diff --git a/.gitignore b/.gitignore index a3bd716..f3d91dc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,8 @@ spring-scaffold-cli/target/ spring-scaffold-cli/*.iml generator-core/target/ generator-core/*.iml -templates-java/target/ -templates-java/*.iml +templates/target/ +templates/*.iml *.iml .idea/ diff --git a/generator-core/pom.xml b/generator-core/pom.xml index bcb8260..07ac8bb 100644 --- a/generator-core/pom.xml +++ b/generator-core/pom.xml @@ -5,7 +5,7 @@ spring-boot-cli br.com.netodevel - 0.0.2.BUILD-SNAPSHOT + 1.1.0.BUILD-SNAPSHOT 4.0.0 generator-core diff --git a/generator-core/src/main/java/br/com/generator/core/GeneratorExecutor.java b/generator-core/src/main/java/br/com/generator/core/GeneratorExecutor.java index 7b8bbcb..b46be23 100644 --- a/generator-core/src/main/java/br/com/generator/core/GeneratorExecutor.java +++ b/generator-core/src/main/java/br/com/generator/core/GeneratorExecutor.java @@ -30,6 +30,18 @@ public File generate(GeneratorOptions options) throws IOException { public File addDependecies(GeneratorOptions options) throws IOException { String contentTemplate = loadPom(options.getTemplatePath()); + + Boolean alreadyDeps = false; + if (options.getDependencies() != null) { + for (int i = 0; i < options.getDependencies().size(); i++) { + if (contentTemplate.contains(options.getDependencies().get(i))) { + System.out.println("[INFO] ".concat(options.getDependencies().get(i)).concat(" already added.")); + alreadyDeps = true; + } + } + } + if (alreadyDeps) return null; + String contentReplaced = templateEngine.replaceValues(contentTemplate, options.getKeyValue()); File fileGenerated = new File(options.getDestination()); diff --git a/generator-core/src/main/java/br/com/generator/core/GeneratorOptions.java b/generator-core/src/main/java/br/com/generator/core/GeneratorOptions.java index 07c4ef0..39637db 100644 --- a/generator-core/src/main/java/br/com/generator/core/GeneratorOptions.java +++ b/generator-core/src/main/java/br/com/generator/core/GeneratorOptions.java @@ -1,5 +1,6 @@ package br.com.generator.core; +import java.util.List; import java.util.Map; public class GeneratorOptions { @@ -10,6 +11,7 @@ public class GeneratorOptions { private String properties; private Map pluginValues; private Map keyValue; + private List dependencies; public String getDestination() { return destination; @@ -58,4 +60,12 @@ public Map getPluginValues() { public void setPluginValues(Map pluginValues) { this.pluginValues = pluginValues; } + + public List getDependencies() { + return dependencies; + } + + public void setDependencies(List dependencies) { + this.dependencies = dependencies; + } } diff --git a/pom.xml b/pom.xml index bde34c8..3a6af01 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 br.com.netodevel spring-boot-cli - 0.0.2.BUILD-SNAPSHOT + 1.1.0.BUILD-SNAPSHOT pom cli-spring-boot-scaffold @@ -14,7 +14,7 @@ spring-scaffold-cli spring-boot-generate generator-core - templates-java + templates diff --git a/spring-boot-generate/pom.xml b/spring-boot-generate/pom.xml index e10031c..35f5194 100644 --- a/spring-boot-generate/pom.xml +++ b/spring-boot-generate/pom.xml @@ -3,7 +3,7 @@ 4.0.0 br.com spring-boot-generate - 0.0.2-SNAPSHOT + 1.1.0.BUILD-SNAPSHOT UTF-8 diff --git a/spring-scaffold-cli/pom.xml b/spring-scaffold-cli/pom.xml index ffa6972..a23a137 100644 --- a/spring-scaffold-cli/pom.xml +++ b/spring-scaffold-cli/pom.xml @@ -3,7 +3,7 @@ 4.0.0 br.com.netodevel spring-scaffold-cli - 0.0.2-SNAPSHOT + 1.1.0.BUILD-SNAPSHOT org.springframework.boot @@ -26,13 +26,13 @@ br.com spring-boot-generate - 0.0.2-SNAPSHOT + 1.1.0.BUILD-SNAPSHOT br.com.netodevel - templates-java - 0.0.2.BUILD-SNAPSHOT + templates + 1.1.0.BUILD-SNAPSHOT diff --git a/spring-scaffold-cli/src/main/java/br/com/command/ScaffoldFactoryCommands.java b/spring-scaffold-cli/src/main/java/br/com/command/ScaffoldFactoryCommands.java index 43c4324..b9bb059 100644 --- a/spring-scaffold-cli/src/main/java/br/com/command/ScaffoldFactoryCommands.java +++ b/spring-scaffold-cli/src/main/java/br/com/command/ScaffoldFactoryCommands.java @@ -1,10 +1,9 @@ package br.com.command; -import br.com.command.model.ModelHandler; -import br.com.generate.helpers.ScaffoldInfoHelper; import br.com.command.controller.ControllerCommand; import br.com.command.controller.ControllerHandler; import br.com.command.model.ModelCommand; +import br.com.command.model.ModelHandler; import br.com.command.repository.RepositoryCommand; import br.com.command.repository.RepositoryHandler; import br.com.command.scaffold.ScaffoldCommand; @@ -15,6 +14,7 @@ import br.com.command.setup.SetupScaffoldHandler; import br.com.command.template.TemplateCommand; import br.com.command.template.TemplateHandler; +import br.com.generate.helpers.ScaffoldInfoHelper; import org.springframework.boot.cli.command.Command; import org.springframework.boot.cli.command.CommandFactory; @@ -30,14 +30,15 @@ public class ScaffoldFactoryCommands implements CommandFactory { public Collection getCommands() { + ScaffoldInfoHelper scaffoldInfoHelper = new ScaffoldInfoHelper(); return Arrays.asList( - new ModelCommand("model", "generate entities", new ModelHandler()), + new ModelCommand("model", "generate entities", new ModelHandler(scaffoldInfoHelper)), new RepositoryCommand("repository", "generate repositories", new RepositoryHandler()), new ServiceCommand("service", "generate services", new ServiceHandler()), new ControllerCommand("controller", "generate controllers", new ControllerHandler()), new ScaffoldCommand("scaffold", "generate api scaffold", new ScaffoldHandler()), new SetupScaffoldCommand("setup:scaffold", "setup scaffold", new SetupScaffoldHandler()), - new TemplateCommand("template", "generate setup project", new TemplateHandler(new ScaffoldInfoHelper()))); + new TemplateCommand("template", "generate setup project", new TemplateHandler(scaffoldInfoHelper))); } } diff --git a/spring-scaffold-cli/src/main/java/br/com/command/model/ModelCommand.java b/spring-scaffold-cli/src/main/java/br/com/command/model/ModelCommand.java index 3ef3aae..172c05b 100644 --- a/spring-scaffold-cli/src/main/java/br/com/command/model/ModelCommand.java +++ b/spring-scaffold-cli/src/main/java/br/com/command/model/ModelCommand.java @@ -1,10 +1,5 @@ package br.com.command.model; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.springframework.boot.cli.command.HelpExample; import org.springframework.boot.cli.command.OptionParsingCommand; import org.springframework.boot.cli.command.options.OptionHandler; @@ -16,20 +11,11 @@ public class ModelCommand extends OptionParsingCommand { public ModelCommand(String name, String description, OptionHandler handler) { - super("model", "generate entities", new ModelHandler()); - } - - public String getUsageHelp() { - // TODO Auto-generated method stub - return "[name-entity] [attributes] [language]"; + super(name, description, handler); } @Override - public Collection getExamples() { - List list = new ArrayList(); - list.add(new HelpExample("create entities java", "model -n User -p name:String")); - list.add(new HelpExample("create entities kotlin", "model -n User -p name:String -l kotlin")); - return list; + public String getUsageHelp() { + return " "; } - } diff --git a/spring-scaffold-cli/src/main/java/br/com/command/model/ModelHandler.java b/spring-scaffold-cli/src/main/java/br/com/command/model/ModelHandler.java index ae6baa8..ac8fb2f 100644 --- a/spring-scaffold-cli/src/main/java/br/com/command/model/ModelHandler.java +++ b/spring-scaffold-cli/src/main/java/br/com/command/model/ModelHandler.java @@ -1,45 +1,96 @@ package br.com.command.model; -import java.io.IOException; -import java.util.Arrays; - +import br.com.generate.helpers.ScaffoldInfoHelper; +import br.com.generator.core.GeneratorOptions; +import br.com.templates.entity.EntityCache; +import br.com.templates.entity.EntityExecutor; +import br.com.templates.entity.EntityGenerator; +import br.com.templates.entity.LombokDependencyGenerator; import joptsimple.OptionSet; import joptsimple.OptionSpec; - import org.springframework.boot.cli.command.options.OptionHandler; import org.springframework.boot.cli.command.status.ExitStatus; -import br.com.generate.source.model.ModelGenerator; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; /** - * * @author NetoDevel * @since 0.0.1 */ public class ModelHandler extends OptionHandler { - @SuppressWarnings("unused") - private OptionSpec nameEntity; - - @SuppressWarnings("unused") - private OptionSpec parametersEntity; - - @Override - protected void options() { - this.nameEntity = option(Arrays.asList("nameEntity", "n"), "Name of entity to generate").withRequiredArg(); - this.parametersEntity = option(Arrays.asList("parameterEntity", "p"), "Parameter of entity to generate").withRequiredArg(); - } - - @Override - protected ExitStatus run(OptionSet options) throws Exception { - String nameClass = (String) options.valueOf("n"); - String parametersClass = (String) options.valueOf("p"); - generateModelJava(nameClass, parametersClass); - return ExitStatus.OK; - } - - private void generateModelJava(String nameClass, String parameters) throws IOException { - new ModelGenerator().generate(nameClass, parameters, "template-model.txt"); - } - + @SuppressWarnings("unused") + private OptionSpec nameEntity; + + @SuppressWarnings("unused") + private OptionSpec parametersEntity; + + private ScaffoldInfoHelper scaffoldInfoHelper; + + public ModelHandler() { + } + + public ModelHandler(ScaffoldInfoHelper scaffoldInfoHelper) { + this.scaffoldInfoHelper = scaffoldInfoHelper; + } + + @Override + protected void options() { + this.nameEntity = option(Arrays.asList("nameEntity", "n"), "Name of entity").withRequiredArg(); + this.parametersEntity = option(Arrays.asList("parameterEntity", "p"), "Parameters of entity").withRequiredArg(); + } + + @Override + protected ExitStatus run(OptionSet options) throws Exception { + String nameClass = (String) options.valueOf("n"); + String parametersClass = (String) options.valueOf("p"); + + if (nameClass == null || nameClass.replace(" ", "").isEmpty()) { + System.out.println("[INFO] - name of entity is required. use: -n ${entity_name}"); + return ExitStatus.ERROR; + } + if (parametersClass == null || parametersClass.replace(" ", "").isEmpty()){ + System.out.println("[INFO] - parameters of entity is required. use: -p ${parameters}"); + return ExitStatus.ERROR; + } + + generateModelJava(nameClass, parametersClass); + return ExitStatus.OK; + } + + private void generateModelJava(String nameClass, String parameters) throws IOException { + EntityExecutor entityExecutor = new EntityExecutor(); + entityExecutor.run(nameClass, parameters); + + lombokGenerate(); + + for (EntityCache entity : entityExecutor.getEntities()) { + GeneratorOptions generatorOptions = new GeneratorOptions(); + generatorOptions.setName(entity.getName().concat(".java")); + generatorOptions.setDestination(scaffoldInfoHelper.getPathPackage().concat("models")); + + Map keyValue = new HashMap<>(); + keyValue.put("${content}", entity.getContent()); + keyValue.put("${package}", scaffoldInfoHelper.getPackage().concat(".models")); + + generatorOptions.setKeyValue(keyValue); + + EntityGenerator entityGenerator = new EntityGenerator(generatorOptions); + entityGenerator.runGenerate(); + } + + } + + private void lombokGenerate() throws IOException { + GeneratorOptions lombokDepsOptions = new GeneratorOptions(); + lombokDepsOptions.setTemplatePath(scaffoldInfoHelper.getPomPath()); + lombokDepsOptions.setDestination(scaffoldInfoHelper.getPomDest()); + + LombokDependencyGenerator lombokDependencyGenerator = new LombokDependencyGenerator(lombokDepsOptions); + lombokDependencyGenerator.runGenerate(); + } + } diff --git a/spring-scaffold-cli/src/main/java/br/com/command/template/TemplateHandler.java b/spring-scaffold-cli/src/main/java/br/com/command/template/TemplateHandler.java index c06fd52..0e4d114 100644 --- a/spring-scaffold-cli/src/main/java/br/com/command/template/TemplateHandler.java +++ b/spring-scaffold-cli/src/main/java/br/com/command/template/TemplateHandler.java @@ -2,10 +2,10 @@ import br.com.generate.helpers.ScaffoldInfoHelper; import br.com.generator.core.GeneratorOptions; -import br.com.templates_java.ComposeTemplate; -import br.com.templates_java.config.jms_aws_sqs.*; -import br.com.templates_java.config.openj9.OpenJ9DockerfileGenerator; -import br.com.templates_java.config.openj9.OpenJ9MavenPluginGenerator; +import br.com.templates.ComposeTemplate; +import br.com.templates.config.jms_aws_sqs.*; +import br.com.templates.config.openj9.OpenJ9DockerfileGenerator; +import br.com.templates.config.openj9.OpenJ9MavenPluginGenerator; import joptsimple.OptionSet; import joptsimple.OptionSpec; import org.springframework.boot.cli.command.options.OptionHandler; diff --git a/spring-scaffold-cli/src/test/java/br/com/command/model/ModelHandlerTest.java b/spring-scaffold-cli/src/test/java/br/com/command/model/ModelHandlerTest.java new file mode 100644 index 0000000..ec238c8 --- /dev/null +++ b/spring-scaffold-cli/src/test/java/br/com/command/model/ModelHandlerTest.java @@ -0,0 +1,61 @@ +package br.com.command.model; + +import br.com.generate.helpers.ScaffoldInfoHelper; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.Mockito; +import org.springframework.boot.cli.command.status.ExitStatus; + +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +public class ModelHandlerTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private File temporaryPath; + + @Before + public void setUp() throws IOException { + temporaryPath = temporaryFolder.newFolder("test-path"); + } + + @Test + public void shouldReturnOk() throws Exception { + ScaffoldInfoHelper scaffoldInfoHelper = mock(ScaffoldInfoHelper.class); + + Mockito.when(scaffoldInfoHelper.getPackage()).thenReturn("com.example"); + Mockito.when(scaffoldInfoHelper.getPathPackage()).thenReturn(temporaryPath.getAbsolutePath().concat("\\com\\example\\")); + Mockito.when(scaffoldInfoHelper.getPomPath()).thenReturn(getClass().getResource("/templates/template-fake-pom.xml").toURI().getPath()); + Mockito.when(scaffoldInfoHelper.getPomDest()).thenReturn(temporaryPath.getAbsolutePath().concat("/pom.xml")); + + ModelHandler modelHandler = new ModelHandler(scaffoldInfoHelper); + ExitStatus exitStatus = modelHandler.run("-n", "User", "-p", "name:String Foo:references(relation:hasMany, name:String)"); + assertEquals(ExitStatus.OK, exitStatus); + } + + @Test + public void givenNoParameters_shouldReturnError() throws Exception { + ScaffoldInfoHelper scaffoldInfoHelper = mock(ScaffoldInfoHelper.class); + + ModelHandler modelHandler = new ModelHandler(scaffoldInfoHelper); + ExitStatus exitStatus = modelHandler.run("-n", "USer"); + assertEquals(ExitStatus.ERROR, exitStatus); + } + + @Test + public void givenNoClass_shouldReturnError() throws Exception { + ScaffoldInfoHelper scaffoldInfoHelper = mock(ScaffoldInfoHelper.class); + + ModelHandler modelHandler = new ModelHandler(scaffoldInfoHelper); + ExitStatus exitStatus = modelHandler.run("-p", "foo:String"); + assertEquals(ExitStatus.ERROR, exitStatus); + } + +} \ No newline at end of file diff --git a/templates-java/pom.xml b/templates/pom.xml similarity index 87% rename from templates-java/pom.xml rename to templates/pom.xml index 2ca0ff8..cbeac07 100644 --- a/templates-java/pom.xml +++ b/templates/pom.xml @@ -5,11 +5,8 @@ spring-boot-cli br.com.netodevel - 0.0.2.BUILD-SNAPSHOT + 1.1.0.BUILD-SNAPSHOT - 4.0.0 - templates-java - @@ -22,11 +19,14 @@ + 4.0.0 + + templates br.com.netodevel generator-core - 0.0.2.BUILD-SNAPSHOT + 1.1.0.BUILD-SNAPSHOT compile diff --git a/templates-java/src/main/java/br/com/templates_java/ComposeTemplate.java b/templates/src/main/java/br/com/templates/ComposeTemplate.java similarity index 91% rename from templates-java/src/main/java/br/com/templates_java/ComposeTemplate.java rename to templates/src/main/java/br/com/templates/ComposeTemplate.java index a276131..4178a9d 100644 --- a/templates-java/src/main/java/br/com/templates_java/ComposeTemplate.java +++ b/templates/src/main/java/br/com/templates/ComposeTemplate.java @@ -1,4 +1,4 @@ -package br.com.templates_java; +package br.com.templates; import br.com.generator.core.Generator; diff --git a/templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/EntryPointMessageGenerator.java b/templates/src/main/java/br/com/templates/config/jms_aws_sqs/EntryPointMessageGenerator.java similarity index 93% rename from templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/EntryPointMessageGenerator.java rename to templates/src/main/java/br/com/templates/config/jms_aws_sqs/EntryPointMessageGenerator.java index ee0247f..f5abf4c 100644 --- a/templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/EntryPointMessageGenerator.java +++ b/templates/src/main/java/br/com/templates/config/jms_aws_sqs/EntryPointMessageGenerator.java @@ -1,4 +1,4 @@ -package br.com.templates_java.config.jms_aws_sqs; +package br.com.templates.config.jms_aws_sqs; import br.com.generator.core.Generator; import br.com.generator.core.GeneratorOptions; diff --git a/templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/MessageListenerGenerator.java b/templates/src/main/java/br/com/templates/config/jms_aws_sqs/MessageListenerGenerator.java similarity index 93% rename from templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/MessageListenerGenerator.java rename to templates/src/main/java/br/com/templates/config/jms_aws_sqs/MessageListenerGenerator.java index 91a470e..3608244 100644 --- a/templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/MessageListenerGenerator.java +++ b/templates/src/main/java/br/com/templates/config/jms_aws_sqs/MessageListenerGenerator.java @@ -1,4 +1,4 @@ -package br.com.templates_java.config.jms_aws_sqs; +package br.com.templates.config.jms_aws_sqs; import br.com.generator.core.Generator; import br.com.generator.core.GeneratorOptions; diff --git a/templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/ProducerMessageGenerator.java b/templates/src/main/java/br/com/templates/config/jms_aws_sqs/ProducerMessageGenerator.java similarity index 92% rename from templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/ProducerMessageGenerator.java rename to templates/src/main/java/br/com/templates/config/jms_aws_sqs/ProducerMessageGenerator.java index d642bd6..69cb6d8 100644 --- a/templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/ProducerMessageGenerator.java +++ b/templates/src/main/java/br/com/templates/config/jms_aws_sqs/ProducerMessageGenerator.java @@ -1,4 +1,4 @@ -package br.com.templates_java.config.jms_aws_sqs; +package br.com.templates.config.jms_aws_sqs; import br.com.generator.core.Generator; diff --git a/templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/SQSDependencyGenerator.java b/templates/src/main/java/br/com/templates/config/jms_aws_sqs/SQSDependencyGenerator.java similarity index 96% rename from templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/SQSDependencyGenerator.java rename to templates/src/main/java/br/com/templates/config/jms_aws_sqs/SQSDependencyGenerator.java index c4a9cd8..a8abc28 100644 --- a/templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/SQSDependencyGenerator.java +++ b/templates/src/main/java/br/com/templates/config/jms_aws_sqs/SQSDependencyGenerator.java @@ -1,4 +1,4 @@ -package br.com.templates_java.config.jms_aws_sqs; +package br.com.templates.config.jms_aws_sqs; import br.com.generator.core.Generator; import br.com.generator.core.GeneratorOptions; diff --git a/templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/SQSPropertiesGenerator.java b/templates/src/main/java/br/com/templates/config/jms_aws_sqs/SQSPropertiesGenerator.java similarity index 95% rename from templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/SQSPropertiesGenerator.java rename to templates/src/main/java/br/com/templates/config/jms_aws_sqs/SQSPropertiesGenerator.java index 01c86f1..1e92222 100644 --- a/templates-java/src/main/java/br/com/templates_java/config/jms_aws_sqs/SQSPropertiesGenerator.java +++ b/templates/src/main/java/br/com/templates/config/jms_aws_sqs/SQSPropertiesGenerator.java @@ -1,4 +1,4 @@ -package br.com.templates_java.config.jms_aws_sqs; +package br.com.templates.config.jms_aws_sqs; import br.com.generator.core.Generator; import br.com.generator.core.GeneratorOptions; diff --git a/templates-java/src/main/java/br/com/templates_java/config/openj9/OpenJ9DockerfileGenerator.java b/templates/src/main/java/br/com/templates/config/openj9/OpenJ9DockerfileGenerator.java similarity index 93% rename from templates-java/src/main/java/br/com/templates_java/config/openj9/OpenJ9DockerfileGenerator.java rename to templates/src/main/java/br/com/templates/config/openj9/OpenJ9DockerfileGenerator.java index b3df4fb..37ac3f1 100644 --- a/templates-java/src/main/java/br/com/templates_java/config/openj9/OpenJ9DockerfileGenerator.java +++ b/templates/src/main/java/br/com/templates/config/openj9/OpenJ9DockerfileGenerator.java @@ -1,4 +1,4 @@ -package br.com.templates_java.config.openj9; +package br.com.templates.config.openj9; import br.com.generator.core.Generator; import br.com.generator.core.GeneratorOptions; diff --git a/templates-java/src/main/java/br/com/templates_java/config/openj9/OpenJ9MavenPluginGenerator.java b/templates/src/main/java/br/com/templates/config/openj9/OpenJ9MavenPluginGenerator.java similarity index 98% rename from templates-java/src/main/java/br/com/templates_java/config/openj9/OpenJ9MavenPluginGenerator.java rename to templates/src/main/java/br/com/templates/config/openj9/OpenJ9MavenPluginGenerator.java index d24c3b1..5405222 100644 --- a/templates-java/src/main/java/br/com/templates_java/config/openj9/OpenJ9MavenPluginGenerator.java +++ b/templates/src/main/java/br/com/templates/config/openj9/OpenJ9MavenPluginGenerator.java @@ -1,4 +1,4 @@ -package br.com.templates_java.config.openj9; +package br.com.templates.config.openj9; import br.com.generator.core.Generator; import br.com.generator.core.GeneratorOptions; diff --git a/templates/src/main/java/br/com/templates/entity/EntityCache.java b/templates/src/main/java/br/com/templates/entity/EntityCache.java new file mode 100644 index 0000000..8f8d692 --- /dev/null +++ b/templates/src/main/java/br/com/templates/entity/EntityCache.java @@ -0,0 +1,28 @@ +package br.com.templates.entity; + +public class EntityCache { + + private String name; + private String content; + + public EntityCache(String name, String content) { + this.name = name; + this.content = content; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/templates/src/main/java/br/com/templates/entity/EntityExecutor.java b/templates/src/main/java/br/com/templates/entity/EntityExecutor.java new file mode 100644 index 0000000..951f101 --- /dev/null +++ b/templates/src/main/java/br/com/templates/entity/EntityExecutor.java @@ -0,0 +1,147 @@ +package br.com.templates.entity; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static java.util.Arrays.asList; + +public class EntityExecutor { + + private static final List supportedTypes = asList("String", "Int", "Date"); + private List entities; + + public String run(String nameClass, String argumentValue) { + entities = new ArrayList<>(); + String generatedClass = this.generateClass(nameClass); + + argumentValue = toPatternScaffold(argumentValue); + String[] attributes = argumentValue.split(" "); + + StringBuilder attributeToReplace = new StringBuilder(); + + String idAttribute = + "\n" + + "\t@Id @GeneratedValue(strategy = GenerationType.AUTO)\n" + + "\tprivate Integer id;"; + + attributeToReplace.append("\t".concat(idAttribute.concat("\n"))); + + for (int i = 0; i < attributes.length; i++) { + String attribute = generateAttribute(attributes[i]); + attributeToReplace.append("\t".concat(attribute.concat("\n"))); + } + + generatedClass = generatedClass.replace("${attributes}", attributeToReplace.toString()); + entities.add(new EntityCache(nameClass, generatedClass)); + return generatedClass; + } + + public String generateAttribute(String attribute) { + if (attribute.contains("references")) { + String relationship = generateRelationship(attribute); + if (relationship != null) return relationship; + } + + validateTokenAtrribute(attribute); + String[] splitAttribute = attribute.split(":"); + validateTypes(splitAttribute[1]); + + if (attribute.contains("Int")) return "private Integer ".concat(splitAttribute[0]).concat(";"); + if (attribute.contains("Date")) return "private Date ".concat(splitAttribute[0]).concat(";"); + if (attribute.contains("String")) return "private String ".concat(splitAttribute[0]).concat(";"); + + return null; + } + + private String generateRelationship(String attribute) { + String attributesOfReferences = getAttributesOfReferences(attribute); + String[] attributes = attributesOfReferences.split(","); + + String classIdentify = attribute.replace(attributesOfReferences, ""); + String[] indentifySplit = classIdentify.split(":"); + + String clazzName = indentifySplit[0]; + String relation = attributes[0].split(":")[1]; + + generateRelationshipClass(attributes, clazzName); + + if (relation.equals("hasMany")) + return ("\t\n" + + "\t@OneToMany\n"+ + "\tprivate List<").concat(clazzName).concat(">").concat(" ".concat(clazzName.toLowerCase()).concat(";")); + if (relation.equals("belongsTo")) + return ("\t\n" + + "\t@OneToOne\n" + + "\tprivate ").concat(clazzName).concat(" ".concat(clazzName.toLowerCase()).concat(";")); + return null; + } + + private void generateRelationshipClass(String[] attributes, String clazzName) { + String generatedClass = this.generateClass(clazzName); + StringBuilder attributeToReplace = new StringBuilder(); + for (int i = 1; i < attributes.length; i++) { + String attributeOfRelation = generateAttribute(attributes[i]); + attributeToReplace.append("\t".concat(attributeOfRelation.concat("\n"))); + } + + generatedClass = generatedClass.replace("${attributes}", attributeToReplace.toString()); + if (entities != null) entities.add(new EntityCache(clazzName, generatedClass)); + } + + private void validateTypes(String o) { + if (!supportedTypes.contains(o)) + throw new EntityValidator(o.concat(" not supported.")); + } + + private void validateTokenAtrribute(String attribute) { + if (!attribute.contains(":")) throw new EntityValidator("attribute should contains ':' "); + } + + public String generateClass(String nameClass) { + return "" + + "@Entity\n" + + "@Data\n" + + "public class ".concat(nameClass).concat(" {\n") + + "${attributes}" + + "}"; + } + + public String getAttributesOfReferences(String attributes) { + Pattern pattern = Pattern.compile("\\((.*?)\\)"); + Matcher matcher = pattern.matcher(attributes); + String result = ""; + if (matcher.find()) { + result = matcher.group(1); + } + return result; + } + + public String getRelation(String attributes) { + String[] splitAttributes = attributes.split(" "); + String[] splitRelation = splitAttributes[0].split(":"); + return splitRelation[1]; + } + + public List getReferencesComplete(String attributes) { + String attributesConverted = toPatternScaffold(attributes); + String[] attributesSplited = attributesConverted.split(" "); + + List attributesToReturn = new ArrayList<>(); + for (int i = 0; i < attributesSplited.length; i++) { + if (attributesSplited[i].contains("references")) { + attributesToReturn.add(attributesSplited[i]); + } + } + return attributesToReturn; + } + + public String toPatternScaffold(String attributes) { + return attributes.replace(", ", ","); + } + + public List getEntities() { + return entities; + } +} diff --git a/templates/src/main/java/br/com/templates/entity/EntityGenerator.java b/templates/src/main/java/br/com/templates/entity/EntityGenerator.java new file mode 100644 index 0000000..48b4e33 --- /dev/null +++ b/templates/src/main/java/br/com/templates/entity/EntityGenerator.java @@ -0,0 +1,22 @@ +package br.com.templates.entity; + +import br.com.generator.core.Generator; +import br.com.generator.core.GeneratorOptions; + +import java.io.File; +import java.io.IOException; + +public class EntityGenerator extends Generator { + + private GeneratorOptions generatorOptions; + + public EntityGenerator(GeneratorOptions generatorOptions) { + this.generatorOptions = generatorOptions; + } + + @Override + public File runGenerate() throws IOException { + this.generatorOptions.setTemplatePath("/templates.entity/entity-template.txt"); + return generate(this.generatorOptions); + } +} diff --git a/templates/src/main/java/br/com/templates/entity/EntityValidator.java b/templates/src/main/java/br/com/templates/entity/EntityValidator.java new file mode 100644 index 0000000..90b7a52 --- /dev/null +++ b/templates/src/main/java/br/com/templates/entity/EntityValidator.java @@ -0,0 +1,8 @@ +package br.com.templates.entity; + +public class EntityValidator extends RuntimeException { + + public EntityValidator(String msg) { + super(msg); + } +} diff --git a/templates/src/main/java/br/com/templates/entity/LombokDependencyGenerator.java b/templates/src/main/java/br/com/templates/entity/LombokDependencyGenerator.java new file mode 100644 index 0000000..d8b07c7 --- /dev/null +++ b/templates/src/main/java/br/com/templates/entity/LombokDependencyGenerator.java @@ -0,0 +1,38 @@ +package br.com.templates.entity; + +import br.com.generator.core.Generator; +import br.com.generator.core.GeneratorOptions; + +import java.io.File; +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class LombokDependencyGenerator extends Generator { + + private GeneratorOptions generatorOptions; + + public LombokDependencyGenerator(GeneratorOptions generatorOptions) { + this.generatorOptions = generatorOptions; + } + + public File runGenerate() throws IOException { + String dependency = "\n" + + " \n" + + " org.projectlombok\n" + + " lombok\n" + + " provided\n" + + " \n" + + ""; + + Map keyValue = new HashMap(); + keyValue.put("", dependency); + this.generatorOptions.setKeyValue(keyValue); + this.generatorOptions.setName("pom.xml"); + this.generatorOptions.setDependencies(Collections.singletonList("org.projectlombok")); + + return addDependency(this.generatorOptions); + } + +} diff --git a/templates/src/main/resources/templates.entity/entity-template.txt b/templates/src/main/resources/templates.entity/entity-template.txt new file mode 100644 index 0000000..e26697f --- /dev/null +++ b/templates/src/main/resources/templates.entity/entity-template.txt @@ -0,0 +1,6 @@ +package ${package}; + +import lombok.Data; +import java.util.List; + +${content} \ No newline at end of file diff --git a/templates-java/src/main/resources/templates/config/openj9/dockerfile-template.txt b/templates/src/main/resources/templates/config/openj9/dockerfile-template.txt similarity index 100% rename from templates-java/src/main/resources/templates/config/openj9/dockerfile-template.txt rename to templates/src/main/resources/templates/config/openj9/dockerfile-template.txt diff --git a/templates-java/src/main/resources/templates/config/template-entrypoint-listener.txt b/templates/src/main/resources/templates/config/template-entrypoint-listener.txt similarity index 100% rename from templates-java/src/main/resources/templates/config/template-entrypoint-listener.txt rename to templates/src/main/resources/templates/config/template-entrypoint-listener.txt diff --git a/templates-java/src/main/resources/templates/config/template-message-listener.txt b/templates/src/main/resources/templates/config/template-message-listener.txt similarity index 100% rename from templates-java/src/main/resources/templates/config/template-message-listener.txt rename to templates/src/main/resources/templates/config/template-message-listener.txt diff --git a/templates-java/src/main/resources/templates/config/template-producer-message.txt b/templates/src/main/resources/templates/config/template-producer-message.txt similarity index 100% rename from templates-java/src/main/resources/templates/config/template-producer-message.txt rename to templates/src/main/resources/templates/config/template-producer-message.txt diff --git a/templates-java/src/test/java/br/com/templates_java/config/jms_aws_sqs/EntryPointMessageGeneratorTest.java b/templates/src/test/java/br/com/templates/config/jms_aws_sqs/EntryPointMessageGeneratorTest.java similarity index 95% rename from templates-java/src/test/java/br/com/templates_java/config/jms_aws_sqs/EntryPointMessageGeneratorTest.java rename to templates/src/test/java/br/com/templates/config/jms_aws_sqs/EntryPointMessageGeneratorTest.java index 94550fb..90f4963 100644 --- a/templates-java/src/test/java/br/com/templates_java/config/jms_aws_sqs/EntryPointMessageGeneratorTest.java +++ b/templates/src/test/java/br/com/templates/config/jms_aws_sqs/EntryPointMessageGeneratorTest.java @@ -1,8 +1,8 @@ -package br.com.templates_java.config.jms_aws_sqs; +package br.com.templates.config.jms_aws_sqs; import br.com.generator.core.GeneratorOptions; -import br.com.templates_java.helper.LoadTemplateTester; +import br.com.templates.helper.LoadTemplateTester; import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Rule; diff --git a/templates-java/src/test/java/br/com/templates_java/config/jms_aws_sqs/MessageListenerGeneratorTest.java b/templates/src/test/java/br/com/templates/config/jms_aws_sqs/MessageListenerGeneratorTest.java similarity index 95% rename from templates-java/src/test/java/br/com/templates_java/config/jms_aws_sqs/MessageListenerGeneratorTest.java rename to templates/src/test/java/br/com/templates/config/jms_aws_sqs/MessageListenerGeneratorTest.java index 5a93f0e..8fe8022 100644 --- a/templates-java/src/test/java/br/com/templates_java/config/jms_aws_sqs/MessageListenerGeneratorTest.java +++ b/templates/src/test/java/br/com/templates/config/jms_aws_sqs/MessageListenerGeneratorTest.java @@ -1,7 +1,7 @@ -package br.com.templates_java.config.jms_aws_sqs; +package br.com.templates.config.jms_aws_sqs; import br.com.generator.core.GeneratorOptions; -import br.com.templates_java.helper.LoadTemplateTester; +import br.com.templates.helper.LoadTemplateTester; import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Rule; diff --git a/templates-java/src/test/java/br/com/templates_java/config/jms_aws_sqs/ProducerMessageGeneratorTest.java b/templates/src/test/java/br/com/templates/config/jms_aws_sqs/ProducerMessageGeneratorTest.java similarity index 95% rename from templates-java/src/test/java/br/com/templates_java/config/jms_aws_sqs/ProducerMessageGeneratorTest.java rename to templates/src/test/java/br/com/templates/config/jms_aws_sqs/ProducerMessageGeneratorTest.java index fd85734..b604105 100644 --- a/templates-java/src/test/java/br/com/templates_java/config/jms_aws_sqs/ProducerMessageGeneratorTest.java +++ b/templates/src/test/java/br/com/templates/config/jms_aws_sqs/ProducerMessageGeneratorTest.java @@ -1,7 +1,7 @@ -package br.com.templates_java.config.jms_aws_sqs; +package br.com.templates.config.jms_aws_sqs; import br.com.generator.core.GeneratorOptions; -import br.com.templates_java.helper.LoadTemplateTester; +import br.com.templates.helper.LoadTemplateTester; import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Rule; diff --git a/templates-java/src/test/java/br/com/templates_java/config/jms_aws_sqs/SQSDependencyGeneratorTest.java b/templates/src/test/java/br/com/templates/config/jms_aws_sqs/SQSDependencyGeneratorTest.java similarity index 95% rename from templates-java/src/test/java/br/com/templates_java/config/jms_aws_sqs/SQSDependencyGeneratorTest.java rename to templates/src/test/java/br/com/templates/config/jms_aws_sqs/SQSDependencyGeneratorTest.java index 9b79dff..8a5ed5e 100644 --- a/templates-java/src/test/java/br/com/templates_java/config/jms_aws_sqs/SQSDependencyGeneratorTest.java +++ b/templates/src/test/java/br/com/templates/config/jms_aws_sqs/SQSDependencyGeneratorTest.java @@ -1,7 +1,7 @@ -package br.com.templates_java.config.jms_aws_sqs; +package br.com.templates.config.jms_aws_sqs; import br.com.generator.core.GeneratorOptions; -import br.com.templates_java.helper.LoadTemplateTester; +import br.com.templates.helper.LoadTemplateTester; import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Ignore; diff --git a/templates-java/src/test/java/br/com/templates_java/config/openj9/OpenJ9DockerfileGeneratorTest.java b/templates/src/test/java/br/com/templates/config/openj9/OpenJ9DockerfileGeneratorTest.java similarity index 94% rename from templates-java/src/test/java/br/com/templates_java/config/openj9/OpenJ9DockerfileGeneratorTest.java rename to templates/src/test/java/br/com/templates/config/openj9/OpenJ9DockerfileGeneratorTest.java index eb2622c..07d66b0 100644 --- a/templates-java/src/test/java/br/com/templates_java/config/openj9/OpenJ9DockerfileGeneratorTest.java +++ b/templates/src/test/java/br/com/templates/config/openj9/OpenJ9DockerfileGeneratorTest.java @@ -1,7 +1,7 @@ -package br.com.templates_java.config.openj9; +package br.com.templates.config.openj9; import br.com.generator.core.GeneratorOptions; -import br.com.templates_java.helper.LoadTemplateTester; +import br.com.templates.helper.LoadTemplateTester; import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Rule; diff --git a/templates-java/src/test/java/br/com/templates_java/config/openj9/OpenJ9MavenPluginGeneratorTest.java b/templates/src/test/java/br/com/templates/config/openj9/OpenJ9MavenPluginGeneratorTest.java similarity index 93% rename from templates-java/src/test/java/br/com/templates_java/config/openj9/OpenJ9MavenPluginGeneratorTest.java rename to templates/src/test/java/br/com/templates/config/openj9/OpenJ9MavenPluginGeneratorTest.java index a7ea81b..d5c240c 100644 --- a/templates-java/src/test/java/br/com/templates_java/config/openj9/OpenJ9MavenPluginGeneratorTest.java +++ b/templates/src/test/java/br/com/templates/config/openj9/OpenJ9MavenPluginGeneratorTest.java @@ -1,8 +1,8 @@ -package br.com.templates_java.config.openj9; +package br.com.templates.config.openj9; import br.com.generator.core.GeneratorOptions; -import br.com.templates_java.config.jms_aws_sqs.SQSDependencyGenerator; -import br.com.templates_java.helper.LoadTemplateTester; +import br.com.templates.config.jms_aws_sqs.SQSDependencyGenerator; +import br.com.templates.helper.LoadTemplateTester; import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Ignore; diff --git a/templates/src/test/java/br/com/templates/entity/EntityCacheTest.java b/templates/src/test/java/br/com/templates/entity/EntityCacheTest.java new file mode 100644 index 0000000..c8f8cdb --- /dev/null +++ b/templates/src/test/java/br/com/templates/entity/EntityCacheTest.java @@ -0,0 +1,67 @@ +package br.com.templates.entity; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class EntityCacheTest { + + private EntityExecutor entityExecutor; + + @Before + public void setUp() { + entityExecutor = new EntityExecutor(); + } + + @Test + public void shouldReturnTwoEntities() { + String valueArgument = "name:String age:Int Foo:references(relation:hasMany, foo:String)"; + entityExecutor.run("User", valueArgument); + assertEquals(2, entityExecutor.getEntities().size()); + } + + @Test + public void shouldReturnContentOfUser() { + String valueArgument = "name:String age:Int Foo:references(relation:hasMany, foo:String)"; + entityExecutor.run("User", valueArgument); + + String expectedValue = "" + + "@Entity\n" + + "@Data\n" + + "public class User {\n" + + "\t\n" + + "\t@Id @GeneratedValue(strategy = GenerationType.AUTO)\n" + + "\tprivate Integer id;\n" + + "\tprivate String name;\n" + + "\tprivate Integer age;\n" + + "\t\t\n" + + "\t@OneToMany\n" + + "\tprivate List foo;\n" + + "}\n"; + + assertEquals(expectedValue.trim(), entityExecutor.getEntities().get(1).getContent().trim()); + } + + @Test + public void shouldReturnContentOfFoo() { + String valueArgument = "name:String age:Int Foo:references(relation:hasMany, foo:String)"; + entityExecutor.run("User", valueArgument); + + String expectedValue = "" + + "@Entity\n" + + "@Data\n" + + "public class Foo {\n" + + "\tprivate String foo;\n" + + "}\n"; + assertEquals(expectedValue.trim(), entityExecutor.getEntities().get(0).getContent()); + } + + @Test + public void shouldReturnThreeCount() { + String valueArgument = "name:String age:Int Foo:references(relation:hasMany, foo:String) Bar:references(relation:belongsTo, bar:String)"; + entityExecutor.run("User", valueArgument); + assertEquals(3, entityExecutor.getEntities().size()); + } + +} diff --git a/templates/src/test/java/br/com/templates/entity/EntityExecutorTest.java b/templates/src/test/java/br/com/templates/entity/EntityExecutorTest.java new file mode 100644 index 0000000..22b52a9 --- /dev/null +++ b/templates/src/test/java/br/com/templates/entity/EntityExecutorTest.java @@ -0,0 +1,233 @@ +package br.com.templates.entity; + +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class EntityExecutorTest { + + private EntityExecutor entityExecutor; + + @Before + public void setUp() { + entityExecutor = new EntityExecutor(); + } + + @Test + public void shouldCreateEntity() { + String expectedValue = "" + + "@Entity\n" + + "@Data\n" + + "public class User {\n" + + "\t\n" + + "\t@Id @GeneratedValue(strategy = GenerationType.AUTO)\n" + + "\tprivate Integer id;\n" + + "\tprivate String name;\n" + + "\tprivate Integer age;\n" + + "}\n"; + + String returnedValue = entityExecutor.run("User", "name:String age:Int"); + assertEquals(expectedValue.trim(), returnedValue.trim()); + } + + @Test(expected = EntityValidator.class) + public void shouldReturnException() { + entityExecutor.generateAttribute("name"); + } + + @Test(expected = EntityValidator.class) + public void givenTypeThatNotExists_shouldInvokeException() { + entityExecutor.generateAttribute("name:OtherType"); + } + + @Test + public void givenRelationHasMany_shouldReturnWithRelation() { + String expectedValue = "" + + "@Entity\n" + + "@Data\n" + + "public class User {\n" + + "\t\n" + + "\t@Id @GeneratedValue(strategy = GenerationType.AUTO)\n" + + "\tprivate Integer id;\n" + + "\tprivate String name;\n" + + "\tprivate Integer age;\n" + + "\t\t\n" + + "\t@OneToMany\n" + + "\tprivate List foo;\n" + + "}\n"; + + String valueArgument = "name:String age:Int Foo:references(relation:hasMany, foo:String)"; + String returnedValue = entityExecutor.run("User", valueArgument); + assertEquals(expectedValue.trim(), returnedValue.trim()); + } + + @Test + public void givenRelationBelongsTo_shouldReturnWithRelation() { + String expectedValue = "" + + "@Entity\n" + + "@Data\n" + + "public class User {\n" + + "\t\n" + + "\t@Id @GeneratedValue(strategy = GenerationType.AUTO)\n" + + "\tprivate Integer id;\n" + + "\tprivate String name;\n" + + "\tprivate Integer age;\n" + + "\t\t\n" + + "\t@OneToOne\n" + + "\tprivate Foo foo;\n" + + "}\n"; + + String valueArgument = "name:String age:Int Foo:references(relation:belongsTo, foo:String)"; + String returnedValue = entityExecutor.run("User", valueArgument); + assertEquals(expectedValue.trim(), returnedValue.trim()); + } + + @Test + public void shouldGenerateClass() { + String expectedValue = "" + + "@Entity\n" + + "@Data\n" + + "public class User {\n" + + "${attributes}" + + "}"; + + String returned = entityExecutor.generateClass("User"); + assertEquals(expectedValue, returned); + } + + @Test + public void shouldGenerateClassWithCorrectName() { + String expectedValue = "" + + "@Entity\n" + + "@Data\n" + + "public class Foo {\n" + + "${attributes}" + + "}"; + + String returned = entityExecutor.generateClass("Foo"); + assertEquals(expectedValue, returned); + } + + @Test + public void shouldGenerateStringAttribute() { + String attributes = "name:String"; + String valueReturned = entityExecutor.generateAttribute(attributes); + assertEquals("private String name;", valueReturned); + } + + @Test + public void shouldGeneratorIntegerAttribute() { + String attributes = "age:Int"; + String valueReturned = entityExecutor.generateAttribute(attributes); + assertEquals("private Integer age;", valueReturned); + } + + @Test + public void shouldGeneratorDateAttribute() { + String attributes = "created:Date"; + String valueReturned = entityExecutor.generateAttribute(attributes); + assertEquals("private Date created;", valueReturned); + } + + @Test + public void shouldReturnCorrectNameStringAttribute() { + String attributes = "description:String"; + String valueReturned = entityExecutor.generateAttribute(attributes); + assertEquals("private String description;", valueReturned); + } + + @Test + public void shouldReturnCorrectNameIntegerAttribute() { + String attributes = "count:Int"; + String valueReturned = entityExecutor.generateAttribute(attributes); + assertEquals("private Integer count;", valueReturned); + } + + @Test + public void shouldReturnCorrectNameDateAttribute() { + String attributes = "updated:Date"; + String valueReturned = entityExecutor.generateAttribute(attributes); + assertEquals("private Date updated;", valueReturned); + } + + @Test + public void shouldReturnHasManyRelation() { + String attributes = "Foo:references(relation:hasMany, foo:String)"; + String valueReturned = entityExecutor.generateAttribute(attributes); + + String expectedValue = "\t\n" + + "\t@OneToMany\n" + + "\tprivate List foo;"; + assertEquals(expectedValue, valueReturned); + } + + @Test + public void shouldReturnBelongsToRelation() { + String attributes = "Foo:references(relation:belongsTo, foo:String)"; + String valueReturned = entityExecutor.generateAttribute(attributes); + + String expectedValue = "\t\n" + + "\t@OneToOne\n" + + "\tprivate Foo foo;"; + + assertEquals(expectedValue, valueReturned); + } + + @Test(expected = EntityValidator.class) + public void givenReferences_wheRelationEmpty_shouldInvokeException() { + String attributes = "Foo:references(foo:String)"; + entityExecutor.generateAttribute(attributes); + } + + @Test + public void shouldReturnCorrectNameHasNameRelation() { + String attributes = "Books:references(relation:hasMany, foo:String)"; + String valueReturned = entityExecutor.generateAttribute(attributes); + + String expectedValue = "\t\n" + + "\t@OneToMany\n" + + "\tprivate List books;"; + + assertEquals(expectedValue, valueReturned); + } + + @Test + public void givenStringBetweenParentheses_shouldReturnAttributes() { + String attributes = "Foo:references(relation:belongsTo foo:String)"; + String getAttributesReferences = entityExecutor.getAttributesOfReferences(attributes); + assertEquals("relation:belongsTo foo:String", getAttributesReferences); + } + + @Test + public void shouldReturnRelation() { + String attributes = "relation:belongsTo foo:String"; + String getAttributesReferences = entityExecutor.getRelation(attributes); + assertEquals("belongsTo", getAttributesReferences); + } + + @Test + public void shouldReturnScaffoldPattern() { + String attributes = "name:String age:Int Foo:references(relation:hasMany, foo:String)"; + String returnConverted = entityExecutor.toPatternScaffold(attributes); + assertEquals("name:String age:Int Foo:references(relation:hasMany,foo:String)", returnConverted); + } + + @Test + public void shouldReturnReferencesAttributeComplete() { + String attributes = "name:String age:Int Foo:references(relation:hasMany, foo:String)"; + List getAttributesReferences = entityExecutor.getReferencesComplete(attributes); + assertEquals("Foo:references(relation:hasMany,foo:String)", getAttributesReferences.get(0)); + } + + @Test + public void shouldReturnReferencesAttributes() { + String attributes = "name:String age:Int Foo:references(relation:hasMany, foo:String) Books:references(relation:belongsTo, foo:String)"; + List getAttributesReferences = entityExecutor.getReferencesComplete(attributes); + assertEquals("Foo:references(relation:hasMany,foo:String)", getAttributesReferences.get(0)); + assertEquals("Books:references(relation:belongsTo,foo:String)", getAttributesReferences.get(1)); + } + +} \ No newline at end of file diff --git a/templates-java/src/test/java/br/com/templates_java/helper/LoadTemplateTester.java b/templates/src/test/java/br/com/templates/helper/LoadTemplateTester.java similarity index 90% rename from templates-java/src/test/java/br/com/templates_java/helper/LoadTemplateTester.java rename to templates/src/test/java/br/com/templates/helper/LoadTemplateTester.java index 134e607..91f6e49 100644 --- a/templates-java/src/test/java/br/com/templates_java/helper/LoadTemplateTester.java +++ b/templates/src/test/java/br/com/templates/helper/LoadTemplateTester.java @@ -1,4 +1,4 @@ -package br.com.templates_java.helper; +package br.com.templates.helper; import org.apache.commons.io.IOUtils; diff --git a/templates-java/src/test/resources/templates/config/fake-application.properties b/templates/src/test/resources/templates/config/fake-application.properties similarity index 100% rename from templates-java/src/test/resources/templates/config/fake-application.properties rename to templates/src/test/resources/templates/config/fake-application.properties diff --git a/templates-java/src/test/resources/templates/config/openj9/dockerfile-template-test.txt b/templates/src/test/resources/templates/config/openj9/dockerfile-template-test.txt similarity index 100% rename from templates-java/src/test/resources/templates/config/openj9/dockerfile-template-test.txt rename to templates/src/test/resources/templates/config/openj9/dockerfile-template-test.txt diff --git a/templates-java/src/test/resources/templates/config/openj9/template-pom-test.xml b/templates/src/test/resources/templates/config/openj9/template-pom-test.xml similarity index 100% rename from templates-java/src/test/resources/templates/config/openj9/template-pom-test.xml rename to templates/src/test/resources/templates/config/openj9/template-pom-test.xml diff --git a/templates-java/src/test/resources/templates/config/template-entrypoint-listener-test.txt b/templates/src/test/resources/templates/config/template-entrypoint-listener-test.txt similarity index 100% rename from templates-java/src/test/resources/templates/config/template-entrypoint-listener-test.txt rename to templates/src/test/resources/templates/config/template-entrypoint-listener-test.txt diff --git a/templates-java/src/test/resources/templates/config/template-message-listener-test.txt b/templates/src/test/resources/templates/config/template-message-listener-test.txt similarity index 100% rename from templates-java/src/test/resources/templates/config/template-message-listener-test.txt rename to templates/src/test/resources/templates/config/template-message-listener-test.txt diff --git a/templates-java/src/test/resources/templates/config/template-pom-test.xml b/templates/src/test/resources/templates/config/template-pom-test.xml similarity index 100% rename from templates-java/src/test/resources/templates/config/template-pom-test.xml rename to templates/src/test/resources/templates/config/template-pom-test.xml diff --git a/templates-java/src/test/resources/templates/config/template-pom.xml b/templates/src/test/resources/templates/config/template-pom.xml similarity index 100% rename from templates-java/src/test/resources/templates/config/template-pom.xml rename to templates/src/test/resources/templates/config/template-pom.xml diff --git a/templates-java/src/test/resources/templates/config/template-producer-message-test.txt b/templates/src/test/resources/templates/config/template-producer-message-test.txt similarity index 100% rename from templates-java/src/test/resources/templates/config/template-producer-message-test.txt rename to templates/src/test/resources/templates/config/template-producer-message-test.txt