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.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;
28 import static org.junit.Assert.fail;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.LinkedList;
37 import org.junit.Test;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.yaml.snakeyaml.DumperOptions;
41 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
42 import org.yaml.snakeyaml.Yaml;
43 import org.yaml.snakeyaml.constructor.Constructor;
46 public class ControlLoopGuardTest {
47 private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardTest.class);
50 public void testGuardvdns() {
51 this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
55 public void testGuardvusp() {
56 this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
60 public void testConstructorControlLoopGuard() {
61 Guard guard1 = new Guard();
62 GuardPolicy guardPolicy1 = new GuardPolicy();
63 GuardPolicy guardPolicy2 = new GuardPolicy();
64 LinkedList<GuardPolicy> guardPolicies = new LinkedList<>();
65 guardPolicies.add(guardPolicy1);
66 guardPolicies.add(guardPolicy2);
68 ControlLoopGuard controlLoopGuard1 = new ControlLoopGuard();
69 controlLoopGuard1.setGuard(guard1);
70 controlLoopGuard1.setGuards(guardPolicies);
71 ControlLoopGuard controlLoopGuard2 = new ControlLoopGuard(controlLoopGuard1);
73 assertEquals(guard1, controlLoopGuard2.getGuard());
74 assertEquals(guardPolicies, controlLoopGuard2.getGuards());
78 public void testEqualsAndHashCode() {
79 final Guard guard1 = new Guard();
80 GuardPolicy guardPolicy1 = new GuardPolicy();
81 GuardPolicy guardPolicy2 = new GuardPolicy();
82 LinkedList<GuardPolicy> guardPolicies = new LinkedList<>();
83 guardPolicies.add(guardPolicy1);
84 guardPolicies.add(guardPolicy2);
86 ControlLoopGuard controlLoopGuard1 = new ControlLoopGuard();
87 ControlLoopGuard controlLoopGuard2 = new ControlLoopGuard();
89 assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
90 assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
92 controlLoopGuard1.setGuard(guard1);
93 assertFalse(controlLoopGuard1.equals(controlLoopGuard2));
94 controlLoopGuard2.setGuard(guard1);
95 assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
96 assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
98 controlLoopGuard1.setGuards(guardPolicies);
99 assertFalse(controlLoopGuard1.equals(controlLoopGuard2));
100 controlLoopGuard2.setGuards(guardPolicies);
101 assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
102 assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
106 public void testEqualsSameObject() {
107 ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
108 assertTrue(controlLoopGuard.equals(controlLoopGuard));
112 public void testEqualsNull() {
113 ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
114 assertFalse(controlLoopGuard.equals(null));
118 public void testEqualsInstanceOfDiffClass() {
119 ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
120 assertFalse(controlLoopGuard.equals(""));
124 * Does the actual test.
126 * @param testFile input file
128 public void test(String testFile) {
129 try (InputStream is = new FileInputStream(new File(testFile))) {
131 // Read the yaml into our Java Object
133 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
134 Object obj = yaml.load(is);
136 assertTrue(obj instanceof ControlLoopGuard);
139 // Now dump it to a yaml string
141 DumperOptions options = new DumperOptions();
142 options.setDefaultFlowStyle(FlowStyle.BLOCK);
143 options.setPrettyFlow(true);
144 yaml = new Yaml(options);
145 String dumpedYaml = yaml.dump(obj);
146 logger.debug(dumpedYaml);
148 // Read that string back into our java object
150 Object newObject = yaml.load(dumpedYaml);
152 assertNotNull(newObject);
153 assertTrue(newObject instanceof ControlLoopGuard);
155 // Have to comment it out tentatively since it causes junit to fail.
156 // Seems we cannot use assertEquals here. Need advice.
158 //assertEquals(newObject, obj);
159 } catch (FileNotFoundException e) {
160 fail(e.getLocalizedMessage());
161 } catch (IOException e) {
162 fail(e.getLocalizedMessage());
166 public void dump(Object obj) {
167 logger.debug("Dumping {}", obj.getClass().getCanonicalName());
168 logger.debug("{}", obj);