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.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;
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;
40 public class ApexConfigProcessorTest {
42 private final ApexConfigProcessor apexConfigProcessor = new ApexConfigProcessor(new StandardCoder());
43 private final Path testResourcesPath = Paths.get("src", "test", "resources", "processor");
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);
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));
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);
64 assertProcessedTemplate(processedTemplate, false,
65 List.of(MISSING_ENTRY.getMessage(ENGINE_SERVICE_PARAMETERS.getKey())));
69 public void testProcessInvalidToscaTemplate() throws IOException {
70 final ProcessedTemplate processedTemplate;
71 try (final FileInputStream fileInputStream = readFileAsStream("ApexConfig-invalid.jsonbad")) {
72 processedTemplate = apexConfigProcessor.process(fileInputStream);
74 assertProcessedTemplate(processedTemplate, false, List.of(INVALID_APEX_CONFIG.getMessage()));
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);
84 assertProcessedTemplate(processedTemplate, false,
85 List.of(INVALID_ENTRY.getMessage(ENGINE_SERVICE_PARAMETERS.getKey())));
88 private void assertProcessedTemplate(final ProcessedTemplate process, boolean isValid,
89 final List<String> expectedErrorList) {
91 assertThat("Template should be valid", process.isValid(), is(isValid));
92 if (isValid || expectedErrorList == null) {
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)));
101 private FileInputStream readFileAsStream(final String fileName) throws FileNotFoundException {
102 final Path path = Paths.get(testResourcesPath.toString(), fileName);
103 return new FileInputStream(path.toFile());
106 private String readFileAsString(final String fileName) throws IOException {
107 final Path path = Paths.get(testResourcesPath.toString(), fileName);
108 return Files.readString(path);