0842b1b188483f485fdb25d7a2711be6477a5592
[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 = readFileAsStream(
61             "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.jsonbad")) {
72             processedTemplate = apexConfigProcessor.process(fileInputStream);
73         }
74         assertProcessedTemplate(processedTemplate, false, List.of(INVALID_APEX_CONFIG.getMessage()));
75     }
76
77     @Test
78     public void testProcessInvalidEngineServiceParameters() throws IOException {
79         final ProcessedTemplate processedTemplate;
80         try (final FileInputStream fileInputStream = readFileAsStream(
81             "ApexConfig-invalid-engineServiceParameters.json")) {
82             processedTemplate = apexConfigProcessor.process(fileInputStream);
83         }
84         assertProcessedTemplate(processedTemplate, false,
85             List.of(INVALID_ENTRY.getMessage(ENGINE_SERVICE_PARAMETERS.getKey())));
86     }
87
88     private void assertProcessedTemplate(final ProcessedTemplate process, boolean isValid,
89         final List<String> expectedErrorList) {
90
91         assertThat("Template should be valid", process.isValid(), is(isValid));
92         if (isValid || expectedErrorList == null) {
93             return;
94         }
95         assertThat("Should contains the expected quantity of errors", process.getErrorSet().size(),
96             is(expectedErrorList.size()));
97         expectedErrorList.forEach(errorMsg -> assertThat("Should contains a specific error message",
98             process.getErrorSet(), contains(errorMsg)));
99     }
100
101     private FileInputStream readFileAsStream(final String fileName) throws FileNotFoundException {
102         final Path path = Paths.get(testResourcesPath.toString(), fileName);
103         return new FileInputStream(path.toFile());
104     }
105
106     private String readFileAsString(final String fileName) throws IOException {
107         final Path path = Paths.get(testResourcesPath.toString(), fileName);
108         return Files.readString(path);
109     }
110
111 }