2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.nio.charset.StandardCharsets;
34 import org.apache.commons.io.IOUtils;
35 import org.junit.Test;
36 import org.onap.policy.controlloop.ControlLoopException;
37 import org.onap.policy.controlloop.policy.FinalResult;
38 import org.onap.policy.controlloop.policy.Policy;
39 import org.onap.policy.controlloop.policy.PolicyResult;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class ControlLoopProcessorTest {
44 private static final Logger logger = LoggerFactory.getLogger(ControlLoopProcessorTest.class);
47 public void testControlLoopProcessor() throws IOException, ControlLoopException {
48 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
49 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
50 this.testSuccess(yamlString);
51 this.testFailure(yamlString);
55 public void testControlLoopProcessorBadYaml() throws IOException {
56 InputStream is = new FileInputStream(new File("src/test/resources/string.yaml"));
57 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
60 new ControlLoopProcessor(yamlString);
61 fail("test should thrown an exception");
62 } catch (Exception e) {
63 assertEquals("Cannot create property=string for JavaBean=ControlLoopPolicy",
64 e.getMessage().substring(0, 60));
69 public void testControlLoopProcessorBadTriggerYaml() throws IOException, ControlLoopException {
70 InputStream is = new FileInputStream(new File("src/test/resources/badtriggerpolicy.yaml"));
71 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
73 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
74 assertNull(clProcessor.getCurrentPolicy());
77 clProcessor.nextPolicyForResult(PolicyResult.SUCCESS);
78 fail("test shold throw an exception here");
79 } catch (ControlLoopException e) {
80 assertEquals("There is no current policy to determine where to go to.", e.getMessage());
85 public void testControlLoopProcessorNoPolicyYaml() throws IOException, ControlLoopException {
86 InputStream is = new FileInputStream(new File("src/test/resources/nopolicy.yaml"));
87 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
89 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
92 clProcessor.getCurrentPolicy();
93 fail("test shold throw an exception here");
94 } catch (ControlLoopException e) {
95 assertEquals("There are no policies defined.", e.getMessage());
100 public void testControlLoopProcessorNextPolicyForResult() throws IOException, ControlLoopException {
101 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
102 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
104 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
105 clProcessor.getCurrentPolicy();
106 clProcessor.nextPolicyForResult(PolicyResult.SUCCESS);
108 clProcessor = new ControlLoopProcessor(yamlString);
109 clProcessor.getCurrentPolicy();
110 clProcessor.nextPolicyForResult(PolicyResult.FAILURE);
112 clProcessor = new ControlLoopProcessor(yamlString);
113 clProcessor.getCurrentPolicy();
114 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
116 clProcessor = new ControlLoopProcessor(yamlString);
117 clProcessor.getCurrentPolicy();
118 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_GUARD);
120 clProcessor = new ControlLoopProcessor(yamlString);
121 clProcessor.getCurrentPolicy();
122 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_RETRIES);
124 clProcessor = new ControlLoopProcessor(yamlString);
125 clProcessor.getCurrentPolicy();
126 clProcessor.nextPolicyForResult(PolicyResult.FAILURE_TIMEOUT);
130 * Test policies in the given yaml following the successfull path.
132 * @param yaml yaml containing the policies to test
133 * @throws ControlLoopException if an error occurs
135 public void testSuccess(String yaml) throws ControlLoopException {
136 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
137 logger.debug("testSuccess: {}", processor.getControlLoop());
139 FinalResult result = processor.checkIsCurrentPolicyFinal();
140 if (result != null) {
141 logger.debug("{}", result);
144 Policy policy = processor.getCurrentPolicy();
145 assertNotNull(policy);
146 logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
147 processor.nextPolicyForResult(PolicyResult.SUCCESS);
152 * Test policies in the given yaml following the failure path.
154 * @param yaml yaml containing the policies to test
155 * @throws ControlLoopException if an error occurs
157 public void testFailure(String yaml) throws ControlLoopException {
158 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
159 logger.debug("testFailure: {}", processor.getControlLoop());
161 FinalResult result = processor.checkIsCurrentPolicyFinal();
162 if (result != null) {
163 logger.debug("{}", result);
166 Policy policy = processor.getCurrentPolicy();
167 assertNotNull(policy);
168 logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
169 processor.nextPolicyForResult(PolicyResult.FAILURE);