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 = readFileAsStream(
95 "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, List.of(MISSING_ENTRY.getMessage(PROPERTIES.getKey())));
112 public void testProcessMoreThanOnePolicy() throws IOException {
113 final ProcessedTemplate processedTemplate;
114 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-more-than-one-policy.json")) {
115 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
117 assertProcessedTemplate(processedTemplate, false, List.of(ONLY_ONE_POLICY_ALLOWED.getMessage()));
121 public void testProcessInvalidToscaTemplate() throws IOException {
122 final ProcessedTemplate processedTemplate;
123 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalid.jsonbad")) {
124 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
126 assertProcessedTemplate(processedTemplate, false, List.of(INVALID_TOSCA_TEMPLATE.getMessage()));
130 public void testProcessInvalidEntryToscaDefinitionsVersion() throws IOException {
131 final ProcessedTemplate processedTemplate;
132 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalid-toscaDefinitions.json")) {
133 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
135 assertProcessedTemplate(processedTemplate, false,
136 List.of(INVALID_ENTRY.getMessage(TOSCA_DEFINITIONS_VERSION.getKey())));
140 public void testProcessInvalidEntryTopologyTemplate() throws IOException {
141 final ProcessedTemplate processedTemplate;
142 try (final FileInputStream fileInputStream = readFileAsStream(
143 "ToscaTemplate-invalidEntry-topologyTemplate.json")) {
144 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
146 assertProcessedTemplate(processedTemplate, false,
147 List.of(INVALID_ENTRY.getMessage(TOPOLOGY_TEMPLATE.getKey())));
151 public void testProcessInvalidEntryPolicies() throws IOException {
152 final ProcessedTemplate processedTemplate;
153 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalidEntry-policies.json")) {
154 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
156 assertProcessedTemplate(processedTemplate, false, List.of(INVALID_ENTRY.getMessage(POLICIES.getKey())));
160 public void testProcessInvalidPolicy() throws IOException {
161 ProcessedTemplate processedTemplate;
162 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalidPolicy1.json")) {
163 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
165 assertProcessedTemplate(processedTemplate, false, List.of(INVALID_POLICY.getMessage()));
167 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalidPolicy2.json")) {
168 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
170 assertProcessedTemplate(processedTemplate, false, List.of(INVALID_POLICY.getMessage()));
174 public void testProcessInvalidEntryProperties() throws IOException {
175 final ProcessedTemplate processedTemplate;
176 try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalidEntry-properties.json")) {
177 processedTemplate = toscaTemplateProcessor.process(fileInputStream);
179 assertProcessedTemplate(processedTemplate, false, List.of(INVALID_ENTRY.getMessage(PROPERTIES.getKey())));
182 private void assertProcessedTemplate(final ProcessedTemplate process, boolean isValid,
183 final List<String> expectedErrorList) {
184 assertThat("Template should be valid", process.isValid(), is(isValid));
185 if (isValid || expectedErrorList == null) {
188 assertThat("Should contains the expected quantity of errors", process.getErrorSet().size(),
189 is(expectedErrorList.size()));
190 expectedErrorList.forEach(errorMsg -> assertThat("Should contains a specific error message",
191 process.getErrorSet(), contains(errorMsg)));
194 private FileInputStream readFileAsStream(final String fileName) throws FileNotFoundException {
195 final Path path = Paths.get(testResourcesPath.toString(), fileName);
196 return new FileInputStream(path.toFile());
199 private String readFileAsString(final String fileName) throws IOException {
200 final Path path = Paths.get(testResourcesPath.toString(), fileName);
201 return Files.readString(path);