Sonar Fixes policy/models, removing model-yaml
[policy/models.git] / models-interactions / model-yaml / src / test / java / org / onap / policy / controlloop / policy / guard / ControlLoopGuardTest.java
1 /*-
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.policy.guard;
23
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
29 import java.io.File;
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;
40
41
42 public class ControlLoopGuardTest {
43     private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardTest.class);
44
45     @Test
46     public void testGuardvdns() throws Exception {
47         this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
48     }
49
50     @Test
51     public void testGuardvusp() throws Exception {
52         this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
53     }
54
55     @Test
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);
63
64         ControlLoopGuard controlLoopGuard1 = new ControlLoopGuard();
65         controlLoopGuard1.setGuard(guard1);
66         controlLoopGuard1.setGuards(guardPolicies);
67         ControlLoopGuard controlLoopGuard2 = new ControlLoopGuard(controlLoopGuard1);
68
69         assertEquals(guard1, controlLoopGuard2.getGuard());
70         assertEquals(guardPolicies, controlLoopGuard2.getGuards());
71     }
72
73     @Test
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);
81
82         ControlLoopGuard controlLoopGuard1 = new ControlLoopGuard();
83         ControlLoopGuard controlLoopGuard2 = new ControlLoopGuard();
84
85         assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
86         assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
87
88         controlLoopGuard1.setGuard(guard1);
89         assertFalse(controlLoopGuard1.equals(controlLoopGuard2));
90         controlLoopGuard2.setGuard(guard1);
91         assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
92         assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
93
94         controlLoopGuard1.setGuards(guardPolicies);
95         assertFalse(controlLoopGuard1.equals(controlLoopGuard2));
96         controlLoopGuard2.setGuards(guardPolicies);
97         assertTrue(controlLoopGuard1.equals(controlLoopGuard2));
98         assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode());
99     }
100
101     @Test
102     public void testEqualsSameObject() {
103         ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
104         assertTrue(controlLoopGuard.equals(controlLoopGuard));
105     }
106
107     @Test
108     public void testEqualsNull() {
109         ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
110         assertFalse(controlLoopGuard.equals(null));
111     }
112
113     @Test
114     public void testEqualsInstanceOfDiffClass() {
115         ControlLoopGuard controlLoopGuard = new ControlLoopGuard();
116         assertFalse(controlLoopGuard.equals(""));
117     }
118
119     /**
120      * Does the actual test.
121      *
122      * @param testFile input file
123      * @throws Exception if an error occurs
124      */
125     public void test(String testFile) throws Exception {
126         try (InputStream is = new FileInputStream(new File(testFile))) {
127             //
128             // Read the yaml into our Java Object
129             //
130             Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
131             Object obj = yaml.load(is);
132             assertNotNull(obj);
133             assertTrue(obj instanceof ControlLoopGuard);
134             dump(obj);
135             //
136             // Now dump it to a yaml string
137             //
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);
144             //
145             // Read that string back into our java object
146             //
147             Object newObject = yaml.load(dumpedYaml);
148             dump(newObject);
149             assertNotNull(newObject);
150             assertTrue(newObject instanceof ControlLoopGuard);
151
152             assertEquals(obj, newObject);
153         }
154     }
155
156     public void dump(Object obj) {
157         logger.debug("Dumping {}", obj.getClass().getName());
158         logger.debug("{}", obj);
159     }
160 }