summaryrefslogtreecommitdiffstats
path: root/pmdictionaryvalidation/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'pmdictionaryvalidation/src/test')
-rw-r--r--pmdictionaryvalidation/src/test/java/org/onap/validation/cli/MainCITest.java144
-rw-r--r--pmdictionaryvalidation/src/test/java/org/onap/validation/cli/command/validate/OutputFilePathGeneratorTest.java43
-rw-r--r--pmdictionaryvalidation/src/test/java/org/onap/validation/cli/command/validate/ToJsonConverterTest.java69
-rw-r--r--pmdictionaryvalidation/src/test/java/org/onap/validation/cli/command/validate/ValidateYamlCommandTest.java96
-rw-r--r--pmdictionaryvalidation/src/test/java/org/onap/validation/cli/core/CliTest.java101
-rw-r--r--pmdictionaryvalidation/src/test/java/org/onap/validation/cli/util/ToResponseModelConverter.java32
-rw-r--r--pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlContentValidatorTest.java15
-rw-r--r--pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java2
-rw-r--r--pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java8
-rw-r--r--pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/util/ArgsTest.java39
-rw-r--r--pmdictionaryvalidation/src/test/resources/Not_Yaml_File.txt1
11 files changed, 539 insertions, 11 deletions
diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/MainCITest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/MainCITest.java
new file mode 100644
index 0000000..c6eb2ad
--- /dev/null
+++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/MainCITest.java
@@ -0,0 +1,144 @@
+/*
+ *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;
+
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.onap.validation.cli.command.validate.OutputFilePathGenerator;
+import org.onap.validation.cli.command.validate.ResponseModel;
+import org.onap.validation.cli.command.validate.ResponseStorage;
+import org.onap.validation.cli.core.Cli;
+import org.onap.validation.cli.core.CommandException;
+import org.onap.validation.cli.core.Console;
+import org.onap.validation.cli.util.ToResponseModelConverter;
+import org.onap.validation.yaml.YamlLoadingUtils;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@ExtendWith(MockitoExtension.class)
+class MainCITest {
+
+ public static final int JSON_RESPONSE_INDEX = 3;
+ public static final int OPERATION_INFO_MSG_INDEX = 5;
+ public static final int NO_ERROR = 0;
+ @Mock
+ private Console console;
+ @Mock
+ private ResponseStorage responseStorage;
+ private final OutputFilePathGenerator filePathGenerator = new OutputFilePathGenerator();
+
+ @Test
+ void shouldReportThatPathToFileWasNotProvided() {
+ // when
+ final int statusCode = Main.run(new String[0], console, responseStorage);
+
+ // then
+ assertThat(statusCode).isEqualTo(Cli.INTERNAL_ERROR_STATUS_CODE);
+ final ArgumentCaptor<CommandException> exceptionCaptor = ArgumentCaptor.forClass(CommandException.class);
+ verify(console).error(exceptionCaptor.capture());
+ verify(console).error(Cli.APPLICATION_INTERNAL_ERROR_MSG);
+ assertThat(exceptionCaptor.getValue().getMessage()).isEqualTo("Command argument is missing: provide a path to file");
+
+ }
+
+ @Test
+ void shouldReportThatFileDoseNotExist() throws IOException {
+ // when
+ final String pathToYaml = "/path/to/nonExisting.yaml";
+ final int statusCode = Main.run(new String[]{pathToYaml}, console, new ResponseStorage());
+
+ // then
+ assertThat(statusCode).isEqualTo(Cli.INTERNAL_ERROR_STATUS_CODE);
+ final ArgumentCaptor<CommandException> exceptionCaptor = ArgumentCaptor.forClass(CommandException.class);
+ verify(console).error(exceptionCaptor.capture());
+ verify(console).error(Cli.APPLICATION_INTERNAL_ERROR_MSG);
+ assertThat(exceptionCaptor.getValue().getMessage()).isEqualTo("File '/path/to/nonExisting.yaml' does not exist!");
+ verify(responseStorage, never()).store(eq(Path.of(pathToYaml)), anyString());
+ }
+
+ @Test
+ void shouldReportThatFileIsBroken() throws IOException {
+ // when
+ final String pathToFile = YamlLoadingUtils.getUrlForGivenPath("Not_Yaml_File.txt").getPath();
+ final int statusCode = Main.run(new String[]{pathToFile}, console, new ResponseStorage());
+
+ // then
+ assertThat(statusCode).isEqualTo(Cli.INTERNAL_ERROR_STATUS_CODE);
+ final ArgumentCaptor<CommandException> exceptionCaptor = ArgumentCaptor.forClass(CommandException.class);
+ verify(console).error(exceptionCaptor.capture());
+ verify(console).error(Cli.APPLICATION_INTERNAL_ERROR_MSG);
+ assertThat(exceptionCaptor.getValue().getMessage()).isEqualTo("Provided yaml file has invalid structure!");
+ verify(responseStorage, never()).store(eq(Path.of(pathToFile)), anyString());
+ }
+
+ @Test
+ void shouldValidateProperYamlFile() throws IOException {
+ // when
+ final String path = YamlLoadingUtils.getUrlForGivenPath(YamlLoadingUtils.PATH_TO_SIMPLE_VALID_SCHEMA).getPath();
+ final int statusCode = Main.run(new String[]{path}, console, responseStorage);
+
+ // then
+ assertThat(statusCode).isEqualTo(Cli.PASS_STATUS_CODE);
+ assertThatResponseWasLoggedAtConsole(
+ path,
+ ResponseModel.ResponseStatus.PASS,
+ NO_ERROR
+ );
+ }
+
+ @Test
+ void shouldValidateYamlWithErrors() throws IOException {
+ // when
+ final String path = YamlLoadingUtils.getUrlForGivenPath(YamlLoadingUtils.PATH_TO_YAML_WITH_WRONG_VALUES).getPath();
+ final int statusCode = Main.run(new String[]{path}, console, responseStorage);
+
+ // then
+ assertThat(statusCode).isEqualTo(Cli.FAILED_STATUS_CODE);
+ assertThatResponseWasLoggedAtConsole(
+ path,
+ ResponseModel.ResponseStatus.FAILED,
+ 4
+ );
+ }
+
+ private void assertThatResponseWasLoggedAtConsole(String pathToFile, ResponseModel.ResponseStatus responseStatus, int expectedErrors) throws IOException {
+ ArgumentCaptor<String> consoleInfoCaptor = ArgumentCaptor.forClass(String.class);
+ verify(console, times(6)).info(consoleInfoCaptor.capture());
+ final List<String> allValues = consoleInfoCaptor.getAllValues();
+ final String json = allValues.get(JSON_RESPONSE_INDEX);
+ final ResponseModel responseModel = ToResponseModelConverter.toModel(json);
+ assertThat(responseModel.getFile()).isEqualTo(pathToFile);
+ assertThat(responseModel.getStatus()).isEqualTo(responseStatus);
+ assertThat(responseModel.getErrors()).hasSize(expectedErrors);
+ assertThat(consoleInfoCaptor.getAllValues().get(OPERATION_INFO_MSG_INDEX)).isEqualTo(Cli.APPLICATION_EXIT_SUCCESSFULLY_MSG);
+ verify(responseStorage).store(eq(filePathGenerator.responsePathFor(Path.of(pathToFile))), eq(json));
+ }
+
+}
diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/command/validate/OutputFilePathGeneratorTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/command/validate/OutputFilePathGeneratorTest.java
new file mode 100644
index 0000000..b4d68b4
--- /dev/null
+++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/command/validate/OutputFilePathGeneratorTest.java
@@ -0,0 +1,43 @@
+/*
+ *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.junit.jupiter.api.Test;
+
+import java.nio.file.Path;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+class OutputFilePathGeneratorTest {
+
+ private final OutputFilePathGenerator filePathGenerator = new OutputFilePathGenerator();
+
+ @Test
+ void shouldReturnPathToFileWithResponse() {
+ // given
+ final Path pathToYaml = Path.of("/some/path/PMDictionary.yaml");
+ final Path expected = Path.of("/some/path/PMDictionary-validation-results.json");
+
+ // when
+ final Path actual = filePathGenerator.responsePathFor(pathToYaml);
+
+ // then
+ assertThat(actual).isEqualTo(expected);
+ }
+
+}
diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/command/validate/ToJsonConverterTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/command/validate/ToJsonConverterTest.java
new file mode 100644
index 0000000..3c97681
--- /dev/null
+++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/command/validate/ToJsonConverterTest.java
@@ -0,0 +1,69 @@
+/*
+ *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.junit.jupiter.api.Test;
+import org.onap.validation.yaml.error.YamlDocumentValidationError;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.onap.validation.cli.util.ToResponseModelConverter.toModel;
+
+class ToJsonConverterTest {
+
+ private static final String PATH_TO_TEST_YAML = "/path/to/test.yaml";
+ private final ToJsonConverter toJsonConverter = new ToJsonConverter();
+
+ @Test
+ void shouldConvertEmptyArray() {
+ // given
+ final ResponseModel responseModel = new ResponseModel(PATH_TO_TEST_YAML,
+ ResponseModel.ResponseStatus.PASS,
+ List.of());
+
+ // when
+ String json = toJsonConverter.convert(responseModel);
+
+ // then
+ ResponseModel actual = toModel(json);
+ assertThat(actual.getFile()).isEqualTo(PATH_TO_TEST_YAML);
+ assertThat(actual.getStatus()).isEqualTo(ResponseModel.ResponseStatus.PASS);
+ assertThat(actual.getErrors()).isEmpty();
+ }
+
+
+ @Test
+ void shouldConvertListOfErrors() {
+ // given
+ final ResponseModel responseModel = new ResponseModel(PATH_TO_TEST_YAML,
+ ResponseModel.ResponseStatus.FAILED,
+ List.of(
+ new YamlDocumentValidationError(1, PATH_TO_TEST_YAML, "error1"),
+ new YamlDocumentValidationError(2, PATH_TO_TEST_YAML, "error2")));
+
+ // when
+ String json = toJsonConverter.convert(responseModel);
+
+ // then
+
+ ResponseModel actual = toModel(json);
+ assertThat(actual.getFile()).isEqualTo(PATH_TO_TEST_YAML);
+ assertThat(actual.getStatus()).isEqualTo(ResponseModel.ResponseStatus.FAILED);
+ assertThat(actual.getErrors()).hasSize(2);
+ }
+}
diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/command/validate/ValidateYamlCommandTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/command/validate/ValidateYamlCommandTest.java
new file mode 100644
index 0000000..6347f75
--- /dev/null
+++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/command/validate/ValidateYamlCommandTest.java
@@ -0,0 +1,96 @@
+/*
+ *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.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.onap.validation.cli.core.CommandException;
+import org.onap.validation.cli.core.CommandResponse;
+import org.onap.validation.yaml.YamlLoadingUtils;
+import org.onap.validation.yaml.util.Args;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.onap.validation.cli.util.ToResponseModelConverter.toModel;
+
+@ExtendWith(MockitoExtension.class)
+class ValidateYamlCommandTest {
+
+ private final ToJsonConverter toJsonConverter = new ToJsonConverter();
+ private ValidatePmDictionaryYamlCommand validateYamlCommand;
+
+ @BeforeEach
+ void setUp() {
+ this.validateYamlCommand = new ValidatePmDictionaryYamlCommand(toJsonConverter);
+ }
+
+ @Test
+ void shouldReportAnErrorWhenFileDoesNotExist() {
+ // given
+ Args args = new Args(List.of("/invalid/filePath/toValidate.yaml"));
+
+ // when
+ assertThatThrownBy(() -> validateYamlCommand.execute(args)).hasMessage("File '/invalid/filePath/toValidate.yaml' does not exist!");
+ }
+
+ @Test
+ void shouldReportAnErrorWhenPathToFileWasNotPass() {
+ // given
+ Args args = new Args(List.of());
+
+ // when
+ assertThatThrownBy(() -> validateYamlCommand.execute(args)).hasMessage("Command argument is missing: provide a path to file");
+ }
+
+ @Test
+ void shouldValidateFileWithoutAnyError() throws CommandException {
+ // given
+ final String path = YamlLoadingUtils.getUrlForGivenPath(YamlLoadingUtils.PATH_TO_SIMPLE_VALID_SCHEMA).getPath();
+ Args args = new Args(List.of(path));
+
+ // when
+ final CommandResponse<String> commandResponse = validateYamlCommand.execute(args);
+
+ // then
+ assertThat(commandResponse.getCommandStatus()).isEqualTo(CommandResponse.CommandStatus.PASS);
+ ResponseModel actual = toModel(commandResponse.getResult());
+ assertThat(actual.getFile()).isEqualTo(path);
+ assertThat(actual.getStatus()).isEqualTo(ResponseModel.ResponseStatus.PASS);
+ assertThat(actual.getErrors()).isEmpty();
+ }
+
+ @Test
+ void shouldReportThatFileHasSomeErrors() throws CommandException {
+ // given
+ final String path = YamlLoadingUtils.getUrlForGivenPath(YamlLoadingUtils.PATH_TO_YAML_WITH_WRONG_VALUES).getPath();
+ Args args = new Args(List.of(path));
+
+ // when
+ final CommandResponse<String> commandResponse = validateYamlCommand.execute(args);
+
+ // then
+ assertThat(commandResponse.getCommandStatus()).isEqualTo(CommandResponse.CommandStatus.FAILED);
+ ResponseModel actual = toModel(commandResponse.getResult());
+ assertThat(actual.getFile()).isEqualTo(path);
+ assertThat(actual.getStatus()).isEqualTo(ResponseModel.ResponseStatus.FAILED);
+ assertThat(actual.getErrors()).hasSize(4);
+ }
+}
diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/core/CliTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/core/CliTest.java
new file mode 100644
index 0000000..c8ad629
--- /dev/null
+++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/core/CliTest.java
@@ -0,0 +1,101 @@
+/*
+ *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.core;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.onap.validation.cli.command.validate.ResponseStorage;
+import org.onap.validation.yaml.util.Args;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+
+@ExtendWith(MockitoExtension.class)
+class CliTest {
+ @Mock
+ private Console console;
+
+ @Mock
+ private ResponseStorage responseStorage;
+
+ @Mock
+ private Command<String> validationCommand;
+
+ private Cli<String> cli;
+
+ @BeforeEach
+ void setUp() {
+ cli = new Cli<>(console, responseStorage);
+ }
+
+ @Test
+ void shouldExecuteCommandWithoutAnyError() throws CommandException {
+
+ // given
+ Args args = new Args(List.of());
+ when(validationCommand.execute(args)).thenReturn(
+ new CommandResponse<>("{ 'errors': [] }", CommandResponse.CommandStatus.PASS)
+ );
+
+ // when
+ final int exitCode = cli.run(args, validationCommand);
+
+ // then
+ assertThat(exitCode).isEqualTo(Cli.PASS_STATUS_CODE);
+ verify(console).info("{ 'errors': [] }");
+ }
+
+ @Test
+ void shouldHandleErrorReportedByCommand() throws CommandException {
+
+ // given
+ Args args = new Args(List.of());
+ final CommandException commandException = new CommandException("It should be reported");
+ Mockito.doThrow(commandException).when(validationCommand).execute(args);
+
+ // when
+ final int exitCode = cli.run(args, validationCommand);
+
+ // then
+ assertThat(exitCode).isEqualTo(Cli.INTERNAL_ERROR_STATUS_CODE);
+ verify(console).error(commandException);
+ }
+
+ @Test
+ void shouldHandleRuntimeException() throws CommandException {
+
+ // given
+ Args args = new Args(List.of());
+ final RuntimeException commandException = new RuntimeException();
+ Mockito.doThrow(commandException).when(validationCommand).execute(args);
+
+ // when
+ final int exitCode = cli.run(args, validationCommand);
+
+ // then
+ assertThat(exitCode).isEqualTo(Cli.INTERNAL_ERROR_STATUS_CODE);
+ verify(console).error(commandException);
+ }
+}
diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/util/ToResponseModelConverter.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/util/ToResponseModelConverter.java
new file mode 100644
index 0000000..d78f493
--- /dev/null
+++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/cli/util/ToResponseModelConverter.java
@@ -0,0 +1,32 @@
+/*
+ *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.util;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import org.onap.validation.cli.command.validate.ResponseModel;
+
+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/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlContentValidatorTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlContentValidatorTest.java
index b6c2548..1412085 100644
--- a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlContentValidatorTest.java
+++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlContentValidatorTest.java
@@ -29,16 +29,19 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.onap.validation.yaml.YamlLoadingUtils.PATH_TO_MULTI_DOCUMENT_INVALID_YAML;
import static org.onap.validation.yaml.YamlLoadingUtils.PATH_TO_VALID_JSON_STYLE_YAML;
-import static org.onap.validation.yaml.YamlLoadingUtils.PATH_TO_VALID_YAML;
+import static org.onap.validation.yaml.YamlLoadingUtils.PATH_TO_YAML_WITH_WRONG_VALUES;
import static org.onap.validation.yaml.YamlLoadingUtils.readFile;
class YamlContentValidatorTest {
+
+ final YamlContentValidator yamlContentValidator = new YamlContentValidator();
+
@Nested
class FromStringPathValidator {
@Test
void shouldReturnCorrectErrorsWhenGivenPathToValidPmDictionaryFile() throws YamlProcessingException {
// given
- String path = getFullPathForGivenResources(PATH_TO_VALID_YAML);
+ String path = getFullPathForGivenResources(PATH_TO_YAML_WITH_WRONG_VALUES);
// when
List<YamlDocumentValidationError> validationErrors = new YamlContentValidator().validate(path);
@@ -65,7 +68,7 @@ class YamlContentValidatorTest {
String path = getFullPathForGivenResources(PATH_TO_MULTI_DOCUMENT_INVALID_YAML);
//when then
- assertThatThrownBy(() -> new YamlContentValidator().validate(path))
+ assertThatThrownBy(() -> yamlContentValidator.validate(path))
.isInstanceOf(ParserException.class)
.hasMessageContaining("expected the node content, but found '<document end>'");
}
@@ -76,7 +79,7 @@ class YamlContentValidatorTest {
String path = "invalid/path/to/pm_dictionary";
//when then
- assertThatThrownBy(() -> new YamlContentValidator().validate(path))
+ assertThatThrownBy(() -> yamlContentValidator.validate(path))
.isInstanceOf(YamlProcessingException.class)
.hasMessageContaining("PM_Dictionary YAML file is empty");
}
@@ -87,7 +90,7 @@ class YamlContentValidatorTest {
@Test
void shouldReturnCorrectErrorsWhenGivenPmDictionaryFileWithErrors() throws YamlProcessingException, IOException {
// given
- byte[] yaml = readFile(PATH_TO_VALID_YAML);
+ byte[] yaml = readFile(PATH_TO_YAML_WITH_WRONG_VALUES);
// when
List<YamlDocumentValidationError> validationErrors = new YamlContentValidator().validate(yaml);
@@ -158,4 +161,4 @@ class YamlContentValidatorTest {
private String getFullPathForGivenResources(String pathToValidYaml) {
return this.getClass().getClassLoader().getResource(pathToValidYaml).getPath();
}
-} \ No newline at end of file
+}
diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java
index 3b26541..3e1f601 100644
--- a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java
+++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java
@@ -106,7 +106,7 @@ class YamlLoaderTest {
@Test
void shouldLoadAllDocumentsFromYamlFile() throws YamlDocumentFactory.YamlDocumentParsingException, IOException {
// when
- List<YamlDocument> documents = YAML_LOADER.loadMultiDocumentYaml(YamlLoadingUtils.readFile(YamlLoadingUtils.PATH_TO_VALID_YAML));
+ List<YamlDocument> documents = YAML_LOADER.loadMultiDocumentYaml(YamlLoadingUtils.readFile(YamlLoadingUtils.PATH_TO_YAML_WITH_WRONG_VALUES));
// then
assertThat(documents).hasSize(EXPECTED_NUMBER_OF_DOCUMENTS);
diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java
index b16d3ea..dc1ce3b 100644
--- a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java
+++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java
@@ -39,7 +39,7 @@ public final class YamlLoadingUtils {
public static final int YAML_DOCUMENT_WITH_WRONG_VALUE_IN_ARRAY_INDEX = 3;
public static final int YAML_DOCUMENT_WITH_MISSING_FIELD_INDEX = 2;
public static final int YAML_DOCUMENT_WITH_MISSING_FIELD_AND_WRONG_VALUE_INDEX = 1;
- public static final String PATH_TO_VALID_YAML = "yaml_schema/PM_Dictionary.yaml";
+ public static final String PATH_TO_YAML_WITH_WRONG_VALUES = "yaml_schema/PM_Dictionary.yaml";
public static final String PATH_TO_VALID_JSON_STYLE_YAML = "yaml_schema/PM_Dictionary_JSON_Style.yaml";
public static final String PATH_TO_SIMPLE_VALID_SCHEMA = "yaml_schema/Simple_Valid_Schema.yaml";
public static final String PATH_TO_SIMPLE_VALID_SCHEMA_MULTI_ROOT = "yaml_schema/Simple_Valid_Schema_Multi_Root.yaml";
@@ -50,7 +50,7 @@ public final class YamlLoadingUtils {
public static final String PATH_TO_INVALID_YAML_WITH_UNKNOWN_ESCAPE_CHARACTER = "yaml_schema/Simple_Unknown_Escape_Character.yaml";
public static List<YamlDocument> loadValidMultiDocumentYamlFile() throws YamlDocumentParsingException {
- return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_YAML));
+ return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_YAML_WITH_WRONG_VALUES));
}
public static List<YamlDocument> loadValidJsonStyleMultiDocumentYamlFile() throws YamlDocumentParsingException {
@@ -58,7 +58,7 @@ public final class YamlLoadingUtils {
}
public static List<YamlDocument> loadValidMultiDocumentYamlFileUsingStringPath() throws YamlProcessingException {
- return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_YAML).getPath());
+ return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_YAML_WITH_WRONG_VALUES).getPath());
}
public static YamlDocument loadSimpleValidYamlSchemaFile() throws YamlDocumentParsingException {
@@ -98,7 +98,7 @@ public final class YamlLoadingUtils {
return Files.readAllBytes(Path.of(file));
}
- private static URL getUrlForGivenPath(String path) {
+ public static URL getUrlForGivenPath(String path) {
return YamlLoadingUtils.class.getClassLoader().getResource(path);
}
}
diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/util/ArgsTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/util/ArgsTest.java
new file mode 100644
index 0000000..0090683
--- /dev/null
+++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/util/ArgsTest.java
@@ -0,0 +1,39 @@
+/*
+ *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.yaml.util;
+
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+class ArgsTest {
+
+ @Test
+ void shouldThrowAnExceptionWhenArgIsNotAvailable() {
+ Args args = new Args(List.of());
+ Assertions.assertThatThrownBy(() -> args.getArg(0)).hasMessage("Argument with index 0 is not available!");
+ }
+
+ @Test
+ void shouldReturnArgumentForGivenIndex() {
+ Args args = new Args(List.of("one","two", "three"));
+ Assertions.assertThat(args.getArg(0)).isEqualTo("one");
+ Assertions.assertThat(args.getArg(1)).isEqualTo("two");
+ Assertions.assertThat(args.getArg(2)).isEqualTo("three");
+ }
+}
diff --git a/pmdictionaryvalidation/src/test/resources/Not_Yaml_File.txt b/pmdictionaryvalidation/src/test/resources/Not_Yaml_File.txt
new file mode 100644
index 0000000..be6ee44
--- /dev/null
+++ b/pmdictionaryvalidation/src/test/resources/Not_Yaml_File.txt
@@ -0,0 +1 @@
+Not a Yaml file!