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