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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.controlloop.policy;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
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;
44 public class ControlLoopPolicyTest {
45 private static final Logger logger = LoggerFactory.getLogger(ControlLoopPolicyTest.class);
49 this.test("src/test/resources/v1.0.0/policy_Test.yaml");
53 public void testvService1() {
54 this.test("src/test/resources/v1.0.0/policy_vService.yaml");
58 public void testOpenLoop() {
59 this.test("src/test/resources/v1.0.0/policy_OpenLoop.yaml");
63 public void testvdns() {
64 this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vDNS.yaml");
68 public void testvFirewall() {
69 // Chenfei to fix this.
70 // this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml");
74 public void testvcpe() {
75 this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vCPE.yaml");
79 public void testvpci() {
80 this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vPCI.yaml");
84 public void testvolte() {
85 this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_VOLTE.yaml");
89 * Does the actual test.
91 * @param testFile input file
93 public void test(String testFile) {
94 try (InputStream is = new FileInputStream(new File(testFile))) {
96 // Read the yaml into our Java Object
98 Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
99 Object obj = yaml.load(is);
101 assertTrue(obj instanceof ControlLoopPolicy);
104 // Now dump it to a yaml string
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);
113 // Read that string back into our java object
115 Object newObject = yaml.load(dumpedYaml);
117 assertNotNull(newObject);
118 assertTrue(newObject instanceof ControlLoopPolicy);
120 // Have to comment it out tentatively since it causes junit to fail.
121 // Seems we cannot use assertEquals here. Need advice.
123 //assertEquals(newObject, obj);
125 // test serialization
126 ControlLoopPolicy policy = (ControlLoopPolicy) obj;
127 ControlLoopPolicy policy2 = Serializer.roundTrip(policy);
128 assertTrue(policy.equals(policy2));
130 } catch (FileNotFoundException e) {
131 fail(e.getLocalizedMessage());
132 } catch (IOException e) {
133 fail(e.getLocalizedMessage());
137 public void dump(Object obj) {
138 logger.debug("Dumping ", obj.getClass().getCanonicalName());
139 logger.debug("{}", obj);