Skip to content

Commit

Permalink
[repository-util] Command line not getting style for repository valid…
Browse files Browse the repository at this point in the history
…ator class #169
  • Loading branch information
donmendelson committed Sep 1, 2022
1 parent 2071326 commit 76f5dc7
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 74 deletions.
19 changes: 16 additions & 3 deletions repository-util/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@ This module provides these applications:

### Running RepositoryValidator

RepositoryValidator ensures that an Orchestra file conforms to the Repository XML schema. Other validations may be added in the future.
RepositoryValidator supports two validation styles:
* Basic validation (the default) merely ensures that an Orchestra file conforms to the Repository XML schema.
* FIX style validation additionally checks contents for conformance to FIX style rules. This is invoked with command line argument `-s FIX`.

RepositoryValidator takes one command line argument, the name of an Orchestra Repository file to validate. Output goes to the console by default.
Other validations may be added in the future.

RepositoryValidator requires one command line argument, the name of an Orchestra Repository file to validate. Output goes to the console by default
but may be directed to a JSON event file suitable for rendering.

Command line arguments:
```
usage: RepositoryValidator [options] <input-file>
-?,--help display usage
-e,--eventlog <arg> path of JSON event file
-s,--style <arg> validation style
```

### Running RepositoryCompressor

Expand Down Expand Up @@ -49,6 +62,6 @@ This Maven module may be included as a dependency in a project as follows (subst
<dependency>
<groupId>io.fixprotocol.orchestra</groupId>
<artifactId>repository-util</artifactId>
<version>1.6.10</version>
<version>1.7.4</version>
</dependency>
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 FIX Protocol Ltd
* Copyright 2019-2022 FIX Protocol Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -17,24 +17,35 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import io.fixprotocol.orchestra.event.EventListener;
import io.fixprotocol.orchestra.event.EventListenerFactory;
import io.fixprotocol.orchestra.event.TeeEventListener;


/**
* Validates an Orchestra repository file
* Validates an Orchestra repository file
*
* <p>Validations include:</p>
* <p>
* Validations include:
* </p>
* <ul>
* <li>Conformance to the repository XML schema</li>
* <li>Syntax of Score DSL expressions</li>
* <li>Syntax of markdown documentation</li>
* <li>Conformance to FIX style rules or basic validation</li
* </ul>
*
* <p>Rules for other protocols may be added in future.</p>
* <p>
* Rules for other protocols may be added in future.
* </p>
*
* @author Don Mendelson
*
Expand All @@ -59,48 +70,114 @@ public Builder inputFile(String inputFilename) {
this.inputFile = inputFilename;
return this;
}

public Builder style(String style) {
this.style = style;
return this;
}
}

/**
* Basic validation only validates XML schema conformance
*/
public static final String BASIC_STYLE = "BASIC";

/**
* FIX validation performs basic validation plus conformance to FIX style rules
*/
public static final String FIX_STYLE = "FIX";
final Logger logger = LogManager.getLogger(RepositoryValidator.class);

public static Builder builder() {
return new Builder();
}

public static EventListener createLogger(OutputStream jsonOutputStream) {
final Logger logger = LogManager.getLogger(RepositoryValidator.class);
final EventListenerFactory factory = new EventListenerFactory();
TeeEventListener eventListener = null;
try {
eventListener = new TeeEventListener();
final EventListener logEventLogger = factory.getInstance("LOG4J");
logEventLogger.setResource(logger);
eventListener.addEventListener(logEventLogger);
if (jsonOutputStream != null) {
final EventListener jsonEventLogger = factory.getInstance("JSON");
jsonEventLogger.setResource(jsonOutputStream);
eventListener.addEventListener(jsonEventLogger);
}
} catch (Exception e) {
logger.error("Error creating event listener", e);
}
return eventListener;
}

/**
* Execute RepositoryValidator with command line arguments
*
* @param args command line arguments
*
* <pre>
* Usage: RepositoryValidator [options] &lt;input-file&gt;
* -e &lt;logfile&gt; name of event log
usage: RepositoryValidator [options] &lt;input-file&gt;
-?,--help display usage
-e,--eventlog &lt;arg&gt; path of JSON event file
-s,--style &lt;arg&gt; validation style
* </pre>
*/
public static void main(String[] args) {
final Builder builder = RepositoryValidator.builder();

for (int i = 0; i < args.length;) {
if ("-e".equals(args[i])) {
if (i < args.length - 1) {
builder.eventLog(args[i + 1]);
i++;
}
} else {
builder.inputFile(args[i]);
RepositoryValidator validator;
try {
validator = RepositoryValidator.parseArgs(args).build();
validator.validate();
} catch (ParseException e) {
System.exit(1);
}
}

static Builder parseArgs(String[] args) throws ParseException {
final Options options = new Options();
options.addOption(Option.builder("e").desc("path of JSON event file").longOpt("eventlog")
.numberOfArgs(1).build());
options.addOption(
Option.builder("s").desc("validation style").longOpt("style").numberOfArgs(1).build());
options.addOption(Option.builder("?").desc("display usage").longOpt("help").build());

final DefaultParser parser = new DefaultParser();
CommandLine cmd;

final Builder builder = new Builder();

try {
cmd = parser.parse(options, args);

if (cmd.hasOption("?")) {
showHelp(options);
System.exit(0);
}

builder.inputFile = !cmd.getArgList().isEmpty() ? cmd.getArgList().get(0) : null;

if (cmd.hasOption("e")) {
builder.eventFile = cmd.getOptionValue("e");
}

if (cmd.hasOption("s")) {
builder.style = cmd.getOptionValue("s");
}
i++;

return builder;
} catch (final ParseException e) {
System.err.println(e.getMessage());
showHelp(options);
throw e;
}
final RepositoryValidator validator = builder.build();
System.exit(validator.validate() ? 0 : 1);
}

static void showHelp(final Options options) {
final HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("RepositoryValidator [options] <input-file>", options);
}

final Logger logger = LogManager.getLogger(RepositoryValidator.class);
private final String eventFile;
private final String inputFile;
private final String style;
Expand All @@ -112,38 +189,22 @@ private RepositoryValidator(Builder builder) {
}

public boolean validate() {
try (EventListener eventLogger = createLogger(eventFile != null ? new FileOutputStream(eventFile) : null)) {
try (EventListener eventLogger =
createLogger(eventFile != null ? new FileOutputStream(eventFile) : null)) {
BasicRepositoryValidator impl;
if (FIX_STYLE.equals(this.style)) {
if (FIX_STYLE.equalsIgnoreCase(this.style)) {
impl = new FixRepositoryValidator(eventLogger);
} else {
impl = new BasicRepositoryValidator(eventLogger);
}
if (inputFile == null) {
throw new IllegalArgumentException("No input file specified");
}
return impl.validate(new FileInputStream(inputFile));
} catch (final Exception e) {
logger.fatal("RepositoryValidator failed", e);
return false;
}
}

public static EventListener createLogger(OutputStream jsonOutputStream) {
final Logger logger = LogManager.getLogger(RepositoryValidator.class);
final EventListenerFactory factory = new EventListenerFactory();
TeeEventListener eventListener = null;
try {
eventListener = new TeeEventListener();
final EventListener logEventLogger = factory.getInstance("LOG4J");
logEventLogger.setResource(logger);
eventListener.addEventListener(logEventLogger);
if (jsonOutputStream != null) {
final EventListener jsonEventLogger = factory.getInstance("JSON");
jsonEventLogger.setResource(jsonOutputStream);
eventListener.addEventListener(jsonEventLogger);
}
} catch (Exception e) {
logger.error("Error creating event listener", e);
}
return eventListener;
}

}
Original file line number Diff line number Diff line change
@@ -1,49 +1,30 @@
package io.fixprotocol.orchestra.repository;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class FixRepositoryValidatorTest {

private FixRepositoryValidator validator;
private io.fixprotocol.orchestra.event.EventListener eventLogger;

@BeforeAll
public static void setupOnce() {
new File(("target/test")).mkdirs();
}

@BeforeEach
public void setUp() throws Exception {
final OutputStream jsonOutputStream =
new FileOutputStream("target/test/repositoryvalidator.json");
eventLogger = RepositoryValidator.createLogger(jsonOutputStream);
validator = new FixRepositoryValidator(eventLogger);
}

@AfterEach
public void cleanUp() throws Exception {
eventLogger.close();
}

@Test
public void testValidateWithErrors() throws FileNotFoundException {
InputStream inputStream = new FileInputStream("src/test/resources/repositorywitherrors.xml");
validator.validate(inputStream);
public void testValidateWithErrors() {
RepositoryValidator
.main(new String[] {"-s", "FIX", "-e", "src/test/resources/repositorywitherrors.json",
"src/test/resources/repositorywitherrors.xml"});
}

@Disabled
@Test
public void testValidate() throws FileNotFoundException {
InputStream inputStream = new FileInputStream("src/test/resources/OrchestraFIXLatest.xml");
validator.validate(inputStream);
public void testValidate() {
RepositoryValidator.main(new String[] {"-s", "FIX", "-e",
"src/test/resources/OrchestraFIXLatest.json", "src/test/resources/OrchestraFIXLatest.xml"});
}

}

0 comments on commit 76f5dc7

Please sign in to comment.