-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ananthdurai/command_line_enrichment
commandline added
- Loading branch information
Showing
10 changed files
with
239 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
java -jar target/schemata-1.0.jar schemata --cmd score $1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> { | ||
|
||
private List<Schema> schemaList; | ||
private static final String VALIDATE_COMMAND = "validate"; | ||
private static final String SCORE_COMMAND = "score"; | ||
private static final Set<String> 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<String> positionalParams; | ||
|
||
public SchemataExecutor(List<Schema> 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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<Schema> 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> { | ||
|
||
private List<Schema> schemaList; | ||
private String schemaName; | ||
|
||
public SchemaScoreApp(List<Schema> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> { | ||
|
||
private List<Schema> schemaList; | ||
|
||
public SchemaValidatorApp(List<Schema> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Schema> 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
java -jar target/schemata-1.0.jar schemata --cmd validate |