[POLICY-22] Reorganizing drools-apps
[policy/drools-applications.git] / controlloop / common / policy-yaml / src / test / java / org / onap / policy / controlloop / policy / ControlLoopPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml 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.policy;
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
31 import org.junit.Test;
32 import org.yaml.snakeyaml.DumperOptions;
33 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
34 import org.yaml.snakeyaml.Yaml;
35 import org.yaml.snakeyaml.constructor.Constructor;
36
37
38 public class ControlLoopPolicyTest {
39         @Test 
40         public void testSDNO() {
41                 this.test("src/test/resources/v1.0.0/policy_SDNO_1702.yaml");
42         }
43
44         @Test 
45         public void test() {
46                 this.test("src/test/resources/v1.0.0/policy_Test.yaml");
47         }
48
49         @Test 
50         public void testMultipleService() {
51                 this.test("src/test/resources/v1.0.0/policy_Test_MultipleService.yaml");
52         }
53
54         @Test
55         public void testF5() {
56                 this.test("src/test/resources/v1.0.0/policy_vSCP_F5_1610.yaml");
57         }
58         
59         @Test 
60         public void testUSP() {
61                 this.test("src/test/resources/v1.0.0/policy_vUSP_1610.yaml");
62         }
63
64         @Test 
65         public void testOpenLoop() {
66                 this.test("src/test/resources/v1.0.0/policy_OpenLoop_1610.yaml");
67         }
68
69         @Test 
70         public void testvProbes() {
71                 this.test("src/test/resources/v1.0.0/policy_vProbes_1610.yaml");
72         }
73
74         @Test 
75         public void test1707() {
76                 this.test("src/test/resources/v2.0.0/policy_vUSP_1707.yaml");
77         }
78
79         @Test 
80         public void testeNodeBALU() {
81                 this.test("src/test/resources/v2.0.0/policy_eNodeB_ALU_1707.yaml");
82         }
83
84         @Test 
85         public void testeNodeBEricsson() {
86                 this.test("src/test/resources/v2.0.0/policy_eNodeB_Ericsson_1707.yaml");
87         }
88
89         public void test(String testFile) {
90                 try (InputStream is = new FileInputStream(new File(testFile))) {
91                         //
92                         // Read the yaml into our Java Object
93                         //
94                         Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
95                         Object obj = yaml.load(is);
96                         assertNotNull(obj);
97                         assertTrue(obj instanceof ControlLoopPolicy);
98                         dump(obj);
99                         //
100                         // Now dump it to a yaml string
101                         //
102                         DumperOptions options = new DumperOptions();
103                         options.setDefaultFlowStyle(FlowStyle.BLOCK);
104                         options.setPrettyFlow(true);
105                         yaml = new Yaml(options);
106                         String dumpedYaml = yaml.dump(obj);
107                         System.out.println(dumpedYaml);
108                         //
109                         // Read that string back into our java object
110                         //
111                         Object newObject = yaml.load(dumpedYaml);
112                         dump(newObject);
113                         assertNotNull(newObject);
114                         assertTrue(newObject instanceof ControlLoopPolicy);
115                         //
116                         // Have to comment it out tentatively since it causes junit to fail. 
117                         // Seems we cannot use assertEquals here. Need advice.
118                         //
119                         //assertEquals(newObject, obj);
120                 } catch (FileNotFoundException e) {
121                         fail(e.getLocalizedMessage());
122                 } catch (IOException e) {
123                         fail(e.getLocalizedMessage());
124                 }
125         }
126         
127         public void dump(Object obj) {
128                 System.out.println("Dumping " + obj.getClass().getCanonicalName());
129                 System.out.println(obj.toString());
130         }
131 }