a30914781eeae261c7b7302eeb2d225bad2ac0a8
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2017-2020 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 java.nio.file.Files;
33 import java.nio.file.Paths;
34 import org.apache.commons.io.IOUtils;
35 import org.junit.Test;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.controlloop.ControlLoopException;
39 import org.onap.policy.controlloop.policy.FinalResult;
40 import org.onap.policy.controlloop.policy.Policy;
41 import org.onap.policy.controlloop.policy.PolicyResult;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class ControlLoopProcessorTest {
47     private static final Logger logger = LoggerFactory.getLogger(ControlLoopProcessorTest.class);
48     private static final StandardCoder coder = new StandardCoder();
49
50     @Test
51     public void testControlLoopProcessor() throws IOException, ControlLoopException {
52         InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
53         String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
54         this.testSuccess(yamlString);
55         this.testFailure(yamlString);
56     }
57
58     @Test
59     public void testControlLoopFromToscaLegacy() throws IOException, CoderException, ControlLoopException {
60         String policy =
61                 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json")));
62         assertNotNull(
63                 new ControlLoopProcessor(coder.decode(policy, ToscaPolicy.class)).getCurrentPolicy());
64     }
65
66     @Test
67     public void testControlLoopFromToscaCompliant() throws IOException, CoderException, ControlLoopException {
68         String policy =
69                   new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-compliant-vcpe.json")));
70         assertNotNull(
71                   new ControlLoopProcessor(coder.decode(policy, ToscaPolicy.class)).getCurrentPolicy());
72
73         policy =
74                 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-compliant-vfw.json")));
75         assertNotNull(
76                 new ControlLoopProcessor(coder.decode(policy, ToscaPolicy.class)).getCurrentPolicy());
77     }
78
79     @Test
80     public void testControlLoopFromToscaCompliantBad() throws IOException, CoderException, ControlLoopException {
81         String policy =
82                 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-compliant-vcpe.json")));
83         ToscaPolicy toscaPolicy = coder.decode(policy, ToscaPolicy.class);
84         toscaPolicy.setType("onap.policies.controlloop.Operational");
85         assertThatThrownBy(() -> new ControlLoopProcessor(toscaPolicy)).hasCauseInstanceOf(CoderException.class);
86     }
87
88     @Test
89     public void testControlLoopProcessorBadYaml() throws IOException {
90         InputStream is = new FileInputStream(new File("src/test/resources/string.yaml"));
91         String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
92
93         assertThatThrownBy(() -> new ControlLoopProcessor(yamlString))
94             .hasMessageStartingWith("Cannot create property=string for JavaBean=ControlLoopPolicy");
95     }
96
97     @Test
98     public void testControlLoopProcessorBadTriggerYaml() throws IOException, ControlLoopException {
99         InputStream is = new FileInputStream(new File("src/test/resources/badtriggerpolicy.yaml"));
100         String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
101
102         ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
103         assertNull(clProcessor.getCurrentPolicy());
104
105         assertThatThrownBy(() -> clProcessor.nextPolicyForResult(PolicyResult.SUCCESS))
106             .hasMessageStartingWith("There is no current policy to determine where to go to.");
107     }
108
109     @Test
110     public void testControlLoopProcessorNoPolicyYaml() throws IOException, ControlLoopException {
111         InputStream is = new FileInputStream(new File("src/test/resources/nopolicy.yaml"));
112         String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
113
114         ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
115         assertThatThrownBy(clProcessor::getCurrentPolicy)
116             .hasMessage("There are no policies defined.");
117     }
118
119     @Test
120     public void testControlLoopProcessorNextPolicyForResult() throws IOException, ControlLoopException {
121         InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
122         String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
123
124         ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
125         clProcessor.getCurrentPolicy();
126         clProcessor.nextPolicyForResult(PolicyResult.SUCCESS);
127
128         clProcessor = new ControlLoopProcessor(yamlString);
129         clProcessor.getCurrentPolicy();
130         clProcessor.nextPolicyForResult(PolicyResult.FAILURE);
131
132         clProcessor = new ControlLoopProcessor(yamlString);
133         clProcessor.getCurrentPolicy();
134         clProcessor.nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
135
136         clProcessor = new ControlLoopProcessor(yamlString);
137         clProcessor.getCurrentPolicy();
138         clProcessor.nextPolicyForResult(PolicyResult.FAILURE_GUARD);
139
140         clProcessor = new ControlLoopProcessor(yamlString);
141         clProcessor.getCurrentPolicy();
142         clProcessor.nextPolicyForResult(PolicyResult.FAILURE_RETRIES);
143
144         clProcessor = new ControlLoopProcessor(yamlString);
145         clProcessor.getCurrentPolicy();
146         clProcessor.nextPolicyForResult(PolicyResult.FAILURE_TIMEOUT);
147     }
148
149     /**
150      * Test policies in the given yaml following the successfull path.
151      *
152      * @param yaml yaml containing the policies to test
153      * @throws ControlLoopException if an error occurs
154      */
155     public void testSuccess(String yaml) throws ControlLoopException {
156         ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
157         logger.debug("testSuccess: {}", processor.getControlLoop());
158         while (true) {
159             FinalResult result = processor.checkIsCurrentPolicyFinal();
160             if (result != null) {
161                 logger.debug("{}", result);
162                 break;
163             }
164             Policy policy = processor.getCurrentPolicy();
165             assertNotNull(policy);
166             logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
167             processor.nextPolicyForResult(PolicyResult.SUCCESS);
168         }
169     }
170
171     /**
172      * Test policies in the given yaml following the failure path.
173      *
174      * @param yaml yaml containing the policies to test
175      * @throws ControlLoopException if an error occurs
176      */
177     public void testFailure(String yaml) throws ControlLoopException {
178         ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
179         logger.debug("testFailure: {}", processor.getControlLoop());
180         while (true) {
181             FinalResult result = processor.checkIsCurrentPolicyFinal();
182             if (result != null) {
183                 logger.debug("{}", result);
184                 break;
185             }
186             Policy policy = processor.getCurrentPolicy();
187             assertNotNull(policy);
188             logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
189             processor.nextPolicyForResult(PolicyResult.FAILURE);
190         }
191     }
192 }