ecf896e951d967192bfe310f6704c7f4fbc047f2
[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.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.doThrow;
29 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter.ToscaKey.ENGINE_SERVICE_PARAMETERS;
30 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter.ToscaKey.POLICIES;
31 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter.ToscaKey.POLICY_TYPE_IMPL;
32 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter.ToscaKey.PROPERTIES;
33 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter.ToscaKey.TOPOLOGY_TEMPLATE;
34 import static org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter.ToscaKey.TOSCA_DEFINITIONS_VERSION;
35
36 import com.google.gson.JsonObject;
37 import java.io.IOException;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Optional;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.mockito.InjectMocks;
47 import org.mockito.MockitoAnnotations;
48 import org.mockito.Spy;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.onap.policy.common.utils.coder.StandardYamlCoder;
52 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
53 import org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.exception.PolicyToscaConverterException;
54
55 public class PolicyToscaConverterTest {
56
57     @Spy
58     private final StandardCoder standardCoder = new StandardCoder();
59     @Spy
60     private final YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
61     @InjectMocks
62     private PolicyToscaConverter policyToscaConverter;
63
64     @Before
65     public void setUp() {
66         MockitoAnnotations.initMocks(this);
67     }
68
69     @Test
70     public void testConvertSuccess() throws IOException, PolicyToscaConverterException, CoderException {
71         final String apexConfig = readResourceFileToString(Paths.get("converter", "ApexConfig.json"));
72         final String apexPolicy = readResourceFileToString(Paths.get("converter", "APEXgRPCPolicy.json"));
73         final String toscaTemplate = readResourceFileToString(Paths.get("converter", "ToscaTemplate.json"));
74         final Optional<String> convert = policyToscaConverter.convert(apexPolicy, apexConfig, toscaTemplate);
75         assertTrue(convert.isPresent());
76         final String convertedYaml = convert.get();
77         final StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
78         @SuppressWarnings("unchecked") final Map<String, Object> yamlAsMap =
79             standardYamlCoder.decode(convertedYaml, Map.class);
80         assertThat(yamlAsMap).containsKeys(TOSCA_DEFINITIONS_VERSION.getKey(), TOPOLOGY_TEMPLATE.getKey());
81         @SuppressWarnings("unchecked") final Map<String, Object> topology_template =
82             (Map<String, Object>) yamlAsMap.get(TOPOLOGY_TEMPLATE.getKey());
83         assertThat(topology_template).containsKey(POLICIES.getKey());
84         @SuppressWarnings("unchecked") final List<Object> policies =
85             (List<Object>) topology_template.get(POLICIES.getKey());
86         assertEquals(1, policies.size());
87         @SuppressWarnings("unchecked") final Map<String, Object> firstPolicyMap = (Map<String, Object>) policies.get(0);
88         assertEquals(1, firstPolicyMap.keySet().size());
89         @SuppressWarnings("unchecked") final Map<String, Object> firstPolicy =
90             (Map<String, Object>) firstPolicyMap.get(firstPolicyMap.keySet().iterator().next());
91         assertThat(firstPolicy).containsKey(PROPERTIES.getKey());
92         @SuppressWarnings("unchecked") final Map<String, Object> propertiesMap =
93             (Map<String, Object>) firstPolicy.get(PROPERTIES.getKey());
94         assertThat(propertiesMap).containsKey(ENGINE_SERVICE_PARAMETERS.getKey());
95         @SuppressWarnings("unchecked") final Map<String, Object> engineServiceParametersProperty =
96             (Map<String, Object>) propertiesMap.get(ENGINE_SERVICE_PARAMETERS.getKey());
97         assertThat(engineServiceParametersProperty).containsKey(POLICY_TYPE_IMPL.getKey());
98     }
99
100     @Test
101     public void testConvertInvalidJsonDecode() throws CoderException {
102         final String invalidJson = "this is an invalid JSON";
103         doThrow(CoderException.class).when(standardCoder).decode(eq(invalidJson), eq(JsonObject.class));
104
105         final String expectedMsg = String.format("Could not convert JSON string to JSON:\n%s", invalidJson);
106         assertThatThrownBy(() -> policyToscaConverter.convert(invalidJson, invalidJson, invalidJson))
107             .isInstanceOf(PolicyToscaConverterException.class)
108             .hasMessage(expectedMsg);
109     }
110
111     @Test
112     public void testConvertInvalidJsonEncodeToString() throws IOException {
113         final String apexConfig = readResourceFileToString(Paths.get("converter", "ApexConfig.json"));
114         final String apexPolicy = readResourceFileToString(Paths.get("converter", "APEXgRPCPolicy.json"));
115         final String toscaTemplate = readResourceFileToString(Paths.get("converter", "ToscaTemplate.json"));
116
117         doThrow(RuntimeException.class).when(yamlJsonTranslator).toYaml(any(JsonObject.class));
118
119         assertThatThrownBy(() -> policyToscaConverter.convert(apexPolicy, apexConfig, toscaTemplate))
120             .isInstanceOf(PolicyToscaConverterException.class)
121             .hasMessageContaining("Could not convert JSON Object to YAML:");
122     }
123
124     @Test
125     public void testCouldNotReadFirstPolicy() throws IOException {
126         final String apexConfig = readResourceFileToString(Paths.get("converter", "ApexConfig.json"));
127         final String apexPolicy = readResourceFileToString(Paths.get("converter", "APEXgRPCPolicy.json"));
128         final String toscaTemplate1 =
129             readResourceFileToString(Paths.get("processor", "ToscaTemplate-missing-policies.json"));
130         final String expectedError =
131             String.format("Could not read the first policy in the '%s' entry under '%s'",
132                 POLICIES.getKey(), TOPOLOGY_TEMPLATE.getKey());
133
134         assertThatThrownBy(() -> policyToscaConverter.convert(apexPolicy, apexConfig, toscaTemplate1))
135             .isInstanceOf(PolicyToscaConverterException.class)
136             .hasMessage(expectedError);
137         final String toscaTemplate2 =
138             readResourceFileToString(Paths.get("processor", "ToscaTemplate-missing-policy.json"));
139
140         assertThatThrownBy(() -> policyToscaConverter.convert(apexPolicy, apexConfig, toscaTemplate2))
141             .isInstanceOf(PolicyToscaConverterException.class)
142             .hasMessage(expectedError);
143     }
144
145     @Test
146     public void testCouldNotReadPolicyProperties() throws IOException {
147         final String apexConfig = readResourceFileToString(Paths.get("converter", "ApexConfig.json"));
148         final String apexPolicy = readResourceFileToString(Paths.get("converter", "APEXgRPCPolicy.json"));
149         final String toscaTemplate =
150             readResourceFileToString(Paths.get("processor", "ToscaTemplate-missing-properties.json"));
151         assertThatThrownBy(() -> policyToscaConverter.convert(apexPolicy, apexConfig, toscaTemplate))
152             .isInstanceOf(PolicyToscaConverterException.class)
153             .hasMessage(String.format("Could not read the policy '%s' entry", PROPERTIES.getKey()));
154     }
155
156     @Test
157     public void testCouldNotReadEngineServiceParameters() throws IOException {
158         final String apexConfig =
159             readResourceFileToString(Paths.get("converter", "ApexConfig-engineServiceParameters-notAnObject.json"));
160         final String apexPolicy = readResourceFileToString(Paths.get("converter", "APEXgRPCPolicy.json"));
161         final String toscaTemplate =
162             readResourceFileToString(Paths.get("converter", "ToscaTemplate.json"));
163         assertThatThrownBy(() -> policyToscaConverter.convert(apexPolicy, apexConfig, toscaTemplate))
164             .isInstanceOf(PolicyToscaConverterException.class)
165             .hasMessage(
166                 String.format("Could not read the '%s' in the Apex Config", ENGINE_SERVICE_PARAMETERS.getKey()));
167     }
168
169     @Test
170     public void testCouldNotReadTopologyTemplate() throws IOException {
171         final String apexConfig = readResourceFileToString(Paths.get("converter", "ApexConfig.json"));
172         final String apexPolicy = readResourceFileToString(Paths.get("converter", "APEXgRPCPolicy.json"));
173         final String toscaTemplate =
174             readResourceFileToString(Paths.get("processor", "ToscaTemplate-missing-topology-template.json"));
175         assertThatThrownBy(() -> policyToscaConverter.convert(apexPolicy, apexConfig, toscaTemplate))
176             .isInstanceOf(PolicyToscaConverterException.class)
177             .hasMessage(
178                 String.format("Could not read the '%s' entry in the Tosca Template", TOPOLOGY_TEMPLATE.getKey()));
179     }
180
181     private String readResourceFileToString(final Path filePathFromResources) throws IOException {
182         final Path resourceDirectory = Paths.get("src", "test", "resources");
183         final Path converter = Paths.get(resourceDirectory.toString(), filePathFromResources.toString());
184         return Files.readString(converter);
185     }
186
187 }