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=========================================================
21 package org.onap.policy.guard;
23 import static org.junit.Assert.fail;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.charset.StandardCharsets;
32 import org.apache.commons.io.IOUtils;
33 import org.yaml.snakeyaml.Yaml;
34 import org.yaml.snakeyaml.constructor.Constructor;
36 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
37 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
40 public final class Util {
42 public static class Pair<A, B> {
46 public Pair(A a, B b) {
52 public static Pair<ControlLoopPolicy, String> loadYaml(String testFile) {
53 try (InputStream is = new FileInputStream(new File(testFile))) {
54 String contents = IOUtils.toString(is, StandardCharsets.UTF_8);
56 // Read the yaml into our Java Object
58 Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
59 Object obj = yaml.load(contents);
61 //String ttt = ((ControlLoopPolicy)obj).policies.getFirst().payload.get("asdas");
62 System.out.println(contents);
63 //for(Policy policy : ((ControlLoopPolicy)obj).policies){
65 return new Pair<ControlLoopPolicy, String>((ControlLoopPolicy) obj, contents);
66 } catch (FileNotFoundException e) {
67 fail(e.getLocalizedMessage());
68 } catch (IOException e) {
69 fail(e.getLocalizedMessage());
74 public static ControlLoopGuard loadYamlGuard(String testFile) {
75 try (InputStream is = new FileInputStream(new File(testFile))) {
76 String contents = IOUtils.toString(is, StandardCharsets.UTF_8);
78 // Read the yaml into our Java Object
80 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
81 Object obj = yaml.load(contents);
82 return (ControlLoopGuard) obj;
83 } catch (FileNotFoundException e) {
84 fail(e.getLocalizedMessage());
85 } catch (IOException e) {
86 fail(e.getLocalizedMessage());