2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.*;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.nio.charset.StandardCharsets;
31 import org.apache.commons.io.IOUtils;
32 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);
58 new ControlLoopProcessor(yamlString);
59 fail("test should thrown an exception");
62 assertEquals("Cannot create property=string for JavaBean=ControlLoopPolicy", e.getMessage().substring(0, 60));
67 public void testControlLoopProcessorBadTriggerYaml() throws IOException, ControlLoopException {
68 InputStream is = new FileInputStream(new File("src/test/resources/badtriggerpolicy.yaml"));
69 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
71 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
72 assertNull(clProcessor.getCurrentPolicy());
75 clProcessor.nextPolicyForResult(PolicyResult.SUCCESS);
76 fail("test shold throw an exception here");
78 catch (ControlLoopException e) {
79 assertEquals("There is no current policy to determine where to go to.", e.getMessage());
84 public void testControlLoopProcessorNoPolicyYaml() throws IOException, ControlLoopException {
85 InputStream is = new FileInputStream(new File("src/test/resources/nopolicy.yaml"));
86 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
88 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
91 clProcessor.getCurrentPolicy();
92 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);
129 public void testSuccess(String yaml) throws ControlLoopException {
130 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
131 logger.debug("testSuccess: {}", processor.getControlLoop());
133 FinalResult result = processor.checkIsCurrentPolicyFinal();
134 if (result != null) {
135 logger.debug("{}", result);
138 Policy policy = processor.getCurrentPolicy();
139 assertNotNull(policy);
140 logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
141 processor.nextPolicyForResult(PolicyResult.SUCCESS);
145 public void testFailure(String yaml) throws ControlLoopException {
146 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
147 logger.debug("testFailure: {}", processor.getControlLoop());
149 FinalResult result = processor.checkIsCurrentPolicyFinal();
150 if (result != null) {
151 logger.debug("{}", result);
154 Policy policy = processor.getCurrentPolicy();
155 assertNotNull(policy);
156 logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
157 processor.nextPolicyForResult(PolicyResult.FAILURE);