2 * ============LICENSE_START=======================================================
3 * policy-yaml unit test
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.policy.guard;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
28 import java.io.FileInputStream;
29 import java.io.InputStream;
30 import java.util.HashMap;
31 import java.util.LinkedList;
32 import java.util.List;
34 import org.junit.Test;
35 import org.onap.policy.controlloop.policy.builder.BuilderException;
36 import org.onap.policy.controlloop.policy.builder.Message;
37 import org.onap.policy.controlloop.policy.builder.MessageLevel;
38 import org.onap.policy.controlloop.policy.builder.Results;
39 import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.yaml.snakeyaml.Yaml;
43 import org.yaml.snakeyaml.constructor.Constructor;
45 public class ControlLoopGuardBuilderTest {
46 private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderTest.class);
49 public void testControlLoopGuard() throws BuilderException {
53 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
55 // Assert there is no guard policies yet
57 Results results = builder.buildSpecification();
58 boolean noGuardPolicies = false;
59 for (Message m : results.getMessages()) {
60 if ("ControlLoop Guard should have at least one guard policies".equals(m.getMessage())
61 && m.getLevel() == MessageLevel.ERROR) {
62 noGuardPolicies = true;
66 assertTrue(noGuardPolicies);
68 // Add a guard policy without limit constraint
70 String clname = "CL_vUSP123";
71 List<String> targets = new LinkedList<>();
75 MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets);
76 GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters);
77 builder = builder.addGuardPolicy(policy1);
79 // Assert there is no limit constraint associated with the only guard policy
81 results = builder.buildSpecification();
82 boolean noConstraint = false;
83 for (Message m : results.getMessages()) {
84 if ("Guard policy guardpolicy1 does not have any limit constraint".equals(m.getMessage())
85 && m.getLevel() == MessageLevel.ERROR) {
90 assertTrue(noConstraint);
92 // Add a constraint to policy1
94 Map<String, String> activeTimeRange = new HashMap<>();
95 activeTimeRange.put("start", "00:00:00-05:00");
96 activeTimeRange.put("end", "23:59:59-05:00");
97 List<String> blacklist = new LinkedList<>();
98 blacklist.add("eNodeB_common_id1");
99 blacklist.add("eNodeB_common_id2");
100 Map<String, String> timeWindow = new HashMap<>();
101 timeWindow.put("value", "10");
102 timeWindow.put("units", "minute");
103 Constraint cons = new Constraint(5, timeWindow, activeTimeRange, blacklist);
104 builder = builder.addLimitConstraint(policy1.getId(), cons);
106 // Add a duplicate constraint to policy1
108 builder = builder.addLimitConstraint(policy1.getId(), cons);
110 // Assert there are duplicate constraints associated with the only guard policy
112 results = builder.buildSpecification();
113 boolean duplicateConstraint = false;
114 for (Message m : results.getMessages()) {
115 if ("Guard policy guardpolicy1 has duplicate limit constraints".equals(m.getMessage())
116 && m.getLevel() == MessageLevel.WARNING) {
117 duplicateConstraint = true;
121 assertTrue(duplicateConstraint);
123 // Remove the duplicate constraint
125 builder = builder.removeLimitConstraint(policy1.getId(), cons);
127 // Add a duplicate guard policy
129 builder = builder.addGuardPolicy(policy1);
130 builder = builder.addLimitConstraint(policy1.getId(), cons);
132 // Assert there are duplicate guard policies
134 results = builder.buildSpecification();
135 boolean duplicateGuardPolicy = false;
136 for (Message m : results.getMessages()) {
137 if ("There are duplicate guard policies".equals(m.getMessage())
138 && m.getLevel() == MessageLevel.WARNING) {
139 duplicateGuardPolicy = true;
143 assertTrue(duplicateGuardPolicy);
145 // Remove the duplicate guard policy
147 builder = builder.removeGuardPolicy(policy1);
149 // Assert there are no Error/Warning message
151 results = builder.buildSpecification();
152 assertTrue(results.getMessages().size() == 1);
156 public void test1() throws Exception {
157 this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
161 public void test2() throws Exception {
162 this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
166 * Do the actual test.
168 * @param testFile input test file
169 * @throws Exception if an error occurs
171 public void test(String testFile) throws Exception {
172 try (InputStream is = new FileInputStream(new File(testFile))) {
174 // Read the yaml into our Java Object
176 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
177 Object obj = yaml.load(is);
179 assertTrue(obj instanceof ControlLoopGuard);
180 ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
182 // Now we're going to try to use the builder to build this.
184 ControlLoopGuardBuilder builder =
185 ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard());
189 if (guardTobuild.getGuards() != null) {
190 builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(
191 new GuardPolicy[guardTobuild.getGuards().size()]));
194 // Build the specification
196 Results results = builder.buildSpecification();
198 // Print out the specification
200 logger.debug(results.getSpecification());