2 * ============LICENSE_START=======================================================
3 * policy-yaml unit test
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.policy.guard;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.LinkedList;
36 import org.junit.Test;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.yaml.snakeyaml.DumperOptions;
40 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
41 import org.yaml.snakeyaml.Yaml;
42 import org.yaml.snakeyaml.constructor.Constructor;
45 public class ControlLoopGuardTest {
46 private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardTest.class);
49 public void testGuardvdns() {
50 this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
54 public void testGuardvusp() {
55 this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
59 public void testConstructorControlLoopGuard() {
60 Guard guard1 = new Guard();
61 GuardPolicy guardPolicy1 = new GuardPolicy();
62 GuardPolicy guardPolicy2 = new GuardPolicy();
63 LinkedList<GuardPolicy> guardPolicies = new LinkedList<>();
64 guardPolicies.add(guardPolicy1);
65 guardPolicies.add(guardPolicy2);
67 ControlLoopGuard controlLoopGuard1 = new ControlLoopGuard();
68 controlLoopGuard1.setGuard(guard1);
69 controlLoopGuard1.setGuards(guardPolicies);
70 ControlLoopGuard controlLoopGuard2 = new ControlLoopGuard(controlLoopGuard1);
72 assertEquals(guard1, controlLoopGuard2.getGuard());
73 assertEquals(guardPolicies, controlLoopGuard2.getGuards());
77 public void testEqualsAndHashCode() {
78 final Guard guard1 = new Guard();
79 GuardPolicy guardPolicy1 = new GuardPolicy();
80 GuardPolicy guardPolicy2 = new GuardPolicy();
81 LinkedList<GuardPolicy> guardPolicies = new LinkedList<>();
82 guardPolicies.add(guardPolicy1);
83 guardPolicies.add(guardPolicy2);
85 ControlLoopGuard controlLoopGuard1 = new ControlLoopGuard();
86 ControlLoopGuard controlLoopGuard2 = new ControlLoopGuard();
88 assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
89 assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
91 controlLoopGuard1.setGuard(guard1);
92 assertFalse(controlLoopGuard1.equals(controlLoopGuard2));
93 controlLoopGuard2.setGuard(guard1);
94 assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
95 assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
97 controlLoopGuard1.setGuards(guardPolicies);
98 assertFalse(controlLoopGuard1.equals(controlLoopGuard2));
99 controlLoopGuard2.setGuards(guardPolicies);
100 assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
101 assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
105 public void testEqualsSameObject() {
106 ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
107 assertTrue(controlLoopGuard.equals(controlLoopGuard));
111 public void testEqualsNull() {
112 ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
113 assertFalse(controlLoopGuard.equals(null));
117 public void testEqualsInstanceOfDiffClass() {
118 ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
119 assertFalse(controlLoopGuard.equals(""));
123 * Does the actual test.
125 * @param testFile input file
127 public void test(String testFile) {
128 try (InputStream is = new FileInputStream(new File(testFile))) {
130 // Read the yaml into our Java Object
132 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
133 Object obj = yaml.load(is);
135 assertTrue(obj instanceof ControlLoopGuard);
138 // Now dump it to a yaml string
140 DumperOptions options = new DumperOptions();
141 options.setDefaultFlowStyle(FlowStyle.BLOCK);
142 options.setPrettyFlow(true);
143 yaml = new Yaml(options);
144 String dumpedYaml = yaml.dump(obj);
145 logger.debug(dumpedYaml);
147 // Read that string back into our java object
149 Object newObject = yaml.load(dumpedYaml);
151 assertNotNull(newObject);
152 assertTrue(newObject instanceof ControlLoopGuard);
154 // Have to comment it out tentatively since it causes junit to fail.
155 // Seems we cannot use assertEquals here. Need advice.
157 //assertEquals(newObject, obj);
158 } catch (FileNotFoundException e) {
159 fail(e.getLocalizedMessage());
160 } catch (IOException e) {
161 fail(e.getLocalizedMessage());
165 public void dump(Object obj) {
166 logger.debug("Dumping {}", obj.getClass().getCanonicalName());
167 logger.debug("{}", obj);