0b61991f1a9c80a7be1cabb8380eebfa211d8a8e
[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-2019 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.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.InputStream;
31 import org.junit.Test;
32 import org.onap.policy.common.utils.io.Serializer;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.yaml.snakeyaml.DumperOptions;
36 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
37 import org.yaml.snakeyaml.Yaml;
38 import org.yaml.snakeyaml.constructor.Constructor;
39
40
41 public class ControlLoopPolicyTest {
42     private static final Logger logger = LoggerFactory.getLogger(ControlLoopPolicyTest.class);
43
44     @Test
45     public void test1() throws Exception {
46         this.test("src/test/resources/v1.0.0/policy_Test.yaml");
47     }
48
49     @Test
50     public void testvService1() throws Exception {
51         this.test("src/test/resources/v1.0.0/policy_vService.yaml");
52     }
53
54     @Test
55     public void testOpenLoop() throws Exception {
56         this.test("src/test/resources/v1.0.0/policy_OpenLoop.yaml");
57     }
58
59     @Test
60     public void testvdns() throws Exception {
61         this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vDNS.yaml");
62     }
63
64     @Test
65     public void testvFirewall() {
66         // Chenfei to fix this.
67         // this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml");
68     }
69
70     @Test
71     public void testvcpe() throws Exception {
72         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vCPE.yaml");
73     }
74
75     @Test
76     public void testvpci() throws Exception {
77         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vPCI.yaml");
78     }
79
80     @Test
81     public void testvolte() throws Exception {
82         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_VOLTE.yaml");
83     }
84
85     /**
86      * Does the actual test.
87      *
88      * @param testFile input file
89      * @throws Exception if an error occurs
90      */
91     public void test(String testFile) throws Exception {
92         try (InputStream is = new FileInputStream(new File(testFile))) {
93             //
94             // Read the yaml into our Java Object
95             //
96             Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
97             Object obj = yaml.load(is);
98             assertNotNull(obj);
99             assertTrue(obj instanceof ControlLoopPolicy);
100             dump(obj);
101             //
102             // Now dump it to a yaml string
103             //
104             DumperOptions options = new DumperOptions();
105             options.setDefaultFlowStyle(FlowStyle.BLOCK);
106             options.setPrettyFlow(true);
107             yaml = new Yaml(options);
108             String dumpedYaml = yaml.dump(obj);
109             logger.debug(dumpedYaml);
110             //
111             // Read that string back into our java object
112             //
113             Object newObject = yaml.load(dumpedYaml);
114             dump(newObject);
115             assertNotNull(newObject);
116             assertTrue(newObject instanceof ControlLoopPolicy);
117             assertEquals(obj, newObject);
118
119             // test serialization
120             ControlLoopPolicy policy = (ControlLoopPolicy) obj;
121             ControlLoopPolicy policy2 = Serializer.roundTrip(policy);
122             assertTrue(policy.equals(policy2));
123         }
124     }
125
126     public void dump(Object obj) {
127         logger.debug("Dumping ", obj.getClass().getName());
128         logger.debug("{}", obj);
129     }
130 }