2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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 org.apache.commons.io.IOUtils;
33 import org.junit.Test;
34 import org.onap.policy.controlloop.ControlLoopException;
35 import org.onap.policy.controlloop.policy.FinalResult;
36 import org.onap.policy.controlloop.policy.Policy;
37 import org.onap.policy.controlloop.policy.PolicyResult;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class ControlLoopProcessorTest {
42 private static final Logger logger = LoggerFactory.getLogger(ControlLoopProcessorTest.class);
45 public void testControlLoopProcessor() throws IOException, ControlLoopException {
46 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
47 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
48 this.testSuccess(yamlString);
49 this.testFailure(yamlString);
53 public void testControlLoopProcessorBadYaml() throws IOException {
54 InputStream is = new FileInputStream(new File("src/test/resources/string.yaml"));
55 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
57 assertThatThrownBy(() -> new ControlLoopProcessor(yamlString))
58 .hasMessageStartingWith("Cannot create property=string for JavaBean=ControlLoopPolicy");
62 public void testControlLoopProcessorBadTriggerYaml() throws IOException, ControlLoopException {
63 InputStream is = new FileInputStream(new File("src/test/resources/badtriggerpolicy.yaml"));
64 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
66 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
67 assertNull(clProcessor.getCurrentPolicy());
69 assertThatThrownBy(() -> clProcessor.nextPolicyForResult(PolicyResult.SUCCESS))
70 .hasMessageStartingWith("There is no current policy to determine where to go to.");
74 public void testControlLoopProcessorNoPolicyYaml() throws IOException, ControlLoopException {
75 InputStream is = new FileInputStream(new File("src/test/resources/nopolicy.yaml"));
76 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
78 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
79 assertThatThrownBy(clProcessor::getCurrentPolicy)
80 .hasMessage("There are no policies defined.");
84 public void testControlLoopProcessorNextPolicyForResult() throws IOException, ControlLoopException {
85 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
86 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
88 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
89 clProcessor.getCurrentPolicy();
90 clProcessor.nextPolicyForResult(PolicyResult.SUCCESS);
92 clProcessor = new ControlLoopProcessor(yamlString);
93 clProcessor.getCurrentPolicy();
94 clProcessor.nextPolicyForResult(PolicyResult.FAILURE);
96 clProcessor = new ControlLoopProcessor(yamlString);
97 clProcessor.getCurrentPolicy();
98 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
100 clProcessor = new ControlLoopProcessor(yamlString);
101 clProcessor.getCurrentPolicy();
102 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_GUARD);
104 clProcessor = new ControlLoopProcessor(yamlString);
105 clProcessor.getCurrentPolicy();
106 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_RETRIES);
108 clProcessor = new ControlLoopProcessor(yamlString);
109 clProcessor.getCurrentPolicy();
110 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_TIMEOUT);
114 * Test policies in the given yaml following the successfull path.
116 * @param yaml yaml containing the policies to test
117 * @throws ControlLoopException if an error occurs
119 public void testSuccess(String yaml) throws ControlLoopException {
120 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
121 logger.debug("testSuccess: {}", processor.getControlLoop());
123 FinalResult result = processor.checkIsCurrentPolicyFinal();
124 if (result != null) {
125 logger.debug("{}", result);
128 Policy policy = processor.getCurrentPolicy();
129 assertNotNull(policy);
130 logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
131 processor.nextPolicyForResult(PolicyResult.SUCCESS);
136 * Test policies in the given yaml following the failure path.
138 * @param yaml yaml containing the policies to test
139 * @throws ControlLoopException if an error occurs
141 public void testFailure(String yaml) throws ControlLoopException {
142 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
143 logger.debug("testFailure: {}", processor.getControlLoop());
145 FinalResult result = processor.checkIsCurrentPolicyFinal();
146 if (result != null) {
147 logger.debug("{}", result);
150 Policy policy = processor.getCurrentPolicy();
151 assertNotNull(policy);
152 logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
153 processor.nextPolicyForResult(PolicyResult.FAILURE);