2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
20 package org.openecomp.policy.controlloop.policy.guard;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.io.InputStream;
32 import org.junit.Test;
33 import org.yaml.snakeyaml.DumperOptions;
34 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
35 import org.yaml.snakeyaml.Yaml;
36 import org.yaml.snakeyaml.constructor.Constructor;
39 public class ControlLoopGuardTest {
42 public void testGuardvDNS() {
43 this.test("src/test/resources/v2.0.0-guard/policy_guard_OpenECOMP_demo_vDNS.yaml");
47 public void testGuardvUSP() {
48 this.test("src/test/resources/v2.0.0-guard/policy_guard_1707_appc.yaml");
51 public void test(String testFile) {
52 try (InputStream is = new FileInputStream(new File(testFile))) {
54 // Read the yaml into our Java Object
56 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
57 Object obj = yaml.load(is);
59 assertTrue(obj instanceof ControlLoopGuard);
62 // Now dump it to a yaml string
64 DumperOptions options = new DumperOptions();
65 options.setDefaultFlowStyle(FlowStyle.BLOCK);
66 options.setPrettyFlow(true);
67 yaml = new Yaml(options);
68 String dumpedYaml = yaml.dump(obj);
69 System.out.println(dumpedYaml);
71 // Read that string back into our java object
73 Object newObject = yaml.load(dumpedYaml);
75 assertNotNull(newObject);
76 assertTrue(newObject instanceof ControlLoopGuard);
78 // Have to comment it out tentatively since it causes junit to fail.
79 // Seems we cannot use assertEquals here. Need advice.
81 //assertEquals(newObject, obj);
82 } catch (FileNotFoundException e) {
83 fail(e.getLocalizedMessage());
84 } catch (IOException e) {
85 fail(e.getLocalizedMessage());
89 public void dump(Object obj) {
90 System.out.println("Dumping " + obj.getClass().getCanonicalName());
91 System.out.println(obj.toString());