summaryrefslogtreecommitdiffstats
path: root/csarvalidation/src
diff options
context:
space:
mode:
Diffstat (limited to 'csarvalidation/src')
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java24
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java13
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java61
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java34
-rw-r--r--csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java155
-rw-r--r--csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java84
6 files changed, 368 insertions, 3 deletions
diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java
index 5ed4690..7f80d1c 100644
--- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java
+++ b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java
@@ -23,13 +23,35 @@ public class YamlDocument {
private final Map<String, ?> yaml;
- public YamlDocument(Map<String, ?> yaml) {
+ YamlDocument(Map<String, ?> yaml) {
this.yaml = yaml;
}
public Map<String, ?> getYaml() {
return yaml;
}
+
+ public boolean containsKey(String key) {
+ return yaml.containsKey(key);
+ }
+
+ public String getValue(String key) {
+ return yaml.get(key).toString();
+ }
+
+ public YamlParametersList getListOfValues(String key)
+ throws YamlParameterListFactory.YamlParameterListParsingException {
+ return new YamlParameterListFactory().createYamlParameterList(
+ yaml.get(key)
+ );
+ }
+
+ public YamlDocument getSubStructure(String name)
+ throws YamlDocumentFactory.YamlDocumentParsingException {
+ return new YamlDocumentFactory().createYamlDocument(
+ yaml.get(name)
+ );
+ }
}
diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java
index 8e571a9..1cda34b 100644
--- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java
+++ b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java
@@ -17,21 +17,30 @@
package org.onap.validation.yaml.model;
+import java.util.HashMap;
import java.util.Map;
public class YamlDocumentFactory {
public YamlDocument createYamlDocument(Object yaml) throws YamlDocumentParsingException {
try {
- Map<String, ?> parsedYaml = (Map<String, ?>) yaml;
+ Map<String, Object> parsedYaml = transformMap((Map) yaml);
return new YamlDocument(parsedYaml);
} catch (ClassCastException e) {
throw new YamlDocumentParsingException(
- String.format("Fail to parse given objects: %s as yaml",yaml), e
+ String.format("Fail to parse given objects: %s as yaml document.", yaml), e
);
}
}
+ private Map<String, Object> transformMap(Map<?, ?> yaml) {
+ Map<String, Object> parsedYaml = new HashMap<>();
+ for (Map.Entry entry: yaml.entrySet()) {
+ parsedYaml.put(entry.getKey().toString(), entry.getValue());
+ }
+ return parsedYaml;
+ }
+
public static class YamlDocumentParsingException extends Exception {
YamlDocumentParsingException(String message, Throwable throwable) {
diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java
new file mode 100644
index 0000000..b85ec7c
--- /dev/null
+++ b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java
@@ -0,0 +1,61 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class YamlParameterListFactory {
+
+ public YamlParametersList createEmptyYamlParameterList() {
+ return new YamlParametersList(Collections.emptyList());
+ }
+
+ public YamlParametersList createYamlParameterList(Object yaml)
+ throws YamlParameterListParsingException {
+ try {
+ return parseYamlToListOfPossibleValues(yaml);
+ } catch (ClassCastException e) {
+ throw new YamlParameterListParsingException(
+ String.format("Fail to parse given objects: %s as list.",yaml), e
+ );
+ }
+ }
+
+ private YamlParametersList parseYamlToListOfPossibleValues(Object yaml) {
+ List<String> parametersList = new ArrayList<>();
+ if( yaml instanceof List) {
+ for (Object element : (List) yaml) {
+ parametersList.add(element.toString());
+ }
+ } else {
+ parametersList.add(yaml.toString());
+ }
+ return new YamlParametersList(parametersList);
+ }
+
+
+ public static class YamlParameterListParsingException extends Exception {
+
+ YamlParameterListParsingException(String message, Throwable throwable) {
+ super(message, throwable);
+ }
+ }
+
+}
diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java
new file mode 100644
index 0000000..2b93c74
--- /dev/null
+++ b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java
@@ -0,0 +1,34 @@
+/*
+ * 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 java.util.List;
+
+public class YamlParametersList {
+
+ private final List<String> parameters;
+
+ YamlParametersList(List<String> parameters) {
+ this.parameters = parameters;
+ }
+
+ public List<String> getParameters() {
+ return parameters;
+ }
+
+}
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<Object, Object> inputMap = new HashMap<>();
+ List<String> testList = Lists.list("element1", "element11");
+ Map<Object, Object> 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<Object, Object> inputMap = new HashMap<>();
+ Map<Object, Object> 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<Object, Object> inputMap = new HashMap<>();
+ List<String> 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<Object, Object> inputMap = new HashMap<>();
+ List<String> 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<Object, Object> 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<Object> 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<Object> 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");
+ }
+
+}