From 3d6d5ed1c133e19c4091bd9ef98b5cc8e83eed51 Mon Sep 17 00:00:00 2001 From: Bartosz Gardziejewski Date: Wed, 1 Jul 2020 14:10:59 +0200 Subject: Add wrappers for yaml generic objects Issue-ID: VNFSDK-594 Signed-off-by: Bartosz Gardziejewski Change-Id: Ic6e2f291a0f8d5a34b75208f7cff68c4262f6cbf --- .../yaml/model/YamlDocumentFactoryTest.java | 155 +++++++++++++++++++++ .../yaml/model/YamlParameterListFactoryTest.java | 84 +++++++++++ 2 files changed, 239 insertions(+) create mode 100644 csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java create mode 100644 csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java (limited to 'csarvalidation/src/test/java/org') diff --git a/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java b/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java new file mode 100644 index 0000000..469b39f --- /dev/null +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java @@ -0,0 +1,155 @@ +/* + * 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.assertj.core.util.Lists; +import org.junit.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.onap.validation.yaml.model.YamlDocumentFactory.YamlDocumentParsingException; +import static org.onap.validation.yaml.model.YamlParameterListFactory.YamlParameterListParsingException; + +public class YamlDocumentFactoryTest { + + @Test + public void shouldTurnMapOfUnknownKeyTypeToMapWithStringKeysAndBeAbleToReturnStringifyValues() + throws YamlDocumentParsingException { + // given + Map inputMap = new HashMap<>(); + List testList = Lists.list("element1", "element11"); + Map testEmptyMap = Collections.emptyMap(); + + inputMap.put("test", testList); + inputMap.put(345, "element2"); + inputMap.put("test2", "element3"); + inputMap.put(2.67, testEmptyMap); + + // when + YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); + + // then + assertThat(document).isNotNull(); + assertThat(document.getYaml()).containsKeys("test", "345", "test2", "2.67"); + + assertThat(document.getYaml().get("test")).isEqualTo(testList); + assertThat(document.getValue("test")).isEqualTo("[element1, element11]"); + + assertThat(document.getValue("345")).isEqualTo("element2"); + assertThat(document.getValue("test2")).isEqualTo("element3"); + + assertThat(document.getYaml().get("2.67")).isEqualTo(testEmptyMap); + assertThat(document.getValue("2.67")).isEqualTo("{}"); + } + + @Test + public void shouldTurnMapOfUnknownKeyTypeToMapWithStringKeysAndBeAbleToExtractSubStructure() + throws YamlDocumentParsingException { + // given + Map inputMap = new HashMap<>(); + Map subStructureMap = new HashMap<>(); + + inputMap.put("test", "element1"); + inputMap.put("structure", subStructureMap); + + subStructureMap.put("subTest1", "subElement1"); + subStructureMap.put("subTest2", "subElement2"); + + // when + YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); + + // then + assertThat(document).isNotNull(); + assertThat(document.getYaml()).containsKeys("test", "structure"); + assertThat(document.getValue("test")).isEqualTo("element1"); + + assertThat(document.getSubStructure("structure")).isNotNull(); + assertThat(document.getSubStructure("structure").getValue("subTest1")).isEqualTo("subElement1"); + assertThat(document.getSubStructure("structure").getValue("subTest2")).isEqualTo("subElement2"); + } + + @Test + public void shouldTurnMapOfUnknownKeyTypeToMapWithStringKeysAndBeAbleToExtractParametersList() + throws YamlDocumentParsingException, YamlParameterListParsingException { + // given + Map inputMap = new HashMap<>(); + List parametersList = new LinkedList<>(); + + inputMap.put("test", "element1"); + inputMap.put("parameters", parametersList); + + parametersList.add("parameter1"); + parametersList.add("parameter2"); + + // when + YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); + + // then + assertThat(document).isNotNull(); + assertThat(document.getYaml()).containsKeys("test", "parameters"); + assertThat(document.getValue("test")).isEqualTo("element1"); + + assertThat(document.getListOfValues("parameters")).isNotNull(); + assertThat(document.getListOfValues("parameters").getParameters()).contains("parameter1","parameter2"); + } + + @Test + public void shouldThrowExceptionIfGetSubStructureIsCalledOnList() + throws YamlDocumentParsingException { + // given + Map inputMap = new HashMap<>(); + List testList = Lists.list("element1", "element2"); + + inputMap.put("test", testList); + + YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); + + // when then + assertThatThrownBy(() -> + document.getSubStructure("test") + ).isInstanceOf(YamlDocumentParsingException.class) + .hasMessageContaining( + String.format("Fail to parse given objects: %s as yaml document", testList) + ); + } + + @Test + public void shouldThrowExceptionIfGetSubStructureIsCalledOnString() + throws YamlDocumentParsingException { + // given + Map inputMap = new HashMap<>(); + + inputMap.put("test", "testElement"); + + YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); + + // when then + assertThatThrownBy(() -> + document.getSubStructure("test") + ).isInstanceOf(YamlDocumentParsingException.class) + .hasMessageContaining( + String.format("Fail to parse given objects: %s as yaml document.", "testElement") + ); + } +} diff --git a/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java b/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java new file mode 100644 index 0000000..1f370cb --- /dev/null +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java @@ -0,0 +1,84 @@ +/* + * 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.assertj.core.util.Lists; +import org.junit.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class YamlParameterListFactoryTest { + + @Test + public void shouldCreateEmptyParametersList() { + // when + YamlParametersList parametersList = new YamlParameterListFactory().createEmptyYamlParameterList(); + + // then + assertThat(parametersList).isNotNull(); + assertThat(parametersList.getParameters()).isEmpty(); + } + + @Test + public void shouldCreateParametersListContainingStringsFromListContainingSimpleTypes() + throws YamlParameterListFactory.YamlParameterListParsingException { + // given + List testList = Lists.list("test1",3,23.45,'a',"test2"); + + // when + YamlParametersList parametersList = new YamlParameterListFactory().createYamlParameterList(testList); + + // then + assertThat(parametersList).isNotNull(); + assertThat(parametersList.getParameters()).hasSize(5); + assertThat(parametersList.getParameters()).contains("test1","test2","3","23.45","a"); + } + + @Test + public void shouldCreateParametersListContainingStringsFromListContainingVariousTypes() + throws YamlParameterListFactory.YamlParameterListParsingException { + // given + List testList = Lists.list("test1",3,Lists.list(2,3,4),"test2"); + + // when + YamlParametersList parametersList = new YamlParameterListFactory().createYamlParameterList(testList); + + // then + assertThat(parametersList).isNotNull(); + assertThat(parametersList.getParameters()).hasSize(4); + assertThat(parametersList.getParameters()).contains("test1","test2","3","[2, 3, 4]"); + } + + @Test + public void shouldCreateListWithOneStringWhenGivenObjectIsNotList() + throws YamlParameterListFactory.YamlParameterListParsingException { + // given + Object testObject = "test"; + + // when + YamlParametersList parametersList = new YamlParameterListFactory().createYamlParameterList(testObject); + + // then + assertThat(parametersList).isNotNull(); + assertThat(parametersList.getParameters()).hasSize(1); + assertThat(parametersList.getParameters()).contains("test"); + } + +} -- cgit 1.2.3-korg