diff options
Diffstat (limited to 'dictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java')
-rw-r--r-- | dictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/dictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java b/dictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java new file mode 100644 index 0000000..d7d1153 --- /dev/null +++ b/dictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java @@ -0,0 +1,130 @@ +/* + * 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.model; + +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.ThrowableAssert.catchThrowable; +import static org.onap.validation.yaml.model.YamlDocumentFactory.YamlDocumentParsingException; + +class YamlDocumentFactoryTest { + + @Test + void shouldTurnMapOfUnknownKeyTypeToMapWithStringKeysAndBeAbleToReturnStringifyValues() + throws YamlDocumentParsingException { + // given + List<String> testList = List.of("element1", "element11"); + Map<Object, Object> testEmptyMap = Collections.emptyMap(); + Map<Object, Object> inputMap = Map.of( + "test", testList, + 345, "element2", + "test2", "element3", + 2.67, testEmptyMap); + + // when + YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); + + // then + assertYamlDocument(document, inputMap); + } + + @Test + void shouldTurnMapOfUnknownKeyTypeToMapWithStringKeysAndBeAbleToExtractSubStructure() + throws YamlDocumentParsingException { + // given + Map<Object, Object> subStructureMap = Map.of( + "subTest1", "subElement1", + "subTest2", "subElement2"); + Map<Object, Object> inputMap = Map.of( + "test", "element1", + "structure", subStructureMap); + + // when + YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); + + // then + assertYamlDocument(document, inputMap); + } + + @Test + void shouldTurnMapOfUnknownKeyTypeToMapWithStringKeysAndBeAbleToExtractParametersList() + throws YamlDocumentParsingException { + // given + List<String> parametersList = List.of("parameter1", "parameter2"); + Map<Object, Object> inputMap = Map.of( + "test", "element1", + "parameters", parametersList); + + // when + YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); + + // then + assertYamlDocument(document, inputMap); + } + + @Test + void shouldThrowExceptionIfGetSubStructureIsCalledOnList() + throws YamlDocumentParsingException { + // given + List<String> testList = List.of("element1", "element2"); + Map<Object, Object> inputMap = Collections.singletonMap("test", testList); + YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); + + // when + Throwable ex = catchThrowable(() -> document.getSubStructure("test")); + + // then + assertYamlDocumentParsingException(ex, testList); + } + + @Test + void shouldThrowExceptionIfGetSubStructureIsCalledOnString() + throws YamlDocumentParsingException { + // given + Map<Object, Object> inputMap = Collections.singletonMap("test", "testElement"); + YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); + + // when + Throwable ex = catchThrowable(() -> document.getSubStructure("test")); + + // then + assertYamlDocumentParsingException(ex, "testElement"); + } + + private void assertYamlDocument(YamlDocument document, Map<Object, Object> inputMap) { + assertThat(document).isNotNull(); + assertThat(document.getYaml()).containsExactlyEntriesOf(mapKeyToString(inputMap)); + } + + private Map<String, Object> mapKeyToString(Map<Object, Object> inputMap) { + return inputMap.entrySet().stream() + .collect(Collectors.toMap(w -> w.getKey().toString(), Map.Entry::getValue)); + } + + private void assertYamlDocumentParsingException(Throwable ex, Object unparsed) { + assertThat(ex) + .isInstanceOf(YamlDocumentParsingException.class) + .hasMessageContaining(String.format("Fail to parse given objects: %s as yaml document.", unparsed)); + } +} |