84fe4491441567a41574f3735cff6578d66aa256
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * unit test
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.processor;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26
27 import java.io.File;
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;
40
41 public class ControlLoopProcessorTest {
42     private static final Logger logger = LoggerFactory.getLogger(ControlLoopProcessorTest.class);
43
44     @Test
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);
50     }
51
52     @Test
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);
56
57         assertThatThrownBy(() -> new ControlLoopProcessor(yamlString))
58             .hasMessageStartingWith("Cannot create property=string for JavaBean=ControlLoopPolicy");
59     }
60
61     @Test
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);
65
66         ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
67         assertNull(clProcessor.getCurrentPolicy());
68
69         assertThatThrownBy(() -> clProcessor.nextPolicyForResult(PolicyResult.SUCCESS))
70             .hasMessageStartingWith("There is no current policy to determine where to go to.");
71     }
72
73     @Test
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);
77
78         ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
79         assertThatThrownBy(clProcessor::getCurrentPolicy)
80             .hasMessage("There are no policies defined.");
81     }
82
83     @Test
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);
87
88         ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
89         clProcessor.getCurrentPolicy();
90         clProcessor.nextPolicyForResult(PolicyResult.SUCCESS);
91
92         clProcessor = new ControlLoopProcessor(yamlString);
93         clProcessor.getCurrentPolicy();
94         clProcessor.nextPolicyForResult(PolicyResult.FAILURE);
95
96         clProcessor = new ControlLoopProcessor(yamlString);
97         clProcessor.getCurrentPolicy();
98         clProcessor.nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
99
100         clProcessor = new ControlLoopProcessor(yamlString);
101         clProcessor.getCurrentPolicy();
102         clProcessor.nextPolicyForResult(PolicyResult.FAILURE_GUARD);
103
104         clProcessor = new ControlLoopProcessor(yamlString);
105         clProcessor.getCurrentPolicy();
106         clProcessor.nextPolicyForResult(PolicyResult.FAILURE_RETRIES);
107
108         clProcessor = new ControlLoopProcessor(yamlString);
109         clProcessor.getCurrentPolicy();
110         clProcessor.nextPolicyForResult(PolicyResult.FAILURE_TIMEOUT);
111     }
112
113     /**
114      * Test policies in the given yaml following the successfull path.
115      *
116      * @param yaml yaml containing the policies to test
117      * @throws ControlLoopException if an error occurs
118      */
119     public void testSuccess(String yaml) throws ControlLoopException {
120         ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
121         logger.debug("testSuccess: {}", processor.getControlLoop());
122         while (true) {
123             FinalResult result = processor.checkIsCurrentPolicyFinal();
124             if (result != null) {
125                 logger.debug("{}", result);
126                 break;
127             }
128             Policy policy = processor.getCurrentPolicy();
129             assertNotNull(policy);
130             logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
131             processor.nextPolicyForResult(PolicyResult.SUCCESS);
132         }
133     }
134
135     /**
136      * Test policies in the given yaml following the failure path.
137      *
138      * @param yaml yaml containing the policies to test
139      * @throws ControlLoopException if an error occurs
140      */
141     public void testFailure(String yaml) throws ControlLoopException {
142         ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
143         logger.debug("testFailure: {}", processor.getControlLoop());
144         while (true) {
145             FinalResult result = processor.checkIsCurrentPolicyFinal();
146             if (result != null) {
147                 logger.debug("{}", result);
148                 break;
149             }
150             Policy policy = processor.getCurrentPolicy();
151             assertNotNull(policy);
152             logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
153             processor.nextPolicyForResult(PolicyResult.FAILURE);
154         }
155     }
156 }