diff options
author | Bogumil Zebek <bogumil.zebek@nokia.com> | 2020-07-07 07:29:30 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-07-07 07:29:30 +0000 |
commit | a31e3acd014ae0f2cb7304bf25c9a7d50ae92eca (patch) | |
tree | e1b94a45dd0bf189200acc90f4d2a46b89e96158 /csarvalidation/src/main | |
parent | aecdd3fcf1f53ab957482f41d8bc64eda44ccb4f (diff) | |
parent | 3d6d5ed1c133e19c4091bd9ef98b5cc8e83eed51 (diff) |
Merge "Add wrappers for yaml generic objects"
Diffstat (limited to 'csarvalidation/src/main')
4 files changed, 129 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; + } + +} |