From 935ef25a33f7ab869df425373a23cfdc6573924b Mon Sep 17 00:00:00 2001 From: Ananth Packkildurai Date: Wed, 29 Dec 2021 16:55:25 -0500 Subject: [PATCH] commandline added --- Makefile | 2 +- pom.xml | 20 +++++- score.sh | 2 + .../java/org/schemata/SchemataExecutor.java | 63 +++++++++++++++++++ src/main/java/org/schemata/SchemataMain.java | 62 +----------------- .../java/org/schemata/app/SchemaScoreApp.java | 41 ++++++++++++ .../org/schemata/app/SchemaValidatorApp.java | 46 ++++++++++++++ .../java/org/schemata/printer/Console.java | 16 +++++ .../org/schemata/SchemataExecutorTest.java | 46 ++++++++++++++ validate.sh | 2 + 10 files changed, 239 insertions(+), 61 deletions(-) create mode 100755 score.sh create mode 100644 src/main/java/org/schemata/SchemataExecutor.java create mode 100644 src/main/java/org/schemata/app/SchemaScoreApp.java create mode 100644 src/main/java/org/schemata/app/SchemaValidatorApp.java create mode 100644 src/main/java/org/schemata/printer/Console.java create mode 100644 src/test/java/org/schemata/SchemataExecutorTest.java create mode 100755 validate.sh diff --git a/Makefile b/Makefile index 81f077a..1f7399a 100644 --- a/Makefile +++ b/Makefile @@ -25,4 +25,4 @@ proto-gen: .PHONY:validate validate: proto-gen package - java -jar target/schemata-1.0.jar validate \ No newline at end of file + java -jar target/schemata-1.0.jar schemata --cmd validate \ No newline at end of file diff --git a/pom.xml b/pom.xml index c202894..d97778e 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,7 @@ 5.8.2 3.12.0 1.5.1 + 4.6.2 @@ -33,6 +34,12 @@ ${junit.version} test + + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + test + org.apache.commons commons-lang3 @@ -48,7 +55,11 @@ commons-collections4 4.4 - + + info.picocli + picocli + ${picocli.version} + @@ -126,6 +137,13 @@ + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + + diff --git a/score.sh b/score.sh new file mode 100755 index 0000000..d991d17 --- /dev/null +++ b/score.sh @@ -0,0 +1,2 @@ +#!/bin/bash +java -jar target/schemata-1.0.jar schemata --cmd score $1 diff --git a/src/main/java/org/schemata/SchemataExecutor.java b/src/main/java/org/schemata/SchemataExecutor.java new file mode 100644 index 0000000..78516ca --- /dev/null +++ b/src/main/java/org/schemata/SchemataExecutor.java @@ -0,0 +1,63 @@ +package org.schemata; + +import java.util.List; +import java.util.Set; +import java.util.concurrent.Callable; +import org.apache.commons.lang3.StringUtils; +import org.schemata.app.SchemaScoreApp; +import org.schemata.app.SchemaValidatorApp; +import org.schemata.domain.Schema; +import org.schemata.printer.Console; +import picocli.CommandLine; + + +@CommandLine.Command(name = "schemata", mixinStandardHelpOptions = true, description = "Schemata commandline tool") +public class SchemataExecutor implements Callable { + + private List schemaList; + private static final String VALIDATE_COMMAND = "validate"; + private static final String SCORE_COMMAND = "score"; + private static final Set validCommands = Set.of(VALIDATE_COMMAND, SCORE_COMMAND); + + @CommandLine.Option(names = "--cmd", description = "Type of the command to execute.", defaultValue = "validate", required = true) + String cmdType; + + @CommandLine.Parameters() + private List positionalParams; + + public SchemataExecutor(List schemaList) { + this.schemaList = schemaList; + } + + @Override + public Integer call() + throws Exception { + + if (StringUtils.isBlank(cmdType) || !validCommands.contains(cmdType.toLowerCase())) { + Console.printError("Given command:" + cmdType + " is invalid"); + return -1; + } + + if (schemaList == null || schemaList.size() == 0) { + Console.printError("Invalid Schema list:" + schemaList); + return -1; + } + + int code = switch (cmdType.toLowerCase()) { + case VALIDATE_COMMAND -> new SchemaValidatorApp(schemaList).call(); + case SCORE_COMMAND -> callScore(); + default -> -1; + }; + return code; + } + + private Integer callScore() + throws Exception { + if (positionalParams == null || positionalParams.size() < 2) { + Console.printError("Schema name not found in the positional argument:" + positionalParams); + return -1; + } + String schema = positionalParams.get(1); + return new SchemaScoreApp(schemaList, schema).call(); + } +} diff --git a/src/main/java/org/schemata/SchemataMain.java b/src/main/java/org/schemata/SchemataMain.java index 1d44e5b..156bcc6 100644 --- a/src/main/java/org/schemata/SchemataMain.java +++ b/src/main/java/org/schemata/SchemataMain.java @@ -1,72 +1,16 @@ package org.schemata; -import com.google.protobuf.GeneratedMessageV3; -import java.util.Arrays; -import java.util.HashSet; import java.util.List; -import java.util.Set; -import org.schemata.domain.Field; import org.schemata.domain.Schema; import org.schemata.parser.SchemaParser; -import org.schemata.schema.BrandBuilder; -import org.schemata.schema.CampaignBuilder; -import org.schemata.schema.CategoryBuilder; -import org.schemata.schema.ProductBuilder; -import org.schemata.schema.UserBuilder; -import org.schemata.validate.FieldValidator; -import org.schemata.validate.SchemaValidator; -import org.schemata.validate.Status; +import picocli.CommandLine; public class SchemataMain { - private static final String VALIDATE_COMMAND = "validate"; - private static final String SCORE_COMMAND = "score"; - private static final Set validCommands = new HashSet<>(Arrays.asList(VALIDATE_COMMAND, SCORE_COMMAND)); - public static void main(String... args) { - - if (args.length < 1) { - throw new IllegalArgumentException("SchemaGuard expect either validate or score as an argument"); - } - - if (!validCommands.contains(args[0].trim().toLowerCase())) { - throw new IllegalArgumentException("SchemaGuard expect either validate or score as an argument"); - } - - String command = args[0]; - - if (command.equalsIgnoreCase(SCORE_COMMAND) && args.length < 2) { - throw new IllegalArgumentException("Entity name is missing. Score command requires entity name to score"); - } - List schemaList = new SchemaParser().parseSchema(SchemaRegistry.registerSchema()); - - if (command.equalsIgnoreCase(VALIDATE_COMMAND)) { - var schemaValidator = new SchemaValidator(); - var fieldValidator = new FieldValidator(); - for (Schema schema : schemaList) { - var schemaResult = schemaValidator.apply(schema); - if (schemaResult.status() == Status.ERROR) { - System.out.println("Error parsing Schema " + schema.name() + "Error Message:" + schemaResult.errorMessages()); - return; - } - - for (Field field : schema.fieldList()) { - var fieldResult = fieldValidator.apply(field); - if (fieldResult.status() == Status.ERROR) { - System.out.println("Error parsing Schema Fields in schema:" + schema.name() + " on field:" + field.name() - + " Error Message:" + fieldResult.errorMessages()); - return; - } - } - } - System.out.println("Schema validation success. No error to report"); - return; - } - - if (command.equalsIgnoreCase(SCORE_COMMAND)) { - - } + int exitCode = new CommandLine(new SchemataExecutor(schemaList)).execute(args); + System.exit(exitCode); } } diff --git a/src/main/java/org/schemata/app/SchemaScoreApp.java b/src/main/java/org/schemata/app/SchemaScoreApp.java new file mode 100644 index 0000000..c3f15f8 --- /dev/null +++ b/src/main/java/org/schemata/app/SchemaScoreApp.java @@ -0,0 +1,41 @@ +package org.schemata.app; + +import java.util.List; +import java.util.concurrent.Callable; +import org.apache.commons.lang3.StringUtils; +import org.schemata.domain.Schema; +import org.schemata.exception.SchemaNotFoundException; +import org.schemata.graph.SchemaGraph; +import org.schemata.printer.Console; + + +public class SchemaScoreApp implements Callable { + + private List schemaList; + private String schemaName; + + public SchemaScoreApp(List schemaList, String schemaName) { + this.schemaList = schemaList; + this.schemaName = schemaName; + } + + @Override + public Integer call() + throws Exception { + + if (StringUtils.isBlank(schemaName)) { + Console.printError("Invalid schema name:" + schemaName); + return -1; + } + + var graph = new SchemaGraph(this.schemaList); + try { + double value = graph.getSchemataScore(schemaName); + Console.printSuccess("Schemata score for " + schemaName + " : " + value); + } catch (SchemaNotFoundException e) { + Console.printError(e.getMessage()); + return -1; + } + return 0; + } +} diff --git a/src/main/java/org/schemata/app/SchemaValidatorApp.java b/src/main/java/org/schemata/app/SchemaValidatorApp.java new file mode 100644 index 0000000..c23e610 --- /dev/null +++ b/src/main/java/org/schemata/app/SchemaValidatorApp.java @@ -0,0 +1,46 @@ +package org.schemata.app; + +import java.util.List; +import java.util.concurrent.Callable; +import org.schemata.domain.Field; +import org.schemata.domain.Schema; +import org.schemata.printer.Console; +import org.schemata.validate.FieldValidator; +import org.schemata.validate.SchemaValidator; +import org.schemata.validate.Status; + + +public class SchemaValidatorApp implements Callable { + + private List schemaList; + + public SchemaValidatorApp(List schemaList) { + this.schemaList = schemaList; + } + + @Override + public Integer call() + throws Exception { + var schemaValidator = new SchemaValidator(); + var fieldValidator = new FieldValidator(); + for (Schema schema : schemaList) { + var schemaResult = schemaValidator.apply(schema); + if (schemaResult.status() == Status.ERROR) { + Console.printError("Error parsing Schema " + schema.name() + "Error Message:" + schemaResult.errorMessages()); + return -1; + } + + for (Field field : schema.fieldList()) { + var fieldResult = fieldValidator.apply(field); + if (fieldResult.status() == Status.ERROR) { + Console.printError( + "Error parsing Schema Fields in schema:" + schema.name() + " on field:" + field.name() + " Error Message:" + + fieldResult.errorMessages()); + return -1; + } + } + } + Console.printSuccess("Schema validation success. No error to report"); + return 0; + } +} diff --git a/src/main/java/org/schemata/printer/Console.java b/src/main/java/org/schemata/printer/Console.java new file mode 100644 index 0000000..7bae39b --- /dev/null +++ b/src/main/java/org/schemata/printer/Console.java @@ -0,0 +1,16 @@ +package org.schemata.printer; + +public class Console { + + public static final String TEXT_RED = "\033[0;31m"; + public static final String TEXT_GREEN = "\033[0;32m"; + public static final String TEXT_RESET = "\u001B[0m"; + + public static void printSuccess(String message) { + System.out.println(TEXT_GREEN + message + TEXT_RESET); + } + + public static void printError(String message) { + System.out.println(TEXT_RED + message + TEXT_RESET); + } +} diff --git a/src/test/java/org/schemata/SchemataExecutorTest.java b/src/test/java/org/schemata/SchemataExecutorTest.java new file mode 100644 index 0000000..3200f40 --- /dev/null +++ b/src/test/java/org/schemata/SchemataExecutorTest.java @@ -0,0 +1,46 @@ +package org.schemata; + + +import java.util.List; +import jdk.jfr.Description; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.schemata.domain.Schema; +import org.schemata.parser.SchemaParser; +import picocli.CommandLine; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +public class SchemataExecutorTest { + + static CommandLine cmd; + + @BeforeAll + static void setup() { + List schemaList = new SchemaParser().parseSchema(SchemaRegistry.registerSchema()); + var executor = new SchemataExecutor(schemaList); + cmd = new CommandLine(executor); + } + + @Test + @Description("Run schema validate function to run all the schema and fields validation rules") + public void testSchemaValidateCmd() { + int exitCode = cmd.execute("schemata", "--cmd", "validate"); + assertEquals(0, exitCode); + } + + @Test + @Description("Test Schema score with an invalid schema name") + public void testScoreWithInvalidSchema() { + int exitCode = cmd.execute("schemata", "--cmd", "score", "User"); + assertEquals(-1, exitCode); + } + + @Test + @Description("Test Schema score with an valid schema name") + public void testScoreWithValidSchema() { + int exitCode = cmd.execute("schemata", "--cmd", "score", "org.schemata.schema.CampaignCategoryTrackerEvent"); + assertEquals(0, exitCode); + } +} diff --git a/validate.sh b/validate.sh new file mode 100755 index 0000000..a00bd82 --- /dev/null +++ b/validate.sh @@ -0,0 +1,2 @@ +#!/bin/bash +java -jar target/schemata-1.0.jar schemata --cmd validate