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
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.guard;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
30 import java.io.FileInputStream;
31 import java.io.InputStream;
32 import java.util.LinkedList;
33 import org.junit.Test;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.yaml.snakeyaml.DumperOptions;
37 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
38 import org.yaml.snakeyaml.Yaml;
39 import org.yaml.snakeyaml.constructor.Constructor;
42 public class ControlLoopGuardTest {
43 private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardTest.class);
46 public void testGuardvdns() throws Exception {
47 this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
51 public void testGuardvusp() throws Exception {
52 this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
56 public void testConstructorControlLoopGuard() {
57 Guard guard1 = new Guard();
58 GuardPolicy guardPolicy1 = new GuardPolicy();
59 GuardPolicy guardPolicy2 = new GuardPolicy();
60 LinkedList<GuardPolicy> guardPolicies = new LinkedList<>();
61 guardPolicies.add(guardPolicy1);
62 guardPolicies.add(guardPolicy2);
64 ControlLoopGuard controlLoopGuard1 = new ControlLoopGuard();
65 controlLoopGuard1.setGuard(guard1);
66 controlLoopGuard1.setGuards(guardPolicies);
67 ControlLoopGuard controlLoopGuard2 = new ControlLoopGuard(controlLoopGuard1);
69 assertEquals(guard1, controlLoopGuard2.getGuard());
70 assertEquals(guardPolicies, controlLoopGuard2.getGuards());
74 public void testEqualsAndHashCode() {
75 final Guard guard1 = new Guard();
76 GuardPolicy guardPolicy1 = new GuardPolicy();
77 GuardPolicy guardPolicy2 = new GuardPolicy();
78 LinkedList<GuardPolicy> guardPolicies = new LinkedList<>();
79 guardPolicies.add(guardPolicy1);
80 guardPolicies.add(guardPolicy2);
82 ControlLoopGuard controlLoopGuard1 = new ControlLoopGuard();
83 ControlLoopGuard controlLoopGuard2 = new ControlLoopGuard();
85 assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
86 assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
88 controlLoopGuard1.setGuard(guard1);
89 assertFalse(controlLoopGuard1.equals(controlLoopGuard2));
90 controlLoopGuard2.setGuard(guard1);
91 assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
92 assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
94 controlLoopGuard1.setGuards(guardPolicies);
95 assertFalse(controlLoopGuard1.equals(controlLoopGuard2));
96 controlLoopGuard2.setGuards(guardPolicies);
97 assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
98 assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
102 public void testEqualsSameObject() {
103 ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
104 assertTrue(controlLoopGuard.equals(controlLoopGuard));
108 public void testEqualsNull() {
109 ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
110 assertFalse(controlLoopGuard.equals(null));
114 public void testEqualsInstanceOfDiffClass() {
115 ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
116 assertFalse(controlLoopGuard.equals(""));
120 * Does the actual test.
122 * @param testFile input file
123 * @throws Exception if an error occurs
125 public void test(String testFile) throws Exception {
126 try (InputStream is = new FileInputStream(new File(testFile))) {
128 // Read the yaml into our Java Object
130 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
131 Object obj = yaml.load(is);
133 assertTrue(obj instanceof ControlLoopGuard);
136 // Now dump it to a yaml string
138 DumperOptions options = new DumperOptions();
139 options.setDefaultFlowStyle(FlowStyle.BLOCK);
140 options.setPrettyFlow(true);
141 yaml = new Yaml(options);
142 String dumpedYaml = yaml.dump(obj);
143 logger.debug(dumpedYaml);
145 // Read that string back into our java object
147 Object newObject = yaml.load(dumpedYaml);
149 assertNotNull(newObject);
150 assertTrue(newObject instanceof ControlLoopGuard);
152 assertEquals(obj, newObject);
156 public void dump(Object obj) {
157 logger.debug("Dumping {}", obj.getClass().getName());
158 logger.debug("{}", obj);