diff options
Diffstat (limited to 'pmdictionaryvalidation/src/main')
-rw-r--r-- | pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlContentValidator.java (renamed from pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java) | 35 | ||||
-rw-r--r-- | pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java | 43 | ||||
-rw-r--r-- | pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java | 8 |
3 files changed, 58 insertions, 28 deletions
diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlContentValidator.java index 2de4f48..5e1238b 100644 --- a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlContentValidator.java @@ -21,6 +21,7 @@ import org.onap.validation.yaml.error.SchemaValidationError; import org.onap.validation.yaml.error.YamlDocumentValidationError; import org.onap.validation.yaml.exception.YamlProcessingException; import org.onap.validation.yaml.model.YamlDocument; +import org.onap.validation.yaml.model.YamlDocumentFactory; import org.onap.validation.yaml.schema.YamlSchema; import org.onap.validation.yaml.schema.YamlSchemaFactory; @@ -28,23 +29,33 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; -public class YamlFileValidator { +public class YamlContentValidator { private static final int FIRST_DOCUMENT_INDEX = 1; + private static final YamlLoader YAML_LOADER = new YamlLoader(new YamlDocumentFactory()); - public List<YamlDocumentValidationError> validateYamlFileWithSchema(String pathToFile) - throws YamlProcessingException { + public List<YamlDocumentValidationError> validate(String pathToFile) + throws YamlProcessingException { + List<YamlDocument> documents = YAML_LOADER.loadMultiDocumentYamlFile(pathToFile); + return getYamlDocumentValidationErrors(documents); + } - List<YamlDocument> documents = new YamlLoader().loadMultiDocumentYamlFile(pathToFile); - if(!documents.isEmpty()) { - return validateDocuments(documents); - } else { + public List<YamlDocumentValidationError> validate(byte[] yamlWithSchema) + throws YamlProcessingException { + List<YamlDocument> documents = YAML_LOADER.loadMultiDocumentYaml(yamlWithSchema); + return getYamlDocumentValidationErrors(documents); + } + + private List<YamlDocumentValidationError> getYamlDocumentValidationErrors(List<YamlDocument> documents) throws YamlProcessingException { + if (documents.isEmpty()) { throw new YamlProcessingException("PM_Dictionary YAML file is empty"); + } else { + return validateDocuments(documents); } } private List<YamlDocumentValidationError> validateDocuments(List<YamlDocument> documents) - throws YamlProcessingException { + throws YamlProcessingException { List<YamlDocumentValidationError> yamlFileValidationErrors = new ArrayList<>(); YamlSchema schema = extractSchema(documents); @@ -52,7 +63,7 @@ public class YamlFileValidator { for (int index = FIRST_DOCUMENT_INDEX; index < documents.size(); index++) { List<SchemaValidationError> validationErrors = validator.validate(documents.get(index)); - yamlFileValidationErrors.addAll(transformErrors(index,validationErrors)); + yamlFileValidationErrors.addAll(transformErrors(index, validationErrors)); } return yamlFileValidationErrors; @@ -60,9 +71,9 @@ public class YamlFileValidator { private List<YamlDocumentValidationError> transformErrors(int index, List<SchemaValidationError> validationErrors) { return validationErrors - .stream() - .map(error->new YamlDocumentValidationError(index, error.getPath(), error.getMessage())) - .collect(Collectors.toList()); + .stream() + .map(error -> new YamlDocumentValidationError(index, error.getPath(), error.getMessage())) + .collect(Collectors.toList()); } private YamlSchema extractSchema(List<YamlDocument> documents) throws YamlProcessingException { diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java index 1a5eef9..c23da0a 100644 --- a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java @@ -24,6 +24,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; @@ -33,29 +34,49 @@ import java.util.List; class YamlLoader { - private static final Logger LOGGER = LoggerFactory.getLogger(YamlLoader.class); + private static final Logger LOGGER = LoggerFactory.getLogger(YamlLoader.class); + private final YamlDocumentFactory documentFactory; + + YamlLoader(YamlDocumentFactory documentFactory) { + this.documentFactory = documentFactory; + } + + List<YamlDocument> loadMultiDocumentYaml(byte[] yamlWithSchema) + throws YamlDocumentFactory.YamlDocumentParsingException { + List<YamlDocument> documents = new ArrayList<>(); + try (InputStream yamlStream = new ByteArrayInputStream(yamlWithSchema)) { + documents.addAll(loadMultiDocumentYaml(yamlStream)); + } catch (IOException e) { + LOGGER.error("Failed to load multi document YAML", e); + } + return documents; + } List<YamlDocument> loadMultiDocumentYamlFile(URL path) - throws YamlDocumentFactory.YamlDocumentParsingException { - List<YamlDocument> documentsFromFile = new ArrayList<>(); + throws YamlDocumentFactory.YamlDocumentParsingException { + List<YamlDocument> documents = new ArrayList<>(); try (InputStream yamlStream = path.openStream()) { - for (Object yamlDocument : new Yaml().loadAll(yamlStream)) { - documentsFromFile.add( - new YamlDocumentFactory().createYamlDocument(yamlDocument) - ); - } + documents.addAll(loadMultiDocumentYaml(yamlStream)); } catch (IOException e) { - LOGGER.error("Failed to load multi document YAML file",e); + LOGGER.error("Failed to load multi document YAML file", e); } - return documentsFromFile; + return documents; } List<YamlDocument> loadMultiDocumentYamlFile(String path) - throws YamlProcessingException { + throws YamlProcessingException { try { return loadMultiDocumentYamlFile(new URL("file://" + path)); } catch (MalformedURLException e) { throw new YamlProcessingException("Fail to read file under given path.", e); } } + + private List<YamlDocument> loadMultiDocumentYaml(InputStream yamlStream) throws YamlDocumentFactory.YamlDocumentParsingException { + List<YamlDocument> documents = new ArrayList<>(); + for (Object yamlDocument : new Yaml().loadAll(yamlStream)) { + documents.add(documentFactory.createYamlDocument(yamlDocument)); + } + return documents; + } } diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java index 0f5b480..a3e9636 100644 --- a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java @@ -24,18 +24,16 @@ import org.onap.validation.yaml.model.YamlDocumentFactory; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Optional; public class YamlSchemaBranchNode extends YamlSchemaNode { private final YamlDocument nextNodesInLazyForm; - private Optional<List<YamlSchemaNode>> nextNodes; + private List<YamlSchemaNode> nextNodes = Collections.emptyList(); YamlSchemaBranchNode(String name, String path, boolean required, String comment, YamlDocument nextNodesInLazyForm) { super(name, path, required, comment); this.nextNodesInLazyForm = nextNodesInLazyForm; - this.nextNodes = Optional.empty(); } @Override @@ -51,7 +49,7 @@ public class YamlSchemaBranchNode extends YamlSchemaNode { @Override public synchronized List<YamlSchemaNode> getNextNodes() throws YamlSchemaProcessingException { try { - return nextNodes.orElseGet(this::loadNextNodes); + return nextNodes.isEmpty() ? this.loadNextNodes() : nextNodes; } catch (YamlSchemaLazyLoadingException lazyLoadingException) { throw new YamlSchemaProcessingException(lazyLoadingException); } @@ -65,7 +63,7 @@ public class YamlSchemaBranchNode extends YamlSchemaNode { .createYamlDocument(nextNodesInLazyForm.getYaml().get(key)); loadedNextNodes.add(new YamlSchemaNodeFactory().createNode(key, getPath() + getName() + "/", substructure)); } - nextNodes = Optional.of(loadedNextNodes); + nextNodes = loadedNextNodes; return loadedNextNodes; } catch (YamlProcessingException e) { throw new YamlSchemaLazyLoadingException("Lazy loading failed, due to yaml parsing exception.",e); |