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);
50 public void testControlLoopProcessor() throws IOException, ControlLoopException {
51 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
52 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
53 this.testSuccess(yamlString);
54 this.testFailure(yamlString);
58 public void testControlLoopFromToscaLegacy() throws IOException, CoderException, ControlLoopException {
60 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json")));
62 new ControlLoopProcessor(new StandardCoder().decode(policy, ToscaPolicy.class)).getCurrentPolicy());
66 public void testControlLoopFromToscaCompliant() throws IOException, CoderException, ControlLoopException {
68 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-compliant-vcpe.json")));
70 new ControlLoopProcessor(new StandardCoder().decode(policy, ToscaPolicy.class)).getCurrentPolicy());
74 public void testControlLoopFromToscaCompliantBad() throws IOException, CoderException, ControlLoopException {
76 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-compliant-vcpe.json")));
77 ToscaPolicy toscaPolicy = new StandardCoder().decode(policy, ToscaPolicy.class);
78 toscaPolicy.setType("onap.policies.controlloop.Operational");
79 assertThatThrownBy(() -> new ControlLoopProcessor(toscaPolicy)).hasCauseInstanceOf(CoderException.class);
83 public void testControlLoopProcessorBadYaml() throws IOException {
84 InputStream is = new FileInputStream(new File("src/test/resources/string.yaml"));
85 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
87 assertThatThrownBy(() -> new ControlLoopProcessor(yamlString))
88 .hasMessageStartingWith("Cannot create property=string for JavaBean=ControlLoopPolicy");
92 public void testControlLoopProcessorBadTriggerYaml() throws IOException, ControlLoopException {
93 InputStream is = new FileInputStream(new File("src/test/resources/badtriggerpolicy.yaml"));
94 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
96 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
97 assertNull(clProcessor.getCurrentPolicy());
99 assertThatThrownBy(() -> clProcessor.nextPolicyForResult(PolicyResult.SUCCESS))
100 .hasMessageStartingWith("There is no current policy to determine where to go to.");
104 public void testControlLoopProcessorNoPolicyYaml() throws IOException, ControlLoopException {
105 InputStream is = new FileInputStream(new File("src/test/resources/nopolicy.yaml"));
106 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
108 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
109 assertThatThrownBy(clProcessor::getCurrentPolicy)
110 .hasMessage("There are no policies defined.");
114 public void testControlLoopProcessorNextPolicyForResult() throws IOException, ControlLoopException {
115 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
116 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
118 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
119 clProcessor.getCurrentPolicy();
120 clProcessor.nextPolicyForResult(PolicyResult.SUCCESS);
122 clProcessor = new ControlLoopProcessor(yamlString);
123 clProcessor.getCurrentPolicy();
124 clProcessor.nextPolicyForResult(PolicyResult.FAILURE);
126 clProcessor = new ControlLoopProcessor(yamlString);
127 clProcessor.getCurrentPolicy();
128 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
130 clProcessor = new ControlLoopProcessor(yamlString);
131 clProcessor.getCurrentPolicy();
132 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_GUARD);
134 clProcessor = new ControlLoopProcessor(yamlString);
135 clProcessor.getCurrentPolicy();
136 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_RETRIES);
138 clProcessor = new ControlLoopProcessor(yamlString);
139 clProcessor.getCurrentPolicy();
140 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_TIMEOUT);
144 * Test policies in the given yaml following the successfull path.
146 * @param yaml yaml containing the policies to test
147 * @throws ControlLoopException if an error occurs
149 public void testSuccess(String yaml) throws ControlLoopException {
150 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
151 logger.debug("testSuccess: {}", processor.getControlLoop());
153 FinalResult result = processor.checkIsCurrentPolicyFinal();
154 if (result != null) {
155 logger.debug("{}", result);
158 Policy policy = processor.getCurrentPolicy();
159 assertNotNull(policy);
160 logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
161 processor.nextPolicyForResult(PolicyResult.SUCCESS);
166 * Test policies in the given yaml following the failure path.
168 * @param yaml yaml containing the policies to test
169 * @throws ControlLoopException if an error occurs
171 public void testFailure(String yaml) throws ControlLoopException {
172 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
173 logger.debug("testFailure: {}", processor.getControlLoop());
175 FinalResult result = processor.checkIsCurrentPolicyFinal();
176 if (result != null) {
177 logger.debug("{}", result);
180 Policy policy = processor.getCurrentPolicy();
181 assertNotNull(policy);
182 logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
183 processor.nextPolicyForResult(PolicyResult.FAILURE);