Skip to content

Commit

Permalink
Merge pull request #5 from ananthdurai/command_line_enrichment
Browse files Browse the repository at this point in the history
commandline added
  • Loading branch information
ananthdurai authored Dec 29, 2021
2 parents e385dc5 + 935ef25 commit 6071434
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ proto-gen:

.PHONY:validate
validate: proto-gen package
java -jar target/schemata-1.0.jar validate
java -jar target/schemata-1.0.jar schemata --cmd validate
20 changes: 19 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<junit.version>5.8.2</junit.version>
<commons-lang.version>3.12.0</commons-lang.version>
<jgrapht.version>1.5.1</jgrapht.version>
<picocli.version>4.6.2</picocli.version>
</properties>

<dependencies>
Expand All @@ -33,6 +34,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand All @@ -48,7 +55,11 @@
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>

<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>${picocli.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -126,6 +137,13 @@
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
2 changes: 2 additions & 0 deletions score.sh
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
63 changes: 63 additions & 0 deletions src/main/java/org/schemata/SchemataExecutor.java
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();
}
}
62 changes: 3 additions & 59 deletions src/main/java/org/schemata/SchemataMain.java
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);
}
}
41 changes: 41 additions & 0 deletions src/main/java/org/schemata/app/SchemaScoreApp.java
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;
}
}
46 changes: 46 additions & 0 deletions src/main/java/org/schemata/app/SchemaValidatorApp.java
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;
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/schemata/printer/Console.java
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);
}
}
46 changes: 46 additions & 0 deletions src/test/java/org/schemata/SchemataExecutorTest.java
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);
}
}
2 changes: 2 additions & 0 deletions validate.sh
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

0 comments on commit 6071434

Please sign in to comment.