[POLICY-22] Reorganizing drools-apps
[policy/drools-applications.git] / controlloop / common / eventmanager / src / test / java / org / onap / policy / controlloop / processor / ControlLoopProcessorTest.java
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
40 public class ControlLoopProcessorTest {
41                 
42         @Test
43         public void test() {
44                 try (InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"))) {
45                         String result = IOUtils.toString(is, StandardCharsets.UTF_8);
46                         this.testSuccess(result);
47                         this.testFailure(result);
48                 } catch (FileNotFoundException e) {
49                         e.printStackTrace();
50                         fail(e.getMessage());
51                 } catch (IOException e) {
52                         e.printStackTrace();
53                         fail(e.getMessage());
54                 } catch (ControlLoopException e) {
55                         e.printStackTrace();
56                         fail(e.getMessage());
57                 }
58         }
59         
60         public void testSuccess(String yaml) throws ControlLoopException {
61                 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
62                 System.out.println("testSuccess: " + processor.getControlLoop().toString());
63                 while (true) {
64                         FinalResult result = processor.checkIsCurrentPolicyFinal();
65                         if (result != null) {
66                                 System.out.println(result);
67                                 break;
68                         }
69                         Policy policy = processor.getCurrentPolicy();
70                         assertNotNull(policy);
71                         System.out.println("current policy is: " + policy.id + " " + policy.name);
72                         processor.nextPolicyForResult(PolicyResult.SUCCESS);
73                 }
74         }
75
76         public void testFailure(String yaml) throws ControlLoopException {
77                 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
78                 System.out.println("testFailure: " + processor.getControlLoop().toString());
79                 while (true) {
80                         FinalResult result = processor.checkIsCurrentPolicyFinal();
81                         if (result != null) {
82                                 System.out.println(result);
83                                 break;
84                         }
85                         Policy policy = processor.getCurrentPolicy();
86                         assertNotNull(policy);
87                         System.out.println("current policy is: " + policy.id + " " + policy.name);
88                         processor.nextPolicyForResult(PolicyResult.FAILURE);
89                 }               
90         }
91
92 }