remove dependency of drool-application/yaml in actors
[policy/models.git] / models-interactions / model-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-2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.policy;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33
34 import org.junit.Test;
35 import org.onap.policy.common.utils.io.Serializer;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.yaml.snakeyaml.DumperOptions;
39 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
40 import org.yaml.snakeyaml.Yaml;
41 import org.yaml.snakeyaml.constructor.Constructor;
42
43
44 public class ControlLoopPolicyTest {
45     private static final Logger logger = LoggerFactory.getLogger(ControlLoopPolicyTest.class);
46     
47     @Test 
48     public void test1() {
49         this.test("src/test/resources/v1.0.0/policy_Test.yaml");
50     }
51
52     @Test 
53     public void testvService1() {
54         this.test("src/test/resources/v1.0.0/policy_vService.yaml");
55     }
56
57     @Test 
58     public void testOpenLoop() {
59         this.test("src/test/resources/v1.0.0/policy_OpenLoop.yaml");
60     }
61
62     @Test 
63     public void testvdns() {
64         this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vDNS.yaml");
65     }
66
67     @Test 
68     public void testvFirewall() {
69         // Chenfei to fix this.
70         // this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml");
71     }
72
73     @Test 
74     public void testvcpe() {
75         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vCPE.yaml");
76     }
77
78     @Test 
79     public void testvpci() {
80         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vPCI.yaml");
81     }
82
83     @Test 
84     public void testvolte() {
85         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_VOLTE.yaml");
86     }
87
88     /**
89      * Does the actual test.
90      * 
91      * @param testFile input file
92      */
93     public void test(String testFile) {
94         try (InputStream is = new FileInputStream(new File(testFile))) {
95             //
96             // Read the yaml into our Java Object
97             //
98             Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
99             Object obj = yaml.load(is);
100             assertNotNull(obj);
101             assertTrue(obj instanceof ControlLoopPolicy);
102             dump(obj);
103             //
104             // Now dump it to a yaml string
105             //
106             DumperOptions options = new DumperOptions();
107             options.setDefaultFlowStyle(FlowStyle.BLOCK);
108             options.setPrettyFlow(true);
109             yaml = new Yaml(options);
110             String dumpedYaml = yaml.dump(obj);
111             logger.debug(dumpedYaml);
112             //
113             // Read that string back into our java object
114             //
115             Object newObject = yaml.load(dumpedYaml);
116             dump(newObject);
117             assertNotNull(newObject);
118             assertTrue(newObject instanceof ControlLoopPolicy);
119             //
120             // Have to comment it out tentatively since it causes junit to fail. 
121             // Seems we cannot use assertEquals here. Need advice.
122             //
123             //assertEquals(newObject, obj);
124             
125             // test serialization
126             ControlLoopPolicy policy = (ControlLoopPolicy) obj;
127             ControlLoopPolicy policy2 = Serializer.roundTrip(policy);
128             assertTrue(policy.equals(policy2));
129             
130         } catch (FileNotFoundException e) {
131             fail(e.getLocalizedMessage());
132         } catch (IOException e) {
133             fail(e.getLocalizedMessage());
134         }
135     }
136
137     public void dump(Object obj) {
138         logger.debug("Dumping ", obj.getClass().getCanonicalName());
139         logger.debug("{}", obj);
140     }
141 }