[POLICY-22] Reorganizing drools-apps
[policy/drools-applications.git] / controlloop / templates / template.demo / src / test / java / org / onap / policy / controlloop / processor / Util.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * demo
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.fail;
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.yaml.snakeyaml.Yaml;
34 import org.yaml.snakeyaml.constructor.Constructor;
35
36 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
37 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
38
39 public final class Util {
40
41         public static class Pair<A, B> {
42                 public final A a;
43                 public final B b;
44                 
45                 public Pair(A a, B b) {
46                         this.a = a;
47                         this.b = b;
48                 }
49         }
50         
51         public static Pair<ControlLoopPolicy, String>   loadYaml(String testFile) {
52                 try (InputStream is = new FileInputStream(new File(testFile))) {
53                         String contents = IOUtils.toString(is, StandardCharsets.UTF_8);
54                         //
55                         // Read the yaml into our Java Object
56                         //
57                         Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
58                         Object obj = yaml.load(contents);
59                         
60                         //String ttt = ((ControlLoopPolicy)obj).policies.getFirst().payload.get("asdas");
61                         System.out.println(contents);
62                         //for(Policy policy : ((ControlLoopPolicy)obj).policies){
63                         
64                         return new Pair<ControlLoopPolicy, String>((ControlLoopPolicy) obj, contents);
65                 } catch (FileNotFoundException e) {
66                         fail(e.getLocalizedMessage());
67                 } catch (IOException e) {
68                         fail(e.getLocalizedMessage());
69                 }
70                 return null;
71         }
72         
73         public static ControlLoopGuard  loadYamlGuard(String testFile) {
74                 try (InputStream is = new FileInputStream(new File(testFile))) {
75                         String contents = IOUtils.toString(is, StandardCharsets.UTF_8);
76                         //
77                         // Read the yaml into our Java Object
78                         //
79                         Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
80                         Object obj = yaml.load(contents);
81                         return (ControlLoopGuard) obj;
82                 } catch (FileNotFoundException e) {
83                         fail(e.getLocalizedMessage());
84                 } catch (IOException e) {
85                         fail(e.getLocalizedMessage());
86                 }
87                 return null;
88         }
89
90 }