71a80dbf710e7a78d8a2c4c6d6e2e863269c6140
[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-2020 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
27 import java.io.FileInputStream;
28 import java.io.InputStreamReader;
29 import org.junit.Test;
30 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class ControlLoopPolicyTest {
35     private static final Logger logger = LoggerFactory.getLogger(ControlLoopPolicyTest.class);
36
37     @Test
38     public void test1() throws Exception {
39         this.test("src/test/resources/v1.0.0/policy_Test.yaml");
40     }
41
42     @Test
43     public void testvService1() throws Exception {
44         this.test("src/test/resources/v1.0.0/policy_vService.yaml");
45     }
46
47     @Test
48     public void testOpenLoop() throws Exception {
49         this.test("src/test/resources/v1.0.0/policy_OpenLoop.yaml");
50     }
51
52     @Test
53     public void testvdns() throws Exception {
54         this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vDNS.yaml");
55     }
56
57     @Test
58     public void testvFirewall() throws Exception {
59         this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml");
60     }
61
62     @Test
63     public void testvcpe() throws Exception {
64         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vCPE.yaml");
65     }
66
67     @Test
68     public void testvpci() throws Exception {
69         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vPCI.yaml");
70     }
71
72     @Test
73     public void testvolte() throws Exception {
74         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_VOLTE.yaml");
75     }
76
77     /**
78      * Does the actual test.
79      *
80      * @param testFile input file
81      * @throws Exception if an error occurs
82      */
83     public void test(String testFile) throws Exception {
84         try (InputStreamReader fileInputStream = new InputStreamReader(new FileInputStream(testFile))) {
85             //
86             // Read the yaml into our Java Object
87             //
88             ControlLoopPolicy controlLoopPolicy1 =
89                 new YamlJsonTranslator().fromYaml(fileInputStream, ControlLoopPolicy.class);
90             assertNotNull(controlLoopPolicy1);
91             dump(controlLoopPolicy1);
92
93             //
94             // Now dump it to a yaml string
95             //
96             String dumpedYaml = new YamlJsonTranslator().toYaml(controlLoopPolicy1);
97             logger.debug(dumpedYaml);
98             //
99             // Read that string back into our java object
100             //
101             ControlLoopPolicy controlLoopPolicy2 =
102                 new YamlJsonTranslator().fromYaml(dumpedYaml, ControlLoopPolicy.class);
103             assertNotNull(controlLoopPolicy2);
104             dump(controlLoopPolicy2);
105
106             // test serialization
107             assertEquals(controlLoopPolicy1, controlLoopPolicy2);
108         }
109     }
110
111     public void dump(Object obj) {
112         logger.debug("Dumping ", obj.getClass().getName());
113         logger.debug("{}", obj);
114     }
115 }