7166621d54fde8dc34b50b7093941b4a843de346
[policy/gui.git] /
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.gui.editors.apex.rest.handling.converter.tosca;
21
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;
35
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;
45
46 public class ToscaTemplateProcessorTest {
47
48     private final ToscaTemplateProcessor toscaTemplateProcessor = new ToscaTemplateProcessor(new StandardCoder());
49     private final Path testResourcesPath = Paths.get("src", "test", "resources", "processor");
50
51     @Test
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);
57         }
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));
61     }
62
63     @Test
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);
68         }
69         assertProcessedTemplate(processedTemplate, false, List.of(MISSING_ENTRY.getMessage(POLICIES.getKey())));
70     }
71
72     @Test
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);
77         }
78         assertProcessedTemplate(processedTemplate, false,
79             List.of(MISSING_ENTRY.getMessage(TOPOLOGY_TEMPLATE.getKey())));
80     }
81
82     @Test
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);
87         }
88         assertProcessedTemplate(processedTemplate, false, List.of(MISSING_POLICY.getMessage()));
89     }
90
91     @Test
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);
97         }
98         assertProcessedTemplate(processedTemplate, false,
99             List.of(MISSING_ENTRY.getMessage(TOSCA_DEFINITIONS_VERSION.getKey())));
100     }
101
102     @Test
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);
107         }
108         assertProcessedTemplate(processedTemplate, false, List.of(MISSING_ENTRY.getMessage(PROPERTIES.getKey())));
109     }
110
111     @Test
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);
116         }
117         assertProcessedTemplate(processedTemplate, false, List.of(ONLY_ONE_POLICY_ALLOWED.getMessage()));
118     }
119
120     @Test
121     public void testProcessInvalidToscaTemplate() throws IOException {
122         final ProcessedTemplate processedTemplate;
123         try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalid.jsonbad")) {
124             processedTemplate = toscaTemplateProcessor.process(fileInputStream);
125         }
126         assertProcessedTemplate(processedTemplate, false, List.of(INVALID_TOSCA_TEMPLATE.getMessage()));
127     }
128
129     @Test
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);
134         }
135         assertProcessedTemplate(processedTemplate, false,
136             List.of(INVALID_ENTRY.getMessage(TOSCA_DEFINITIONS_VERSION.getKey())));
137     }
138
139     @Test
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);
145         }
146         assertProcessedTemplate(processedTemplate, false,
147             List.of(INVALID_ENTRY.getMessage(TOPOLOGY_TEMPLATE.getKey())));
148     }
149
150     @Test
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);
155         }
156         assertProcessedTemplate(processedTemplate, false, List.of(INVALID_ENTRY.getMessage(POLICIES.getKey())));
157     }
158
159     @Test
160     public void testProcessInvalidPolicy() throws IOException {
161         ProcessedTemplate processedTemplate;
162         try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalidPolicy1.json")) {
163             processedTemplate = toscaTemplateProcessor.process(fileInputStream);
164         }
165         assertProcessedTemplate(processedTemplate, false, List.of(INVALID_POLICY.getMessage()));
166
167         try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalidPolicy2.json")) {
168             processedTemplate = toscaTemplateProcessor.process(fileInputStream);
169         }
170         assertProcessedTemplate(processedTemplate, false, List.of(INVALID_POLICY.getMessage()));
171     }
172
173     @Test
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);
178         }
179         assertProcessedTemplate(processedTemplate, false, List.of(INVALID_ENTRY.getMessage(PROPERTIES.getKey())));
180     }
181
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) {
186             return;
187         }
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)));
192     }
193
194     private FileInputStream readFileAsStream(final String fileName) throws FileNotFoundException {
195         final Path path = Paths.get(testResourcesPath.toString(), fileName);
196         return new FileInputStream(path.toFile());
197     }
198
199     private String readFileAsString(final String fileName) throws IOException {
200         final Path path = Paths.get(testResourcesPath.toString(), fileName);
201         return Files.readString(path);
202     }
203
204 }