461b26e6e4c9cf85f4c1380ac3b9876d62838935
[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 =
95             readFileAsStream("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,
109             List.of(MISSING_ENTRY.getMessage(PROPERTIES.getKey())));
110     }
111
112     @Test
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);
117         }
118         assertProcessedTemplate(processedTemplate, false,
119             List.of(ONLY_ONE_POLICY_ALLOWED.getMessage()));
120     }
121
122     @Test
123     public void testProcessInvalidToscaTemplate() throws IOException {
124         final ProcessedTemplate processedTemplate;
125         try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalid.json")) {
126             processedTemplate = toscaTemplateProcessor.process(fileInputStream);
127         }
128         assertProcessedTemplate(processedTemplate, false,
129             List.of(INVALID_TOSCA_TEMPLATE.getMessage()));
130     }
131
132     @Test
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);
138         }
139         assertProcessedTemplate(processedTemplate, false,
140             List.of(INVALID_ENTRY.getMessage(TOSCA_DEFINITIONS_VERSION.getKey())));
141     }
142
143     @Test
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);
149         }
150         assertProcessedTemplate(processedTemplate, false,
151             List.of(INVALID_ENTRY.getMessage(TOPOLOGY_TEMPLATE.getKey())));
152     }
153
154     @Test
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);
160         }
161         assertProcessedTemplate(processedTemplate, false,
162             List.of(INVALID_ENTRY.getMessage(POLICIES.getKey())));
163     }
164
165     @Test
166     public void testProcessInvalidPolicy() throws IOException {
167         ProcessedTemplate processedTemplate;
168         try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalidPolicy1.json")) {
169             processedTemplate = toscaTemplateProcessor.process(fileInputStream);
170         }
171         assertProcessedTemplate(processedTemplate, false,
172             List.of(INVALID_POLICY.getMessage()));
173
174         try (final FileInputStream fileInputStream = readFileAsStream("ToscaTemplate-invalidPolicy2.json")) {
175             processedTemplate = toscaTemplateProcessor.process(fileInputStream);
176         }
177         assertProcessedTemplate(processedTemplate, false,
178             List.of(INVALID_POLICY.getMessage()));
179     }
180
181     @Test
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);
187         }
188         assertProcessedTemplate(processedTemplate, false,
189             List.of(INVALID_ENTRY.getMessage(PROPERTIES.getKey())));
190     }
191
192
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) {
197             return;
198         }
199         assertThat("Should contains the expected quantity of errors",
200             process.getErrorSet().size(), is(expectedErrorList.size()));
201         expectedErrorList
202             .forEach(errorMsg -> assertThat("Should contains a specific error message", process.getErrorSet(),
203                 contains(errorMsg)));
204     }
205
206     private FileInputStream readFileAsStream(final String fileName) throws FileNotFoundException {
207         final Path path = Paths.get(testResourcesPath.toString(), fileName);
208         return new FileInputStream(path.toFile());
209     }
210
211     private String readFileAsString(final String fileName) throws IOException {
212         final Path path = Paths.get(testResourcesPath.toString(), fileName);
213         return Files.readString(path);
214     }
215
216 }