7bd18a3ff79f5b3e4fa7d99ec85516d79860f42b
[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.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.charset.StandardCharsets;
31
32 import org.apache.commons.io.IOUtils;
33 import org.junit.Test;
34
35 import org.onap.policy.controlloop.ControlLoopException;
36 import org.onap.policy.controlloop.policy.FinalResult;
37 import org.onap.policy.controlloop.policy.Policy;
38 import org.onap.policy.controlloop.policy.PolicyResult;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class ControlLoopProcessorTest {
43         private static final Logger logger = LoggerFactory.getLogger(ControlLoopProcessorTest.class);
44         
45         @Test
46         public void test() {
47                 try (InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"))) {
48                         String result = IOUtils.toString(is, StandardCharsets.UTF_8);
49                         this.testSuccess(result);
50                         this.testFailure(result);
51                 } catch (FileNotFoundException e) {
52                         e.printStackTrace();
53                         fail(e.getMessage());
54                 } catch (IOException e) {
55                         e.printStackTrace();
56                         fail(e.getMessage());
57                 } catch (ControlLoopException e) {
58                         e.printStackTrace();
59                         fail(e.getMessage());
60                 }
61         }
62         
63         public void testSuccess(String yaml) throws ControlLoopException {
64                 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
65                 logger.debug("testSuccess: {}", processor.getControlLoop());
66                 while (true) {
67                         FinalResult result = processor.checkIsCurrentPolicyFinal();
68                         if (result != null) {
69                                 logger.debug("{}", result);
70                                 break;
71                         }
72                         Policy policy = processor.getCurrentPolicy();
73                         assertNotNull(policy);
74                         logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
75                         processor.nextPolicyForResult(PolicyResult.SUCCESS);
76                 }
77         }
78
79         public void testFailure(String yaml) throws ControlLoopException {
80                 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
81                 logger.debug("testFailure: {}", processor.getControlLoop());
82                 while (true) {
83                         FinalResult result = processor.checkIsCurrentPolicyFinal();
84                         if (result != null) {
85                                 logger.debug("{}", result);
86                                 break;
87                         }
88                         Policy policy = processor.getCurrentPolicy();
89                         assertNotNull(policy);
90                         logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
91                         processor.nextPolicyForResult(PolicyResult.FAILURE);
92                 }               
93         }
94
95 }