87ccdb5b9b0d7107c99d7b365a21795966e99b11
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * unit test
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
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.*;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.nio.charset.StandardCharsets;
30
31 import org.apache.commons.io.IOUtils;
32 import org.junit.Test;
33
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                 try {
58                         new ControlLoopProcessor(yamlString);
59                         fail("test should thrown an exception");
60                 }
61                 catch (Exception e) {
62                         assertEquals("Cannot create property=string for JavaBean=ControlLoopPolicy", e.getMessage().substring(0, 60));
63                 }
64         }
65
66         @Test
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);
70                 
71                 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
72                 assertNull(clProcessor.getCurrentPolicy());
73                 
74                 try {
75                         clProcessor.nextPolicyForResult(PolicyResult.SUCCESS);
76                         fail("test shold throw an exception here");
77                 }
78                 catch (ControlLoopException e) {
79                         assertEquals("There is no current policy to determine where to go to.", e.getMessage());
80                 }
81         }
82
83         @Test
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);
87                 
88                 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
89
90                 try {
91                         clProcessor.getCurrentPolicy();
92                         fail("test shold throw an exception here");
93                 }
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         public void testSuccess(String yaml) throws ControlLoopException {
130                 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
131                 logger.debug("testSuccess: {}", processor.getControlLoop());
132                 while (true) {
133                         FinalResult result = processor.checkIsCurrentPolicyFinal();
134                         if (result != null) {
135                                 logger.debug("{}", result);
136                                 break;
137                         }
138                         Policy policy = processor.getCurrentPolicy();
139                         assertNotNull(policy);
140                         logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
141                         processor.nextPolicyForResult(PolicyResult.SUCCESS);
142                 }
143         }
144
145         public void testFailure(String yaml) throws ControlLoopException {
146                 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
147                 logger.debug("testFailure: {}", processor.getControlLoop());
148                 while (true) {
149                         FinalResult result = processor.checkIsCurrentPolicyFinal();
150                         if (result != null) {
151                                 logger.debug("{}", result);
152                                 break;
153                         }
154                         Policy policy = processor.getCurrentPolicy();
155                         assertNotNull(policy);
156                         logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
157                         processor.nextPolicyForResult(PolicyResult.FAILURE);
158                 }               
159         }
160 }