2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.processor;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.nio.charset.StandardCharsets;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import org.apache.commons.io.IOUtils;
35 import org.junit.Test;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.controlloop.ControlLoopException;
39 import org.onap.policy.controlloop.policy.FinalResult;
40 import org.onap.policy.controlloop.policy.Policy;
41 import org.onap.policy.controlloop.policy.PolicyResult;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class ControlLoopProcessorTest {
47 private static final Logger logger = LoggerFactory.getLogger(ControlLoopProcessorTest.class);
48 private static final StandardCoder coder = new StandardCoder();
51 public void testControlLoopProcessor() throws IOException, ControlLoopException {
52 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
53 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
54 this.testSuccess(yamlString);
55 this.testFailure(yamlString);
59 public void testControlLoopFromToscaLegacy() throws IOException, CoderException, ControlLoopException {
61 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json")));
63 new ControlLoopProcessor(coder.decode(policy, ToscaPolicy.class)).getCurrentPolicy());
67 public void testControlLoopFromToscaCompliant() throws IOException, CoderException, ControlLoopException {
69 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-compliant-vcpe.json")));
71 new ControlLoopProcessor(coder.decode(policy, ToscaPolicy.class)).getCurrentPolicy());
74 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-compliant-vfw.json")));
76 new ControlLoopProcessor(coder.decode(policy, ToscaPolicy.class)).getCurrentPolicy());
80 public void testControlLoopFromToscaCompliantBad() throws IOException, CoderException, ControlLoopException {
82 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-compliant-vcpe.json")));
83 ToscaPolicy toscaPolicy = coder.decode(policy, ToscaPolicy.class);
84 toscaPolicy.setType("onap.policies.controlloop.Operational");
85 assertThatThrownBy(() -> new ControlLoopProcessor(toscaPolicy)).hasCauseInstanceOf(CoderException.class);
89 public void testControlLoopProcessorBadYaml() throws IOException {
90 InputStream is = new FileInputStream(new File("src/test/resources/string.yaml"));
91 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
93 assertThatThrownBy(() -> new ControlLoopProcessor(yamlString))
94 .hasMessageStartingWith("Cannot create property=string for JavaBean=ControlLoopPolicy");
98 public void testControlLoopProcessorBadTriggerYaml() throws IOException, ControlLoopException {
99 InputStream is = new FileInputStream(new File("src/test/resources/badtriggerpolicy.yaml"));
100 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
102 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
103 assertNull(clProcessor.getCurrentPolicy());
105 assertThatThrownBy(() -> clProcessor.nextPolicyForResult(PolicyResult.SUCCESS))
106 .hasMessageStartingWith("There is no current policy to determine where to go to.");
110 public void testControlLoopProcessorNoPolicyYaml() throws IOException, ControlLoopException {
111 InputStream is = new FileInputStream(new File("src/test/resources/nopolicy.yaml"));
112 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
114 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
115 assertThatThrownBy(clProcessor::getCurrentPolicy)
116 .hasMessage("There are no policies defined.");
120 public void testControlLoopProcessorNextPolicyForResult() throws IOException, ControlLoopException {
121 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
122 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
124 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
125 clProcessor.getCurrentPolicy();
126 clProcessor.nextPolicyForResult(PolicyResult.SUCCESS);
128 clProcessor = new ControlLoopProcessor(yamlString);
129 clProcessor.getCurrentPolicy();
130 clProcessor.nextPolicyForResult(PolicyResult.FAILURE);
132 clProcessor = new ControlLoopProcessor(yamlString);
133 clProcessor.getCurrentPolicy();
134 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
136 clProcessor = new ControlLoopProcessor(yamlString);
137 clProcessor.getCurrentPolicy();
138 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_GUARD);
140 clProcessor = new ControlLoopProcessor(yamlString);
141 clProcessor.getCurrentPolicy();
142 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_RETRIES);
144 clProcessor = new ControlLoopProcessor(yamlString);
145 clProcessor.getCurrentPolicy();
146 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_TIMEOUT);
150 * Test policies in the given yaml following the successfull path.
152 * @param yaml yaml containing the policies to test
153 * @throws ControlLoopException if an error occurs
155 public void testSuccess(String yaml) throws ControlLoopException {
156 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
157 logger.debug("testSuccess: {}", processor.getControlLoop());
159 FinalResult result = processor.checkIsCurrentPolicyFinal();
160 if (result != null) {
161 logger.debug("{}", result);
164 Policy policy = processor.getCurrentPolicy();
165 assertNotNull(policy);
166 logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
167 processor.nextPolicyForResult(PolicyResult.SUCCESS);
172 * Test policies in the given yaml following the failure path.
174 * @param yaml yaml containing the policies to test
175 * @throws ControlLoopException if an error occurs
177 public void testFailure(String yaml) throws ControlLoopException {
178 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
179 logger.debug("testFailure: {}", processor.getControlLoop());
181 FinalResult result = processor.checkIsCurrentPolicyFinal();
182 if (result != null) {
183 logger.debug("{}", result);
186 Policy policy = processor.getCurrentPolicy();
187 assertNotNull(policy);
188 logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
189 processor.nextPolicyForResult(PolicyResult.FAILURE);