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