2a6dc7bbd6d83054923ee2e06f8cb4bc9289f3bd
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * unit test
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
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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.nio.charset.StandardCharsets;
33
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;
42
43 public class ControlLoopProcessorTest {
44     private static final Logger logger = LoggerFactory.getLogger(ControlLoopProcessorTest.class);
45
46     @Test
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);
52     }
53
54     @Test
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);
58
59         try {
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));
65         }
66     }
67
68     @Test
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);
72
73         ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
74         assertNull(clProcessor.getCurrentPolicy());
75
76         try {
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());
81         }
82     }
83
84     @Test
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);
88
89         ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
90
91         try {
92             clProcessor.getCurrentPolicy();
93             fail("test shold throw an exception here");
94         } catch (ControlLoopException e) {
95             assertEquals("There are no policies defined.", e.getMessage());
96         }
97     }
98
99     @Test
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);
103
104         ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
105         clProcessor.getCurrentPolicy();
106         clProcessor.nextPolicyForResult(PolicyResult.SUCCESS);
107
108         clProcessor = new ControlLoopProcessor(yamlString);
109         clProcessor.getCurrentPolicy();
110         clProcessor.nextPolicyForResult(PolicyResult.FAILURE);
111
112         clProcessor = new ControlLoopProcessor(yamlString);
113         clProcessor.getCurrentPolicy();
114         clProcessor.nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
115
116         clProcessor = new ControlLoopProcessor(yamlString);
117         clProcessor.getCurrentPolicy();
118         clProcessor.nextPolicyForResult(PolicyResult.FAILURE_GUARD);
119
120         clProcessor = new ControlLoopProcessor(yamlString);
121         clProcessor.getCurrentPolicy();
122         clProcessor.nextPolicyForResult(PolicyResult.FAILURE_RETRIES);
123
124         clProcessor = new ControlLoopProcessor(yamlString);
125         clProcessor.getCurrentPolicy();
126         clProcessor.nextPolicyForResult(PolicyResult.FAILURE_TIMEOUT);
127     }
128
129     /**
130      * Test policies in the given yaml following the successfull path.
131      * 
132      * @param yaml yaml containing the policies to test
133      * @throws ControlLoopException if an error occurs
134      */
135     public void testSuccess(String yaml) throws ControlLoopException {
136         ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
137         logger.debug("testSuccess: {}", processor.getControlLoop());
138         while (true) {
139             FinalResult result = processor.checkIsCurrentPolicyFinal();
140             if (result != null) {
141                 logger.debug("{}", result);
142                 break;
143             }
144             Policy policy = processor.getCurrentPolicy();
145             assertNotNull(policy);
146             logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
147             processor.nextPolicyForResult(PolicyResult.SUCCESS);
148         }
149     }
150
151     /**
152      * Test policies in the given yaml following the failure path.
153      * 
154      * @param yaml yaml containing the policies to test
155      * @throws ControlLoopException if an error occurs
156      */
157     public void testFailure(String yaml) throws ControlLoopException {
158         ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
159         logger.debug("testFailure: {}", processor.getControlLoop());
160         while (true) {
161             FinalResult result = processor.checkIsCurrentPolicyFinal();
162             if (result != null) {
163                 logger.debug("{}", result);
164                 break;
165             }
166             Policy policy = processor.getCurrentPolicy();
167             assertNotNull(policy);
168             logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
169             processor.nextPolicyForResult(PolicyResult.FAILURE);
170         }
171     }
172 }