From 2aa0e23898e01eafffe643643eefa8fcdb2ff9c6 Mon Sep 17 00:00:00 2001 From: burdziak Date: Tue, 4 Jun 2019 10:22:20 +0200 Subject: Make Validation groovy rules easier for testing Issue-ID: AAI-2470 Change-Id: Ie28390f4ab3c9bd4f8e469812d6b19ecd82c2879 Signed-off-by: burdziak --- .gitignore | 21 +-- pom.xml | 1 + .../org/onap/aai/validation/ValidationCmd.java | 175 +++++++++++++++++++++ src/main/resources/validation-cmd-beans.xml | 83 ++++++++++ 4 files changed, 271 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/onap/aai/validation/ValidationCmd.java create mode 100644 src/main/resources/validation-cmd-beans.xml diff --git a/.gitignore b/.gitignore index aef0495..5e13100 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,14 @@ -logs/ -debug-logs/ -appconfig-local/ -bundleconfig-local/etc/auth/tomcat_keystore -.classpath +.settings .project -.settings/ -staticContent/ -target/ -src/main/java/META-INF/ +.classpath .checkstyle +target/ +**/logs/ +**/debug-logs/ +/.pydevproject +/bin/ +appconfig-local/ +**/.idea/ +*/.idea +*.iml +.idea/ diff --git a/pom.xml b/pom.xml index 6822fc0..c75f0e3 100644 --- a/pom.xml +++ b/pom.xml @@ -255,6 +255,7 @@ spring-boot-maven-plugin ${spring-boot.version} + ${start-class} true diff --git a/src/main/java/org/onap/aai/validation/ValidationCmd.java b/src/main/java/org/onap/aai/validation/ValidationCmd.java new file mode 100644 index 0000000..92ed5ad --- /dev/null +++ b/src/main/java/org/onap/aai/validation/ValidationCmd.java @@ -0,0 +1,175 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.validation; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import org.onap.aai.validation.config.RuleIndexingConfig; +import org.onap.aai.validation.exception.ValidationServiceException; +import org.onap.aai.validation.reader.EventReader; +import org.onap.aai.validation.result.ValidationResult; +import org.onap.aai.validation.result.Violation; +import org.onap.aai.validation.ruledriven.RuleDrivenValidator; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.web.support.SpringBootServletInitializer; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +@Configuration +@EnableAutoConfiguration +@ImportResource("classpath:validation-cmd-beans.xml") +public class ValidationCmd extends SpringBootServletInitializer { + + private static final String EVENT_FILE_PATH = "event.json"; + + private List rulesFoldersPaths =new ArrayList<>(); + private String eventFilePath = EVENT_FILE_PATH; + + private void printUsage() { + System.out.println("ValidationCmd is designed to test defined rules from command line. Usage:"); + System.out.println("ValidationCmd [-e eventFilePath] [-r rulesFolderPath][]"); + System.out + .println("-e eventFilePath - path to file with json event. Default: " + EVENT_FILE_PATH); + System.out.println( + "-r rulesFolderPath - path to foler with rules definition - *.groovy fils. If more than one then each requires -r"); + } + + private boolean parseCmd(String[] args) { + if (args == null || args.length <= 0) { + return true; + } + + for (int i = 0; i < args.length; i++) { + String s = args[i]; + if (s == null || s.isEmpty()) { + continue; + } + + if (0 == "-e".compareTo(s)) { + i++; + if (i >= args.length) { + System.out.println("After -e missing eventFilePath"); + printUsage(); + return false; + } + eventFilePath = args[i]; + } else if (0 == "-r".compareTo(s)) { + i++; + if (i >= args.length) { + System.out.println("After -r missing rulesFoldersPath"); + printUsage(); + return false; + } + rulesFoldersPaths.add(Paths.get(args[i])); + } else { + System.out.println("Unknown parameter: " + s); + printUsage(); + return false; + } + } + + return true; + } + + private RuleDrivenValidator createRuleDrivenValidator(ApplicationContext ctx) { + + EventReader eventReader = ctx.getBean(EventReader.class); + RuleIndexingConfig ruleIndexingConfig = ctx.getBean(RuleIndexingConfig.class); + + RuleDrivenValidator validator = new RuleDrivenValidator(rulesFoldersPaths, null, eventReader, + ruleIndexingConfig); + + return validator; + } + + private void validate(RuleDrivenValidator validator) + throws IOException, ValidationServiceException { + + String event = new String(Files.readAllBytes(Paths.get(eventFilePath))); + + List resultList = validator.validate(event); + + printResults(resultList); + } + + private void printResults(List resultList) { + if (resultList == null) { + return; + } + + System.out.println("--RESULT---------------------"); + for (ValidationResult vr : resultList) { + System.out.println("--VIOLATIONS---------------------"); + for(Violation violation:vr.getViolations()){ + System.out.println("- "); + System.out.println(violation.toString()); + } + System.out.println("--JSON---------------------"); + System.out.println(vr.getViolations()); + String s = vr.toJson(); + System.out.println(s); + } + System.out.println("--RESULT-END---------------------"); + } + + private void printConfig() { + + System.out.print("rulesFoldersPaths= " ); + for(Path path: rulesFoldersPaths) + System.out.print(path+","); + System.out.println(); + System.out.println("eventFilePath= " + eventFilePath); + } + + /** + * Create and run the Application. + * + * @param args the command line arguments + */ + public static void main(String[] args) { + + System.out.println("user.dir=" + System.getProperty("user.dir")); + ApplicationContext ctx = new AnnotationConfigApplicationContext(ValidationCmd.class); + + ValidationCmd validationCmd = new ValidationCmd(); + try { + if (!validationCmd.parseCmd(args)) { + return; + } + + validationCmd.printConfig(); + + RuleDrivenValidator validator = validationCmd.createRuleDrivenValidator(ctx); + + validationCmd.validate(validator); + + } catch (Exception e) { + System.out.println(e.getMessage()); + e.printStackTrace(); + } + } +} diff --git a/src/main/resources/validation-cmd-beans.xml b/src/main/resources/validation-cmd-beans.xml new file mode 100644 index 0000000..8c317d4 --- /dev/null +++ b/src/main/resources/validation-cmd-beans.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- cgit 1.2.3-korg