a12f7e19bbf34b2cf97b4191525e1ef9cf79073e
[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.ApexConfigProcessor.ErrorMessage.INVALID_APEX_CONFIG;
26 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ApexConfigProcessor.ErrorMessage.INVALID_ENTRY;
27 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ApexConfigProcessor.ErrorMessage.MISSING_ENTRY;
28 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter.ToscaKey.ENGINE_SERVICE_PARAMETERS;
29
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.List;
37 import org.junit.Test;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39
40 public class ApexConfigProcessorTest {
41
42     private final ApexConfigProcessor apexConfigProcessor = new ApexConfigProcessor(new StandardCoder());
43     private final Path testResourcesPath = Paths.get("src", "test", "resources", "processor");
44
45     @Test
46     public void testProcessSuccess() throws IOException {
47         final String fileName = "ApexConfig.json";
48         final ProcessedTemplate process;
49         try (final FileInputStream fileInputStream = readFileAsStream(fileName)) {
50             process = apexConfigProcessor.process(fileInputStream);
51         }
52         assertThat("Template should be valid", process.isValid(), is(true));
53         final String expectedContent = readFileAsString(fileName);
54         assertThat("Content should be the same", process.getContent(), is(expectedContent));
55     }
56
57     @Test
58     public void testProcessMissingPoliciesEntry() throws IOException {
59         final ProcessedTemplate processedTemplate;
60         try (final FileInputStream fileInputStream =
61             readFileAsStream("ApexConfig-missing-engineServiceParameters.json")) {
62             processedTemplate = apexConfigProcessor.process(fileInputStream);
63         }
64         assertProcessedTemplate(processedTemplate, false,
65             List.of(MISSING_ENTRY.getMessage(ENGINE_SERVICE_PARAMETERS.getKey())));
66     }
67
68     @Test
69     public void testProcessInvalidToscaTemplate() throws IOException {
70         final ProcessedTemplate processedTemplate;
71         try (final FileInputStream fileInputStream = readFileAsStream("ApexConfig-invalid.json")) {
72             processedTemplate = apexConfigProcessor.process(fileInputStream);
73         }
74         assertProcessedTemplate(processedTemplate, false,
75             List.of(INVALID_APEX_CONFIG.getMessage()));
76     }
77
78     @Test
79     public void testProcessInvalidEngineServiceParameters() throws IOException {
80         final ProcessedTemplate processedTemplate;
81         try (final FileInputStream fileInputStream =
82             readFileAsStream("ApexConfig-invalid-engineServiceParameters.json")) {
83             processedTemplate = apexConfigProcessor.process(fileInputStream);
84         }
85         assertProcessedTemplate(processedTemplate, false,
86             List.of(INVALID_ENTRY.getMessage(ENGINE_SERVICE_PARAMETERS.getKey())));
87     }
88
89     private void assertProcessedTemplate(final ProcessedTemplate process, boolean isValid,
90                                          final List<String> expectedErrorList) {
91
92         assertThat("Template should be valid", process.isValid(), is(isValid));
93         if (isValid || expectedErrorList == null) {
94             return;
95         }
96         assertThat("Should contains the expected quantity of errors",
97             process.getErrorSet().size(), is(expectedErrorList.size()));
98         expectedErrorList
99             .forEach(errorMsg -> assertThat("Should contains a specific error message", process.getErrorSet(),
100                 contains(errorMsg)));
101     }
102
103     private FileInputStream readFileAsStream(final String fileName) throws FileNotFoundException {
104         final Path path = Paths.get(testResourcesPath.toString(), fileName);
105         return new FileInputStream(path.toFile());
106     }
107
108     private String readFileAsString(final String fileName) throws IOException {
109         final Path path = Paths.get(testResourcesPath.toString(), fileName);
110         return Files.readString(path);
111     }
112
113 }