2 * ============LICENSE_START=======================================================
3 * policy-yaml unit test
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.HashMap;
33 import java.util.LinkedList;
34 import java.util.List;
37 import org.junit.Test;
38 import org.onap.policy.controlloop.policy.builder.BuilderException;
39 import org.onap.policy.controlloop.policy.builder.Message;
40 import org.onap.policy.controlloop.policy.builder.MessageLevel;
41 import org.onap.policy.controlloop.policy.builder.Results;
42 import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.yaml.snakeyaml.Yaml;
46 import org.yaml.snakeyaml.constructor.Constructor;
48 public class ControlLoopGuardBuilderTest {
49 private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderTest.class);
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 noGuardPolicies = false;
63 for (Message m : results.getMessages()) {
64 if (m.getMessage().equals("ControlLoop Guard should have at least one guard policies")
65 && m.getLevel() == MessageLevel.ERROR) {
66 noGuardPolicies = true;
70 assertTrue(noGuardPolicies);
72 // Add a guard policy without limit constraint
74 String clname = "CL_vUSP123";
75 LinkedList<String> targets = new LinkedList<String>();
79 MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets);
80 GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters);
81 builder = builder.addGuardPolicy(policy1);
83 // Assert there is no limit constraint associated with the only guard policy
85 results = builder.buildSpecification();
86 boolean noConstraint = false;
87 for (Message m : results.getMessages()) {
88 if (m.getMessage().equals("Guard policy guardpolicy1 does not have any limit constraint")
89 && m.getLevel() == MessageLevel.ERROR) {
94 assertTrue(noConstraint);
96 // Add a constraint to policy1
98 Map<String, String> activeTimeRange = new HashMap<String, String>();
99 activeTimeRange.put("start", "00:00:00-05:00");
100 activeTimeRange.put("end", "23:59:59-05:00");
101 List<String> blacklist = new LinkedList<String>();
102 blacklist.add("eNodeB_common_id1");
103 blacklist.add("eNodeB_common_id2");
104 Map<String, String> timeWindow = new HashMap<String, String>();
105 timeWindow.put("value", "10");
106 timeWindow.put("units", "minute");
107 Constraint cons = new Constraint(5, timeWindow, activeTimeRange, blacklist);
108 builder = builder.addLimitConstraint(policy1.getId(), cons);
110 // Add a duplicate constraint to policy1
112 builder = builder.addLimitConstraint(policy1.getId(), cons);
114 // Assert there are duplicate constraints associated with the only guard policy
116 results = builder.buildSpecification();
117 boolean duplicateConstraint = false;
118 for (Message m : results.getMessages()) {
119 if (m.getMessage().equals("Guard policy guardpolicy1 has duplicate limit constraints")
120 && m.getLevel() == MessageLevel.WARNING) {
121 duplicateConstraint = true;
125 assertTrue(duplicateConstraint);
127 // Remove the duplicate constraint
129 builder = builder.removeLimitConstraint(policy1.getId(), cons);
131 // Add a duplicate guard policy
133 builder = builder.addGuardPolicy(policy1);
134 builder = builder.addLimitConstraint(policy1.getId(), cons);
136 // Assert there are duplicate guard policies
138 results = builder.buildSpecification();
139 boolean duplicateGuardPolicy = false;
140 for (Message m : results.getMessages()) {
141 if (m.getMessage().equals("There are duplicate guard policies")
142 && m.getLevel() == MessageLevel.WARNING) {
143 duplicateGuardPolicy = true;
147 assertTrue(duplicateGuardPolicy);
149 // Remove the duplicate guard policy
151 builder = builder.removeGuardPolicy(policy1);
153 // Assert there are no Error/Warning message
155 results = builder.buildSpecification();
156 assertTrue(results.getMessages().size() == 1);
158 } catch (BuilderException e) {
159 fail(e.getMessage());
164 public void test1() {
165 this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml");
169 public void test2() {
170 this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml");
174 * Do the actual test.
176 * @param testFile input test file
178 public void test(String testFile) {
179 try (InputStream is = new FileInputStream(new File(testFile))) {
181 // Read the yaml into our Java Object
183 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
184 Object obj = yaml.load(is);
186 assertTrue(obj instanceof ControlLoopGuard);
187 ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
189 // Now we're going to try to use the builder to build this.
191 ControlLoopGuardBuilder builder =
192 ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard());
196 if (guardTobuild.getGuards() != null) {
197 builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(
198 new GuardPolicy[guardTobuild.getGuards().size()]));
201 // Build the specification
203 Results results = builder.buildSpecification();
205 // Print out the specification
207 logger.debug(results.getSpecification());
209 } catch (FileNotFoundException e) {
210 fail(e.getLocalizedMessage());
211 } catch (IOException e) {
212 fail(e.getLocalizedMessage());
213 } catch (BuilderException e) {
214 fail(e.getLocalizedMessage());