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.*;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
30 import org.junit.Test;
31 import org.yaml.snakeyaml.DumperOptions;
32 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
33 import org.yaml.snakeyaml.Yaml;
34 import org.yaml.snakeyaml.constructor.Constructor;
37 public class ControlLoopGuardTest {
40 public void testGuardvDNS() {
41 this.test("src/test/resources/v2.0.0-guard/policy_guard_OpenECOMP_demo_vDNS.yaml");
45 public void testGuardvUSP() {
46 this.test("src/test/resources/v2.0.0-guard/policy_guard_1707_appc.yaml");
49 public void test(String testFile) {
50 try (InputStream is = new FileInputStream(new File(testFile))) {
52 // Read the yaml into our Java Object
54 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
55 Object obj = yaml.load(is);
57 assertTrue(obj instanceof ControlLoopGuard);
60 // Now dump it to a yaml string
62 DumperOptions options = new DumperOptions();
63 options.setDefaultFlowStyle(FlowStyle.BLOCK);
64 options.setPrettyFlow(true);
65 yaml = new Yaml(options);
66 String dumpedYaml = yaml.dump(obj);
67 System.out.println(dumpedYaml);
69 // Read that string back into our java object
71 Object newObject = yaml.load(dumpedYaml);
73 assertNotNull(newObject);
74 assertTrue(newObject instanceof ControlLoopGuard);
76 // Have to comment it out tentatively since it causes junit to fail.
77 // Seems we cannot use assertEquals here. Need advice.
79 //assertEquals(newObject, obj);
80 } catch (FileNotFoundException e) {
81 fail(e.getLocalizedMessage());
82 } catch (IOException e) {
83 fail(e.getLocalizedMessage());
87 public void dump(Object obj) {
88 System.out.println("Dumping " + obj.getClass().getCanonicalName());
89 System.out.println(obj.toString());