Fixed the Policy API issues and Bugfixes
[policy/engine.git] / ECOMP-ControlloopPolicy / src / test / java / org / openecomp / policy / controlloop / policy / guard / ControlLoopGuardBuilderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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 package org.openecomp.policy.controlloop.policy.guard;
21
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.HashMap;
32 import java.util.LinkedList;
33 import java.util.List;
34 import java.util.Map;
35
36 import org.junit.Test;
37 import org.openecomp.policy.controlloop.policy.builder.BuilderException;
38 import org.openecomp.policy.controlloop.policy.builder.Message;
39 import org.openecomp.policy.controlloop.policy.builder.MessageLevel;
40 import org.openecomp.policy.controlloop.policy.builder.Results;
41 import org.openecomp.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
42 import org.yaml.snakeyaml.Yaml;
43 import org.yaml.snakeyaml.constructor.Constructor;
44
45 public class ControlLoopGuardBuilderTest {
46         
47         @Test
48         public void testControlLoopGuard() {
49                 try {
50                         //
51                         // Create a builder
52                         //
53                         ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
54                         //
55                         // Assert there is no guard policies yet
56                         //
57                         Results results = builder.buildSpecification();
58                         boolean no_guard_policies = false;
59                         for (Message m : results.getMessages()) {
60                                 if (m.getMessage().equals("ControlLoop Guard should have at least one guard policies") && m.getLevel() == MessageLevel.ERROR) {
61                                         no_guard_policies = true;
62                                         break;
63                                 }
64                         }
65                         assertTrue(no_guard_policies);
66                         //
67                         // Add a guard policy without limit constraint
68                         //
69                         GuardPolicy policy1 = new GuardPolicy("1111", "guardpolicy1", "guardpolicy1", "APPC", "restart");
70                         builder = builder.addGuardPolicy(policy1);
71                         //
72                         // Assert there is no limit constraint associated with the only guard policy
73                         //
74                         results = builder.buildSpecification();
75                         boolean no_constraint = false;
76                         for (Message m : results.getMessages()) {
77                                 if (m.getMessage().equals("Guard policy guardpolicy1 does not have any limit constraint") && m.getLevel() == MessageLevel.ERROR) {
78                                         no_constraint = true;
79                                         break;
80                                 }
81                         }
82                         assertTrue(no_constraint);
83                         //
84                         // Add a constraint to policy1
85                         //
86                         Map<String, String> time_in_range = new HashMap<String, String>();
87                         time_in_range.put("arg2", "PT5H");
88                         time_in_range.put("arg3", "PT24H");
89                         List<String> blacklist = new LinkedList<String>();
90                         blacklist.add("eNodeB_common_id1");
91                         blacklist.add("eNodeB_common_id2");
92                         Constraint cons = new Constraint(5, "PT15M", time_in_range, blacklist);
93                         builder = builder.addLimitConstraint(policy1.getId(), cons);
94                         //
95                         // Add a duplicate constraint to policy1
96                         //
97                         builder = builder.addLimitConstraint(policy1.getId(), cons);
98                         //
99                         // Assert there are duplicate constraints associated with the only guard policy
100                         //
101                         results = builder.buildSpecification();
102                         boolean duplicate_constraint = false;
103                         for (Message m : results.getMessages()) {
104                                 if (m.getMessage().equals("Guard policy guardpolicy1 has duplicate limit constraints") && m.getLevel() == MessageLevel.WARNING) {
105                                         duplicate_constraint = true;
106                                         break;
107                                 }
108                         }
109                         assertTrue(duplicate_constraint);
110                         //
111                         // Remove the duplicate constraint
112                         //
113                         builder = builder.removeLimitConstraint(policy1.getId(), cons);
114                         //
115                         // Add a duplicate guard policy 
116                         //
117                         builder = builder.addGuardPolicy(policy1);
118                         builder = builder.addLimitConstraint(policy1.getId(), cons);
119                         //
120                         // Assert there are duplicate guard policies
121                         //
122                         results = builder.buildSpecification();
123                         boolean duplicate_guard_policy = false;
124                         for (Message m : results.getMessages()) {
125                                 if (m.getMessage().equals("There are duplicate guard policies") && m.getLevel() == MessageLevel.WARNING) {
126                                         duplicate_guard_policy = true;
127                                         break;
128                                 }
129                         }
130                         assertTrue(duplicate_guard_policy);
131                         //
132                         // Remove the duplicate guard policy
133                         //
134                         builder = builder.removeGuardPolicy(policy1);
135                         //
136                         // Assert there are no Error/Warning message
137                         //
138                         results = builder.buildSpecification();
139                         assertTrue(results.getMessages().size() == 1);
140                         //
141                 } catch (BuilderException e) {
142                         fail(e.getMessage());
143                 }
144         }
145
146         @Test
147         public void test1() {
148                 this.test("src/test/resources/v2.0.0-guard/policy_guard_OpenECOMP_demo_vDNS.yaml");
149         }
150
151         @Test
152         public void test2() {
153                 this.test("src/test/resources/v2.0.0-guard/policy_guard_1707_appc.yaml");
154         }
155         
156         public void test(String testFile) {
157                 try (InputStream is = new FileInputStream(new File(testFile))) {
158                         //
159                         // Read the yaml into our Java Object
160                         //
161                         Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
162                         Object obj = yaml.load(is);
163                         assertNotNull(obj);
164                         assertTrue(obj instanceof ControlLoopGuard);
165                         ControlLoopGuard guardTobuild = (ControlLoopGuard) obj;
166                         //
167                         // Now we're going to try to use the builder to build this.
168                         //
169                         ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard());
170                         //
171                         // Add guard policy
172                         //
173                         if (guardTobuild.getGuards() != null) {
174                                 builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(new GuardPolicy[guardTobuild.getGuards().size()]));
175                         }
176                         //
177                         // Build the specification
178                         //
179                         Results results = builder.buildSpecification();
180                         //
181                         // Print out the specification
182                         //
183                         System.out.println(results.getSpecification());
184                         //
185                 } catch (FileNotFoundException e) {
186                         fail(e.getLocalizedMessage());
187                 } catch (IOException e) {
188                         fail(e.getLocalizedMessage());
189                 } catch (BuilderException e) {
190                         fail(e.getLocalizedMessage());
191                 }
192         }
193 }