[POLICY-22] Reorganizing drools-apps
[policy/drools-applications.git] / controlloop / common / policy-yaml / src / test / java / org / onap / policy / controlloop / policy / guard / ControlLoopGuardBuilderTest.java
1 /*-
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.policy.guard;
22
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.yaml.snakeyaml.Yaml;
26 import org.yaml.snakeyaml.constructor.Constructor;
27
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31
32 import java.io.File;
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;
40 import java.util.Map;
41
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;
47
48 public class ControlLoopGuardBuilderTest {
49         
50         @Ignore 
51         @Test
52         public void testControlLoopGuard() {
53                 try {
54                         //
55                         // Create a builder
56                         //
57                         ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
58                         //
59                         // Assert there is no guard policies yet
60                         //
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;
66                                         break;
67                                 }
68                         }
69                         assertTrue(no_guard_policies);
70                         //
71                         // Add a guard policy without limit constraint
72                         //
73                         GuardPolicy policy1 = new GuardPolicy("1111", "guardpolicy1", "guardpolicy1", "APPC", "restart");
74                         builder = builder.addGuardPolicy(policy1);
75                         //
76                         // Assert there is no limit constraint associated with the only guard policy
77                         //
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) {
82                                         no_constraint = true;
83                                         break;
84                                 }
85                         }
86                         assertTrue(no_constraint);
87                         //
88                         // Add a constraint to policy1
89                         //
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);
101                         //
102                         // Add a duplicate constraint to policy1
103                         //
104                         builder = builder.addLimitConstraint(policy1.id, cons);
105                         //
106                         // Assert there are duplicate constraints associated with the only guard policy
107                         //
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;
113                                         break;
114                                 }
115                         }
116                         assertTrue(duplicate_constraint);
117                         //
118                         // Remove the duplicate constraint
119                         //
120                         builder = builder.removeLimitConstraint(policy1.id, cons);
121                         //
122                         // Add a duplicate guard policy 
123                         //
124                         builder = builder.addGuardPolicy(policy1);
125                         builder = builder.addLimitConstraint(policy1.id, cons);
126                         //
127                         // Assert there are duplicate guard policies
128                         //
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;
134                                         break;
135                                 }
136                         }
137                         assertTrue(duplicate_guard_policy);
138                         //
139                         // Remove the duplicate guard policy
140                         //
141                         builder = builder.removeGuardPolicy(policy1);
142                         //
143                         // Assert there are no Error/Warning message
144                         //
145                         results = builder.buildSpecification();
146                         assertTrue(results.getMessages().size() == 1);
147                         //
148                 } catch (BuilderException e) {
149                         fail(e.getMessage());
150                 }
151         }
152         
153         @Test
154         public void test1() {
155                 this.test("src/test/resources/v2.0.0-guard/policy_guard_vUSP_1707_appc.yaml");
156         }
157         
158         public void test(String testFile) {
159                 try (InputStream is = new FileInputStream(new File(testFile))) {
160                         //
161                         // Read the yaml into our Java Object
162                         //
163                         Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
164                         Object obj = yaml.load(is);
165                         assertNotNull(obj);
166                         assertTrue(obj instanceof ControlLoopGuard);
167                         ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
168                         //
169                         // Now we're going to try to use the builder to build this.
170                         //
171                         ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.guard);
172                         //
173                         // Add guard policy
174                         //
175                         if (guardTobuild.guards != null) {
176                                 builder = builder.addGuardPolicy(guardTobuild.guards.toArray(new GuardPolicy[guardTobuild.guards.size()]));
177                         }
178                         //
179                         // Build the specification
180                         //
181                         Results results = builder.buildSpecification();
182                         //
183                         // Print out the specification
184                         //
185                         System.out.println(results.getSpecification());
186                         //
187                 } catch (FileNotFoundException e) {
188                         fail(e.getLocalizedMessage());
189                 } catch (IOException e) {
190                         fail(e.getLocalizedMessage());
191                 } catch (BuilderException e) {
192                         fail(e.getLocalizedMessage());
193                 }
194         }
195 }