2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.gui.editors.apex.rest.handling.converter.tosca;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.contains;
24 import static org.hamcrest.Matchers.is;
25 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter.ToscaKey.POLICIES;
26 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter.ToscaKey.PROPERTIES;
27 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter.ToscaKey.TOPOLOGY_TEMPLATE;
28 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter.ToscaKey.TOSCA_DEFINITIONS_VERSION;
29 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ToscaTemplateProcessor.ErrorMessage.INVALID_ENTRY;
30 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ToscaTemplateProcessor.ErrorMessage.INVALID_POLICY;
31 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ToscaTemplateProcessor.ErrorMessage.INVALID_TOSCA_TEMPLATE;
32 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ToscaTemplateProcessor.ErrorMessage.MISSING_ENTRY;
33 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ToscaTemplateProcessor.ErrorMessage.MISSING_POLICY;
34 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ToscaTemplateProcessor.ErrorMessage.ONLY_ONE_POLICY_ALLOWED;
36 import java.io.FileInputStream;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.List;
43 import org.junit.Test;
44 import org.onap.policy.common.utils.coder.StandardCoder;
46 public class ToscaTemplateProcessorTest {
48 private final ToscaTemplateProcessor toscaTemplateProcessor = new ToscaTemplateProcessor(new StandardCoder());
49 private final Path testResourcesPath = Paths.get("src", "test", "resources", "processor");
52 public void testProcessSuccess() throws IOException {
53 final String fileName = "ToscaTemplate.json";
54 final ProcessedTemplate process;
55 try (final FileInputStream fileInputStream = readFileAsStream(fileName)) {
56 process = toscaTemplateProcessor.process(fileInputStream);
58 assertThat("Template should be valid", process.isValid(), is(true));
59 final String expectedContent = readFileAsString(fileName);
60 assertThat("Content should be the same", process.getContent(), is(expectedContent));
64 public void testProcessMissingPoliciesEntry() throws IOException {
65 final ProcessedTemplate processedTemplate;
66 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-missing-policies.json")) {
67 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
69 assertProcessedTemplate(processedTemplate, false, List.of(MISSING_ENTRY.getMessage(POLICIES.getKey())));
73 public void testProcessMissingTopologyTemplate() throws IOException {
74 final ProcessedTemplate processedTemplate;
75 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-missing-topology-template.json")) {
76 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
78 assertProcessedTemplate(processedTemplate, false,
79 List.of(MISSING_ENTRY.getMessage(TOPOLOGY_TEMPLATE.getKey())));
83 public void testProcessMissingPolicy() throws IOException {
84 final ProcessedTemplate processedTemplate;
85 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-missing-policy.json")) {
86 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
88 assertProcessedTemplate(processedTemplate, false, List.of(MISSING_POLICY.getMessage()));
92 public void testProcessMissingToscaDefinitionsVersion() throws IOException {
93 final ProcessedTemplate processedTemplate;
94 try (final FileInputStream fileInputStream =
95 readFileAsStream("ToscaTemplate-missing-tosca-definitions-version.json")) {
96 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
98 assertProcessedTemplate(processedTemplate, false,
99 List.of(MISSING_ENTRY.getMessage(TOSCA_DEFINITIONS_VERSION.getKey())));
103 public void testProcessMissingProperties() throws IOException {
104 final ProcessedTemplate processedTemplate;
105 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-missing-properties.json")) {
106 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
108 assertProcessedTemplate(processedTemplate, false,
109 List.of(MISSING_ENTRY.getMessage(PROPERTIES.getKey())));
113 public void testProcessMoreThanOnePolicy() throws IOException {
114 final ProcessedTemplate processedTemplate;
115 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-more-than-one-policy.json")) {
116 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
118 assertProcessedTemplate(processedTemplate, false,
119 List.of(ONLY_ONE_POLICY_ALLOWED.getMessage()));
123 public void testProcessInvalidToscaTemplate() throws IOException {
124 final ProcessedTemplate processedTemplate;
125 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalid.json")) {
126 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
128 assertProcessedTemplate(processedTemplate, false,
129 List.of(INVALID_TOSCA_TEMPLATE.getMessage()));
133 public void testProcessInvalidEntryToscaDefinitionsVersion() throws IOException {
134 final ProcessedTemplate processedTemplate;
135 try (final FileInputStream fileInputStream =
136 readFileAsStream("ToscaTemplate-invalid-toscaDefinitions.json")) {
137 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
139 assertProcessedTemplate(processedTemplate, false,
140 List.of(INVALID_ENTRY.getMessage(TOSCA_DEFINITIONS_VERSION.getKey())));
144 public void testProcessInvalidEntryTopologyTemplate() throws IOException {
145 final ProcessedTemplate processedTemplate;
146 try (final FileInputStream fileInputStream =
147 readFileAsStream("ToscaTemplate-invalidEntry-topologyTemplate.json")) {
148 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
150 assertProcessedTemplate(processedTemplate, false,
151 List.of(INVALID_ENTRY.getMessage(TOPOLOGY_TEMPLATE.getKey())));
155 public void testProcessInvalidEntryPolicies() throws IOException {
156 final ProcessedTemplate processedTemplate;
157 try (final FileInputStream fileInputStream =
158 readFileAsStream("ToscaTemplate-invalidEntry-policies.json")) {
159 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
161 assertProcessedTemplate(processedTemplate, false,
162 List.of(INVALID_ENTRY.getMessage(POLICIES.getKey())));
166 public void testProcessInvalidPolicy() throws IOException {
167 ProcessedTemplate processedTemplate;
168 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalidPolicy1.json")) {
169 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
171 assertProcessedTemplate(processedTemplate, false,
172 List.of(INVALID_POLICY.getMessage()));
174 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalidPolicy2.json")) {
175 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
177 assertProcessedTemplate(processedTemplate, false,
178 List.of(INVALID_POLICY.getMessage()));
182 public void testProcessInvalidEntryProperties() throws IOException {
183 final ProcessedTemplate processedTemplate;
184 try (final FileInputStream fileInputStream =
185 readFileAsStream("ToscaTemplate-invalidEntry-properties.json")) {
186 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
188 assertProcessedTemplate(processedTemplate, false,
189 List.of(INVALID_ENTRY.getMessage(PROPERTIES.getKey())));
193 private void assertProcessedTemplate(final ProcessedTemplate process, boolean isValid,
194 final List<String> expectedErrorList) {
195 assertThat("Template should be valid", process.isValid(), is(isValid));
196 if (isValid || expectedErrorList == null) {
199 assertThat("Should contains the expected quantity of errors",
200 process.getErrorSet().size(), is(expectedErrorList.size()));
202 .forEach(errorMsg -> assertThat("Should contains a specific error message", process.getErrorSet(),
203 contains(errorMsg)));
206 private FileInputStream readFileAsStream(final String fileName) throws FileNotFoundException {
207 final Path path = Paths.get(testResourcesPath.toString(), fileName);
208 return new FileInputStream(path.toFile());
211 private String readFileAsString(final String fileName) throws IOException {
212 final Path path = Paths.get(testResourcesPath.toString(), fileName);
213 return Files.readString(path);