diff options
Diffstat (limited to 'dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate')
6 files changed, 273 insertions, 0 deletions
diff --git a/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/OutputFilePathGenerator.java b/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/OutputFilePathGenerator.java new file mode 100644 index 0000000..2ed7285 --- /dev/null +++ b/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/OutputFilePathGenerator.java @@ -0,0 +1,41 @@ +/* + *Copyright 2020 Nokia + * + * 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. + * + */ +package org.onap.validation.cli.command.validate; + +import java.nio.file.Path; + +public class OutputFilePathGenerator { + + public static final int FILE_NAME_ABBREVIATION_INDEX = 0; + public static final String BY_PERIOD_REGEX = "\\."; + public static final String POST_FIX = "-validation-results.json"; + + public Path responsePathFor(Path filePath) { + final Path parent = filePath.getParent(); + final String fileNameAbbreviation = getFileNameAbbreviation(filePath); + return Path.of(parent.toString(), createFileName(fileNameAbbreviation)); + } + + private String createFileName(String fileNameAbbreviation) { + return fileNameAbbreviation + POST_FIX; + } + + private String getFileNameAbbreviation(Path filePath) { + final Path fileName = filePath.getFileName(); + return fileName.toString().split(BY_PERIOD_REGEX)[FILE_NAME_ABBREVIATION_INDEX]; + } +} diff --git a/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ResponseModel.java b/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ResponseModel.java new file mode 100644 index 0000000..033031d --- /dev/null +++ b/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ResponseModel.java @@ -0,0 +1,51 @@ +/* + *Copyright 2020 Nokia + * + * 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. + * + */ +package org.onap.validation.cli.command.validate; + +import org.onap.validation.yaml.error.YamlDocumentValidationError; + +import java.util.Collections; +import java.util.List; + +public class ResponseModel { + + private final String file; + private final ResponseStatus status; + private final List<YamlDocumentValidationError> errors; + + public ResponseModel(String file, ResponseStatus status, List<YamlDocumentValidationError> errors) { + this.file = file; + this.status = status; + this.errors = errors; + } + + public String getFile() { + return file; + } + + public List<YamlDocumentValidationError> getErrors() { + return Collections.unmodifiableList(errors); + } + + public ResponseStatus getStatus() { + return status; + } + + public enum ResponseStatus { + PASS, FAILED + } +} diff --git a/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ResponseStorage.java b/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ResponseStorage.java new file mode 100644 index 0000000..3daed16 --- /dev/null +++ b/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ResponseStorage.java @@ -0,0 +1,30 @@ +/* + *Copyright 2020 Nokia + * + * 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. + * + */ +package org.onap.validation.cli.command.validate; + +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Path; + +public class ResponseStorage{ + + public <T> void store(Path filePath, T response) throws IOException { + try (PrintWriter out = new PrintWriter(filePath.toFile())) { + out.println(response); + } + } +} diff --git a/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ToJsonConverter.java b/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ToJsonConverter.java new file mode 100644 index 0000000..168daf8 --- /dev/null +++ b/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ToJsonConverter.java @@ -0,0 +1,28 @@ +/* + *Copyright 2020 Nokia + * + * 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. + * + */ +package org.onap.validation.cli.command.validate; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + + +public class ToJsonConverter { + public String convert(ResponseModel responseModel) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + return gson.toJson(responseModel); + } +} diff --git a/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ToResponseModelConverter.java b/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ToResponseModelConverter.java new file mode 100644 index 0000000..674068f --- /dev/null +++ b/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ToResponseModelConverter.java @@ -0,0 +1,31 @@ +/* + *Copyright 2020 Nokia + * + * 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. + * + */ +package org.onap.validation.cli.command.validate; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public final class ToResponseModelConverter { + + private ToResponseModelConverter() { + } + + public static ResponseModel toModel(String json) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + return gson.fromJson(json, ResponseModel.class); + } +} diff --git a/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ValidateDictionaryYamlCommand.java b/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ValidateDictionaryYamlCommand.java new file mode 100644 index 0000000..68b0b7a --- /dev/null +++ b/dictionaryvalidation/src/main/java/org/onap/validation/cli/command/validate/ValidateDictionaryYamlCommand.java @@ -0,0 +1,92 @@ +/* + *Copyright 2020 Nokia + * + * 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. + * + */ +package org.onap.validation.cli.command.validate; + +import org.onap.validation.cli.core.Command; +import org.onap.validation.cli.core.CommandException; +import org.onap.validation.cli.core.CommandResponse; +import org.onap.validation.yaml.YamlContentValidator; +import org.onap.validation.yaml.error.YamlDocumentValidationError; +import org.onap.validation.yaml.exception.YamlProcessingException; +import org.onap.validation.yaml.util.Args; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +public class ValidateDictionaryYamlCommand implements Command<String> { + + public static final int PATH_TO_FILE_INDEX = 0; + private final YamlContentValidator yamlContentValidator = new YamlContentValidator(); + + private final ToJsonConverter toJsonConverter; + private final OutputFilePathGenerator filePathGenerator = new OutputFilePathGenerator(); + + public ValidateDictionaryYamlCommand(ToJsonConverter toJsonConverter) { + this.toJsonConverter = toJsonConverter; + } + + @Override + public CommandResponse<String> execute(Args args) throws CommandException { + final String pathToFile = resolvePathToFile(args); + + try { + return validate(pathToFile); + } catch (YamlProcessingException e) { + throw new CommandException("Provided yaml file has invalid structure!", e); + } + } + + @Override + public String getName() { + return "Validate Dictionary yaml"; + } + + @Override + public Path getOutputFilePath(Args args) throws CommandException { + final String pathToFile = resolvePathToFile(args); + return this.filePathGenerator.responsePathFor(Path.of(pathToFile)); + } + + private CommandResponse<String> validate(String pathToFile) throws YamlProcessingException { + final List<YamlDocumentValidationError> errors = yamlContentValidator.validate(pathToFile); + return new CommandResponse<>( + toJsonConverter.convert(new ResponseModel(pathToFile, resolveResponseStatus(errors), errors)) + ,resolveCommandStatus(errors) + ); + } + + private ResponseModel.ResponseStatus resolveResponseStatus(List<YamlDocumentValidationError> errors) { + return errors.isEmpty() ? ResponseModel.ResponseStatus.PASS : ResponseModel.ResponseStatus.FAILED; + } + + private CommandResponse.CommandStatus resolveCommandStatus(List<YamlDocumentValidationError> errors) { + return errors.isEmpty() ? CommandResponse.CommandStatus.PASS : CommandResponse.CommandStatus.FAILED; + } + + private String resolvePathToFile(Args args) throws CommandException { + try { + final String pathToFile = args.getArg(PATH_TO_FILE_INDEX); + if (!Files.exists(Path.of(pathToFile))) { + throw new CommandException(String.format("File '%s' does not exist!", pathToFile)); + } + return pathToFile; + } catch (IllegalArgumentException ex) { + throw new CommandException("Command argument is missing: provide a path to file", ex); + } + } +} |