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.*;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.LinkedList;
32 import org.junit.Test;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.yaml.snakeyaml.DumperOptions;
36 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
37 import org.yaml.snakeyaml.Yaml;
38 import org.yaml.snakeyaml.constructor.Constructor;
41 public class ControlLoopGuardTest {
42 private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardTest.class);
45 public void testGuardvDNS() {
46 this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
50 public void testGuardvUSP() {
51 this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
55 public void testConstructorControlLoopGuard(){
56 Guard guard1 = new Guard();
57 GuardPolicy guardPolicy1 = new GuardPolicy();
58 GuardPolicy guardPolicy2 = new GuardPolicy();
59 LinkedList<GuardPolicy> guardPolicies = new LinkedList<>();
60 guardPolicies.add(guardPolicy1);
61 guardPolicies.add(guardPolicy2);
63 ControlLoopGuard controlLoopGuard1 = new ControlLoopGuard();
64 controlLoopGuard1.setGuard(guard1);
65 controlLoopGuard1.setGuards(guardPolicies);
66 ControlLoopGuard controlLoopGuard2 = new ControlLoopGuard(controlLoopGuard1);
68 assertEquals(guard1, controlLoopGuard2.getGuard());
69 assertEquals(guardPolicies, controlLoopGuard2.getGuards());
73 public void testEqualsAndHashCode() {
74 Guard guard1 = new Guard();
75 GuardPolicy guardPolicy1 = new GuardPolicy();
76 GuardPolicy guardPolicy2 = new GuardPolicy();
77 LinkedList<GuardPolicy> guardPolicies = new LinkedList<>();
78 guardPolicies.add(guardPolicy1);
79 guardPolicies.add(guardPolicy2);
81 ControlLoopGuard controlLoopGuard1 = new ControlLoopGuard();
82 ControlLoopGuard controlLoopGuard2 = new ControlLoopGuard();
84 assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
85 assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
87 controlLoopGuard1.setGuard(guard1);
88 assertFalse(controlLoopGuard1.equals(controlLoopGuard2));
89 controlLoopGuard2.setGuard(guard1);
90 assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
91 assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
93 controlLoopGuard1.setGuards(guardPolicies);
94 assertFalse(controlLoopGuard1.equals(controlLoopGuard2));
95 controlLoopGuard2.setGuards(guardPolicies);
96 assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
97 assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
101 public void testEqualsSameObject() {
102 ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
103 assertTrue(controlLoopGuard.equals(controlLoopGuard));
107 public void testEqualsNull() {
108 ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
109 assertFalse(controlLoopGuard.equals(null));
113 public void testEqualsInstanceOfDiffClass() {
114 ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
115 assertFalse(controlLoopGuard.equals(""));
118 public void test(String testFile) {
119 try (InputStream is = new FileInputStream(new File(testFile))) {
121 // Read the yaml into our Java Object
123 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
124 Object obj = yaml.load(is);
126 assertTrue(obj instanceof ControlLoopGuard);
129 // Now dump it to a yaml string
131 DumperOptions options = new DumperOptions();
132 options.setDefaultFlowStyle(FlowStyle.BLOCK);
133 options.setPrettyFlow(true);
134 yaml = new Yaml(options);
135 String dumpedYaml = yaml.dump(obj);
136 logger.debug(dumpedYaml);
138 // Read that string back into our java object
140 Object newObject = yaml.load(dumpedYaml);
142 assertNotNull(newObject);
143 assertTrue(newObject instanceof ControlLoopGuard);
145 // Have to comment it out tentatively since it causes junit to fail.
146 // Seems we cannot use assertEquals here. Need advice.
148 //assertEquals(newObject, obj);
149 } catch (FileNotFoundException e) {
150 fail(e.getLocalizedMessage());
151 } catch (IOException e) {
152 fail(e.getLocalizedMessage());
156 public void dump(Object obj) {
157 logger.debug("Dumping {}", obj.getClass().getCanonicalName());
158 logger.debug("{}", obj);