/* * 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 testList = List.of("element1", "element11"); Map testEmptyMap = Collections.emptyMap(); Map 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 subStructureMap = Map.of( "subTest1", "subElement1", "subTest2", "subElement2"); Map 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 parametersList = List.of("parameter1", "parameter2"); Map 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 testList = List.of("element1", "element2"); Map 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 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 inputMap) { assertThat(document).isNotNull(); assertThat(document.getYaml()).containsExactlyEntriesOf(mapKeyToString(inputMap)); } private Map mapKeyToString(Map 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)); } }