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;
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 ControlLoopPolicyTest {
41 this.test("src/test/resources/v1.0.0/policy_Test.yaml");
45 public void testMultipleService() {
46 this.test("src/test/resources/v1.0.0/policy_Test_MultipleService.yaml");
50 public void testOpenLoop() {
51 this.test("src/test/resources/v1.0.0/policy_OpenLoop_1610.yaml");
55 public void testOpenECOMPvDNS() {
56 this.test("src/test/resources/v2.0.0/policy_OpenECOMP_demo_vDNS.yaml");
59 public void test(String testFile) {
60 try (InputStream is = new FileInputStream(new File(testFile))) {
62 // Read the yaml into our Java Object
64 Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
65 Object obj = yaml.load(is);
67 assertTrue(obj instanceof ControlLoopPolicy);
70 // Now dump it to a yaml string
72 DumperOptions options = new DumperOptions();
73 options.setDefaultFlowStyle(FlowStyle.BLOCK);
74 options.setPrettyFlow(true);
75 yaml = new Yaml(options);
76 String dumpedYaml = yaml.dump(obj);
77 System.out.println(dumpedYaml);
79 // Read that string back into our java object
81 Object newObject = yaml.load(dumpedYaml);
83 assertNotNull(newObject);
84 assertTrue(newObject instanceof ControlLoopPolicy);
86 // Have to comment it out tentatively since it causes junit to fail.
87 // Seems we cannot use assertEquals here. Need advice.
89 //assertEquals(newObject, obj);
90 } catch (FileNotFoundException e) {
91 fail(e.getLocalizedMessage());
92 } catch (IOException e) {
93 fail(e.getLocalizedMessage());
97 public void dump(Object obj) {
98 System.out.println("Dumping " + obj.getClass().getCanonicalName());
99 System.out.println(obj.toString());