2 * ============LICENSE_START=======================================================
3 * policy-yaml unit test
4 * ================================================================================
5 * Copyright (C) 2017-2018 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;
26 import static org.junit.Assert.fail;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.HashMap;
34 import java.util.LinkedList;
35 import java.util.List;
38 import org.junit.Test;
39 import org.onap.policy.controlloop.policy.builder.BuilderException;
40 import org.onap.policy.controlloop.policy.builder.Message;
41 import org.onap.policy.controlloop.policy.builder.MessageLevel;
42 import org.onap.policy.controlloop.policy.builder.Results;
43 import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.yaml.snakeyaml.Yaml;
47 import org.yaml.snakeyaml.constructor.Constructor;
49 public class ControlLoopGuardBuilderTest {
50 private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderTest.class);
53 public void testControlLoopGuard() {
58 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
60 // Assert there is no guard policies yet
62 Results results = builder.buildSpecification();
63 boolean noGuardPolicies = false;
64 for (Message m : results.getMessages()) {
65 if (m.getMessage().equals("ControlLoop Guard should have at least one guard policies")
66 && m.getLevel() == MessageLevel.ERROR) {
67 noGuardPolicies = true;
71 assertTrue(noGuardPolicies);
73 // Add a guard policy without limit constraint
75 String clname = "CL_vUSP123";
76 LinkedList<String> targets = new LinkedList<String>();
80 MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets);
81 GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters);
82 builder = builder.addGuardPolicy(policy1);
84 // Assert there is no limit constraint associated with the only guard policy
86 results = builder.buildSpecification();
87 boolean noConstraint = false;
88 for (Message m : results.getMessages()) {
89 if (m.getMessage().equals("Guard policy guardpolicy1 does not have any limit constraint")
90 && m.getLevel() == MessageLevel.ERROR) {
95 assertTrue(noConstraint);
97 // Add a constraint to policy1
99 Map<String, String> activeTimeRange = new HashMap<String, String>();
100 activeTimeRange.put("start", "00:00:00-05:00");
101 activeTimeRange.put("end", "23:59:59-05:00");
102 List<String> blacklist = new LinkedList<String>();
103 blacklist.add("eNodeB_common_id1");
104 blacklist.add("eNodeB_common_id2");
105 Map<String, String> timeWindow = new HashMap<String, String>();
106 timeWindow.put("value", "10");
107 timeWindow.put("units", "minute");
108 Constraint cons = new Constraint(5, timeWindow, activeTimeRange, blacklist);
109 builder = builder.addLimitConstraint(policy1.getId(), cons);
111 // Add a duplicate constraint to policy1
113 builder = builder.addLimitConstraint(policy1.getId(), cons);
115 // Assert there are duplicate constraints associated with the only guard policy
117 results = builder.buildSpecification();
118 boolean duplicateConstraint = false;
119 for (Message m : results.getMessages()) {
120 if (m.getMessage().equals("Guard policy guardpolicy1 has duplicate limit constraints")
121 && m.getLevel() == MessageLevel.WARNING) {
122 duplicateConstraint = true;
126 assertTrue(duplicateConstraint);
128 // Remove the duplicate constraint
130 builder = builder.removeLimitConstraint(policy1.getId(), cons);
132 // Add a duplicate guard policy
134 builder = builder.addGuardPolicy(policy1);
135 builder = builder.addLimitConstraint(policy1.getId(), cons);
137 // Assert there are duplicate guard policies
139 results = builder.buildSpecification();
140 boolean duplicateGuardPolicy = false;
141 for (Message m : results.getMessages()) {
142 if (m.getMessage().equals("There are duplicate guard policies")
143 && m.getLevel() == MessageLevel.WARNING) {
144 duplicateGuardPolicy = true;
148 assertTrue(duplicateGuardPolicy);
150 // Remove the duplicate guard policy
152 builder = builder.removeGuardPolicy(policy1);
154 // Assert there are no Error/Warning message
156 results = builder.buildSpecification();
157 assertTrue(results.getMessages().size() == 1);
159 } catch (BuilderException e) {
160 fail(e.getMessage());
165 public void test1() {
166 this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
170 public void test2() {
171 this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
175 * Do the actual test.
177 * @param testFile input test file
179 public void test(String testFile) {
180 try (InputStream is = new FileInputStream(new File(testFile))) {
182 // Read the yaml into our Java Object
184 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
185 Object obj = yaml.load(is);
187 assertTrue(obj instanceof ControlLoopGuard);
188 ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
190 // Now we're going to try to use the builder to build this.
192 ControlLoopGuardBuilder builder =
193 ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard());
197 if (guardTobuild.getGuards() != null) {
198 builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(
199 new GuardPolicy[guardTobuild.getGuards().size()]));
202 // Build the specification
204 Results results = builder.buildSpecification();
206 // Print out the specification
208 logger.debug(results.getSpecification());
210 } catch (FileNotFoundException e) {
211 fail(e.getLocalizedMessage());
212 } catch (IOException e) {
213 fail(e.getLocalizedMessage());
214 } catch (BuilderException e) {
215 fail(e.getLocalizedMessage());