2 * ============LICENSE_START=======================================================
3 * policy-yaml unit test
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.controlloop.policy.guard;
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.yaml.snakeyaml.Yaml;
26 import org.yaml.snakeyaml.constructor.Constructor;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
33 import java.io.FileInputStream;
34 import java.io.FileNotFoundException;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.util.HashMap;
38 import java.util.LinkedList;
39 import java.util.List;
42 import org.onap.policy.controlloop.policy.builder.BuilderException;
43 import org.onap.policy.controlloop.policy.builder.Message;
44 import org.onap.policy.controlloop.policy.builder.MessageLevel;
45 import org.onap.policy.controlloop.policy.builder.Results;
46 import org.onap.policy.controlloop.poligy.guard.builder.ControlLoopGuardBuilder;
48 public class ControlLoopGuardBuilderTest {
52 public void testControlLoopGuard() {
57 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
59 // Assert there is no guard policies yet
61 Results results = builder.buildSpecification();
62 boolean no_guard_policies = false;
63 for (Message m : results.getMessages()) {
64 if (m.getMessage().equals("ControlLoop Guard should have at least one guard policies") && m.getLevel() == MessageLevel.ERROR) {
65 no_guard_policies = true;
69 assertTrue(no_guard_policies);
71 // Add a guard policy without limit constraint
73 GuardPolicy policy1 = new GuardPolicy("1111", "guardpolicy1", "guardpolicy1", "APPC", "restart");
74 builder = builder.addGuardPolicy(policy1);
76 // Assert there is no limit constraint associated with the only guard policy
78 results = builder.buildSpecification();
79 boolean no_constraint = false;
80 for (Message m : results.getMessages()) {
81 if (m.getMessage().equals("Guard policy guardpolicy1 does not have any limit constraint") && m.getLevel() == MessageLevel.ERROR) {
86 assertTrue(no_constraint);
88 // Add a constraint to policy1
90 Map<String, String> time_in_range = new HashMap<String, String>();
91 time_in_range.put("arg2", "PT5H");
92 time_in_range.put("arg3", "PT24H");
93 List<String> blacklist = new LinkedList<String>();
94 blacklist.add("eNodeB_common_id1");
95 blacklist.add("eNodeB_common_id2");
96 Map<String, String> duration = new HashMap<String, String>();
97 duration.put("value", "10");
98 duration.put("units", "minute");
99 Constraint cons = new Constraint(5, duration, time_in_range, blacklist);
100 builder = builder.addLimitConstraint(policy1.id, cons);
102 // Add a duplicate constraint to policy1
104 builder = builder.addLimitConstraint(policy1.id, cons);
106 // Assert there are duplicate constraints associated with the only guard policy
108 results = builder.buildSpecification();
109 boolean duplicate_constraint = false;
110 for (Message m : results.getMessages()) {
111 if (m.getMessage().equals("Guard policy guardpolicy1 has duplicate limit constraints") && m.getLevel() == MessageLevel.WARNING) {
112 duplicate_constraint = true;
116 assertTrue(duplicate_constraint);
118 // Remove the duplicate constraint
120 builder = builder.removeLimitConstraint(policy1.id, cons);
122 // Add a duplicate guard policy
124 builder = builder.addGuardPolicy(policy1);
125 builder = builder.addLimitConstraint(policy1.id, cons);
127 // Assert there are duplicate guard policies
129 results = builder.buildSpecification();
130 boolean duplicate_guard_policy = false;
131 for (Message m : results.getMessages()) {
132 if (m.getMessage().equals("There are duplicate guard policies") && m.getLevel() == MessageLevel.WARNING) {
133 duplicate_guard_policy = true;
137 assertTrue(duplicate_guard_policy);
139 // Remove the duplicate guard policy
141 builder = builder.removeGuardPolicy(policy1);
143 // Assert there are no Error/Warning message
145 results = builder.buildSpecification();
146 assertTrue(results.getMessages().size() == 1);
148 } catch (BuilderException e) {
149 fail(e.getMessage());
154 public void test1() {
155 this.test("src/test/resources/v2.0.0-guard/policy_guard_vUSP_1707_appc.yaml");
158 public void test(String testFile) {
159 try (InputStream is = new FileInputStream(new File(testFile))) {
161 // Read the yaml into our Java Object
163 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
164 Object obj = yaml.load(is);
166 assertTrue(obj instanceof ControlLoopGuard);
167 ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
169 // Now we're going to try to use the builder to build this.
171 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.guard);
175 if (guardTobuild.guards != null) {
176 builder = builder.addGuardPolicy(guardTobuild.guards.toArray(new GuardPolicy[guardTobuild.guards.size()]));
179 // Build the specification
181 Results results = builder.buildSpecification();
183 // Print out the specification
185 System.out.println(results.getSpecification());
187 } catch (FileNotFoundException e) {
188 fail(e.getLocalizedMessage());
189 } catch (IOException e) {
190 fail(e.getLocalizedMessage());
191 } catch (BuilderException e) {
192 fail(e.getLocalizedMessage());